我正在研究一个序列预测问题,我在这方面没有太多经验,所以下面的一些问题可能很幼稚。
仅供参考:我在这里创建了一个关注 CRF 的后续问题
我有以下问题:
我想预测多个非独立变量的二进制序列。
输入:
我有一个包含以下变量的数据集:
- 时间戳
- A组和B组
- 在特定时间戳对应每个组的二进制信号
此外,假设以下情况:
- 我们可以从时间戳(例如一天中的小时)中提取额外的属性,这些属性可以用作外部预测器
- 我们认为 A 组和 B 组不是独立的,因此联合建模他们的行为可能是最佳的
binary_signal_group_A
并且binary_signal_group_B
是我想使用(1)它们过去的行为和(2)从每个时间戳中提取的附加信息来预测的 2 个非独立变量。
到目前为止我所做的:
# required libraries
import re
import numpy as np
import pandas as pd
from keras import Sequential
from keras.layers import LSTM
data_length = 18 # how long our data series will be
shift_length = 3 # how long of a sequence do we want
df = (pd.DataFrame # create a sample dataframe
.from_records(np.random.randint(2, size=[data_length, 3]))
.rename(columns={0:'a', 1:'b', 2:'extra'}))
# NOTE: the 'extra' variable refers to a generic predictor such as for example 'is_weekend' indicator, it doesn't really matter what it is
# shift so that our sequences are in rows (assuming data is sorted already)
colrange = df.columns
shift_range = [_ for _ in range(-shift_length, shift_length+1) if _ != 0]
for c in colrange:
for s in shift_range:
if not (c == 'extra' and s > 0):
charge = 'next' if s > 0 else 'last' # 'next' variables is what we want to predict
formatted_s = '{0:02d}'.format(abs(s))
new_var = '{var}_{charge}_{n}'.format(var=c, charge=charge, n=formatted_s)
df[new_var] = df[c].shift(s)
# drop unnecessary variables and trim missings generated by the shift operation
df.dropna(axis=0, inplace=True)
df.drop(colrange, axis=1, inplace=True)
df = df.astype(int)
df.head() # check it out
# a_last_03 a_last_02 ... extra_last_02 extra_last_01
# 3 0 1 ... 0 1
# 4 1 0 ... 0 0
# 5 0 1 ... 1 0
# 6 0 0 ... 0 1
# 7 0 0 ... 1 0
# [5 rows x 15 columns]
# separate predictors and response
response_df_dict = {}
for g in ['a','b']:
response_df_dict[g] = df[[c for c in df.columns if 'next' in c and g in c]]
# reformat for LSTM
# the response for every row is a matrix with depth of 2 (the number of groups) and width = shift_length
# the predictors are of the same dimensions except the depth is not 2 but the number of predictors that we have
response_array_list = []
col_prefix = set([re.sub('_\d+$','',c) for c in df.columns if 'next' not in c])
for c in col_prefix:
current_array = df[[z for z in df.columns if z.startswith(c)]].values
response_array_list.append(current_array)
# reshape into samples (1), time stamps (2) and channels/variables (0)
response_array = np.array([response_df_dict['a'].values,response_df_dict['b'].values])
response_array = np.reshape(response_array, (response_array.shape[1], response_array.shape[2], response_array.shape[0]))
predictor_array = np.array(response_array_list)
predictor_array = np.reshape(predictor_array, (predictor_array.shape[1], predictor_array.shape[2], predictor_array.shape[0]))
# feed into the model
model = Sequential()
model.add(LSTM(8, input_shape=(predictor_array.shape[1],predictor_array.shape[2]), return_sequences=True)) # the number of neurons here can be anything
model.add(LSTM(2, return_sequences=True)) # should I use an activation function here? the number of neurons here must be equal to the # of groups we are predicting
model.summary()
# _________________________________________________________________
# Layer (type) Output Shape Param #
# =================================================================
# lstm_62 (LSTM) (None, 3, 8) 384
# _________________________________________________________________
# lstm_63 (LSTM) (None, 3, 2) 88
# =================================================================
# Total params: 472
# Trainable params: 472
# Non-trainable params: 0
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy']) # is it valid to use crossentropy and accuracy as metric?
model.fit(predictor_array, response_array, epochs=10, batch_size=1)
model_preds = model.predict_classes(predictor_array) # not gonna worry about train/test split here
model_preds.shape # should return (12, 3, 2) or (# of records, # of timestamps, # of groups which are a and b)
# (12, 3)
model_preds
# array([[1, 0, 0],
# [0, 0, 0],
# [1, 0, 0],
# [0, 0, 0],
# [1, 0, 0],
# [0, 0, 0],
# [0, 0, 0],
# [0, 0, 0],
# [0, 0, 0],
# [0, 0, 0],
# [1, 0, 0],
# [0, 0, 0]])
问题:
这里的主要问题是:我如何让它工作,以便模型预测两组的下一个 N 序列?
此外,我想问以下问题:
- 预计 A 组和 B 组是互相关的,但是,尝试通过单个模型同时输出 A 和 B 序列是否有效,或者我应该拟合 2 个单独的模型,一个预测 A,另一个预测 B,但都使用历史 A 和 B 数据作为输入?
- 虽然我在模型中的最后一层是形状为 (None, 3, 2) 的 LSTM,但预测输出的形状为 (12, 3),而我预计它是 (12, 2) - 我在做什么这里错了,如果是这样,我将如何解决这个问题?
- 就输出 LSTM 层而言,在这里使用激活函数会不会是个好主意,比如 sigmoid?为什么/为什么不?
- 使用分类类型损失(二进制交叉熵)和度量(准确度)来优化序列是否有效?
- LSTM 模型在这里是最佳选择吗?有人认为 CRF 或某些 HMM 类型的模型在这里会更好吗?
非常感谢!