【问题标题】:back propagate doesn't work in Tensorflow反向传播在 Tensorflow 中不起作用
【发布时间】:2017-05-23 02:40:54
【问题描述】:

我是 TensorFlow 的新生。最近我想用一个两层神经网络模型拟合一个非线性函数“y = 1 + sin(x * pi/4)”。程序代码如下:

    #!/usr/bin/python

    import tensorflow as tf
    import numpy as np
    import math
    import matplotlib.pyplot as plt

    def check_mode():
        x_data = np.linspace(-2,2,100)
        y_data = [1 + math.sin(x  * math.pi/4) for x in x_data]
        w_1 = tf.Variable(tf.random_uniform([1,2],0,0.5))
        b_1 = tf.Variable(tf.random_uniform([1,2],0,0.5))
        w_2 = tf.Variable(tf.random_uniform([2,1],0,0.5))
        b_2 = tf.Variable(tf.random_uniform([1,1],0,0.5))
        saver = tf.train.Saver()
        with tf.Session() as sess:
            saver.restore(sess,"mode.ckpt")
            print("lay1: ",sess.run(w_1),sess.run(b_1))
            print("lay2: ",sess.run(w_2),sess.run(b_2))
            a = []
            for x_i in x_data:
                w_plus_b = tf.matmul([[x_i]],w_1) + b_1
                a_1 = sigma(w_plus_b)
                a_2 = tf.matmul(a_1,w_2) + b_2
                a.append(sess.run(a_2[0][0]))
        print a
        draw_point(a,x_data,y_data)
        return
    def draw_point(a,x_data,y_data):
        fx,ax = plt.subplots()
        plt.plot(x_data,y_data,'o-')
        plt.plot(x_data,a,'k-')
        plt.show()  


    def sigma(x):
        return tf.div(tf.constant(1.0),tf.add(tf.constant(1.0),tf.exp(tf.negative(x))))

    def first_function():
        x_data = np.linspace(-2,2,100)
        y_data = [1 + math.sin(x  * math.pi/4) for x in x_data]

        x_i = tf.placeholder(tf.float32,[1,1])
        y_data_i = tf.placeholder(tf.float32,[1,1])

        w_1 = tf.Variable(tf.random_uniform([1,2],0,0.5))
        b_1 = tf.Variable(tf.random_uniform([1,2],0,0.5))

        w_2 = tf.Variable(tf.random_uniform([2,1],0,0.5))
        b_2 = tf.Variable(tf.random_uniform([1,1],0,0.5))

        z_1 = tf.add(tf.matmul(x_i,w_1), b_1)
        a_1 = sigma(z_1)
        a_2 = tf.add(tf.matmul(a_1,w_2),b_2)
        diff = tf.subtract(a_2,y_data_i)    

        loss = tf.multiply(diff,diff)

        optimizer = tf.train.GradientDescentOptimizer(0.1)
        train = optimizer.minimize(loss)

        init = tf.initialize_all_variables()

        sess = tf.Session()
        sess.run(init)
        saver = tf.train.Saver()
        for step in range(100):
            sess.run(train,feed_dict={x_i:[[x_data[step]]],y_data_i:[[y_data[step]]]})

        print("lay1: ",sess.run(w_1),sess.run(b_1))
        print("lay2: ",sess.run(w_2),sess.run(b_2))
        saver.save(sess,"mode.ckpt")
        return

    def main():
        #first_function()
        check_mode()
        return

    if __name__ == '__main__':
        main()

我不确定tensorflow是否以及如何自动完成反向传播?我真的不想自己实现反向传播。我的代码有问题吗?非常感谢您的帮助!!

【问题讨论】:

  • 它确实看起来像完成了背部道具,但是看起来你训练它的次数并不多。具体来说,您的训练循环只遍历每个数据点一次。经常看到许多迭代。如果你把它放在一个单一且最简单的工作脚本中,我很乐意运行你的代码并尝试修复它。例如,删除不属于您的问题的保存和其他不必要的代码。
  • 非常感谢。代码在github.com/xiaoyangzai/test/blob/master/back_propagate.py 再次感谢!
  • 再次感谢您!我已经循环计算了 1000 次!非常非常非常感谢!!!!谢谢!!
  • 在 Stackoverflow 上,鼓励您投票而不是说 谢谢。我会将我的评论作为答案发布,因此您可以接受它作为答案或支持它。干杯。

标签: tensorflow neural-network backpropagation


【解决方案1】:

看起来它确实完成了背部支撑,但看起来你训练它的次数并不多。具体来说,您的训练循环只遍历每个数据点一次。在数据中尝试多次循环。

【讨论】:

    猜你喜欢
    • 2017-01-28
    • 1970-01-01
    • 2017-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-19
    • 1970-01-01
    相关资源
    最近更新 更多