Fra*_*TEC 5 python nlp deep-learning tensorflow
在 85 个 epoch 之后,我的模型(具有 3 个 LSTM 层的 RNN)的损失(余弦距离)变为 NaN。为什么会发生这种情况,我该如何解决?我的模型的输出也变成 NaN。
我的模型:
tf.reset_default_graph()
seqlen = tf.placeholder(tf.int32, [None])
x_id = tf.placeholder(tf.int32, [None, None])
y_id = tf.placeholder(tf.int32, [None, None])
embeddings_matrix = tf.placeholder(np.float32, [vocabulary_size, embedding_size])
x_emb = tf.nn.embedding_lookup(embeddings_matrix, x_id)
y_emb = tf.nn.embedding_lookup(embeddings_matrix, y_id)
cells = [tf.contrib.rnn.LSTMCell(s, activation=a) for s, a in [(400, tf.nn.relu), (400, tf.nn.relu), (400, tf.nn.tanh)]]
cell = tf.contrib.rnn.MultiRNNCell(cells)
outputs, _ = tf.nn.dynamic_rnn(cell, x_emb, dtype=tf.float32, sequence_length=seqlen)
loss = tf.losses.cosine_distance(tf.nn.l2_normalize(outputs, 2), tf.nn.l2_normalize(y_emb, 2), 1)
tf.summary.scalar('loss', loss)
opt = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(loss)
merged = tf.summary.merge_all()
Run Code Online (Sandbox Code Playgroud)
训练输出:
Epoch 80/100
Time : 499 s Loss : 0.972911523852701 Val Loss : 0.9729658
Epoch 81/100
Time : 499 s Loss : 0.9723407568655597 Val Loss : 0.9718646
Epoch 82/100
Time : 499 s Loss : 0.9718870568505438 Val Loss : 0.971976
Epoch 83/100
Time : 499 s Loss : 0.9913996352643445 Val Loss : 0.990693
Epoch 84/100
Time : 499 s Loss : 0.9901496524596137 Val Loss : 0.98957264
Epoch 85/100
Time : 499 s Loss : nan Val Loss : nan
Epoch 86/100
Time : 498 s Loss : nan Val Loss : nan
Epoch 87/100
Time : 498 s Loss : nan Val Loss : nan
Epoch 88/100
Time : 499 s Loss : nan Val Loss : nan
Epoch 89/100
Time : 498 s Loss : nan Val Loss : nan
Epoch 90/100
Time : 498 s Loss : nan Val Loss : nan
Run Code Online (Sandbox Code Playgroud)
蓝色曲线是训练数据的损失,橙色曲线是验证数据的损失。
ADAM 使用的学习率为 0.001。
我的 x 和 y 的形状如下:[批量大小,最大序列长度],它们都设置为无,因为每个时期的最后一批较小,并且每个批次的最大序列长度发生变化。
x 和 y 通过嵌入查找并变为 [批量大小、最大序列长度、嵌入大小] 的形状,填充词的嵌入是一个向量为 0。
动态 rnn 获取每个序列的长度(代码中的 seqlen,形状为 [batch size]),因此它只会对每个序列的确切长度进行预测,其余的输出将用零向量填充,对于 y。
我的猜测是输出的值变得非常接近于零,一旦它们被平方来计算余弦距离,它们就会变成 0,所以它导致除以零。
余弦距离公式:
我不知道我是否正确,也不知道如何防止这种情况发生。
编辑:
我刚刚检查了每一层的权重,它们都是 NaN
已解决:
使用 l2 正则化有效。
tf.reset_default_graph()
seqlen = tf.placeholder(tf.int32, [None])
x_id = tf.placeholder(tf.int32, [None, None])
y_id = tf.placeholder(tf.int32, [None, None])
embeddings_matrix = tf.placeholder(np.float32, [vocabulary_size, embedding_size])
x_emb = tf.nn.embedding_lookup(embeddings_matrix, x_id)
y_emb = tf.nn.embedding_lookup(embeddings_matrix, y_id)
cells = [tf.contrib.rnn.LSTMCell(s, activation=a) for s, a in [(400, tf.nn.relu), (400, tf.nn.relu), (400, tf.nn.tanh)]]
cell = tf.contrib.rnn.MultiRNNCell(cells)
outputs, _ = tf.nn.dynamic_rnn(cell, x_emb, dtype=tf.float32, sequence_length=seqlen)
regularizer = tf.reduce_sum([tf.nn.l2_loss(v) for v in tf.trainable_variables()])
cos_distance = tf.losses.cosine_distance(tf.nn.l2_normalize(outputs, 2), tf.nn.l2_normalize(y_emb, 2), 1)
loss = cos_distance + beta * regularizer
opt = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(loss)
tf.summary.scalar('loss', loss)
tf.summary.scalar('regularizer', regularizer)
tf.summary.scalar('cos_distance', cos_distance)
merged = tf.summary.merge_all()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2067 次 |
| 最近记录: |