【问题标题】:Learn the sum of two numbers in Tensorflow在 TensorFlow 中学习两个数字的总和
【发布时间】:2018-01-19 13:07:36
【问题描述】:

我正在尝试训练一个神经网络来预测两个数字的总和。但我不明白我的模型有什么问题。模型由 2 个输入、2 个隐藏层和 1 个输出层组成。每 1000 次迭代我打印测试执行,但结果越来越小。

import numpy as np
import tensorflow as tf


input_size = 2
hidden_size = 3
out_size = 1


def generate_test_data():
    inp = 0.5*np.random.rand(10, 2)
    oup = np.zeros((10, 1))
    for idx, val in enumerate(inp):
        oup[idx] = np.array([val[0] + val[1]])
    return inp, oup


def create_network():
    x = tf.placeholder(tf.float32, [None, input_size])

    w01 = tf.Variable(tf.truncated_normal([input_size, hidden_size], stddev=0.1))
    y1 = tf.sigmoid(tf.matmul(tf.sigmoid(x), w01))

    w12 = tf.Variable(tf.truncated_normal([hidden_size, out_size], stddev=0.1))
    y2 = tf.sigmoid(tf.matmul(y1, w12))

    y_ = tf.placeholder(tf.float32, [None, out_size])
    return x, y_, y2


def train(x, y_, y2):
    cross_entropy = tf.reduce_mean(
        tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y2)
    )
    train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)

    sess = tf.InteractiveSession()
    tf.global_variables_initializer().run()

    # Train
    for i in range(100000):
        batch_xs, batch_ys = generate_test_data()
        sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})

        # Test
        if i % 1000 == 0:
            out_batch = sess.run(y2, {x: batch_xs})
            inx = 0
            print(batch_xs[inx][0], " + ", batch_xs[inx][1], " = ", out_batch[inx][0])



(x, y_, y2) = create_network()
train(x, y_, y2)

每 1000 次迭代输出:

0.37301352864927173  + 0.28949461772342683  = 0.49111518
0.050899466843458474 + 0.006174158992116541 = 0.0025260744
0.3974852369427063   + 0.22402098418952499  = 0.00090828544
0.15735921047969498  + 0.39645077887600294  = 0.0005903727
0.23560825884336228  + 0.29010766384718145  = 0.0004317883
0.4250063393420791   + 0.24181166029062096  = 0.00031525563
                                            = smaller and smaller

【问题讨论】:

    标签: python tensorflow machine-learning neural-network deep-learning


    【解决方案1】:

    交叉熵损失用于分类问题,而您的任务显然是回归。计算出的 cross_entropy 值没有意义,因此是结果。

    将您的损失更改为:

    cross_entropy = tf.reduce_mean(
        tf.nn.l2_loss(y_ - y2)
    )
    

    ...您会看到更明智的结果。

    【讨论】:

    【解决方案2】:

    马克西姆,非常感谢。现在可以了。

    import numpy as np
    import tensorflow as tf
    
    
    input_size = 2
    hidden_size = 3
    out_size = 1
    
    
    def generate_test_data():
        inp = 0.5*np.random.rand(10, 2)
        oup = np.zeros((10, 1))
        for idx, val in enumerate(inp):
            oup[idx] = np.array([val[0] + val[1]])
        return inp, oup
    
    
    def create_network():
        x = tf.placeholder(tf.float32, [None, input_size])
    
        w01 = tf.Variable(tf.truncated_normal([input_size, hidden_size], stddev=0.1))
        y1 = tf.matmul(x, w01)
    
        w12 = tf.Variable(tf.truncated_normal([hidden_size, out_size], stddev=0.1))
        y2 = tf.matmul(y1, w12)
    
        y_ = tf.placeholder(tf.float32, [None, out_size])
        return x, y_, y2
    
    
    def train(x, y_, y2):
        cross_entropy = tf.reduce_mean(
            tf.nn.l2_loss(y_ - y2)
        )
        train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)
    
        sess = tf.InteractiveSession()
        tf.global_variables_initializer().run()
    
        # Train
        for i in range(100000):
            batch_xs, batch_ys = generate_test_data()
            sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})
    
            # Test
            if i % 2000 == 0:
                out_batch = sess.run(y2, {x: batch_xs})
                inx = 0
                print(batch_xs[inx][0], " + ", batch_xs[inx][1], " = ", out_batch[inx][0], "|", batch_xs[inx][0] + batch_xs[inx][1])
    
    
    (x, y_, y2) = create_network()
    train(x, y_, y2)
    

    【讨论】:

      【解决方案3】:

      如果您认为预测每个数字是一个分类问题,您预测“0123456789”中的值,您可以使用交叉熵作为损失。如需参考,请参阅Keras - Addition RNN Example

      但正如 Maxim 所说,它不应该用于回归问题。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-12-29
        • 2018-10-08
        • 1970-01-01
        • 2017-08-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多