为什么这种简单的Keras模型不收敛?
损耗函数不接近0。它似乎不会融合,并且始终无法预测y。 我尝试使用初始化器,激活和层尺寸。这里的任何见解将不胜感激。
import tensorflow as tf
import keras
activation = 'relu'
initializer = 'he_uniform'
input_layer = tf.keras.layers.Input(shape=(1,),batch_size=1)
dense_layer = keras.layers.Dense(
32,
activation=activation,
kernel_initializer=initializer
)(input_layer)
dense_layer = keras.layers.Dense(
32,
activation=activation,
kernel_initializer=initializer
)(dense_layer)
predictions = keras.layers.Dense(1)(
dense_layer
)
model = keras.models.Model(inputs=input_layer, outputs=[predictions])
model.summary()
optimizer = tf.keras.optimizers.Adam(learning_rate=0.0001)
x = tf.constant([[727.], [1424.], [379], [1777], [51.]])
y = tf.constant([[1.], [1.], [0.], [1.], [0.]])
for item in tf.data.Dataset.from_tensor_slices((x,y)).shuffle(5).repeat():
with tf.GradientTape() as tape:
x = item[0]
output = model(x)
loss = keras.losses.BinaryCrossentropy(
from_logits=True
)(item[1], output)
# loss = item[1] - output[0]
dy_dx = tape.gradient(loss, model.trainable_variables)
optimizer.apply_gradients(zip(dy_dx, model.trainable_weights))
print("batch", item[0], "x", "output", output, "expected", item[1], "gradient", dy_dx[-1])
print("loss", loss)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的输入数量很大,这会导致数值问题,并且您不会批量输入,这可能会导致每个批处理产生非常大的梯度(再次,由于输入数量很大),可能是不同的方向。当我将
.batch(5)
添加到数据集定义时,它可以正常工作shuffle
,因为每个批次包含完整的数据集)以改善梯度估计值,这应该很快收敛。
和注意:您使用 no 激活功能与二进制跨透明镜“从logits”是正确的,因此请忽略人们告诉您其他情况。
Your input numbers are huge which leads to numerical issues, and you are not batching your inputs which leads to each batch producing very large gradients (again, due to the large input numbers) in possibly different directions. It works fine when I
.batch(5)
to the dataset definition (in fact, just replacedshuffle
because every batch contains the full dataset) to improve the gradient estimates,This should converge very quickly.
And note: You using no activation function with a binary cross-entropy "from logits" is correct, so ignore people telling you otherwise.
您的输出层 -
预测
- 缺少激活。 无激活
参数。从您的代码看来您正在进行二进制分类,因此输出层应具有'Sigmoid'
激活。推断时,请确保将模型的输出舍入0或1以获取预测。
Your output layer -
predictions
- is missing an activation.keras.layers.Dense
has a default value ofNone
for theactivation
parameter. From your code it looks like you are doing binary classification, therefore your output layer should have a'sigmoid'
activation.On inference be sure to round the output of the model to 0 or 1 to get the predictions.