使用Keras对多个独立序列进行二进制分类

时间:2020-08-02 09:47:18

标签: python keras classification sequence prediction

我正在尝试使用Keras对多个独立序列进行分类。我的数据看起来像这样(具有不同股票及其价值的示例)。

  _stock     2010   2011   2012   2013   2014
----------- ------ ------ ------ ------ ------
 foo          100    200    250    300    400
 bar           50    100    100     50     25
 pear         100    250    250    300    400
 raspberry    100    200    300    400    500
 banana        50     20     10     10      5

我想对数据进行分类,如以下结构所示。标签已经为每种股票预先定义(监督学习)。

  _stock          label
----------- -----------------
 foo         0 (not falling)
 bar         1 (falling)
 pear        0 (not falling)
 raspberry   0 (not falling)
 banana      1 (falling)

最后,如果可能的话,我还要预测下一个时间步长的值。

  _stock     2015
----------- ------
 foo          450
 bar           10
 pear         500
 raspberry    600
 banana         1

当前,我仅使用一堆工作正常的密集层,但我认为在此设置中我没有以正确的方式利用每一列之间的关系。此外,我认为使用此设置无法预测。我想使用LSTM网络之类的东西,但我不知道如何更改实现。

# current network
from keras.models import Sequential
n_timesteps = len(data.columns)

model = Sequential()
model.add(Dense(100, activation="relu", input_dim=n_timesteps))
model.add(Dense(100, activation="relu"))
model.add(Dense(1, activation="sigmoid"))
model.compile(optimizer="adam", loss="binary_crossentropy", metrics=["accuracy"])

model.fit(x_train, y_train, epochs=100, validation_data=(x_test, y_test))

2 个答案:

答案 0 :(得分:4)

这种学习方式称为multi-task learning。您可以具有多个输出和多个损失函数。要处理数据集的顺序性质,您仍然可以使用LSTM。在这里,我将显示简单的数据。

import tensorflow as tf 
import numpy as np
layers = tf.keras.layers

timesteps = 32
channels = 16;
x = np.random.randn(100, timesteps, channels)

binary_y = np.random.randint(0, 2, size=(x.shape[0], 1))
reg_y = np.random.randn(x.shape[0], 1)

inputs = layers.Input(shape=(timesteps, channels))
hidden = layers.LSTM(32)(inputs)
out1 = layers.Dense(1, activation="sigmoid", name="binary_out")(hidden)
out2 = layers.Dense(1, activation=None, name="reg_out")(hidden)

model = tf.keras.Model(inputs=inputs, outputs=[out1, out2])

model.compile(loss={"binary_out":"binary_crossentropy", "reg_out":"mse"}, optimizer='adam', metrics={"binary_out":"accuracy"})

model.fit(x, [binary_y, reg_y], epochs=10)


Epoch 1/10
4/4 [==============================] - 0s 7ms/step - loss: 1.6842 - binary_out_loss: 0.6987 - reg_out_loss: 0.9855 - binary_out_accuracy: 0.5300
Epoch 2/10
4/4 [==============================] - 0s 6ms/step - loss: 1.6395 - binary_out_loss: 0.6937 - reg_out_loss: 0.9458 - binary_out_accuracy: 0.5400
Epoch 3/10
4/4 [==============================] - 0s 6ms/step - loss: 1.6124 - binary_out_loss: 0.6913 - reg_out_loss: 0.9211 - binary_out_accuracy: 0.5500
Epoch 4/10
4/4 [==============================] - 0s 7ms/step - loss: 1.5864 - binary_out_loss: 0.6886 - reg_out_loss: 0.8978 - binary_out_accuracy: 0.5600
Epoch 5/10
4/4 [==============================] - 0s 7ms/step - loss: 1.5660 - binary_out_loss: 0.6863 - reg_out_loss: 0.8797 - binary_out_accuracy: 0.5600
Epoch 6/10
4/4 [==============================] - 0s 7ms/step - loss: 1.5424 - binary_out_loss: 0.6832 - reg_out_loss: 0.8593 - binary_out_accuracy: 0.5500
Epoch 7/10
4/4 [==============================] - 0s 7ms/step - loss: 1.5206 - binary_out_loss: 0.6806 - reg_out_loss: 0.8400 - binary_out_accuracy: 0.5600
Epoch 8/10
4/4 [==============================] - 0s 6ms/step - loss: 1.5013 - binary_out_loss: 0.6785 - reg_out_loss: 0.8229 - binary_out_accuracy: 0.5600
Epoch 9/10
4/4 [==============================] - 0s 6ms/step - loss: 1.4816 - binary_out_loss: 0.6759 - reg_out_loss: 0.8057 - binary_out_accuracy: 0.5700
Epoch 10/10
4/4 [==============================] - 0s 6ms/step - loss: 1.4641 - binary_out_loss: 0.6737 - reg_out_loss: 0.7904 - binary_out_accuracy: 0.5800

答案 1 :(得分:2)

所以您可能想要做的是:

1,如果尚未规范化/标准化数据

2,您只想预测一个变量:大多数可能的下一个价格

3,您可以使用此下一个价格来确定股票是上涨还是下跌-只需将其与之前的价格进行比较

我建议您仅在有足够数据(即每天/每小时...统计数据)的情况下使用LSTM等递归网络-如果您的数据集非常有限(如示例中所示),则基本回归算法应与很好。

作为旁注,通常只通过一个网络/算法预测一个标签比同时预测一个标签通常更容易,更准确。尝试为每个标签训练N个不同的模型。