import tensorflow as tf
import os
import numpy as np
x = tf.placeholder(tf.float32,[None,1], name='x')
y = tf.placeholder(tf.float32,[None,1], name='y')
w = tf.Variable(initial_value=tf.constant(0.0))# 定义一个计算步数的变量
global_steps = tf.Variable(initial_value=0, trainable=False)# global_steps = tf.train.get_or_create_global_step()
learning_rate = tf.train.exponential_decay(
learning_rate=0.1,
global_step=global_steps,
decay_steps=10,
decay_rate=0.9)# 定义损失
loss = tf.pow(w*x - y,2)
train_opt = tf.train.GradientDescentOptimizer(learning_rate=learning_rate).\
minimize(loss, global_step=global_steps)# 构建会话
epochs =3
batch_cnt =10with tf.Session()as sess:
sess.run(tf.global_variables_initializer())for e inrange(epochs):for batch inrange(batch_cnt):
feed ={x: np.linspace(1,2,10).reshape([10,1]),
y: np.linspace(1,2,10).reshape([10,1])}
sess.run(train_opt, feed)print(sess.run(learning_rate))print(sess.run(global_steps))
D:\Anaconda\python.exe D:/AI20/HJZ/04-深度学习/2-TensorFlow基础/tf-代码/11_学习率的衰减.py
2019-12-0521:46:32.379345: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX AVX2
0.0989519310.0979148420.0968886230.09587315540.09486832550.0938740460.09289016670.0919166180.0909532590.089999996100.08905673110.08812335120.087199755130.08628584140.0853815150.08448663160.08360115170.08272495180.08185793190.08099999200.08015105210.07931101220.078479774230.077657245240.07684334250.076037966260.07524103270.07445245280.07367214290.0728999930
Process finished with exit code 0