我对机器学习算法(基本的随机森林和线性回归类型的东西)有一些自学的知识。我决定分支并开始与Keras学习RNN。在查看大多数通常涉及库存预测的示例时,我没有找到实现多个功能的任何基本示例,除了第一列是功能日期,另一列是输出。我是否缺少关键的基本事物?
如果有人举个例子,我将不胜感激。
谢谢!
我对机器学习算法(基本的随机森林和线性回归类型的东西)有一些自学的知识。我决定分支并开始与Keras学习RNN。在查看大多数通常涉及库存预测的示例时,我没有找到实现多个功能的任何基本示例,除了第一列是功能日期,另一列是输出。我是否缺少关键的基本事物?
如果有人举个例子,我将不胜感激。
谢谢!
Answers:
递归神经网络(RNN)旨在学习序列数据。如您所料,它们绝对可以将多个功能作为输入!Keras的RNN接受时间步长T的 2D输入(T,F)和特征F(我在这里忽略了批次尺寸)。
但是,您并不总是需要或想要中间时间步长t = 1、2 ...(T -1)。因此,Keras灵活地支持两种模式。要使其输出所有T个时间步长,return_sequences=True
请在构造时传递到您的RNN(例如LSTM
或GRU
)。如果只希望最后一个时间步长t = T,则使用return_sequences=False
(如果不传递return_sequences
给构造函数,则为默认设置)。
以下是这两种模式的示例。
这是训练LSTM(RNN类型)的快速示例,该LSTM使整个序列保持不变。在此示例中,每个输入数据点具有2个时间步长,每个时间步长都具有3个功能;输出数据有2个时间步长(因为return_sequences=True
),每个时间步长都有4个数据点(因为这是我传递给的大小LSTM
)。
import keras.layers as L
import keras.models as M
import numpy
# The inputs to the model.
# We will create two data points, just for the example.
data_x = numpy.array([
# Datapoint 1
[
# Input features at timestep 1
[1, 2, 3],
# Input features at timestep 2
[4, 5, 6]
],
# Datapoint 2
[
# Features at timestep 1
[7, 8, 9],
# Features at timestep 2
[10, 11, 12]
]
])
# The desired model outputs.
# We will create two data points, just for the example.
data_y = numpy.array([
# Datapoint 1
[
# Target features at timestep 1
[101, 102, 103, 104],
# Target features at timestep 2
[105, 106, 107, 108]
],
# Datapoint 2
[
# Target features at timestep 1
[201, 202, 203, 204],
# Target features at timestep 2
[205, 206, 207, 208]
]
])
# Each input data point has 2 timesteps, each with 3 features.
# So the input shape (excluding batch_size) is (2, 3), which
# matches the shape of each data point in data_x above.
model_input = L.Input(shape=(2, 3))
# This RNN will return timesteps with 4 features each.
# Because return_sequences=True, it will output 2 timesteps, each
# with 4 features. So the output shape (excluding batch size) is
# (2, 4), which matches the shape of each data point in data_y above.
model_output = L.LSTM(4, return_sequences=True)(model_input)
# Create the model.
model = M.Model(input=model_input, output=model_output)
# You need to pick appropriate loss/optimizers for your problem.
# I'm just using these to make the example compile.
model.compile('sgd', 'mean_squared_error')
# Train
model.fit(data_x, data_y)
另一方面,如果您想训练仅输出序列中最后一个时间步的LSTM,则需要进行设置return_sequences=False
(或将其从构造函数中完全删除,因为这False
是默认设置)。然后,您的输出数据(data_y
在上面的示例中)需要重新排列,因为您只需要提供最后的时间步长即可。因此,在第二个示例中,每个输入数据点仍具有2个时间步长,每个时间步长都具有3个功能。但是,输出数据对于每个数据点来说都是单个矢量,因为我们已将所有内容展平为单个时间步长。不过,每个输出向量仍然具有4个特征(因为这是我传递给的大小LSTM
)。
import keras.layers as L
import keras.models as M
import numpy
# The inputs to the model.
# We will create two data points, just for the example.
data_x = numpy.array([
# Datapoint 1
[
# Input features at timestep 1
[1, 2, 3],
# Input features at timestep 2
[4, 5, 6]
],
# Datapoint 2
[
# Features at timestep 1
[7, 8, 9],
# Features at timestep 2
[10, 11, 12]
]
])
# The desired model outputs.
# We will create two data points, just for the example.
data_y = numpy.array([
# Datapoint 1
# Target features at timestep 2
[105, 106, 107, 108],
# Datapoint 2
# Target features at timestep 2
[205, 206, 207, 208]
])
# Each input data point has 2 timesteps, each with 3 features.
# So the input shape (excluding batch_size) is (2, 3), which
# matches the shape of each data point in data_x above.
model_input = L.Input(shape=(2, 3))
# This RNN will return timesteps with 4 features each.
# Because return_sequences=False, it will output 2 timesteps, each
# with 4 features. So the output shape (excluding batch size) is
# (2, 4), which matches the shape of each data point in data_y above.
model_output = L.LSTM(4, return_sequences=False)(model_input)
# Create the model.
model = M.Model(input=model_input, output=model_output)
# You need to pick appropriate loss/optimizers for your problem.
# I'm just using these to make the example compile.
model.compile('sgd', 'mean_squared_error')
# Train
model.fit(data_x, data_y)
data_x
则将仅包含一个数据点,并且该数据点将具有四个时间步长,每个时间步长均为3个维度(同样,您必须以data_y
相同的方式合并)。您使用的时间步数仅取决于您要建模的内容(以及与该过程相关的时间步数)。