使用Keras使用ANN预测正弦 [英] Predicting sine with ANN using Keras

查看:431
本文介绍了使用Keras使用ANN预测正弦的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是机器学习的新手,所以如果这个问题经常出现,我会提前道歉,因为我无法找到令人满意的答案. 作为教学活动,我一直在尝试训练ANN以预测正弦波.我的问题是,尽管我的神经网络准确地训练了正弦波的形状,但在验证集和较大的输入中却没有这样做.因此,我首先将输入和输出作为

I am new to machine learning so I will apologize in advance if this question is somewhat recurrent, as I haven't been able to find a satisfying answer to my problem. As a pedagogical exercise I have been trying to train an ANN to predict a sine wave. My problem is that although my neural network trains accurately the shape of the sine, it somewhat fails to do so in the validation set and to larger inputs. So I start by feeding my input and output as

x = np.arange(800).reshape(-1,1) / 50
y = np.sin(x)/2

其余代码按

model = Sequential()
model.add(Dense(20, input_shape=(1,),activation = 'tanh',use_bias = True))
model.add(Dense(20,activation = 'tanh',use_bias = True))
model.add(Dense(1,activation = 'tanh',use_bias = True))
model.compile(loss='mean_squared_error', optimizer=Adam(lr=0.005), metrics=['mean_squared_error'])


history = model.fit(x,y,validation_split=0.2, epochs=2000, batch_size=400, verbose=0)

然后我设计一个测试,定义为

I then devise a test, which is defined as

x1= np.arange(1600).reshape(-1,1) / 50
y1 = np.sin(x1)/2
prediction = model.predict(x1, verbose=1)

所以问题是ANN在验证集中显然开始失败,并预测正弦波的继续.

So the problem is that the ANN clearly starts to fail in the validation set and to predict a continuation of the sine wave.

验证集的怪异行为:

除了训练集以外,无法预测其他任何事情:

Failure to predict anything other than the training set:

那么,我在做什么错? ANN是否无法继续正弦波?我试图微调大多数可用参数,但均未成功.关于相似问题的大多数常见问题解答都是由于过拟合造成的,但我无法解决此问题.

So, what am I doing wrong? Is the ANN incapable of continuing the sine wave? I have tried to fine tune most of the available parameters without success. Most of the FAQs regarding similar issues are due to overfitting, but I haven't been able to solve this.

推荐答案

如上所述,您的NN无法掌握"数据的循环特性.

As pointed above, your NN is not capable of "grasping" cyclic nature of your data.

您只能将DNN由密集层组成,就像智能​​版本的线性回归一样-使用DNN的原因是拥有可以由网络本身学习"的高级非线性抽象特征,而不是手工工程特征.相反,这些功能很难描述和理解.

You can think of your DNN made of dense layers only as of smarter version of linear regression — the reason to use DNN is to have high-level non-linear abstract features that can be "learned" by network itself, instead of engineering features by hand. On contrary, this features are mostly hard to describe and understand.

因此,一般而言,DNN可以很好地预测中间"的未知点,x距离训练集越远,预测的准确性就越差.再次,一般.

So, in general DNNs are good for predicting unknown points "in the middle", more far your x from training set, less accurate prediction will be. Again, in general.

要预测本质上是周期性的事物,您应该使用更复杂的体系结构,或者对数据进行预处理,即了解季节"或基频".

To predict things that are cyclic in nature, you should either use more sophisticated architectures, or pre-process your data, i.e. by understanding "seasonality" or "base frequency".

这篇关于使用Keras使用ANN预测正弦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