Keras LSTM 更大的特征压倒更小的特征?
Keras LSTM Larger Feature Overwhelm Smaller Ones?
我有这种怀疑已经很长时间了,但无法弄清楚是不是这样,所以情况是这样的:
我正在尝试构建一个模型,该模型具有来自 3 个不同输入的 3 个特征:
- 一个文本序列
- 一个浮点数
- 一个浮点数
现在所有这三个组成一个时间步。但是由于我使用 glove 使用 100 个维度对我的文本序列进行矢量化,因此一个 20 个单词的文本序列最终的长度为 2000。因此,每一步的总输入长度为 2002(每个时间步长一个矩阵,其形状(1, 2002) 被输入,其中 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 个独立特征
正如我在评论中提到的,一种方法是采用双分支模型,其中一个分支处理文本数据,另一个分支处理两个浮动特征。最后将两个分支的输出合并在一起:
# 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')
正如我在代码中评论的那样,这只是一个简单的说明。您可能需要进一步添加或删除层或正则化并调整超参数。
我有这种怀疑已经很长时间了,但无法弄清楚是不是这样,所以情况是这样的:
我正在尝试构建一个模型,该模型具有来自 3 个不同输入的 3 个特征:
- 一个文本序列
- 一个浮点数
- 一个浮点数
现在所有这三个组成一个时间步。但是由于我使用 glove 使用 100 个维度对我的文本序列进行矢量化,因此一个 20 个单词的文本序列最终的长度为 2000。因此,每一步的总输入长度为 2002(每个时间步长一个矩阵,其形状(1, 2002) 被输入,其中 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 个独立特征
正如我在评论中提到的,一种方法是采用双分支模型,其中一个分支处理文本数据,另一个分支处理两个浮动特征。最后将两个分支的输出合并在一起:
# 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')
正如我在代码中评论的那样,这只是一个简单的说明。您可能需要进一步添加或删除层或正则化并调整超参数。