Coder Social home page Coder Social logo

lpq29743 / ram Goto Github PK

View Code? Open in Web Editor NEW
68.0 3.0 21.0 426 KB

A TensorFlow implementation for "Recurrent Attention Network on Memory for Aspect Sentiment Analysis"

License: MIT License

Python 100.00%
sentiment-analysis aspect-based-sentiment-analysis

ram's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

ram's Issues

运行代码中遇到了错误,想请教一下。

因为300维的glove太大,我使用了glove.6B.100d.txt,并将main.py中embedding_dim和n_hidden改成了100,但是运行中遇到了报错。请问您知道如何解决?谢谢。
Traceback (most recent call last):
File "main.py", line 47, in
tf.app.run()
File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/platform/app.py", line 40, in run
_run(main=main, argv=argv, flags_parser=_parse_flags_tolerate_undef)
File "/usr/local/lib/python3.6/dist-packages/absl/app.py", line 299, in run
_run_main(main, args)
File "/usr/local/lib/python3.6/dist-packages/absl/app.py", line 250, in _run_main
sys.exit(main(argv))
File "main.py", line 38, in main
FLAGS.word2vec = load_word_embeddings(FLAGS.embedding_fname, FLAGS.embedding_dim, FLAGS.word2id)
File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/platform/flags.py", line 88, in setattr
return self.dict['__wrapped'].setattr(name, value)
File "/usr/local/lib/python3.6/dist-packages/absl/flags/_flagvalues.py", line 499, in setattr
return self._set_unknown_flag(name, value)
File "/usr/local/lib/python3.6/dist-packages/absl/flags/_flagvalues.py", line 375, in _set_unknown_flag
raise _exceptions.UnrecognizedFlagError(name, value)
absl.flags._exceptions.UnrecognizedFlagError: Unknown command line flag 'word2vec'

Visualize the attention

Hello

Thank you for the implementation. Please, can you help me to visualize the attention weights at each hop as mentioned figures in the original paper?

Regards,

关于准确率的问题

您好请问您运行的准确率是多少?
我运行结果一个是78.9一个是70.1跟论文给的准确度差距比较大

关于变长序列的取平均操作

您的代码中使用aspect_inputs = tf.reduce_mean(aspect_inputs, 1)对target的词向量取平均,但是target是一个变长序列,这样取平均以padding后的max_len做分母,是不是应该以target的实际长度做分母?另外我用实际长度取平均时,效果却不如你这样好,我现在很困惑。。。

Using CNN with Bi-LSTM

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

Question

Hello,

Please, I want to ask you about the implemented Bidirectional LSTM (Bi-LSTM), is it a deep one? because it is mentioned in the RAM paper that the proposed approach uses 2 layers of (Bi-LSTM) but I can't see that in your code!

Thank you,

about the code

thanks for publishing the code, but i faced with an error. I run the code and add these lines in the main.py:
tf.app.flags.DEFINE_string('word2id', '', 'word to id')
tf.app.flags.DEFINE_string('max_sentence_len', '', 'max length of sentences')
tf.app.flags.DEFINE_string('max_aspect_len', '', 'max length of aspects')

after passing the first error, the new one appear. the error is :
There are 5198 words in the dataset, the max length of sentence is 79, and the max length of aspect is 23
Loading training data and testing data ...
Read 3602 sentences from data/restaurant/train.xml
Read 1120 sentences from data/restaurant/test.xml
Loading pre-trained word vectors ...
Traceback (most recent call last):
File "main.py", line 47, in
tf.app.run()
File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/platform/app.py", line 125, in run
_sys.exit(main(argv))
File "main.py", line 38, in main
FLAGS.word2vec = load_word_embeddings(FLAGS.embedding_fname, FLAGS.embedding_dim, FLAGS.word2id)
File "/content/RAM/utils.py", line 158, in load_word_embeddings
word2vec[word2id[content[0]]] = np.array(list(map(float, content[1:])))
ValueError: could not convert string to float: '.'

can you help me in this ? best regards

Help

