tfe 简单 案例 自动优化 线性拟合

原文链接: 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值