1

我正在尝试使用 tensorflow-gpu==2.0.0-rc0 在 Keras 中为我的模型使用自定义 ElmoEmbeddingLayer。

这是课程:

class ElmoEmbeddingLayer(Layer):
    def __init__(self, trainable=True, output_mode="default", **kwargs):
        assert output_mode in ["default", "word_emb", "lstm_outputs1", "lstm_outputs2", "elmo"]
        assert trainable in [True, False]
        self.output_mode = output_mode
        self.trainable = trainable
        super(ElmoEmbeddingLayer, self).__init__(**kwargs)

    def build(self, input_shape):
        self.elmo = hub.Module('https://tfhub.dev/google/elmo/2', trainable=self.trainable,
                               name="{}_module".format(self.name))

        self.trainable_weights += K.tf.trainable_variables(scope="^{}_module/.*".format(self.name))
        super(ElmoEmbeddingLayer, self).build(input_shape)

    def call(self, x, mask=None):
        result = self.elmo(
            K.squeeze(K.cast(x, tf.string), axis=1),
            as_dict=True,
            signature='default',                      
            )['elmo']
        return result

    def compute_mask(self, inputs, mask=None):
        return K.not_equal(inputs, '--PAD--')

    def compute_output_shape(self, input_shape):
        if self.output_mode == "default":
            return (input_shape[0], 1024)
        if self.output_mode == "word_emb":
            return (input_shape[0], self.max_length, 512)
        if self.output_mode == "lstm_outputs1":
            return (input_shape[0], self.max_length, 1024)
        if self.output_mode == "lstm_outputs2":
            return (input_shape[0], self.max_length, 1024)
        if self.output_mode == "elmo":
            return (input_shape[0], self.max_length, 1024)

    def get_config(self):
        config = {
            'output_mode': self.output_mode 
        }
        return list(config.items()) 

这是我的模型:

def train_model(lstm_layers = 300, dropout_rate = 0.1, learning_rate = 0.01,
                batch_s = 25, verbose = 0, save_model = bool(0), epchs = 10):  


  model = tf.keras.Sequential([
    tf.keras.layers.Input(shape=(max_lenght,), dtype="string"),
    ElmoEmbeddingLayer(output_mode="default", trainable=True),                           
    #tf.keras.layers.Embedding(max_lenght+1 , embedding_dim, input_length=max_lenght), 
    #tf.keras.layers.Reshape((max_lenght, embedding_dim)),            
    #tf.keras.layers.Bidirectional(LSTM(int(lstm_layers))),
    tf.keras.layers.Dropout(dropout_rate),   
    tf.keras.layers.Dense(1, activation='sigmoid')
  ])

  model.summary()

  adam=Adam(lr=learning_rate)
  model.compile(loss='binary_crossentropy', optimizer=adam, metrics=['accuracy'])
  history = model.fit(np.array(data_train), np.array(labels_train), epochs=epchs,
                    batch_size = int(batch_s), validation_data=(np.array(data_test), np.array(labels_test)), verbose = verbose)

但是,当我尝试拟合我的模型时,我遇到了这个错误,我该如何解决?

RuntimeError: variable_scope elmo_embedding_layer_14_module/ 未使用,但相应的 name_scope 已被占用。

4

1 回答 1

0

我认为您可以做的是更改 tensorflow 的版本,因为 tfhub 不适用于 2.0。所以你可以降级。

!pip install tensorflow==1.15
!pip install "tensorflow_hub>=0.6.0"
!pip3 install tensorflow_text==1.15

它对我有用。

于 2020-04-25T17:32:43.090 回答