Thank you for sharing this code. But please I need your help, when I run the code the following error appears to me:
File "main.py", line 45, in
tf.app.run()
File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/platform/app.py", line 125, in run
_sys.exit(main(argv))
File "main.py", line 29, in main
FLAGS.word2id, FLAGS.max_sentence_len, FLAGS.max_aspect_len = get_data_info(FLAGS.train_fname, FLAGS.test_fname, FLAGS.data_info, FLAGS.pre_processed)
File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/platform/flags.py", line 88, in setattr
return self.dict['__wrapped'].setattr(name, value)
File "/usr/local/lib/python3.6/dist-packages/absl/flags/_flagvalues.py", line 496, in setattr
return self._set_unknown_flag(name, value)
File "/usr/local/lib/python3.6/dist-packages/absl/flags/_flagvalues.py", line 374, in _set_unknown_flag
raise _exceptions.UnrecognizedFlagError(name, value)
absl.flags._exceptions.UnrecognizedFlagError: Unknown command line flag 'word2id'

please help me if you can.
Regards,

Glove Embedding

Can I use another embedding representation such as FastText, because I want to run this code for Arabic content and there is no Glove embedding for Arabic?

Thanks,

Help

Thank you for your effort. I need to add more features to word embeddings such as lexical features (positive, negative, and neutral) where each one of these features represented as a one-hot vector. So can you please help me to concatenate such features.

I have already tried to do that by adding the following code at utils.py:
def load_sentiment_dictionary():
pos_list = list()
neg_list = list()
sent_words_dict = dict()
fneg = open('pos.txt', 'r',encoding='utf8')
fpos = open('neg.txt', 'r',encoding='utf8')
for line in fpos:
if not line.strip() in sent_words_dict:
sent_words_dict[line.strip()] = 0
pos_list.append(line.strip())
for line in fneg:
if not line.strip() in sent_words_dict:
sent_words_dict[line.strip()] = 1
neg_list.append(line.strip())
fneg.close()
fpos.close()
return pos_list, neg_list, sent_words_dict
pos_list, neg_list, sent_words_dict = load_sentiment_dictionary()
sentiment_for_word_tmp = list()
with open('words.txt','r', encoding="utf8") as f:
for line in f:
for word in line:
words = line.strip().split(' ')
for word in words:
if word in pos_list:
sentiment_for_word_tmp.append(1)
elif word in neg_list:
sentiment_for_word_tmp.append(2)
else:
sentiment_for_word_tmp.append(0)
sentiment_for_word.append(sentiment_for_word_tmp)

and adding the following code at model.py:
self.tf_X_sent_for_word = tf.placeholder(tf.int32, shape = [None, self.max_sentence_len])
X_sent_for_word = tf.one_hot(self.tf_X_sent_for_word, 3, on_value = 20.0, off_value = 10.0, axis = -1)
sentences = tf.concat([self.sentences, X_sent_for_word], 2)
sentences = tf.cast(sentences,tf.float32)

but using this way I got the following error:
Traceback (most recent call last):
File "M:\Anaconda\envs\py3\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 455, in _apply_op_helper
as_ref=input_arg.is_ref)
File "M:\Anaconda\envs\py3\lib\site-packages\tensorflow\python\framework\ops.py", line 1209, in internal_convert_n_to_tensor
ctx=ctx))
File "M:\Anaconda\envs\py3\lib\site-packages\tensorflow\python\framework\ops.py", line 1144, in internal_convert_to_tensor
ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
File "M:\Anaconda\envs\py3\lib\site-packages\tensorflow\python\framework\ops.py", line 981, in _TensorTensorConversionFunction
(dtype.name, t.dtype.name, str(t)))
ValueError: Tensor conversion requested dtype int32 for Tensor with dtype float32: 'Tensor("inputs/one_hot:0", shape=(?, 86, 3), dtype=float32)'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "main_with_lex.py", line 55, in
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_with_lex.py", line 50, in main
model.build_model()
File "C:\Users\Saja\Desktop\RAM-master - Copy\model_with_lex.py", line 44, in build_model
sentences = tf.concat([self.sentences, X_sent_for_word], 2)
File "M:\Anaconda\envs\py3\lib\site-packages\tensorflow\python\ops\array_ops.py", line 1124, in concat
return gen_array_ops.concat_v2(values=values, axis=axis, name=name)
File "M:\Anaconda\envs\py3\lib\site-packages\tensorflow\python\ops\gen_array_ops.py", line 1201, in concat_v2
"ConcatV2", values=values, axis=axis, name=name)
File "M:\Anaconda\envs\py3\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 483, in _apply_op_helper
raise TypeError("%s that don't all match." % prefix)
TypeError: Tensors in list passed to 'values' of 'ConcatV2' Op have types [int32, float32] that don't all match.

Please if you can help me to implement this idea or to solve this error.
Thank you.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.