Closed PrivateThink closed 6 years ago
你把修改后的代码贴一下
注释的是你的代码,其他是修改的
# aspect_outputs, aspect_state = tf.nn.dynamic_rnn(
# tf.contrib.rnn.LSTMCell(self.n_hidden),
# inputs=aspect_inputs,
# sequence_length=self.aspect_lens,
# dtype=tf.float32,
# scope='aspect_lstm'
# )
# Forward direction cell
aspect_lstm_fw_cell = tf.contrib.rnn.LSTMCell(self.n_hidden)
# Backward direction cell
aspect_lstm_bw_cell = tf.contrib.rnn.LSTMCell(self.n_hidden)
#双向的LSTM
print("aspect_inputs",aspect_inputs)
aspect_outputs, _, =tf.nn.bidirectional_dynamic_rnn(cell_fw=aspect_lstm_fw_cell,
cell_bw=aspect_lstm_bw_cell,
inputs=aspect_inputs,
sequence_length=self.aspect_lens,
dtype=tf.float32,
scope='aspect_bilstm')
## Concat the outputs to create the new input.
aspect_outputs=tf.concat(aspect_outputs, 2)
batch_size = tf.shape(aspect_outputs)[0]
aspect_avg = tf.reduce_mean(aspect_outputs, 1)
# context_outputs, context_state = tf.nn.dynamic_rnn(
# tf.contrib.rnn.LSTMCell(self.n_hidden),
# inputs=context_inputs,
# sequence_length=self.context_lens,
# dtype=tf.float32,
# scope='context_lstm'
# )
context_lstm_fw_cell = tf.contrib.rnn.LSTMCell(self.n_hidden)
# Backward direction cell
context_lstm_bw_cell = tf.contrib.rnn.LSTMCell(self.n_hidden)
# 双向的LSTM
context_outputs, _ = tf.nn.bidirectional_dynamic_rnn(cell_fw=context_lstm_fw_cell,
cell_bw=context_lstm_bw_cell,
inputs=context_inputs,
sequence_length=self.context_lens,
dtype=tf.float32,
scope='context_bilstm')
context_outputs=tf.concat(context_outputs, 2)
context_avg = tf.reduce_mean(context_outputs, 1)
粗略看好像没什么问题,具体的问题是?
这个是错误,劳烦您看下 Training ... Traceback (most recent call last): File "C:\software\Python35\lib\site-packages\tensorflow\python\client\session.py", line 1361, in _do_call return fn(*args) File "C:\software\Python35\lib\site-packages\tensorflow\python\client\session.py", line 1340, in _run_fn target_list, status, run_metadata) File "C:\software\Python35\lib\site-packages\tensorflow\python\framework\errors_impl.py", line 516, in exit c_api.TF_GetCode(self.status.status)) tensorflow.python.framework.errors_impl.InvalidArgumentError: Matrix size-incompatible: In[0]: [23,600], In[1]: [300,300] [[Node: dynamic_rnn/while/MatMul = MatMul[T=DT_FLOAT, transpose_a=false, transpose_b=false, _device="/job:localhost/replica:0/task:0/device:CPU:0"](dynamic_rnn/while/TensorArrayReadV3, dynamic_rnn/while/MatMul/Enter)]]
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "G:/paperCode/IAN-master/main.py", line 44, in
Caused by op 'dynamic_rnn/while/MatMul', defined at:
File "G:/paperCode/IAN-master/main.py", line 44, in
InvalidArgumentError (see above for traceback): Matrix size-incompatible: In[0]: [23,600], In[1]: [300,300] [[Node: dynamic_rnn/while/MatMul = MatMul[T=DT_FLOAT, transpose_a=false, transpose_b=false, _device="/job:localhost/replica:0/task:0/device:CPU:0"](dynamic_rnn/while/TensorArrayReadV3, dynamic_rnn/while/MatMul/Enter)]]
双向 LSTM 输出的隐藏向量的 size 是单向 LSTM 输出的隐藏向量的 size 的两倍,所以你需要修改权值的大小
如果将源码中的LSM换成双向的LSTM,应该如何修改代码呢? 我自己修改了,但是出现问题了,麻烦您能看看怎么解决