ITPub博客

首页 > 应用开发 > > tensorflow一段代码读懂一个道理: 正向传播,反向传播,网络模型

tensorflow一段代码读懂一个道理: 正向传播,反向传播,网络模型

作者:wh7577 时间:2019-08-29 13:45:24 0 删除 编辑
import simple_and_naive_tensorflow as tf
import numpy as np


# Prepare train data
# w=2, b=10, err=0.33
train_X = np.linspace(-1, 1, 100)
train_Y = 2 * train_X + np.random.randn(*train_X.shape) * 0.33 + 10


# Define the model
X = tf.placeholder("float", "X")
Y = tf.placeholder("float", "Y")
w = tf.Variable(0.0, name="weight")
b = tf.Variable(0.0, name="bias")
loss = tf.square(Y - X * w - b)
train_op = tf.GradientDescentOptimizer(0.01).minimize(loss)


# Create session to run
with tf.Session() as sess:
    sess.run(tf.initialize_all_variables())


    epoch = 1
    for i in range(10):
        for (x, y) in zip(train_X, train_Y):
            _, w_value, b_value = sess.run([train_op, w, b],feed_dict={X: x, Y: y})
        print("Epoch: {}, w: {}, b: {}".format(epoch, w_value, b_value))
        epoch += 1


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/2480/viewspace-3130047/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-08-23

  • 博文量
    105
  • 访问量
    15385