tensorflow学习(二)

# 线性回归
import tensorflow as tf

# 定义输入x,y
x = tf.placeholder(tf.float32, shape=[5], name='x')
y = tf.placeholder(tf.float32, shape=[5], name='y')
# 定义变量w,b
w = tf.Variable(0., dtype=tf.float32, name='weight')
b = tf.Variable(0., dtype=tf.float32, name='bias')
# 得到预测值
predict = w * x + b
# 最小二乘法获得损失
loss = tf.reduce_mean(tf.square(predict - y))
# 梯度下降法获取最小损失
optimizer = tf.train.GradientDescentOptimizer(0.01).minimize(loss=loss)
# 初始化
init = tf.global_variables_initializer()
# 训练
with tf.Session() as sess:
    sess.run(init)
    for index in range(2000):
        sess.run(optimizer, feed_dict={x: [1, 2, 3, 4, 5], y: [6, 7, 8, 9, 10]})
        print("weight = ", sess.run(w), ",bias = ", sess.run(b))
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容