我正在尝试制作我的第一个张量流模型,但是我遇到了一些问题。看起来它使火车正确,但是当它进行预测时,它只返回(几乎)总是相同的值。这是代码:
n_classes = 2
tf.reset_default_graph()
x = tf.placeholder('float')
y = tf.placeholder('float')
keep_rate = tf.placeholder(tf.float32)
weights = {'W_conv1':tf.Variable(tf.random_normal([3,3,3,1,32]),
'W_conv2':tf.Variable(tf.random_normal([3,3,3,32,64])),
'W_fc':tf.Variable(tf.random_normal([54080,1024])),
'out':tf.Variable(tf.random_normal([1024, n_classes]))}
biases = {'b_conv1':tf.Variable(tf.random_normal([32])),
'b_conv2':tf.Variable(tf.random_normal([64])),
'b_fc':tf.Variable(tf.random_normal([1024])),
'out':tf.Variable(tf.random_normal([n_classes]))}
def conv3d(x, W):
return tf.nn.conv3d(x, W, strides=[1,1,1,1,1], padding='SAME')
def maxpool3d(x):
return tf.nn.max_pool3d(x, ksize=[1,2,2,2,1], strides=[1,2,2,2,1], padding='SAME')
def convolutional_neural_network(x, keep_rate):
x = tf.reshape(x, shape=[-1, IMG_SIZE_PX, IMG_SIZE_PX, SLICE_COUNT, 1])
conv1 = tf.nn.relu(conv3d(x, weights['W_conv1']) + biases['b_conv1'])
conv1 = maxpool3d(conv1)
conv2 = tf.nn.relu(conv3d(conv1, weights['W_conv2']) + biases['b_conv2'])
conv2 = maxpool3d(conv2)
fc = tf.reshape(conv2,[-1, 54080])
fc = tf.nn.relu(tf.matmul(fc, weights['W_fc'])+biases['b_fc'])
fc = tf.nn.dropout(fc, keep_rate)
output = tf.matmul(fc, weights['out'])+biases['out']
return output
much_data = np.load('F:/Kaggle/Data Science Bowl 2017/Script/muchdata-50-50-20.npy')
train_data = much_data[:-100]
validation_data = much_data[-100:]
def train_neural_network(x):
prediction = convolutional_neural_network(x, keep_rate)
cost = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits(logits=prediction, labels=y) )
optimizer = tf.train.AdamOptimizer(learning_rate=1e-3).minimize(cost)
hm_epochs = 10
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for epoch in range(hm_epochs):
epoch_loss = 0
for data in train_data:
X = data[0]
Y = data[1]
_, c = sess.run([optimizer, cost], feed_dict={x: X, y: Y, keep_rate: 0.75})
epoch_loss += c
print('Epoch', epoch+1, 'completed out of',hm_epochs,'loss:',epoch_loss)
correct = tf.equal(tf.argmax(prediction, 1), tf.argmax(y, 1))
accuracy = tf.reduce_mean(tf.cast(correct, 'float'))
print('Accuracy:',accuracy.eval({x:[i[0] for i in validation_data], y:[i[1] for i in validation_data], keep_rate: 1.}))
print('Done. Finishing accuracy:')
print('Accuracy:',accuracy.eval({x:[i[0] for i in validation_data], y:[i[1] for i in validation_data], keep_rate: 1.}))
eval_data = np.load('F:/Kaggle/Data Science Bowl 2017/Script/eval_data-50-50-20.npy')
probabilities = tf.nn.softmax(prediction)
sol = []
for data in eval_data:
X = data[0]
id = data[1]
probs = probabilities.eval(feed_dict={x: X, keep_rate: 1.})
pred = prediction.eval(feed_dict={x: X, keep_rate: 1.})
print('Outputs: ',pred)
print('Probs: ',probs)
sol.append([id, probs[0,1]])
print(sol)
我还检查了模型训练期间的预测,如果我将 keep_rate 设置为 1,我在最后也几乎总是得到恒定的预测。在第一个时期有很多变化,但在最后一个时期,神经网络似乎总是对每个图像进行相同的预测。它似乎收敛到一个独特的预测值,而不考虑我传递给神经网络的图像。我检查了一百遍还是看不出错误在哪里。
这是我在 eval_data 中获得的一些图像的示例(当我打印 train_data 时的行为相同):
Probs: [[ 0.76099759 0.23900245]]
Outputs: [[-0.017277 -1.1754334]]
Probs: [[ 0.76099759 0.23900245]]
Outputs: [[-0.017277 -1.1754334]]
Probs: [[ 0.76099759 0.23900245]]
Outputs: [[ 117714.1953125 -47536.32421875]]
Probs: [[ 1. 0.]]
Outputs: [[-0.017277 -1.1754334]]
Probs: [[ 0.76099759 0.23900245]]
Outputs: [[-0.017277 -1.1754334]]
Probs: [[ 0.76099759 0.23900245]]
Outputs: [[-0.017277 -1.1754334]]
Probs: [[ 0.76099759 0.23900245]]
请注意,它们几乎总是相同的,但有时我会看到一些奇怪的值,例如
Outputs: [[ 117714.1953125 -47536.32421875]]
Probs: [[ 1. 0.]]
如何解决这个问题?
我也遇到了同样的问题,我花了两周时间才找到原因。它可能对你有帮助。我的问题是由于嘈杂的数据集和高学习率造成的。由于 ReLU 激活可能会杀死神经元,因此当数据集有噪声时,大多数 ReLU 将死亡(不会激活任何输入,因为它认为其输入无用),然后网络可能只会学习最终标签的一些固定分布。因此结果固定为任何输入。
我的解决方案是使用
tf.nn.leaky_relu()
,因为它不会杀死负输入。