Tensorflow搭建卷积神经网络问题及解决

本文针对使用Tensorfolw搭建卷积神经网络遇到的问题进行解析和解决。

    问题1
ValueError: Only call `softmax_cross_entropy_with_logits` with named arguments (labels=..., logits=..., ...)

【原因】

#softmax_cross_entropy_with_logits未指定logits和labels参数
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(pred, y))

【解决方案】

cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=pred, labels=y))
    问题2
InvalidArgumentError (see above for traceback): logits and labels must be same size: logits_size=[32,10] labels_size=[128,10]
	 [[Node: SoftmaxCrossEntropyWithLogits = SoftmaxCrossEntropyWithLogits[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/cpu:0"](Reshape_3, Reshape_4)]]

【原因】

logits和labels向量尺寸不一致。

【解决方案】 重新计算卷积层和池化层输出节点矩阵。 使用0填充,输出节点矩阵仅与步长有关, o u t p u t s i z e = i n p u t s i z e s t r i d e s output_{size}=frac{input_{size}}{strides} outputsize=stridesinputsize。 不使用0填充,输出节点矩阵和步长、卷积核有关, o u t p u t s i z e = i n p u t s i z e − k e r n e l s i z e s t r i d e s + 1 output_{size}=frac{input_{size}-kernel_{size}}{strides}+1 outputsize=stridesinputsize−kernelsize+1。


[参考文献] [1]


经验分享 程序员 微信小程序 职场和发展