lpq29743 / RAM

A TensorFlow implementation for "Recurrent Attention Network on Memory for Aspect Sentiment Analysis"
MIT License
68 stars 21 forks source link

Using CNN with Bi-LSTM #8

Closed saja1994 closed 5 years ago

saja1994 commented 5 years ago

I am sorry for distributing you again, but please I want to use a CNN along with the Bi-LSTM to build the memory module. I have a problem output reshaping.

my code is the following:

with tf.name_scope('dynamic_rnn'):
    conv_outputs = []
    max_feature_length = self.max_sentence_len - max(self.filter_sizes) + 1
    # Convolutional layer with different lengths of filters in parallel
    # No max-pooling
    for i, filter_size in enumerate(self.filter_sizes):
        with tf.variable_scope('conv-%s' % filter_size):
            # [filter size, embedding size, channels, number of filters]
            filter_shape = [filter_size, self.embedding_dim, 1, self.num_filters]
            W = tf.get_variable('weights', filter_shape, initializer=tf.truncated_normal_initializer(stddev=0.1))
            b = tf.get_variable('biases', [self.num_filters], initializer=tf.constant_initializer(0.0))
            # Convolution
            conv = tf.nn.conv2d(inputs,
                                W,
                                strides=[1, 1, 1, 1],
                                padding='VALID',
                                name='conv')
            # Activation function
            h = tf.nn.relu(tf.nn.bias_add(conv, b), name='relu')

            # Remove channel dimension
            h_reshape = tf.squeeze(h, [2])
            # Cut the feature sequence at the end based on the maximum filter length
            h_reshape = h_reshape[:, :max_feature_length, :]

            conv_outputs.append(h_reshape)

    # Concatenate the outputs from different filters
    if len(self.filter_sizes) > 1:
        rnn_inputs = tf.concat(conv_outputs, -1)
    else:
        rnn_inputs = h_reshape

    # LSTM cells
    lstm_cell_fw = tf.contrib.rnn.LSTMCell(
        self.n_hidden,
        initializer=tf.orthogonal_initializer(),
    )
    lstm_cell_bw = tf.contrib.rnn.LSTMCell(
        self.n_hidden,
        initializer=tf.orthogonal_initializer(),
    )
    # Add dropout to LSTM cells
    lstm_cell_fw = tf.contrib.rnn.DropoutWrapper(lstm_cell_fw, output_keep_prob=self.dropout_keep_prob)
    lstm_cell_bw = tf.contrib.rnn.DropoutWrapper(lstm_cell_bw, output_keep_prob=self.dropout_keep_prob)

    # Feed the CNN outputs to LSTM network
    outputs, state, _ = tf.nn.static_bidirectional_rnn(
        lstm_cell_fw,
        lstm_cell_bw,
        tf.unstack(tf.transpose(rnn_inputs, perm=[1, 0, 2])),
        sequence_length=self.sentence_lens,
        dtype=tf.float32,
        scope='BiLSTM'
    )
    outputs = tf.reshape(tf.concat(outputs, 1), [-1, self.max_sentence_len, self.n_hidden * 2])
    batch_size = tf.shape(outputs)[0]

But the following error appeard to me:

Traceback (most recent call last):
  File "M:\Anaconda\envs\py3\lib\site-packages\tensorflow\python\client\session.py", line 1292, in _do_call
    return fn(*args)
  File "M:\Anaconda\envs\py3\lib\site-packages\tensorflow\python\client\session.py", line 1277, in _run_fn
    options, feed_dict, fetch_list, target_list, run_metadata)
  File "M:\Anaconda\envs\py3\lib\site-packages\tensorflow\python\client\session.py", line 1367, in _call_tf_sessionrun
    run_metadata)
