richliao / textClassifier

Text classifier for Hierarchical Attention Networks for Document Classification
Apache License 2.0
1.07k stars 379 forks source link

textClassifierRNN.py:'TensorVariable' object has no attribute 'get_value' #10

Closed tyronedong closed 7 years ago

tyronedong commented 7 years ago

When I implement the model in textClassifierRNN.py, I got an error and don't know how to fix it.

Traceback (most recent call last): File "lstm_text_classification.py", line 241, in model.fit(x_train, y_train, nb_epoch=15, shuffle=True, validation_split=0.1) File "/usr/local/lib/python3.4/dist-packages/keras/engine/training.py", line 1494, in fit self._make_train_function() File "/usr/local/lib/python3.4/dist-packages/keras/engine/training.py", line 1018, in _make_train_function self.total_loss) File "/usr/local/lib/python3.4/dist-packages/keras/optimizers.py", line 416, in get_updates shapes = [K.get_variable_shape(p) for p in params] File "/usr/local/lib/python3.4/dist-packages/keras/optimizers.py", line 416, in shapes = [K.get_variable_shape(p) for p in params] File "/usr/local/lib/python3.4/dist-packages/keras/backend/theano_backend.py", line 1162, in get_variable_shape return x.get_value(borrow=True, return_internal_type=True).shape AttributeError: 'TensorVariable' object has no attribute 'get_value'

Do you know any solution? Thanks.

YuliyaLi commented 7 years ago

It was caused by the version of Keras, which can be solved by replace the AttLayer layer code by:

class AttLayer(Layer): def init(self, **kwargs): self.init = initializers.get('normal')

self.input_spec = [InputSpec(ndim=3)]

    super(AttLayer, self).__init__(**kwargs)
def build(self, input_shape):
    assert len(input_shape)==3
    # Create a trainable weight variable for this layer.
    self.W = self.add_weight(name='W',
                                  shape=(input_shape[-1],),
                                  initializer='uniform',
                                  trainable=True)
    super(AttLayer, self).build(input_shape)  # be sure you call this somewhere!
def call(self, x, mask=None):
    eij = K.tanh(K.dot(x, self.W))
    ai = K.exp(eij)
    weights = ai/K.sum(ai, axis=1).dimshuffle(0,'x')
    weighted_input = x*weights.dimshuffle(0,1,'x')
    return weighted_input.sum(axis=1)
#def get_output_shape_for(self, input_shape):
def compute_output_shape(self, input_shape):
    return (input_shape[0], input_shape[-1])

hope can help you.

tyronedong commented 7 years ago

Thanks! The error disappears after replacing the AttLayer layer code.