I am trying to classify multiple independent sequences using Keras. My data looks like this (example with different stocks and their values).
_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
I would like to classify the data like shown in the following structure. The labels are already pre-defined for each stock (supervised learning).
_stock label
----------- -----------------
foo 0 (not falling)
bar 1 (falling)
pear 0 (not falling)
raspberry 0 (not falling)
banana 1 (falling)
Finally, I would also like to predict the value at the next timestep, if possible.
_stock 2015
----------- ------
foo 450
bar 10
pear 500
raspberry 600
banana 1
Currently I'm just using a bunch of Dense Layers which is working fine, but I think that I'm not utilizing the relationship between each column in the right way with this setup. Furthermore I don't think that a prediction is possible with this setup. I would like to use something like an an LSTM network, but I don't know how to change my implementation.
# 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))
This type of learning is called multi-task learning. You can have multiple outputs and multiple loss functions. To handle the sequential nature of the dataset you can still use LSTM. Here I will show with simple data.
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