训练神经网络计算 atan2(y, x)
Training NN to calculate atan2(y, x)
我一直在研究 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 次迭代,此神经网络仍会失败:
Testing atan2(0.949040, 0.756997) = 0.897493 -> 0.987712
我还尝试在输出层上使用线性激活函数 (FANN_LINEAR
)。没有运气。事实上,结果要糟糕得多。经过 100,000,000 次迭代后,我们得到:
Testing atan2(0.949040, 0.756997) = 0.897493 -> 7.648625
这比随机初始化权重时更糟糕。神经网络如何在训练后变得更糟?
我发现 FANN_LINEAR
的这个问题与其他测试一致。当需要线性输出时(例如在计算 Q 值时,它对应任意大或小的奖励),这种方法失败得很惨,而且错误实际上似乎随着训练而增加。
所以这是怎么回事?使用完全连接的 2-3-1 NN 是否不适合这种情况?隐藏层中的对称 sigmoid 激活函数是否不合适?我看不出还有什么可能导致此错误。
您面临的问题是正常的,并且您的预测器的质量不会通过增加迭代次数来提高,您应该通过添加一些层或增加 NN 的大小来增加 NN 的大小隐藏层。例如,您可以尝试使用 2-256-128-1 而不是 2-3-1。通常这样效果会更好。如果你想看看这个 simple code 我在 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))
运行此代码将得到
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.0010
我一直在研究 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 次迭代,此神经网络仍会失败:
Testing atan2(0.949040, 0.756997) = 0.897493 -> 0.987712
我还尝试在输出层上使用线性激活函数 (FANN_LINEAR
)。没有运气。事实上,结果要糟糕得多。经过 100,000,000 次迭代后,我们得到:
Testing atan2(0.949040, 0.756997) = 0.897493 -> 7.648625
这比随机初始化权重时更糟糕。神经网络如何在训练后变得更糟?
我发现 FANN_LINEAR
的这个问题与其他测试一致。当需要线性输出时(例如在计算 Q 值时,它对应任意大或小的奖励),这种方法失败得很惨,而且错误实际上似乎随着训练而增加。
所以这是怎么回事?使用完全连接的 2-3-1 NN 是否不适合这种情况?隐藏层中的对称 sigmoid 激活函数是否不合适?我看不出还有什么可能导致此错误。
您面临的问题是正常的,并且您的预测器的质量不会通过增加迭代次数来提高,您应该通过添加一些层或增加 NN 的大小来增加 NN 的大小隐藏层。例如,您可以尝试使用 2-256-128-1 而不是 2-3-1。通常这样效果会更好。如果你想看看这个 simple code 我在 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))
运行此代码将得到
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.0010