#coding:utf-8
#设损失函数 loss=(w+1)^2,令w初值为常数5.反向传播就是求最优w，即求最小loss对应的w的值
import tensorflow as tf
#定义待优化参数w的初值赋5
w=tf.Variable(tf.constant(5,dtype=tf.float32))
#定义损失函数loss
loss=tf.square(w+1)
#定义反向传播方法
train_step=tf.train.GradientDescentOptimizer(0.2).minimize(loss)
#生成会话，训练40轮
with tf.Session() as sess:
    init_op=tf.global_variables_initializer()
    sess.run(init_op)
    for i in range(40):
        sess.run(train_step)
        w_val=sess.run(w)
        loss_val=sess.run(loss)
        print("After %s steps: w is %f, loss is %f."%(i,w_val,loss_val))
#学习率改为1
train_step=tf.train.GradientDescentOptimizer(1).minimize(loss)
with tf.Session() as sess:
    init_op=tf.global_variables_initializer()
    sess.run(init_op)
    for i in range(40):
        sess.run(train_step)
        w_val=sess.run(w)
        loss_val=sess.run(loss)
        print("After %s steps: w is %f, loss is %f."%(i,w_val,loss_val))

45b64942fb3f8cfd596b40745d9a6d1438a70ec6
<mask token>

<mask token>
with tf.Session() as sess:
    init_op = tf.global_variables_initializer()
    sess.run(init_op)
    for i in range(40):
        sess.run(train_step)
        w_val = sess.run(w)
        loss_val = sess.run(loss)
        print('After %s steps: w is %f, loss is %f.' % (i, w_val, loss_val))
<mask token>
with tf.Session() as sess:
    init_op = tf.global_variables_initializer()
    sess.run(init_op)
    for i in range(40):
        sess.run(train_step)
        w_val = sess.run(w)
        loss_val = sess.run(loss)
        print('After %s steps: w is %f, loss is %f.' % (i, w_val, loss_val))

<mask token>
w = tf.Variable(tf.constant(5, dtype=tf.float32))
loss = tf.square(w + 1)
train_step = tf.train.GradientDescentOptimizer(0.2).minimize(loss)
with tf.Session() as sess:
    init_op = tf.global_variables_initializer()
    sess.run(init_op)
    for i in range(40):
        sess.run(train_step)
        w_val = sess.run(w)
        loss_val = sess.run(loss)
        print('After %s steps: w is %f, loss is %f.' % (i, w_val, loss_val))
train_step = tf.train.GradientDescentOptimizer(1).minimize(loss)
with tf.Session() as sess:
    init_op = tf.global_variables_initializer()
    sess.run(init_op)
    for i in range(40):
        sess.run(train_step)
        w_val = sess.run(w)
        loss_val = sess.run(loss)
        print('After %s steps: w is %f, loss is %f.' % (i, w_val, loss_val))

import tensorflow as tf
w = tf.Variable(tf.constant(5, dtype=tf.float32))
loss = tf.square(w + 1)
train_step = tf.train.GradientDescentOptimizer(0.2).minimize(loss)
with tf.Session() as sess:
    init_op = tf.global_variables_initializer()
    sess.run(init_op)
    for i in range(40):
        sess.run(train_step)
        w_val = sess.run(w)
        loss_val = sess.run(loss)
        print('After %s steps: w is %f, loss is %f.' % (i, w_val, loss_val))
train_step = tf.train.GradientDescentOptimizer(1).minimize(loss)
with tf.Session() as sess:
    init_op = tf.global_variables_initializer()
    sess.run(init_op)
    for i in range(40):
        sess.run(train_step)
        w_val = sess.run(w)
        loss_val = sess.run(loss)
        print('After %s steps: w is %f, loss is %f.' % (i, w_val, loss_val))

#coding:utf-8
#设损失函数 loss=(w+1)^2,令w初值为常数5.反向传播就是求最优w，即求最小loss对应的w的值
import tensorflow as tf
#定义待优化参数w的初值赋5
w=tf.Variable(tf.constant(5,dtype=tf.float32))
#定义损失函数loss
loss=tf.square(w+1)
#定义反向传播方法
train_step=tf.train.GradientDescentOptimizer(0.2).minimize(loss)
#生成会话，训练40轮
with tf.Session() as sess:
    init_op=tf.global_variables_initializer()
    sess.run(init_op)
    for i in range(40):
        sess.run(train_step)
        w_val=sess.run(w)
        loss_val=sess.run(loss)
        print("After %s steps: w is %f, loss is %f."%(i,w_val,loss_val))
#学习率改为1
train_step=tf.train.GradientDescentOptimizer(1).minimize(loss)
with tf.Session() as sess:
    init_op=tf.global_variables_initializer()
    sess.run(init_op)
    for i in range(40):
        sess.run(train_step)
        w_val=sess.run(w)
        loss_val=sess.run(loss)
        print("After %s steps: w is %f, loss is %f."%(i,w_val,loss_val))
