我最长久以来一直对此表示怀疑,但无法弄清楚是否是这种情况,所以这里是这种情况:
我正在尝试通过3个不同的输入来构建具有3个特征的模型:
现在,所有这三个步骤组成一个步骤。但是,由于我使用手套将100个维度的文本序列向量化,因此20个单词的文本序列最终的长度为2000。因此,每步的总输入长度为2002(每个时间步长为具有形状的矩阵) (2002年1月),其中有2000个来自单一功能。
文本序列是否压倒了两个浮点数,所以无论浮点数的值与预测值无关吗?如果是这样,我该怎么解决?也许手动权衡每个功能应该使用多少?附有代码
def build_model(embedding_matrix) -> Model:
text = Input(shape=(9, news_text.shape[1]), name='text')
price = Input(shape=(9, 1), name='price')
volume = Input(shape=(9, 1), name='volume')
text_layer = Embedding(
embedding_matrix.shape[0],
embedding_matrix.shape[1],
weights=[embedding_matrix]
)(text)
text_layer = Dropout(0.2)(text_layer)
# Flatten the vectorized text matrix
text_layer = Reshape((9, int_shape(text_layer)[2] * int_shape(text_layer)[3]))(text_layer)
inputs = concatenate([
text_layer,
price,
volume
])
output = Convolution1D(128, 5, activation='relu')(inputs)
output = MaxPool1D(pool_size=4)(output)
output = LSTM(units=128, dropout=0.2, return_sequences=True)(output)
output = LSTM(units=128, dropout=0.2, return_sequences=True)(output)
output = LSTM(units=128, dropout=0.2)(output)
output = Dense(units=2, activation='linear', name='output')(output)
model = Model(
inputs=[text, price, volume],
outputs=[output]
)
model.compile(optimizer='adam', loss='mean_squared_error')
return model
编辑:请注意,输入到lstm中的形状是(?,9,2002),这意味着现在来自文本的2000会被视为2000个独立特征
答案 0 :(得分:1)
正如我在评论中提到的,一种方法是拥有两个分支模型,其中一个分支处理文本数据,另一个分支处理两个浮点功能。最后,两个分支的输出合并在一起:
# Branch one: process text data
text_input = Input(shape=(news_text.shape[1],), name='text')
text_emb = Embedding(embedding_matrix.shape[0],embedding_matrix.shape[1],
weights=[embedding_matrix])(text_input)
# you may alternatively use only Conv1D + MaxPool1D or
# stack multiple LSTM layers on top of each other or
# use a combination of Conv1D, MaxPool1D and LSTM
text_conv = Convolution1D(128, 5, activation='relu')(text_emb)
text_lstm = LSTM(units=128, dropout=0.2)(text_conv)
# Branch two: process float features
price_input = Input(shape=(9, 1), name='price')
volume_input = Input(shape=(9, 1), name='volume')
pv = concatenate([price_input, volume_input])
# you can also stack multiple LSTM layers on top of each other
pv_lstm = LSTM(units=128, dropout=0.2)(pv)
# merge output of branches
text_pv = concatenate([text_lstm, pv_lstm])
output = Dense(units=2, activation='linear', name='output')(text_pv)
model = Model(
inputs=[text_input, price_input, volume_input],
outputs=[output]
)
model.compile(optimizer='adam', loss='mean_squared_error')
正如我在代码中评论的那样,这只是一个简单的例子。您可能需要进一步添加或删除图层或正则化并调整超参数。