预测sin和cos混合函数
def sin_cos(x): return pd.DataFrame(dict(a=np.sin(x), b=np.cos(x)), index=x) X, y = generate_data(sin_cos, 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 #9500, epoch #117, avg. train loss: 0.00120, avg. val loss: 0.00118 # Step #9600, epoch #118, avg. train loss: 0.00121, avg. val loss: 0.00118 # Step #9700, epoch #119, avg. train loss: 0.00118, avg. val loss: 0.00118 # Step #9800, epoch #120, avg. train loss: 0.00118, avg. val loss: 0.00116 # Step #9900, epoch #122, avg. train loss: 0.00118, avg. val loss: 0.00115 # Step #10000, epoch #123, avg. train loss: 0.00117, avg. val loss: 0.00115预测测试数据
mse = mean_squared_error(regressor.predict(X['test']), y['test']) print ("Error: {}".format(mse)) # 0.001144真实的sin_cos函数
预测的sin_cos函数
预测x*sin函数 def x_sin(x): return x * np.sin(x) X, y = generate_data(x_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 #32500, epoch #401, avg. train loss: 0.48248, avg. val loss: 15.98678 # Step #33800, epoch #417, avg. train loss: 0.47391, avg. val loss: 15.92590 # Step #35100, epoch #433, avg. train loss: 0.45570, avg. val loss: 15.77346 # Step #36400, epoch #449, avg. train loss: 0.45853, avg. val loss: 15.61680 # Step #37700, epoch #465, avg. train loss: 0.44212, avg. val loss: 15.48604 # Step #39000, epoch #481, avg. train loss: 0.43224, avg. val loss: 15.43947预测测试数据
mse = mean_squared_error(regressor.predict(X['test']), y['test']) print ("Error: {}".format(mse)) # 61.024454351真实的x*sin函数
预测的x*sin函数
译者信息:侠天,专注于大数据、机器学习和数学相关的内容,并有个人公众号:bigdata_ny分享相关技术文章。
英文原文:Sequence prediction using recurrent neural networks(LSTM) with TensorFlow