原文链接: tfe 简单 案例 自动优化 线性拟合
上一篇: 使用conda 配置tf2 开发环境
下一篇: tfe 配合 Keras model 线性拟合 和 自己处理梯度进行线性拟合
自动计算梯度
import tensorflow as tf
import tensorflow.contrib.eager as tfe
tf.enable_eager_execution()
def leaky_relu(x):
if x < 0:
return x * 0.1
else:
return x
grad = tfe.gradients_function(leaky_relu)
print(grad(4.0))
print(grad(-3.0))
[<tf.Tensor: id=5, shape=(), dtype=float32, numpy=1.0>]
[<tf.Tensor: id=15, shape=(), dtype=float32, numpy=0.1>]
自动根据梯度优化数值
import tensorflow as tf
tf.enable_eager_execution()
w = tf.get_variable("w", initializer=3.0)
def loss(x):
return x * w
optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.1)
for i in range(5):
optimizer.minimize(lambda: loss(5))
print(w.numpy())
# 2.5
# 2.0
# 1.5
# 1.0
# 0.5
学习率为0.01
2.95
2.9
2.8500001
2.8000002
2.7500002
线性拟合
import tensorflow as tf
import numpy as np
tf.enable_eager_execution()
def get_data():
x = np.random.random()
y = 2 * x - 1
return x, y
lr = .01
train_step = 10000
show_step = 100
w = tf.Variable(0.)
b = tf.Variable(0.)
def loss():
x, y = get_data()
out = x * w + b
return (out - y) ** 2
def main():
train = tf.train.AdamOptimizer(lr)
for i in range(1, 1 + train_step):
train.minimize(loss)
if not i % show_step:
print(i, w.numpy(), b.numpy())
if __name__ == '__main__':
main()
9500 2.0 -1.0
9600 2.0 -1.0
9700 2.0000002 -1.0000008
9800 1.9999999 -1.0000001
9900 1.9999962 -0.9999975
10000 1.9999998 -0.99999946