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

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

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

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

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))
}

 

你可能感兴趣的:(TensorFlow,深度学习)