train的过程其实就是修改计算图中的tf.Variable的过程。
import tensorflow as tf
label = tf.constant(1,dtype = tf.float32)
prediction_to_train = tf.Variable(3,dtype=tf.float32) # 指定了待训练的变量(参数)
manual_compute_loss = tf.square(prediction_to_train - label) # 指定损失函数为待训练参数与常量1的平方
optimizer = tf.train.GradientDescentOptimizer(0.01) # 选择优化器和对应的参数
train_step = optimizer.minimize(manual_compute_loss) # 使用梯度下降优化损失函数
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
for _ in range(10):
print('variable is ', sess.run(prediction_to_train), ' and the loss is ',sess.run(manual_compute_loss))
sess.run(train_step)
variable is 3.0 and the loss is 4.0
variable is 2.96 and the loss is 3.8416002
variable is 2.9208 and the loss is 3.6894724
variable is 2.882384 and the loss is 3.5433698
variable is 2.8447363 and the loss is 3.403052
variable is 2.8078415 and the loss is 3.268291
。。。。。。。
。。。
variable is 2.0062745 and the loss is 1.0125883
variable is 1.986149 and the loss is 0.9724898
variable is 1.966426 and the loss is 0.9339792
。。。。
。。。
variable is 1.0000029 and the loss is 8.185452e-12
variable is 1.0000029 and the loss is 8.185452e-12
variable is 1.0000029 and the loss is 8.185452e-12
variable is 1.0000029 and the loss is 8.185452e-12
variable is 1.0000029 and the loss is 8.185452e-12
根据train是修改计算图中tf.Variable(默认是计算图中所有tf.Variable,可以通过var_list指定)的事实,可以使用tf.constant或者python变量的形式来规避常量被训练,这也是迁移学习要用到的技巧。
label = tf.constant(1,dtype = tf.float32) # 标签
x = tf.placeholder(dtype = tf.float32)
w1 = tf.Variable(4,dtype=tf.float32) # 待训练参数1
w2 = tf.Variable(4,dtype=tf.float32) # 待训练参数2
w3 = tf.constant(4,dtype=tf.float32) # 常量权值
y_predict = w1*x+w2*x+w3*x # 函数的输出
make_up_loss = tf.square(y_predict - label)
optimizer = tf.train.GradientDescentOptimizer(0.01)
train_step = optimizer.minimize(make_up_loss)
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
for _ in range(2):
w1_,w2_,w3_,loss_ = sess.run([w1,w2,w3,make_up_loss],feed_dict={x:1})
print('variable is w1:',w1_,' w2:',w2_,' w3:',w3_, ' and the loss is ',loss_)
sess.run(train_step,{x:1})
variable is w1: -1.4999986 w2: -1.4999986 w3: 4.0 and the loss is 8.185452e-12
variable is w1: -1.4999986 w2: -1.4999986 w3: 4.0 and the loss is 8.185452e-12
label = tf.constant(1,dtype = tf.float32)
x = tf.placeholder(dtype = tf.float32)
w1 = tf.Variable(4,dtype=tf.float32)
w2 = tf.Variable(4,dtype=tf.float32)
w3 = tf.constant(4,dtype=tf.float32)
y_predict = w1*x+w2*x+w3*x
#define losses and train
make_up_loss = tf.square(y_predict - label)
optimizer = tf.train.GradientDescentOptimizer(0.01)
train_step = optimizer.minimize(make_up_loss,var_list = w2) # 指定待训练的变量为w2
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
for _ in range(7):
w1_,w2_,w3_,loss_ = sess.run([w1,w2,w3,make_up_loss],feed_dict={x:1})
print('variable is w1:',w1_,' w2:',w2_,' w3:',w3_, ' and the loss is ',loss_)
sess.run(train_step,{x:1})
variable is w1: 4.0 w2: -6.99948 w3: 4.0 and the loss is 2.7063857e-07
variable is w1: 4.0 w2: -6.9994903 w3: 4.0 and the loss is 2.5983377e-07
variable is w1: 4.0 w2: -6.9995003 w3: 4.0 and the loss is 2.4972542e-07
variable is w1: 4.0 w2: -6.9995103 w3: 4.0 and the loss is 2.398176e-07
variable is w1: 4.0 w2: -6.9995203 w3: 4.0 and the loss is 2.3011035e-07
variable is w1: 4.0 w2: -6.99953 w3: 4.0 and the loss is 2.2105178e-07
variable is w1: 4.0 w2: -6.9995394 w3: 4.0 and the loss is 2.1217511e-07
若无可训练的 tf.Variable 变量,则会报错:
ValueError: No variables to optimize.
用var_list指定一个 tf.constant 常量,也无法实现:
NotImplementedError: ('Trying to update a Tensor ', )
label = tf.constant(1,dtype = tf.float32)
x = tf.placeholder(dtype = tf.float32)
w1 = tf.Variable(4,dtype=tf.float32)
with tf.name_scope(name='selected_variable_to_trian'):
w2 = tf.Variable(4,dtype=tf.float32)
w3 = tf.constant(4,dtype=tf.float32)
y_predict = w1*x+w2*x+w3*x
make_up_loss = (y_predict - label)**3
optimizer = tf.train.GradientDescentOptimizer(0.01)
output_vars = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope='selected_variable_to_trian') # 指定训练的变量域
train_step = optimizer.minimize(make_up_loss,var_list = output_vars)
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
for _ in range(3000):
w1_,w2_,w3_,loss_ = sess.run([w1,w2,w3,make_up_loss],feed_dict={x:1})
print('variable is w1:',w1_,' w2:',w2_,' w3:',w3_, ' and the loss is ',loss_)
sess.run(train_step,{x:1})
variable is w1: 4.0 w2: -6.988893 w3: 4.0 and the loss is 1.3702081e-06
variable is w1: 4.0 w2: -6.988897 w3: 4.0 and the loss is 1.3687968e-06
variable is w1: 4.0 w2: -6.9889007 w3: 4.0 and the loss is 1.3673865e-06
variable is w1: 4.0 w2: -6.9889045 w3: 4.0 and the loss is 1.3659771e-06
variable is w1: 4.0 w2: -6.9889083 w3: 4.0 and the loss is 1.3645688e-06
variable is w1: 4.0 w2: -6.988912 w3: 4.0 and the loss is 1.3631613e-06
variable is w1: 4.0 w2: -6.988916 w3: 4.0 and the loss is 1.3617548e-06
variable is w1: 4.0 w2: -6.9889197 w3: 4.0 and the loss is 1.3603493e-06
转自:https://mp.csdn.net/mdeditor/97611750