我正在研究二进制分类问题,其中网络采用两个输入并输出此输入对的标签。
基本上,我使用编码器层首先嵌入并连接嵌入结果。接下来,我将使用RNN结构对连接结果进行分类。但我无法找到编写代码的正确方法。我在下面附上我的代码。
input_size = n_feature # the number of features
encoder_size = 2000 # output dim for each encoder
dropout_rate = 0.5
X1 = Input(shape=(input_size, ), name='input_1')
X2 = Input(shape=(input_size, ), name='input_2')
encoder = Sequential()
encoder.add(Dropout(dropout_rate, input_shape=(input_size, )))
encoder.add(Dense(encoder_size, activation='relu'))
encoded_1 = encoder(X1)
encoded_2 = encoder(X2)
merged = concatenate([encoded_1, encoded_2])
#----------Need Help---------------#
comparer = Sequential()
comparer.add(LSTM(512, input_shape=(encoder_size*2, ), return_sequences=True))
comparer.add(Dropout(dropout_rate))
comparer.add(TimeDistributed(Dense(1)))
comparer.add(Activation('sigmoid'))
#----------Need Help---------------#
Y = comparer(merged)
model = Model(inputs=[X1, X2], outputs=Y)
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
对于LSTM图层,输入应为(None, encoder_size*2)
。我尝试使用Y = comparer(K.transpose(merged))
来重塑LSTM图层的输入但是我失败了。顺便说一句,对于这个网络,输入形状为(input_size,)
,输出形状为(1,)
。
答案 0 :(得分:1)
如果想要在时间序列中转换输入向量,您可以简单地重塑它:
comparer = Sequential()
#reshape the vector into a time series form: (None, timeSteps, features)
comparer.add(Reshape((2 * encoder_size,1), input_shape=(2*encoder_size,))
#don't return sequences, you don't want a sequence as result:
comparer.add(LSTM(512, return_sequences=False))
comparer.add(Dropout(dropout_rate))
#Don't use a TimeDistributed, you're not dealing with a series anymore
comparer.add(Dense(1))
comparer.add(Activation('sigmoid'))