Tensorflow——Cannot reshape a tensor with 6553600 elements to shape的问题解决

该博客介绍了在TensorFlow中遇到的`ValueError: Cannot reshape a tensor`问题,原因是尝试将具有6553600个元素的张量重塑为不匹配的形状。解决方案是调整卷积层的卷积核数量,从32改为64,以匹配张量的元素数量。这表明在构建神经网络模型时,确保张量维度的一致性至关重要。
摘要由CSDN通过智能技术生成

ValueError: Cannot reshape a tensor with 6553600 elements to shape [100,32,32,32] (3276800 elements) 问题解决

错误代码:

x = tf.placeholder(tf.float32, [100, 3072])
x_image = tf.reshape(x, [-1, 32, 32, 3])
# x = tf.placeholder(dtype=tf.float32, shape=[None, 32, 32, 3])

y_ = tf.placeholder(dtype=tf.float32, shape=[None, 10])
W_e_conv1 = weight_variable([5, 5, 3, 32], "w_e_conv1")
b_e_conv1 = bias_variable([32], "b_e_conv1")
h_e_conv1 = tf.nn.relu(tf.add(conv2d(x_image, W_e_conv1), b_e_conv1))#(?,32,32,64)
conv1 = attention(h_e_conv1, 32, sn=False, scope='attention', reuse=False)

可以看出是32个5*5的卷积核

完整错误形式:

ValueError:Cannot reshape a tensor with 6553600 elements to shape [100,32,32,32] (3276800 elements) for 'attention/Reshape_3' (op: 'Reshape') with input shapes: [100,1024,64], [4] and with input tensors computed as partial shapes: input[1] = [100,32,32,32].

解释:
ValueError:无法将具有6553600个元素的张量重塑为形状[100,32,32,32](3276800个元素)的形状[100,32,32,32](3276800个元素),输入形状为[1001024,64],[4]且输入张量计算为部分形状:input[1]=[100,32,32,32]。

解决方案:
将卷积核的个数变成64即可

x = tf.placeholder(tf.float32, [100, 3072])
x_image = tf.reshape(x, [-1, 32, 32, 3])
# x = tf.placeholder(dtype=tf.float32, shape=[None, 32, 32, 3])

y_ = tf.placeholder(dtype=tf.float32, shape=[None, 10])
# x_origin = tf.reshape(x, [-1, 28, 28, 1])

#具体含义是[卷积核的高度,卷积核的宽度,图像通道数,卷积核个数],要求类型与参数input相同,有一个地方需要注意,第三维in_channels,就是参数input的第四维
W_e_conv1 = weight_variable([5, 5, 3, 64], "w_e_conv1")
b_e_conv1 = bias_variable([64], "b_e_conv1")
h_e_conv1 = tf.nn.relu(tf.add(conv2d(x_image, W_e_conv1), b_e_conv1))#(?,32,32,64)
conv1 = attention(h_e_conv1, 32, sn=False, scope='attention', reuse=False)
# print(conv1.get_shape())
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值