在Keras(深度学习库)中,RepeatVector + TimeDistributed =错误?

时间:2016-08-17 03:32:39

标签: deep-learning keras

嗨,我是初学者。

我正在制作一些模特。

步骤1.输入批次和单词列表,(BATCH_SIZE,WORD_INDEX_LIST) 步骤2.获取每个单词的嵌入字(BATCH_SIZE,WORD_LENGTH,EMBEDDING_SIZE) 步骤3.平均每个批次中的每个单词嵌入。 (BATCH_SIZE,EMBEDDING_SIZE) 步骤4.重复向量N,(BATCH_SIZE,N,EMBEDDING_SIZE) 步骤5.每次执行密集层

所以,我写代码。

MAX_LEN = 20 ( = WORD_INDEX_LIST)

 step 1
layer_target_input = Input(shape=(MAX_LEN,), dtype="int32", name="layer_target_input")


# step2
layer_embedding = Embedding(input_dim = n_symbols+1, output_dim=vector_dim,input_length=MAX_LEN, 
                                                 name="embedding", weights= [embedding_weights],trainable = False)
encoded_target = layer_embedding(layer_target_input)


# step 3
encoded_target_agg = KL.core.Lambda( lambda x: K.sum(x, axis=1) )(encoded_target)


#step 4
encoded_target_agg_repeat = KL.RepeatVector( MAX_LEN)(encoded_target_agg)



# step 5
layer_annotated_tahn = KL.Dense(output_dim=50, name="layer_tahn") 
layer_annotated_tahn_td = KL.TimeDistributed(layer_annotated_tahn) (encoded_target_agg_repeat)

model = KM.Model(input=[layer_target_input], output=[ layer_annotated_tahn_td])

r = model.predict({ "layer_target_input":dev_targ}) # dev_targ = (2, 20, 300)

但是,当我运行此代码时, 结果如下。

Traceback (most recent call last):
  File "Main.py", line 127, in <module>
    r = model.predict({ "layer_target_input":dev_targ})
  File "/usr/local/anaconda/lib/python2.7/site-packages/Keras-1.0.7-py2.7.egg/keras/engine/training.py", line 1180, in predict
    batch_size=batch_size, verbose=verbose)
  File "/usr/local/anaconda/lib/python2.7/site-packages/Keras-1.0.7-py2.7.egg/keras/engine/training.py", line 888, in _predict_loop
    outs[i][batch_start:batch_end] = batch_out
ValueError: could not broadcast input array from shape (30,20,50) into shape (2,20,50)

批量大小改变的原因? 我错了什么?

1 个答案:

答案 0 :(得分:2)

问题出在Lambda运算符中。在你的情况下,它需要一个形状张量(batch_size, max_len, embedding_size),并且预计会产生一个形状张量(batch_size, embedding_size)。但是,Lambda操作系统并不知道您在内部应用了哪种转换,因此在图形编译过程中错误地假设形状没有变化,因此假设输出形状为{{1} }。接下来的(batch_size, max_len, embedding_size)期望输入是二维的,但从不断言它是这种情况。它产生预期形状的方式是RepeastVector。由于(batch_size, num_repetitions, in_shape[1])错误地将其形状报告为Lambda(batch_size, max_len, embedding_size)现在将其形状报告为RepeatVector而不是预期(batch_size, num_repetitions, max_len)。在您的情况下,(batch_size, num_repetitions, embedding_size)num_repetitions相同,因此max_len将其形状报告为RepeastVector(batch_size, max_len, max_len)的工作方式是:

TimeDistributed(Dense)

现在Reshape((-1, input_shape[2])) Dense() Reshape((-1, input_shape[1], num_outputs)) 被误认为是input_shape[2]而不是max_len,但是给出的实际张量具有正确的embedding_size形状,所以最终会发生什么是:

(batch_size, max_len, embedding_size)

在你的情况下Reshape((batch_size * embedding_size, max_len)) Dense() Reshape((batch_size * embedding_size / max_len, max_len, num_outputs)) 恰好是batch_size * embedding_size / max_len,那就是你的错误形状来自哪里。

要解决此问题,您需要明确告诉2 * 300 / 20 = 30您希望它生成的形状:

Lambda