【TensorFlow】ValueError: Shape must be rank 1 but is rank 0 for ' ’ with input shapes: [].问题

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lyq_12/article/details/85071656

基于TensorFlow训练mnist数据集出现如下错误:

检测代码,发现是偏置设置格式错误导致。

1、错误代码: 

# 定义权重和偏置
n_input = 784
n_output = 10
weights = {
    'wc1': tf.Variable(tf.random_normal([3, 3, 1, 64], stddev=0.1)),
    'wc2': tf.Variable(tf.random_normal([3, 3, 64, 128], stddev=0.1)),
    'wd1': tf.Variable(tf.random_normal([7 * 7 * 128, 1024], stddev=0.1)),
    'wd2': tf.Variable(tf.random_normal([1024, n_output], stddev=0.1))
}
biases = {
    'bc1': tf.Variable(tf.random_normal(64), stddev=0.1),
    'bc2': tf.Variable(tf.random_normal(128), stddev=0.1),
    'bd1': tf.Variable(tf.random_normal(1024), stddev=0.1),
    'bd2': tf.Variable(tf.random_normal(n_output), stddev=0.1)
}

2、修改后的代码如下:

把biases的"()"改为"[]"

#定义权重和偏置
n_input = 784
n_output= 10
weights = {
    'wc1':tf.Variable(tf.random_normal([3,3,1,64], stddev=0.1)),
    'wc2':tf.Variable(tf.random_normal([3,3,64,128],stddev=0.1)),
    'wd1':tf.Variable(tf.random_normal([7*7*128,1024],stddev=0.1)),
    'wd2':tf.Variable(tf.random_normal([1024,n_output],stddev=0.1))
}
biases = {
    'bc1':tf.Variable(tf.random_normal([64], stddev=0.1)),
    'bc2':tf.Variable(tf.random_normal([128],stddev=0.1)),
    'bd1':tf.Variable(tf.random_normal([1024],stddev=0.1)),
    'bd2':tf.Variable(tf.random_normal([n_output],stddev=0.1))
}

猜你喜欢

转载自blog.csdn.net/lyq_12/article/details/85071656