首先,我是tensorflow和nn领域的新手,所以基本上我发现了一个非常适合我的要求的出色实现:
An example for Natural Language Processing (NER)
有两层:char。提取和上下文提取 我的目标是在两层上都使用GRU单元。
对于上下文层,我摆脱了时间主变换和切换单元格类型的困扰,
with tf.variable_scope("words"):
cell_fw = tf.contrib.rnn.GRUCell(params['lstm_size'])
cell_bw = tf.contrib.rnn.GRUCell(params['lstm_size'])
(output_fw, output_bw), _ = tf.nn.bidirectional_dynamic_rnn(
cell_fw, cell_bw, embeddings,
sequence_length=nwords, dtype=tf.float32)
output = tf.concat([output_fw, output_bw], axis=-1)
output = tf.layers.dropout(output, rate=dropout, training=training)
所以我在char层上尝试了同样的事情
num_of_chars = tf.reshape(nchars, [-1])
with tf.variable_scope("chars"):
cell_fw = tf.contrib.rnn.GRUCell(params['char_lstm_size'])
cell_bw = tf.contrib.rnn.GRUCell(params['char_lstm_size'])
_, ((_, output_fw), (_, output_bw)) = tf.nn.bidirectional_dynamic_rnn(cell_fw,
cell_bw, flat, sequence_length=num_of_chars, dtype=tf.float32)
output = tf.concat([output_fw, output_bw], axis=-1)
char_embeddings = tf.reshape(output, [-1, dim_words, 50])
但是我遇到了错误:
“仅当急切执行为” TypeError时,Tensor对象是可迭代的: 仅在启用急切执行时,Tensor对象才是可迭代的。至 使用tf.map_fn迭代此张量。
我找不到解决方案,“ tf.enable_eager_execution()”不能完成任务。
谢谢