使用TensorFlow的递归神经网络(LSTM)进行序列预测

本篇文章介绍使用TensorFlow的递归神经网络(LSTM)进行序列预测。作者在网上找到的使用LSTM模型的案例都是解决自然语言处理的问题,而没有一个是来预测连续值的。

所以呢,这里是基于历史观察数据进行实数序列的预测。传统的神经网络模型并不能解决这种问题,进而开发出递归神经网络模型,递归神经网络模型可以存储历史数据来预测未来的事情。

在这个例子里将预测几个函数:

正弦函数:sin

使用TensorFlow的递归神经网络(LSTM)进行序列预测

同时存在正弦函数和余弦函数:sin和cos

使用TensorFlow的递归神经网络(LSTM)进行序列预测

x*sin(x)

使用TensorFlow的递归神经网络(LSTM)进行序列预测

首先,建立LSTM模型,lstm_model,这个模型有一系列的不同时间步的lstm单元(cell),紧跟其后的是稠密层。

def lstm_model(time_steps, rnn_layers, dense_layers=None): def lstm_cells(layers): if isinstance(layers[0], dict): return [tf.nn.rnn_cell.DropoutWrapper(tf.nn.rnn_cell.BasicLSTMCell(layer['steps']), layer['keep_prob']) if layer.get('keep_prob') else tf.nn.rnn_cell.BasicLSTMCell(layer['steps']) for layer in layers] return [tf.nn.rnn_cell.BasicLSTMCell(steps) for steps in layers] def dnn_layers(input_layers, layers): if layers and isinstance(layers, dict): return skflow.ops.dnn(input_layers, layers['layers'], activation=layers.get('activation'), dropout=layers.get('dropout')) elif layers: return skflow.ops.dnn(input_layers, layers) else: return input_layers def _lstm_model(X, y): stacked_lstm = tf.nn.rnn_cell.MultiRNNCell(lstm_cells(rnn_layers)) x_ = skflow.ops.split_squeeze(1, time_steps, X) output, layers = tf.nn.rnn(stacked_lstm, x_, dtype=dtypes.float32) output = dnn_layers(output[-1], dense_layers) return skflow.models.linear_regression(output, y) return _lstm_model

所建立的模型期望输入数据的维度与(batch size,第一个lstm cell的时间步长time_step,特征数量num_features)相关。
接下来我们按模型所能接受的数据方式来准备数据。

def rnn_data(data, time_steps, labels=False): """ creates new data frame based on previous observation * example: l = [1, 2, 3, 4, 5] time_steps = 2 -> labels == False [[1, 2], [2, 3], [3, 4]] -> labels == True [2, 3, 4, 5] """ rnn_df = [] for i in range(len(data) - time_steps): if labels: try: rnn_df.append(data.iloc[i + time_steps].as_matrix()) except AttributeError: rnn_df.append(data.iloc[i + time_steps]) else: data_ = data.iloc[i: i + time_steps].as_matrix() rnn_df.append(data_ if len(data_.shape) > 1 else [[i] for i in data_]) return np.array(rnn_df) def split_data(data, val_size=0.1, test_size=0.1): """ splits data to training, validation and testing parts """ ntest = int(round(len(data) * (1 - test_size))) nval = int(round(len(data.iloc[:ntest]) * (1 - val_size))) df_train, df_val, df_test = data.iloc[:nval], data.iloc[nval:ntest], data.iloc[ntest:] return df_train, df_val, df_test def prepare_data(data, time_steps, labels=False, val_size=0.1, test_size=0.1): """ Given the number of `time_steps` and some data. prepares training, validation and test data for an lstm cell. """ df_train, df_val, df_test = split_data(data, val_size, test_size) return (rnn_data(df_train, time_steps, labels=labels), rnn_data(df_val, time_steps, labels=labels), rnn_data(df_test, time_steps, labels=labels)) def generate_data(fct, x, time_steps, seperate=False): """generate data with based on a function fct""" data = fct(x) if not isinstance(data, pd.DataFrame): data = pd.DataFrame(data) train_x, val_x, test_x = prepare_data(data['a'] if seperate else data, time_steps) train_y, val_y, test_y = prepare_data(data['b'] if seperate else data, time_steps, labels=True) return dict(train=train_x, val=val_x, test=test_x), dict(train=train_y, val=val_y, test=test

这将会创建一个数据让模型可以查找过去time_steps步来预测数据。比如,LSTM模型的第一个cell是10 time_steps cell,为了做预测我们需要输入10个历史数据点。y值跟我们想预测的第十个值相关。
现在创建一个基于LSTM模型的回归量。

regressor = skflow.TensorFlowEstimator(model_fn=lstm_model(TIMESTEPS, RNN_LAYERS, DENSE_LAYERS), n_classes=0, verbose=1, steps=TRAINING_STEPS, optimizer='Adagrad', learning_rate=0.03, batch_size=BATCH_SIZE)

预测sin函数

X, y = generate_data(np.sin, np.linspace(0, 100, 10000), TIMESTEPS, seperate=False) # create a lstm instance and validation monitor validation_monitor = skflow.monitors.ValidationMonitor(X['val'], y['val'], n_classes=0, print_steps=PRINT_STEPS, early_stopping_rounds=1000, logdir=LOG_DIR) regressor.fit(X['train'], y['train'], validation_monitor, logdir=LOG_DIR) # > last training steps # Step #9700, epoch #119, avg. train loss: 0.00082, avg. val loss: 0.00084 # Step #9800, epoch #120, avg. train loss: 0.00083, avg. val loss: 0.00082 # Step #9900, epoch #122, avg. train loss: 0.00082, avg. val loss: 0.00082 # Step #10000, epoch #123, avg. train loss: 0.00081, avg. val loss: 0.00081

预测测试数据

mse = mean_squared_error(regressor.predict(X['test']), y['test']) print ("Error: {}".format(mse)) # 0.000776

真实sin函数

使用TensorFlow的递归神经网络(LSTM)进行序列预测

预测sin函数

使用TensorFlow的递归神经网络(LSTM)进行序列预测

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/6b43192054a6a77131c524e1ea110d91.html