使用Keras层的Elmo嵌入层

时间:2019-04-30 19:52:53

标签: python keras deep-learning google-colaboratory keras-layer

from keras.layers import Dense, Input, RepeatVector, Lambda, Permute, Multiply, Concatenate
from keras.engine import Layer
from keras.layers.convolutional import Conv1D
from keras.models import Model
from keras.optimizers import RMSprop, Adam
from keras import backend as K
from keras.layers.wrappers import TimeDistributed
import tensorflow_hub as hub
import tensorflow as tf

class ElmoEmbeddingLayer(Layer):
    def __init__(self, **kwargs):
        self.dimensions = 512
        self.trainable=True
        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',
                      )['word_emb']
        return result

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

    def compute_output_shape(self, input_shape):
        return (input_shape, self.dimensions)

model_param = dict(hidden_dim=100,enc_timesteps=MAX_LEN_ENCODING_QUERY,dec_timesteps=MAX_LEN_ENCODING_PASSAGE,
                  random_size=4,lr = 0.001)

hidden_dim = model_param["hidden_dim"]

question = Input(shape=(1,), dtype = "string", name = "question_base_inner")
question_len = Input(shape = (model_param["enc_timesteps"],) , dtype= 'float32' , name='question_len') #l_q

answer = Input(shape=(1,), dtype='string' , name = 'answer_good_base_inner')
answer_len = Input(shape=(model_param["dec_timesteps"],),dtype = 'float32', name='answer_len')#l_a

question_emb = ElmoEmbeddingLayer()(question)
answer_emb = ElmoEmbeddingLayer()(answer)

ques_filter_repeat_len = RepeatVector(model_param["dec_timesteps"])(question_len)# dec_timesteps, question_len 70,15
ans_filter_repeat_len = RepeatVector(model_param["enc_timesteps"])(answer_len)# enc_timesteps, answer_len 15,70

ans_repeat_len = RepeatVector(model_param["hidden_dim"])(answer_len)#hidden_dim, answer_len
ans_repear_vec = Permute((2,1))(ans_repeat_len)#answer_len, hidden_len

ques_repeat_len = RepeatVector(model_param["hidden_dim"])(question_len)#hidden_dim, question_len
ques_repear_vec = Permute((2,1))(ques_repeat_len)#question_len, hidden_dim

    #do not have previous layer
SigmoidDense = Dense(hidden_dim,activation="sigmoid")
TanhDense = Dense(hidden_dim,activation="tanh")

QueTimeSigmoidDense = TimeDistributed(SigmoidDense)#,name="que_time_s")
QueTimeTanhDense = TimeDistributed(TanhDense)#, name="que_time_t")

AnsTimeSigmoidDense = TimeDistributed(SigmoidDense)#,name = "ans_time_s")
AnsTimeTanhDense = TimeDistributed(TanhDense)#, name = "ans_time_t")

question_sig = QueTimeSigmoidDense(question_emb)
question_tanh = QueTimeTanhDense(question_emb)
...........

我在运行此代码时遇到此错误-

AssertionError                            Traceback (most recent call last)
<ipython-input-72-b7d8772c388f> in <module>()
----> 1 question_sig = QueTimeSigmoidDense(question_emb)
      2 question_tanh = QueTimeTanhDense(question_emb)
      3 print(question_sig,"\n" ,question_tanh)

1 frames
/usr/local/lib/python3.6/dist-packages/keras/engine/base_layer.py in __call__(self, inputs, **kwargs)
    429                                          'You can build it manually via: '
    430                                          '`layer.build(batch_input_shape)`')
--> 431                 self.build(unpack_singleton(input_shapes))
    432                 self.built = True
    433 

/usr/local/lib/python3.6/dist-packages/keras/layers/wrappers.py in build(self, input_shape)
    193 
    194     def build(self, input_shape):
--> 195         assert len(input_shape) >= 3
    196         self.input_spec = InputSpec(shape=input_shape)
    197         child_input_shape = (input_shape[0],) + input_shape[2:]

我知道ElmoEmbeddingLayer出了点问题。此代码取自https://github.com/strongio/keras-elmo/blob/master/Elmo%20Keras.ipynb

如果有帮助,我正在google-colab上运行此代码。稍后,我要下载模型的权重和配置,因此,建议对代码进行任何更改,以便我可以检索到整个模型。

我们将不胜感激任何帮助。

0 个答案:

没有答案