在keras中组合多个RNN
Combining multiple RNNs in keras
我有 2 个(独立长的)特征向量序列(a 和 b),我想用作神经网络的输入。
这个想法是构建 2 个基于 GRU 的编码器(每个序列一个)。
我宁愿有一个解码器(采用 2 个编码状态)来产生最终输出(一个数字)。
我的训练数据类似于
([a11, a11, a12], [b11, b12, b13]) => v1
([a21, a21], [b21, b22, b23, b24]) => v2
([a31], [b31, b32]) => v3
其中 axy(和 bxy)是特征向量。 vx是神经网络的输出值(一个浮点数)。
# lenA maximum length of a sequence
# lenB maximum length of b sequence
# featuresA number of features for a
# featuresB number of features for b
enc1InputData=np.zeros((count, lenA, featuresA))
enc2InputData=np.zeros((count, lenB, featuresB))
decoderInputData=np.zeros((count, 1, 1))
decoderOutputData=np.zeros((count, 1))
# Initialize input data
...
enc1Input = Input(shape=(None, featuresA))
enc1 = GRU(256, return_state=True)
enc1Output, enc1State = enc1(enc1Input)
enc2Input = Input(shape=(None, featuresB))
enc2 = GRU(256, return_state=True)
enc2Output, enc2State = enc2(enc2Input)
encOutput=concatenate([enc1State, enc2State])
decoderInput = Input(shape=(None, 1))
decoderGru = GRU(256+256, return_sequences=True, return_state=True)
decoderOutput, _ = decoderGru(decoderInput, initial_state=encOutput)
for i in range(3):
decoderDense=Dense(parms.dim3, activation="relu")
decoderOutput = decoderDense(decoderOutput)
#flatten = Flatten()
#decoderOutput=flatten(decoderOutput)
decoderTanh = Dense(1, activation="tanh")
decoderOutput = decoderTanh(decoderOutput)
model=Model([enc1Input, enc2Input, decoderInput], decoderOutput)
model.compile(optimizer="adam", loss="mse")
model.fit([enc1InputData, enc2InputData, decoderInputData], decoderOutputData, batch_size=256, epochs=100, validation_split=0.2)
这给了我一个
ValueError: Error when checking target: expected dense_4 to have 3 dimensions, but got array with shape (2, 1)
包括 Flatten 步骤得到
ValueError: The shape of the input to "Flatten" is not fully defined (got (None, 256)). Make sure to pass a complete "input_shape" or "batch_input_shape" argument to the first layer in your model.
下面的 GRU 层需要一个形状为 (None, 1)
的输入(不包括批量大小)并输出一个形状为 (None, 512)
的张量(即 序列 的 512 维向量)因为 return_sequences=True
,如果将其设置为 False
,输出的形状为 (512,)
(即只是一个向量而不是序列)。
decoderInput = Input(shape=(None, 1))
decoderGru = GRU(256+256, return_sequences=True, return_state=True)
因此,具有 return_sequences=True
意味着接下来的 Dense
层也接受 512 维向量的序列,即 (None, 512)
和 returns 形状为 [= 的张量19=] 其中 dense_units
是此 Dense
层的隐藏单元数。但是,您的训练输出的形状为 (1,)
.
所以,我认为 return_sequences=True
并不是您想要做的。设置它 False
应该可以解决问题。
我有 2 个(独立长的)特征向量序列(a 和 b),我想用作神经网络的输入。 这个想法是构建 2 个基于 GRU 的编码器(每个序列一个)。 我宁愿有一个解码器(采用 2 个编码状态)来产生最终输出(一个数字)。
我的训练数据类似于
([a11, a11, a12], [b11, b12, b13]) => v1
([a21, a21], [b21, b22, b23, b24]) => v2
([a31], [b31, b32]) => v3
其中 axy(和 bxy)是特征向量。 vx是神经网络的输出值(一个浮点数)。
# lenA maximum length of a sequence
# lenB maximum length of b sequence
# featuresA number of features for a
# featuresB number of features for b
enc1InputData=np.zeros((count, lenA, featuresA))
enc2InputData=np.zeros((count, lenB, featuresB))
decoderInputData=np.zeros((count, 1, 1))
decoderOutputData=np.zeros((count, 1))
# Initialize input data
...
enc1Input = Input(shape=(None, featuresA))
enc1 = GRU(256, return_state=True)
enc1Output, enc1State = enc1(enc1Input)
enc2Input = Input(shape=(None, featuresB))
enc2 = GRU(256, return_state=True)
enc2Output, enc2State = enc2(enc2Input)
encOutput=concatenate([enc1State, enc2State])
decoderInput = Input(shape=(None, 1))
decoderGru = GRU(256+256, return_sequences=True, return_state=True)
decoderOutput, _ = decoderGru(decoderInput, initial_state=encOutput)
for i in range(3):
decoderDense=Dense(parms.dim3, activation="relu")
decoderOutput = decoderDense(decoderOutput)
#flatten = Flatten()
#decoderOutput=flatten(decoderOutput)
decoderTanh = Dense(1, activation="tanh")
decoderOutput = decoderTanh(decoderOutput)
model=Model([enc1Input, enc2Input, decoderInput], decoderOutput)
model.compile(optimizer="adam", loss="mse")
model.fit([enc1InputData, enc2InputData, decoderInputData], decoderOutputData, batch_size=256, epochs=100, validation_split=0.2)
这给了我一个
ValueError: Error when checking target: expected dense_4 to have 3 dimensions, but got array with shape (2, 1)
包括 Flatten 步骤得到
ValueError: The shape of the input to "Flatten" is not fully defined (got (None, 256)). Make sure to pass a complete "input_shape" or "batch_input_shape" argument to the first layer in your model.
下面的 GRU 层需要一个形状为 (None, 1)
的输入(不包括批量大小)并输出一个形状为 (None, 512)
的张量(即 序列 的 512 维向量)因为 return_sequences=True
,如果将其设置为 False
,输出的形状为 (512,)
(即只是一个向量而不是序列)。
decoderInput = Input(shape=(None, 1))
decoderGru = GRU(256+256, return_sequences=True, return_state=True)
因此,具有 return_sequences=True
意味着接下来的 Dense
层也接受 512 维向量的序列,即 (None, 512)
和 returns 形状为 [= 的张量19=] 其中 dense_units
是此 Dense
层的隐藏单元数。但是,您的训练输出的形状为 (1,)
.
所以,我认为 return_sequences=True
并不是您想要做的。设置它 False
应该可以解决问题。