Keras神经网络函数逼近

时间:2018-07-03 22:11:00

标签: neural-network keras

我正在尝试近似以下功能: enter image description here

但是我最好的结果是:

enter image description here (损失功能在右侧) 我什至尝试了5万个纪元,结果也差不多。

型号:

model = Sequential()
model.add(Dense(40, input_dim=1,kernel_initializer='he_normal', activation='relu'))
model.add(Dense(20, activation='relu'))
model.add(Dense(10, activation='relu'))
model.add(Dense(1,input_dim=1, activation=activation_fun))
model.compile(loss='mse', optimizer='adam', metrics=['mse', 'mae', 'mape', 'cosine'])
history = model.fit(x, y, batch_size=32, epochs=5000, verbose=0)

preds = model.predict(x_test)
prettyPlot(x,y,x_test,preds,history,'linear',5000)
model.summary()

prettyPlot 函数创建绘图。

如何在不更改NN拓扑的情况下获得更好的结果?我不希望它变大或变大。如果可能的话,我想使用更少的隐藏层和神经元。

我要近似的函数:

def fun(X):
    return math.sin(1.2*X + 0.5) + math.cos(2.5*X + 0.2) + math.atan(2*X + 1) -  math.cos(2*X + 0.5) 

样本:

range = 20
x = np.arange(0, range, 0.01).reshape(-1,1)
y = np.array(list(map(fun, x))).reshape(-1,1)

x_test = (np.random.rand(range*10)*range).reshape(-1,1)
y_test = np.array(list(map(fun, x_test))).reshape(-1,1)

然后使用MinMaxScaler将y和y_test标准化。

scalerY= MinMaxScaler((0,1))
scalerY.fit(y)
scalerY.fit(y_test)
y = scalerY.transform(y)
y_test = scalerY.transform(y_test)

最后一层的激活函数是线性的。

1 个答案:

答案 0 :(得分:1)

如果准确性仍然存在问题,请尝试使用10k-100k之类的许多数据点。

相关问题