用正弦函数训练神经网络 [英] Train neural network with sine function
问题描述
我想用sine()函数训练一个神经网络.
I want to train a neural network with the sine() function.
当前,我使用此代码和(大脑 gem):
Currently I use this code and the (cerebrum gem):
require 'cerebrum'
input = Array.new
300.times do |i|
inputH = Hash.new
inputH[:input]=[i]
sinus = Math::sin(i)
inputH[:output] = [sinus]
input.push(inputH)
end
network = Cerebrum.new
network.train(input, {
error_threshold: 0.00005,
iterations: 40000,
log: true,
log_period: 1000,
learning_rate: 0.3
})
res = Array.new
300.times do |i|
result = network.run([i])
res.push(result[0])
end
puts "#{res}"
但是它不起作用,如果我运行训练有素的网络,我会得到一些奇怪的输出值(而不是得到正弦曲线的一部分).
But it does not work, if I run the trained network I get some weird output values (instead of getting a part of the sine curve).
那么,我做错了什么?
So, what I am doing wrong?
推荐答案
Cerebrum是一种非常基本且缓慢的NN实现. Ruby中有更好的选择,例如ruby-fann
gem.
Cerebrum is a very basic and slow NN implementation. There are better options in Ruby, such as ruby-fann
gem.
您的问题很可能是网络过于简单.您尚未指定任何隐藏层-看起来代码为您的案例分配了一个默认的隐藏层,其中包含3个神经元.
Most likely your problem is the network is too simple. You have not specified any hidden layers - it looks like the code assigns a default hidden layer with 3 neurons in it for your case.
尝试类似的东西:
network = Cerebrum.new({
learning_rate: 0.01,
momentum: 0.9,
hidden_layers: [100]
})
并希望它永远需要训练,再加上效果还不是很好.
and expect it to take forever to train, plus still not be very good.
此外,您对300个输出的选择范围太广-对于网络而言,它看起来像是噪声,并且在点之间插值效果不好.神经网络无法以某种方式找出哦,那一定是正弦波"并与其匹配.相反,它在点之间进行插值-一次在多个维度上进行操作时,就会出现聪明点,也许是发现了手动检查无法轻易发现的结构.为了给它提供学习某些东西的合理机会,我建议您给它一些密集点,例如您当前使用sinus = Math::sin(i)
的位置,而不是使用:
Also, your choice of 300 outputs is too broad - to the network it will look mostly like noise and it won't interpolate well between points. A neural network does not somehow figure out "oh, that must be a sine wave" and match to it. Instead it interpolates between the points - the clever bit happens when it does so in multiple dimensions at once, perhaps finding structure that you could not spot so easily with a manual inspection. To give it a reasonable chance of learning something, I suggest you give it much denser points e.g. where you currently have sinus = Math::sin(i)
instead use:
sinus = Math::sin(i.to_f/10)
在正弦波中仍然有将近5次迭代.希望这足以证明网络可以学习任意功能.
That's still almost 5 iterations through the sine wave. Which should hopefully be enough to prove that the network can learn an arbitrary function.
这篇关于用正弦函数训练神经网络的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!