1. 程式人生 > >Tensorflow搭建卷積神經網路問題及解決

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填充,輸出節點矩陣僅與步長有關,outputsize=inputsizestridesoutput_{size}=\frac{input_{size}}{strides}。 不使用0填充,輸出節點矩陣和步長、卷積核有關,outputsize=inputsizekernelsizestrides+1output_{size}=\frac{input_{size}-kernel_{size}}{strides}+1

elsize+1