欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

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

发布时间:2025/3/21 编程问答 46 豆豆
生活随笔 收集整理的这篇文章主要介绍了 【TensorFlow】ValueError: Shape must be rank 1 but is rank 0 for ' ’ with input shapes: [].问题 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

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

 

总结

以上是生活随笔为你收集整理的【TensorFlow】ValueError: Shape must be rank 1 but is rank 0 for ' ’ with input shapes: [].问题的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。