首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >TensorFlow MLP损耗增加

TensorFlow MLP损耗增加
EN

Data Science用户
提问于 2019-10-02 18:55:47
回答 1查看 135关注 0票数 1

当我训练我的模型时,损失在每一个时代都在增加。我觉得这是一个简单的解决方案,我遗漏了一些显而易见的东西,但我不知道它是什么。任何帮助都将不胜感激。

神经网络:

代码语言:javascript
复制
def neural_network(data):
    hidden_L1 = {'weights': tf.Variable(tf.random_normal([784, neurons_L1])),
                'biases': tf.Variable(tf.random_normal([neurons_L1]))}

    hidden_L2 = {'weights': tf.Variable(tf.random_normal([neurons_L1, neurons_L2])),
                'biases': tf.Variable(tf.random_normal([neurons_L2]))}

    output_L = {'weights': tf.Variable(tf.random_normal([neurons_L2, num_of_classes])),
                'biases': tf.Variable(tf.random_normal([num_of_classes]))}

    L1 = tf.add(tf.matmul(data, hidden_L1['weights']), hidden_L1['biases']) #matrix multiplication
    L1 = tf.nn.relu(L1)

    L2 = tf.add(tf.matmul(L1, hidden_L2['weights']), hidden_L2['biases']) #matrix multiplication
    L2 = tf.nn.relu(L2)

    output = tf.add(tf.matmul(L2, output_L['weights']), output_L['biases']) #matrix multiplication
    output = tf.nn.softmax(output)

    return output

我的损失,优化和循环每一个时代:

代码语言:javascript
复制
output = neural_network(x)
loss = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits(logits=output, labels=y) )
optimiser = tf.train.AdamOptimizer().minimize(loss)

init = tf.global_variables_initializer()

epochs = 5
total_batch_count = 60000//batch_size

with tf.Session() as sess:
    sess.run(init)

    for epoch in range(epochs):

        avg_loss = 0 

        for i in range(total_batch_count):

            batch_x, batch_y = next_batch(batch_size, x_train, y_train)

            _, c = sess.run([optimiser, loss], feed_dict = {x:batch_x, y:batch_y})

            avg_loss +=c/total_batch_count

            print("epoch = ", epoch + 1, "loss =", avg_loss)

    sess.close()

我有一种感觉,我的问题在于我所写的每一个时代的损失函数或循环,然而我对TensorFlow并不熟悉,无法解决这个问题。

EN

回答 1

Data Science用户

回答已采纳

发布于 2019-10-03 08:20:58

您使用的是函数软极_交叉_熵_使用_逻辑,根据Tensorflow的文档,该函数具有以下logits规范,

  • logits:每个标签的激活,通常是线性输出.这些活化能被解释为未归一化的对数概率。

因此,您应该在非线性应用程序之前传递激活(在您的例子中,softmax)。您可以通过执行以下操作来修复它,

代码语言:javascript
复制
def neural_network(data):
    hidden_L1 = {'weights': tf.Variable(tf.random_normal([784, neurons_L1])),
                'biases': tf.Variable(tf.random_normal([neurons_L1]))}

    hidden_L2 = {'weights': tf.Variable(tf.random_normal([neurons_L1, neurons_L2])),
                'biases': tf.Variable(tf.random_normal([neurons_L2]))}

    output_L = {'weights': tf.Variable(tf.random_normal([neurons_L2, num_of_classes])),
                'biases': tf.Variable(tf.random_normal([num_of_classes]))}

    L1 = tf.add(tf.matmul(data, hidden_L1['weights']), hidden_L1['biases']) #matrix multiplication
    L1 = tf.nn.relu(L1)

    L2 = tf.add(tf.matmul(L1, hidden_L2['weights']), hidden_L2['biases']) #matrix multiplication
    L2 = tf.nn.relu(L2)

    logits = tf.add(tf.matmul(L2, output_L['weights']), output_L['biases']) #matrix multiplication
    output = tf.nn.softmax(logits)

    return output, logits

然后,在函数之外,可以检索逻辑,并将其传递给丢失函数,如下面的示例所示,

代码语言:javascript
复制
output, logits = neural_network(x)
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits,
                                                             labels=y))

我注意到,您可能仍然感兴趣的输出张量,以计算您的网络的准确性。如果此替换不起作用,您还应该在AdamOptimizer (见这里的文档)上试验学习速率参数。

票数 1
EN
页面原文内容由Data Science提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://datascience.stackexchange.com/questions/61163

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档