这是我定义的模型:
def build_model():
input_layer = keras.layers.Input(name="Input", shape=(MAX_LEN), dtype='int64')
bert = BertForPreTraining.from_pretrained('digitalepidemiologylab/covid-twitter-bert-v2')(input_layer)
bert = bert[0][:,0,:]
x = keras.layers.Bidirectional(keras.layers.LSTM(256, name="LSTM", activation='tanh', dropout=0.3), name="Bidirectional_LSTM")(bert)
x = keras.layers.Dense(64, 'relu')(x)
output_layer = keras.layers.Dense(1, 'sigmoid', name="Output")(x)
model = keras.Model(inputs=input_layer, outputs=output_layer)
model.compile(loss=loss,
optimizer=optimizer)
return model
跑步时
model = build_model()
这是我遇到的错误。
AttributeError Traceback (most recent call last)
<ipython-input-57-671884cecb64> in <module>()
----> 1 model = build_model()
4 frames
<ipython-input-56-ef0d67347557> in build_model()
1 def build_model():
2 input_layer = keras.layers.Input(name="Input", shape=(MAX_LEN), dtype='int64')
----> 3 bert = BertForPreTraining.from_pretrained('digitalepidemiologylab/covid-twitter-bert-v2')(input_layer)
4 bert = bert[0][:,0,:]
5 x = keras.layers.Bidirectional(keras.layers.LSTM(256, name="LSTM", activation='tanh', dropout=0.3), name="Bidirectional_LSTM")(bert)
/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
720 result = self._slow_forward(*input, **kwargs)
721 else:
--> 722 result = self.forward(*input, **kwargs)
723 for hook in itertools.chain(
724 _global_forward_hooks.values(),
/usr/local/lib/python3.6/dist-packages/transformers/modeling_bert.py in forward(self, input_ids, attention_mask, token_type_ids, position_ids, head_mask, inputs_embeds, labels, next_sentence_label, output_attentions, output_hidden_states, return_dict, **kwargs)
938 output_attentions=output_attentions,
939 output_hidden_states=output_hidden_states,
--> 940 return_dict=return_dict,
941 )
942
/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
720 result = self._slow_forward(*input, **kwargs)
721 else:
--> 722 result = self.forward(*input, **kwargs)
723 for hook in itertools.chain(
724 _global_forward_hooks.values(),
/usr/local/lib/python3.6/dist-packages/transformers/modeling_bert.py in forward(self, input_ids, attention_mask, token_type_ids, position_ids, head_mask, inputs_embeds, encoder_hidden_states, encoder_attention_mask, output_attentions, output_hidden_states, return_dict)
793 raise ValueError("You cannot specify both input_ids and inputs_embeds at the same time")
794 elif input_ids is not None:
--> 795 input_shape = input_ids.size()
796 elif inputs_embeds is not None:
797 input_shape = inputs_embeds.size()[:-1]
AttributeError: 'Tensor' object has no attribute 'size'