tensorflow.python.framework.errors_impl.InvalidArgumentError: Input to reshape is a tensor with 1574400 values, but the requested shape requires a multiple of 51600
         [[{{node dynamic_rnn/Reshape}} = Reshape[T=DT_FLOAT, Tshape=DT_INT32, _class=["loc:@dynamic_rnn/TensorArrayUnstack/TensorArrayScatter/TensorArrayScatterV3"], _device="/job:localhost/replica:0/task:0/device:CPU:0"](dynamic_rnn/concat_83, dynamic_rnn/Reshape/shape)]]

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "main.py", line 89, in <module>
    tf.app.run()
  File "M:\Anaconda\envs\py3\lib\site-packages\tensorflow\python\platform\app.py", line 125, in run
    _sys.exit(main(argv))
  File "main.py", line 87, in main
    model.run(train_data, test_data)
  File "C:\Users\Saja\Desktop\RAM-master\model.py", line 359, in run
    train_loss, train_acc = self.train(train_data)
  File "C:\Users\Saja\Desktop\RAM-master\model.py", line 318, in train
    _, loss, step, summary = self.sess.run([self.optimizer, self.cost, self.global_step, self.train_summary_op], feed_dict=sample)
  File "M:\Anaconda\envs\py3\lib\site-packages\tensorflow\python\client\session.py", line 887, in run
    run_metadata_ptr)
  File "M:\Anaconda\envs\py3\lib\site-packages\tensorflow\python\client\session.py", line 1110, in _run
    feed_dict_tensor, options, run_metadata)
  File "M:\Anaconda\envs\py3\lib\site-packages\tensorflow\python\client\session.py", line 1286, in _do_run
    run_metadata)
  File "M:\Anaconda\envs\py3\lib\site-packages\tensorflow\python\client\session.py", line 1308, in _do_call
    raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.InvalidArgumentError: Input to reshape is a tensor with 1574400 values, but the requested shape requires a multiple of 51600
         [[{{node dynamic_rnn/Reshape}} = Reshape[T=DT_FLOAT, Tshape=DT_INT32, _class=["loc:@dynamic_rnn/TensorArrayUnstack/TensorArrayScatter/TensorArrayScatterV3"], _device="/job:localhost/replica:0/task:0/device:CPU:0"](dynamic_rnn/concat_83, dynamic_rnn/Reshape/shape)]]

Caused by op 'dynamic_rnn/Reshape', defined at:
  File "main.py", line 89, in <module>
    tf.app.run()
  File "M:\Anaconda\envs\py3\lib\site-packages\tensorflow\python\platform\app.py", line 125, in run
    _sys.exit(main(argv))
  File "main.py", line 86, in main
    model.build_model()
  File "C:\Users\Saja\Desktop\RAM-master\model.py", line 231, in build_model
    outputs = tf.reshape(tf.concat(outputs, 1), [-1, self.max_sentence_len, self.n_hidden * 2])
  File "M:\Anaconda\envs\py3\lib\site-packages\tensorflow\python\ops\gen_array_ops.py", line 7546, in reshape
    "Reshape", tensor=tensor, shape=shape, name=name)
  File "M:\Anaconda\envs\py3\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 787, in _apply_op_helper
    op_def=op_def)
  File "M:\Anaconda\envs\py3\lib\site-packages\tensorflow\python\util\deprecation.py", line 488, in new_func
    return func(*args, **kwargs)
  File "M:\Anaconda\envs\py3\lib\site-packages\tensorflow\python\framework\ops.py", line 3272, in create_op
    op_def=op_def)
  File "M:\Anaconda\envs\py3\lib\site-packages\tensorflow\python\framework\ops.py", line 1768, in __init__
    self._traceback = tf_stack.extract_stack()

InvalidArgumentError (see above for traceback): Input to reshape is a tensor with 1574400 values, but the requested shape requires a multiple of 51600
         [[{{node dynamic_rnn/Reshape}} = Reshape[T=DT_FLOAT, Tshape=DT_INT32, _class=["loc:@dynamic_rnn/TensorArrayUnstack/TensorArrayScatter/TensorArrayScatterV3"], _device="/job:localhost/replica:0/task:0/device:CPU:0"](dynamic_rnn/concat_83, dynamic_rnn/Reshape/shape)]]

Can you please help me to overcome this issue, Thanks

lpq29743 commented 5 years ago

I‘m sorry for the late reply. Have you solved the problem, and could you share the solution.

saja1994 commented 5 years ago

Unfortunately no but I'm still interested to find the solution. Thank you.

lpq29743 commented 5 years ago

The error occurred because you ignored the difference between "SAME" padding and "VALID" padding (see here).

Try the following code:

conv = tf.nn.conv2d(inputs, W, strides=[1, 1, 1, 1], padding='SAME', name='conv')