gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 00:54:59 26 4
gpt4 key购买 nike

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

  _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))

最佳答案

这种类型的学习称为 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

关于python - 使用 Keras 对多个独立序列进行二进制分类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63214545/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com