1 Star 0 Fork 0

walker_sue/Tensorflow2-Tutorial

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
convert-from-old-version.py 855 Bytes
一键复制 编辑 原始数据 按行查看 历史
morvanzhou 提交于 2019-10-22 15:53 . update
import tensorflow as tf
import numpy as np
data_x = np.random.normal(size=[1000, 1])
noise = np.random.normal(size=[1000, 1]) * 0.2
data_y = data_x * 3. + 2. + noise
class Model:
def __init__(self):
super(Model, self).__init__()
self.w = tf.Variable(0.1, dtype=tf.float32)
self.b = tf.Variable(0.1, dtype=tf.float32)
def call(self, x):
return self.w * x + self.b
model = Model()
var_list = [model.w, model.b]
opt = tf.optimizers.SGD(0.1)
for t in range(100):
with tf.GradientTape() as tape:
y_ = model.call(data_x)
loss = tf.reduce_mean(tf.square(data_y - y_))
grad = tape.gradient(loss, var_list)
opt.apply_gradients(zip(grad, var_list))
if t % 10 == 0:
print("loss={:.2f} | w={:.2f} | b={:.2f}".format(
loss, model.w.numpy(), model.b.numpy())
)
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/walker-sue/Tensorflow2-Tutorial.git
git@gitee.com:walker-sue/Tensorflow2-Tutorial.git
walker-sue
Tensorflow2-Tutorial
Tensorflow2-Tutorial
master

搜索帮助