Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementation using tf.contrib.seq2seq. #7

Closed
MtDersvan opened this issue Jul 4, 2017 · 7 comments
Closed

Implementation using tf.contrib.seq2seq. #7

MtDersvan opened this issue Jul 4, 2017 · 7 comments

Comments

@MtDersvan
Copy link
Contributor

Hi, your Hierarchical Attention Networks for Document Classification (Yang et al., 2016) implementation looks very good, especially after bug fixes, nice work!

I wonder what it would take to reimplement your HANs with the new tf.contrib.seq2seq API (> r.1.2). There are some examples how to do encoder-decoder attention with seq2seq, but no examples of pure encoding ('reducing') attention.

Thank you very much!

@maoyanying
Copy link

Could you please tell me to generate yelp_academic_dataset_review.json?

@maoyanying
Copy link

Could you please tell me how to generate yelp_academic_dataset_review.json?

@MtDersvan
Copy link
Contributor Author

@maoyanying you need to register and download the dataset from here https://www.yelp.com/dataset_challenge. After, use the code in the README to launch the data prep:

python3 yelp_prepare.py yelp_academic_dataset_review.json

@maoyanying
Copy link

Thanks a lot.I have done it.When I use the code :
python3 worker.py --mode=eval
There is a erro:
File "worker.py", line 211, in
main()
File "worker.py", line 208, in main
evaluate(task.read_devset(epochs=1))
File "worker.py", line 142, in evaluate
model, _ = model_fn(s, restore_only=True)
File "worker.py", line 87, in HAN_model_1
is_training=is_training,
File "/home/bfs/Myy/NN/HATTdeep-text-classifier-master/HAN_model.py", line 64, in init
self._init_embedding(scope)
File "/home/bfs/Myy/NN/HATTdeep-text-classifier-master/HAN_model.py", line 101, in _init_embedding
dtype=tf.float32)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/variable_scope.py", line 988, in get_variable
custom_getter=custom_getter)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/variable_scope.py", line 890, in get_variable
custom_getter=custom_getter)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/variable_scope.py", line 348, in get_variable
validate_shape=validate_shape)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/variable_scope.py", line 333, in _true_getter
caching_device=caching_device, validate_shape=validate_shape)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/variable_scope.py", line 684, in _get_single_variable
validate_shape=validate_shape)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/variables.py", line 197, in init
expected_shape=expected_shape)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/variables.py", line 315, in _init_from_args
self._snapshot = array_ops.identity(self._variable, name="read")
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/gen_array_ops.py", line 1490, in identity
result = _op_def_lib.apply_op("Identity", input=input, name=name)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/op_def_library.py", line 763, in apply_op
op_def=op_def)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 2327, in create_op
original_op=self._default_original_op, op_def=op_def)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 1226, in init
self._traceback = _extract_stack()

FailedPreconditionError (see above for traceback): Attempting to use uninitialized value tcm/embedding/embedding_matrix
[[Node: tcm/embedding/embedding_matrix/read = IdentityT=DT_FLOAT, _class=["loc:@tcm/embedding/embedding_matrix"], _device="/job:localhost/replica:0/task:0/cpu:0"]]

How do you deal with it?

@MtDersvan
Copy link
Contributor Author

@maoyanying I did not encounter such an error. Check python and tensorflow version, works for me on python 3.5 and tensorflow 1.2.

@gaussic
Copy link

gaussic commented Jan 25, 2018

@MtDersvan Have you implemented it yet? I'm interested in implementing it recently.

@MtDersvan
Copy link
Contributor Author

Hi @gaussic ! Sorry for the late reply!
I don't think it was meant by tensorflow core team for that, I have asked them but to no avail(
So, I just implemented it in tensorflow without new seq2seq API.
It's actually quite easy:

def attention_reduction(self,
                        inputs,
                        output_size,
                        initializer=tf.contrib.layers.xavier_initializer(),
                        activation_fn=tf.tanh, 
                        scope=None):
    
    with tf.variable_scope(scope or 'Attention_Reduction') as scope:
        attention_context_vector = tf.get_variable(
            name='attention_context_vector',
            shape=[output_size],
            initializer=initializer,
            dtype=tf.float32)
        input_projection = tf.contrib.layers.fully_connected(
            inputs,
            output_size,
            activation_fn=activation_fn,
            scope=scope)
        vector_attention = tf.reduce_sum(
            tf.multiply(input_projection, attention_context_vector), 
            axis=2, 
            keep_dims=True)
        attention_weights = tf.nn.softmax(vector_attention, dim=1)
        weighted_projection = tf.multiply(input_projection, attention_weights)

        outputs = tf.reduce_sum(weighted_projection, axis=1)

        return outputs, attention_weights

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants