gpt4 book ai didi

python - Keras 错误 : Expected size[1] in [0, 0],但得到 1

转载 作者:行者123 更新时间:2023-12-01 09:08:33 24 4
gpt4 key购买 nike

我正在尝试在 Keras 中的更大的 seq2seq 模型中构建解码器,但当我运行 fit 函数时,我不断收到以下错误。否则模型构建得很好。

InvalidArgumentError: Expected size[1] in [0, 0], but got 1
[[Node: lambda_2/Slice = Slice[Index=DT_INT32, T=DT_FLOAT,
_device="/job:localhost/replica:0/task:0/device:CPU:0"](lambda_1/Slice,
metrics/acc/Const, lambda_2/Slice/size)]]

lambda_x/Slice 似乎指的是循环中的 lambda 函数。

我的模型有 4 个形状为 (N, 11)、(N, 3)、(N, 11)、(N, 3) 的输入,并输出形状为 的 softmax 分布(N,11,1163)

下面是我的解码器代码,其中使用了分离器层:

def _decoder_serial_input(self, encoder_states, state_h, state_c):
"""
Compute one-by-one input to decoder, taking output from previous time-step as input
:param encoder_states: All the encoder states
:param state_h: starting hidden state
:param state_c: starting cell state
:return: Concatenated output which is shape = (N, Timestep, Input dims)
"""

all_outputs = []
states = [state_h, state_c]
inputs = self.decoder_inputs # Shape = N x num_timestep

repeat = RepeatVector(1, name="decoder_style")
conc_1 = Concatenate(axis=-1, name="concatenate_decoder")
conc_att = Concatenate(axis=-1, name="concatenate_attention")

for t in range(self.max_timestep):

# This slices the input. -1 is to accept everything in that dimension
inputs = Lambda(lambda x: K.slice(x, start=[0, t], size=[-1, 1]))(inputs)

embedding_output = self.embedding_decoder(inputs)
style_labels = repeat(self.decoder_style_label)

concat = conc_1([embedding_output, style_labels]) # Join to style label

decoder_output_forward, state_h, state_c = self.decoder(concat, initial_state=states)

if self.attention:
context, _ = self._one_step_attention(encoder_states, state_h) # Size of latent dims
decoder_output_forward = conc_att([context, decoder_output_forward])

outputs = self.decoder_softmax_output(decoder_output_forward) # Shape = (N, 1, input dims)

all_outputs.append(outputs)
states = [state_h, state_c]

return Concatenate(axis=1, name="conc_dec_forward")(all_outputs)

有谁知道为什么我会收到此错误?谢谢。

最佳答案

我解决了这个问题。问题是我将 Lambda 层的输出设置为输入变量,这是错误的。这改变了 lambda 层的输入张量的形状。在第一次迭代中,它是 (N, 11),如所希望的,但在循环的后续迭代中,它变成了 (N, 1),这导致了错误。

关于python - Keras 错误 : Expected size[1] in [0, 0],但得到 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51850633/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com