tensorflow - 使用 GradientTape 在 TensorFlow 2.0 线性回归示例中爆炸损失
问题描述
我正在尝试为多元线性回归构建一个小的教育示例,但是 LOSS 一直在增加,直到它爆炸而不是变得更小,知道吗?
import tensorflow as tf
tf.__version__
import numpy as np
data = np.array(
[
[100,35,35,12,0.32],
[101,46,35,21,0.34],
[130,56,46,3412,12.42],
[131,58,48,3542,13.43]
]
)
x = data[:,1:-1]
y_target = data[:,-1]
def loss_function(y, pred):
return tf.reduce_mean(tf.square(y - pred))
def train(b, w, x, y, lr=0.012):
with tf.GradientTape() as t:
current_loss = loss_function(y, linear_model(x))
lr_weight, lr_bias = t.gradient(current_loss, [w, b])
w.assign_sub(lr * lr_weight)
b.assign_sub(lr * lr_bias)
epochs = 80
for epoch_count in range(epochs):
real_loss = loss_function(y_target, linear_model(x))
train(b, w, x, y_target, lr=0.12)
print(f"Epoch count {epoch_count}: Loss value: {real_loss.numpy()}")
如果我使用“正确”值初始化权重(通过 scikit-learn 回归器发现),甚至会发生这种情况
w = tf.Variable([-1.76770250e-04,3.46688912e-01,2.43827475e-03],dtype=tf.float64)
b = tf.Variable(-11.837184241807234,dtype=tf.float64)
解决方案
以下是您如何将 TF2 优化器用于玩具示例(根据评论)。我知道这不是答案,但我不想在评论部分发布这个,因为它会弄乱缩进和所有这些。
tf_x = tf.Variable(tf.constant(2.0,dtype=tf.float32),name='x')
optimizer = tf.optimizers.SGD(learning_rate=0.1)
# Optimizing tf_x using gradient tape
x_series, y_series = [],[]
for step in range(5):
x_series.append(tf_x.numpy().item())
with tf.GradientTape() as tape:
tf_y = tf_x**2
gradients = tape.gradient(tf_y, tf_x)
optimizer.apply_gradients(zip([gradients], [tf_x]))
推荐阅读
- c# - 给游戏对象速度/速度并转换向量点
- python - 如何拆分列表中值的重复计数并显示实际值?
- spring - 在测试期间替换具有 Autowired 依赖项的 Spring bean
- perl - 如何使用 Perl 更新自定义 Bugzilla 扩展中的自定义 Bugzilla 字段?
- r - 如何将每月总计除以R中的季节性每月比率
- angular6 - 从循环标签中单击标签时是否有机会更改 ngModel 值
- ios - 即使我的应用程序不是前台应用程序,是否可以在 iOS 上运行持久 tcp 侦听套接字?
- php - 根据php中的概率返回随机数组键
- angular - 默认检查不适用于角度 2 单选按钮表单控件
- python - TypeError: float() 参数必须是字符串或数字,而不是“元组”