我一直在研究Q强化学习的实现,其中Q(π,a)是用神经网络近似的。在解决问题的过程中,我将问题简化为一个非常简单的第一步:训练一个神经网络来计算atan2(y,x)。
我使用FANN来解决这个问题,但是这个库在很大程度上是无关的,因为这个问题更多地是关于适当的技术使用。
我一直在努力教神经网络,给定输入= {x,y},计算输出= atan2(y,x)。
这是我一直在使用的天真的方法。这是非常简单的,但我试图保持这个简单的工作。
#include "fann.h"
#include <cstdio>
#include <random>
#include <cmath>
int main()
{
// creates a 3 layered, densely connected neural network, 2-3-1
fann *ann = fann_create_standard(3, 2, 3, 1);
// set the activation functions for the layers
fann_set_activation_function_hidden(ann, FANN_SIGMOID_SYMMETRIC);
fann_set_activation_function_output(ann, FANN_SIGMOID_SYMMETRIC);
fann_type input[2];
fann_type expOut[1];
fann_type *calcOut;
std::default_random_engine rng;
std::uniform_real_distribution<double> unif(0.0, 1.0);
for (int i = 0; i < 100000000; ++i) {
input[0] = unif(rng);
input[1] = unif(rng);
expOut[0] = atan2(input[1], input[0]);
// does a single incremental training round
fann_train(ann, input, expOut);
}
input[0] = unif(rng);
input[1] = unif(rng);
expOut[0] = atan2(input[1], input[0]);
calcOut = fann_run(ann, input);
printf("Testing atan2(%f, %f) = %f -> %f\n", input[1], input[0], expOut[0], calcOut[0]);
fann_destroy(ann);
return 0;
}超级简单,对吧?然而,即使经过100,000,000次迭代,这个神经网络也失败了:
测试atan2(0.949040,0.756997) = 0.897493 -> 0.987712
我还尝试在输出层(FANN_LINEAR)上使用线性激活函数。不走运。事实上,结果要糟糕得多。经过100,000,000次迭代,我们得到:
测试atan2(0.949040,0.756997) = 0.897493 -> 7.648625
这比随机初始化权值还要糟糕。训练后神经网络怎么会变坏?
我发现FANN_LINEAR的这个问题与其他测试是一致的。当需要线性输出时(例如,在计算Q值时,它对应于任意大小的奖励),这种方法很不幸地失败了,而且误差实际上似乎随着训练而增加。
那是怎么回事?在这种情况下,使用完全连接的2-3-1NN是否不合适?隐藏层中的对称乙状结肠激活函数是否不合适?我看不出还有什么能解释这个错误。
发布于 2020-01-14 08:10:18
您所面临的问题是正常的,您的预测器的质量不会通过增加迭代次数来提高,您应该通过添加一些层或增加隐藏层的大小来增加NN的大小。例如,您可以尝试2-256-128-1,而不是2-3-1。通常情况下,这样做会更好。如果您想查看一下我用python编写的这个简单代码来完成相同的任务,那么它会很好地工作。
import numpy as np
from numpy import arctan2
from keras.models import Sequential
from keras.layers import Dense, InputLayer
nn_atan2 = Sequential()
nn_atan2.add(Dense(256, activation="sigmoid", input_shape=(2,)))
nn_atan2.add(Dense(128, activation="sigmoid"))
nn_atan2.add(Dense(1, activation='tanh'))
nn_atan2.compile(optimizer="adam", loss="mse")
nn_atan2.summary()
N = 100000
X = np.random.uniform(size=(N,2) )
y = arctan2(X[:,0], X[:,1])/(np.pi*0.5)
nn_atan2.fit(X,y, epochs=10, batch_size=128)
def predict(x, y):
return float(nn_atan2.predict(np.array([[x, y]]))*(np.pi*0.5))Runnin这段代码将给
Epoch 1/10
100000/100000 [==============================] - 3s 26us/step - loss: 0.0289
Epoch 2/10
100000/100000 [==============================] - 2s 24us/step - loss: 0.0104
Epoch 3/10
100000/100000 [==============================] - 2s 24us/step - loss: 0.0102
Epoch 4/10
100000/100000 [==============================] - 2s 24us/step - loss: 0.0096
Epoch 5/10
100000/100000 [==============================] - 2s 24us/step - loss: 0.0082
Epoch 6/10
100000/100000 [==============================] - 2s 23us/step - loss: 0.0051
Epoch 7/10
100000/100000 [==============================] - 2s 23us/step - loss: 0.0027
Epoch 8/10
100000/100000 [==============================] - 2s 23us/step - loss: 0.0019
Epoch 9/10
100000/100000 [==============================] - 2s 23us/step - loss: 0.0014
Epoch 10/10
100000/100000 [==============================] - 2s 23us/step - loss: 0.0010https://stackoverflow.com/questions/59691534
复制相似问题