Tensorflow返回相同的预测

时间:2017-04-10 18:01:26

标签: python tensorflow neural-network deep-learning

我试图制作我的第一个张量流模型,但是我有一些问题。似乎它使火车正确,但是当它进行预测时,它只返回(几乎)总是相同的值。这是代码:

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.]]

希望有人有答案,这让我感到头疼。

非常感谢您的耐心等待!我还是Tensorflow的新手:D

1 个答案:

答案 0 :(得分:1)

我遇到了同样的问题,我花了两周的时间才找到原因。它可能对你有帮助。我的问题是由于噪声数据集和高学习率。由于ReLU激活可以杀死神经元,当数据集有噪声时,大多数ReLU将死亡(不激活任何输入,因为它认为其输入无用),然后网络可能只学习最终标签的一些固定分布。所以结果固定在任何输入上。

我的解决方案是使用tf.nn.leaky_relu(),因为它不会杀死负面输入。