python - 当输入向量中有一些零时,运行 biLstm 模型将得到“浮动错误 8”
问题描述
我自己做填充过程,所以“辩论”、“理由”、“索赔”或“授权”中有一些 0 值。放入 BiLSTM 架构将得到“浮动错误 8”,无需任何其他提醒。
此错误意味着某些数字除以 0 或索引超出范围。
但在模型中,不应有任何数字除以 0。
这是代码:
debate = tf.placeholder(tf.float32,[None,48,300])
reason = tf.placeholder(tf.float32,[None,48,300])
claim = tf.placeholder(tf.float32,[None,48,300])
warrant = tf.placeholder(tfenter code here.float32,[None,48,300])
y = tf.placeholder(tf.float32,[None,2])
n_hidden = 300
w = weight_variable([n_hidden,2])
b = bias_variable([2])
def bilstm(x, weights, biases):
lstm_f = tf.contrib.rnn.LSTMCell(n_hidden, forget_bias = 1.0)
lstm_b = tf.contrib.rnn.LSTMCell(n_hidden, forget_bias = 1.0)
(alloutputs, output_states) = tf.nn.bidirectional_dynamic_rnn(lstm_f, lstm_b, x, dtype = tf.float32)
(outputs, state) = output_states
(output_state_fw, output_state_bw) = state
return tf.matmul(tf.add(output_state_fw, output_state_bw), weights) + biases
final_representation = tf.concat([debate,reason,claim,warrant],1)
prediction = bilstm(final_representation,w,b)
cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y,logits=prediction))
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
当我将相同的输入放入正常的 RNN 架构时,它可以工作。这是可以运行的 RNN 代码。
def RNN(X,weights,biases):
lstm_cell = tf.nn.rnn_cell.LSTMCell(n_hidden,use_peepholes=True)
#final_state[0] cell state
#final_state[1] hidden_state
outputs,final_state = tf.nn.dynamic_rnn(lstm_cell,X,dtype=tf.float32)
results = tf.nn.softmax(tf.matmul(final_state[1],weights)+biases)
return results
谁能告诉我发生了什么?我误解了 biLSTM 模型吗?biLSTM中不应该有任何数字除以0!
提前致谢。
解决方案
推荐阅读
- c++ - type_traits 中检查类型是否为 const char* 的函数是什么?
- javascript - JQuery AJAX url 调用 - 有时不起作用
- php - 查询具有关系的模型字段
- azure - 如何通过命令行将 Azure 服务主体详细信息传递给 Ansible?
- javascript - 如何通过 Web 应用程序的跨浏览器播放 .h264(例如:globalwarming.h264)视频文件
- git - 解决远程 git 上的合并冲突问题
- html - 如何将逗号放在菜单的一个列表单词中
- c# - 无法使用弹出表单将新数据创建到数据库中
- tooltip - 在 Angular 中使用自定义组件作为工具提示
- tensorflow - tf.nn.dilation2d 如何计算梯度并学习其过滤器