线性拟合"/>
matlab错误使用tfe,tfe 简单 案例 自动优化 线性拟合
自动计算梯度
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))
[]
[]
自动根据梯度优化数值
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
更多推荐
matlab错误使用tfe,tfe 简单 案例 自动优化 线性拟合
发布评论