Art*_*tur 6 neural-network keras
但我最好的结果看起来像:
(右侧的损失函数)我什至尝试过 50k epoch,得到类似的结果。
模型:
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()
Run Code Online (Sandbox Code Playgroud)
PrettyPlot函数创建绘图。
如何在不改变神经网络拓扑的情况下获得更好的结果?我不希望它太大或太宽。如果可能的话,我想使用更少的隐藏层和神经元。
我想要近似的函数:
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)
Run Code Online (Sandbox Code Playgroud)
样品:
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)
Run Code Online (Sandbox Code Playgroud)
然后使用 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)
Run Code Online (Sandbox Code Playgroud)
最后一层的激活函数是线性的。