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

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

Closed
tyronedong opened this issue Jun 27, 2017 · 2 comments
Closed

Comments

@tyronedong
Copy link

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
Copy link

YuliyaLi commented Jul 8, 2017

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
Copy link
Author

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

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

2 participants