Python神经网络预测

时间:2017-06-29 10:13:01

标签: neural-network

我正在开展一个项目,我有两个不同的数字:

1-第一个数字最大值为1500,最小值为200.

2-秒数字最大值为200,最小值为10.

3-我想创建神经网络,添加样本并训练网络预测最后一个数字,例如:

900,67 equals 87
870,99 equals 100
1000,50 equals ?

什么类型的神经网络可以用于我的项目?

1 个答案:

答案 0 :(得分:0)

在此示例中,您输入两个值并获得一个。

import numpy as np
import keras
from keras import Sequential
from keras.layers import Dense
import matplotlib.pyplot as plt

MIN = np.random.rand(100)*500
MAX = np.random.rand(100)*500 + 500
x = np.concatenate((MIN.reshape(-1,1),MAX.reshape(-1,1)),axis = 1)
y = np.sin(x[:,0])*500 + np.cos(x[:,1])*500

x_max = x.max()
y_max = y.max()
x = x/x_max
y = (y-y.min())/(y_max-y.min())
model = Sequential()
model.add(Dense(200,input_dim = 2, activation = 'relu'))
model.add(Dense(100, activation = 'sigmoid'))

model.add(Dense(100, activation = 'sigmoid'))
model.add(Dense(1,activation = 'relu'))
opt = keras.optimizers.Adam(learning_rate=0.001, beta_1=0.6, beta_2=0.97, amsgrad=False)
model.compile(loss='mean_squared_error',optimizer=opt , metrics=['mse'])
model.fit(x, y, epochs=10000, batch_size=2)

y_hat = model.predict(x)
plt.figure(figsize=(10,5))
plt.plot(y)
plt.plot(y_hat.reshape(-1))

这是结果:

enter image description here

您将需要进行预处理和后处理,对来自神经网络的输入进行归一化和重新缩放输出。这是输入:

enter image description here

使用示例:

In [10]: model.predict(np.array([0.234,0.567]).reshape(-1,2))
Out[10]: array([[0.61975896]], dtype=float32)