当我使用tf.keras.layers.LSTM(尝试使用GPU实例)时,我遇到以下错误,而普通的keras.layers.LSTM可以正常工作(不使用GPU)
如果我使用keras.layers.LSTM,则我用于创建模型的模型可以正常工作。
def build_model(embedding_matrix, num_aux_targets):
title = Input(shape=(MAX_LEN,))
question_body = Input(shape=(MAX_LEN,))
answer = Input(shape=(MAX_LEN,))
#category = Input(shape=(1,))
title_embb = Embedding(*embedding_matrix.shape, weights=[embedding_matrix], trainable=False )(title)
question_body_embb = Embedding(*embedding_matrix.shape, weights=[embedding_matrix], trainable=False)(question_body)
answer_embb = Embedding(*embedding_matrix.shape, weights=[embedding_matrix], trainable=False)(answer)
concat = Concatenate(axis=1)
embb_final = concat([title_embb,question_body_embb,answer_embb])
x1 = SpatialDropout1D(0.3)(embb_final)
x1 = Bidirectional(tf.keras.layers.LSTM(LSTM_UNITS, return_sequences=True))(x1)
hidden1 = concatenate([
GlobalMaxPooling1D()(x1),
GlobalAveragePooling1D()(x1),#layer returns a fixed-length output vector for each example by averaging over the sequence dimension. This allows the model to handle input
#of variable length in the simplest way possible.
])
hidden1 = add([hidden1, Dense(DENSE_HIDDEN_UNITS, activation='relu')(hidden1)])
result = Dense(30, activation='sigmoid')(hidden1)
model = Model(inputs=[title,question_body,answer], outputs= result)
model._name = 'mymodel'
model.compile(loss='binary_crossentropy',metrics = ['accuracy'], optimizer='adam')
model.summary()
return model
调用模型后,出现以下错误:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
/opt/conda/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/base_layer.py in __setattr__(self, name, value)
2238 try:
-> 2239 super(tracking.AutoTrackable, self).__setattr__(name, value)
2240 except AttributeError:
AttributeError: can't set attribute
During handling of the above exception, another exception occurred:
AttributeError Traceback (most recent call last)
<ipython-input-62-8baf6fbe617d> in <module>
1 for model_idx in range(NUM_MODELS):
----> 2 model = build_model(embedding_matrix,1)
3 for global_epoch in range(EPOCHS):
4
5 model.fit(
<ipython-input-61-925b81ce0745> in build_model(embedding_matrix, num_aux_targets)
12
13 x1 = SpatialDropout1D(0.3)(embb_final)
---> 14 x1 = Bidirectional(keras.layers.LSTM(LSTM_UNITS, return_sequences=True,recurrent_activation='sigmoid',recurrent_dropout=0,unroll=False,use_bias=True))(x1)
15 hidden1 = concatenate([
16 GlobalMaxPooling1D()(x1),
/opt/conda/lib/python3.6/site-packages/keras/layers/wrappers.py in __init__(self, layer, merge_mode, weights, **kwargs)
366 'Merge mode should be one of '
367 '{"sum", "mul", "ave", "concat", None}')
--> 368 self._set_sublayers(layer)
369 self.merge_mode = merge_mode
370 if weights:
/opt/conda/lib/python3.6/site-packages/keras/engine/base_layer.py in wrapped_fn(*args, **kwargs)
28 prev_value = _DISABLE_TRACKING.value
29 _DISABLE_TRACKING.value = True
---> 30 out = func(*args, **kwargs)
31 _DISABLE_TRACKING.value = prev_value
32 return out
/opt/conda/lib/python3.6/site-packages/keras/layers/wrappers.py in _set_sublayers(self, layer)
390 config['go_backwards'] = not config['go_backwards']
391 self.backward_layer = layer.__class__.from_config(config)
--> 392 self.forward_layer.name = 'forward_' + self.forward_layer.name
393 self.backward_layer.name = 'backward_' + self.backward_layer.name
394
/opt/conda/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/base_layer.py in __setattr__(self, name, value)
2242 ('Can\'t set the attribute "{}", likely because it conflicts with '
2243 'an existing read-only @property of the object. Please choose a '
-> 2244 'different name.').format(name))
2245 return
2246
AttributeError: Can't set the attribute "name", likely because it conflicts with an existing read-only @property of the object. Please choose a different name.
我在代码中未使用任何属性“名称”,也不允许升级或降级Keras或TF版本。