是否有使用Python中的NARX模型训练和预测/推断数据的端到端示例?
有图书馆PyNeurgen NARX PyNeurgen library
但是PyNeurgen的文档不是很完整。
该OP似乎已经编写了Keras实现,但是代码缺少用于推理/预测的实现。 NARX implementation using keras
python参考方案
我认为您正在寻找model.fit()
和model.predict()
的等效项,它们是pyneurgen中的net.learn()
和net.test()
。基于此tutorial和example,我编写了此演示(在python 2.7中),以使您了解如何完成此演示。希望对您有所帮助。
import math
import numpy as np
import matplotlib.pylab as plt
from pyneurgen.neuralnet import NeuralNet
from pyneurgen.recurrent import NARXRecurrent
def plot_results(x, y, y_true):
plt.subplot(3, 1, 1)
plt.plot([i[1] for i in population])
plt.title("Population")
plt.grid(True)
plt.subplot(3, 1, 2)
plt.plot(x, y, 'bo', label='targets')
plt.plot(x, y_true, 'ro', label='actuals')
plt.grid(True)
plt.legend(loc='lower left', numpoints=1)
plt.title("Test Target Points vs Actual Points")
plt.subplot(3, 1, 3)
plt.plot(range(1, len(net.accum_mse) + 1, 1), net.accum_mse)
plt.xlabel('epochs')
plt.ylabel('mean squared error')
plt.grid(True)
plt.title("Mean Squared Error by Epoch")
plt.show()
def generate_data():
# all samples are drawn from this population
pop_len = 200
factor = 1.0 / float(pop_len)
population = [[i, math.sin(float(i) * factor * 10.0)] for i in range(pop_len)]
population_shuffle = population[:]
all_inputs = []
all_targets = []
np.random.shuffle(population_shuffle)
for position, target in population_shuffle:
all_inputs.append([position * factor])
all_targets.append([target])
# print(all_inputs[-1], all_targets[-1])
return population, all_inputs, all_targets
# generate data
population, all_inputs, all_targets = generate_data()
# NARXRecurrent
input_nodes, hidden_nodes, output_nodes = 1, 10, 1
output_order, incoming_weight_from_output = 3, .6
input_order, incoming_weight_from_input = 2, .4
# init neural network
net = NeuralNet()
net.init_layers(input_nodes, [hidden_nodes], output_nodes,
NARXRecurrent(output_order, incoming_weight_from_output,
input_order, incoming_weight_from_input))
net.randomize_network()
net.set_halt_on_extremes(True)
# set constrains and rates
net.set_random_constraint(.5)
net.set_learnrate(.1)
# set inputs and outputs
net.set_all_inputs(all_inputs)
net.set_all_targets(all_targets)
# set lengths
length = len(all_inputs)
learn_end_point = int(length * .8)
# set ranges
net.set_learn_range(0, learn_end_point)
net.set_test_range(learn_end_point + 1, length - 1)
# add activation to layer 1
net.layers[1].set_activation_type('tanh')
# fit data to model
net.learn(epochs=150, show_epoch_results=True, random_testing=False)
# define mean squared error
mse = net.test()
print "Testing mse = ", mse
# define data
x = [item[0][0] * 200.0 for item in net.get_test_data()]
y = [item[0][0] for item in net.test_targets_activations]
y_true = [item[1][0] for item in net.test_targets_activations]
# plot results
plot_results(x, y, y_true)
结果如下图:
有关更多示例,请查看以下链接:
Air_Quality_ppm_prediction/NARXImproved.ipynb
NARX-Prediction-Model/NARX_Neural_Network.ipynb
CS230project/NARX_PyNeurGen.ipynb
TimeSeriesNotes/narx.py
我在Windows上使用Python 3和sqlite3。我正在开发一个使用数据库存储联系人的小型应用程序。我注意到,如果应用程序被强制关闭(通过错误或通过任务管理器结束),则会收到sqlite3错误(sqlite3.OperationalError:数据库已锁定)。我想这是因为在应用程序关闭之前,我没有正确关闭数据库连接。我已经试过了: connectio…
Python pytz时区函数返回的时区为9分钟 - python由于某些原因,我无法从以下代码中找出原因:>>> from pytz import timezone >>> timezone('America/Chicago') 我得到:<DstTzInfo 'America/Chicago' LMT-1 day, 18:09:00 STD…
用大写字母拆分字符串,但忽略AAA Python Regex - python我的正则表达式:vendor = "MyNameIsJoe. I'mWorkerInAAAinc." ven = re.split(r'(?<=[a-z])[A-Z]|[A-Z](?=[a-z])', vendor) 以大写字母分割字符串,例如:'我的名字是乔。 I'mWorkerInAAAinc”变成…
如何打印浮点数的全精度[Python] - python我编写了以下函数,其中传递了x,y的值:def check(x, y): print(type(x)) print(type(y)) print(x) print(y) if x == y: print "Yes" 现在当我打电话check(1.00000000000000001, 1.0000000000000002)它正在打印:<…
Python:如何根据另一列元素明智地查找一列中的空单元格计数? - pythondf = pd.DataFrame({'user': ['Bob', 'Jane', 'Alice','Jane', 'Alice','Bob', 'Alice'], 'income…