如何使用Keras预测功能/表? [英] How to predict a function/table using Keras?

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

问题描述

我目前正在学习喀拉拉邦.我的目标是创建一个可以预测函数值的简单模型.首先,我创建两个数组,一个用于X值,一个用于对应的Y值.

I am currently learning keras. My goal is to create a simple model, that predicts values of a function. At first I create two arrays, one for the X-Values and one for the corresponding Y-Values.

# declare and init arrays for training-data
X = np.arange(0.0, 10.0, 0.05)
Y = np.empty(shape=0, dtype=float)

# Calculate Y-Values
for x in X:
    Y = np.append(Y, float(0.05*(15.72807*x - 7.273893*x**2 + 1.4912*x**3 - 0.1384615*x**4 + 0.00474359*x**5)))

然后我创建并训练模型

# model architecture
model = Sequential()
model.add(Dense(1, input_shape=(1,)))
model.add(Dense(5))
model.add(Dense(1, activation='linear'))

# compile model
model.compile(loss='mean_absolute_error', optimizer='adam', metrics=['accuracy'])

# train model
model.fit(X, Y, epochs=150, batch_size=10)

并使用模型预测值

# declare and init arrays for prediction
YPredict = np.empty(shape=0, dtype=float)

# Predict Y
YPredict = model.predict(X)

# plot training-data and prediction
plt.plot(X, Y, 'C0')
plt.plot(X, YPredict, 'C1')

# show graph
plt.show()

我得到以下输出(蓝色是训练数据,橙色是预测):

and I get this output (blue is training-data, orange is prediction):

我做错了什么?我想这是网络体系结构的根本问题,对吧?

What did I do wrong? I guess it's a fundamental problem with the network-architecture, right?

推荐答案

问题确实出在您的网络体系结构上.具体来说,您在所有层中都使用了线性激活:这意味着网络只能容纳线性函数.您应该在输出层中保持线性激活,但是应该在隐藏层中使用ReLU激活:

The problem is indeed with your network architecture. Specifically, you are using linear activations in all layers: this means that the network can only fit linear functions. You should keep a linear activation in the output layer, but you should use a ReLU activation in the hidden layer:

model.add(Dense(1, input_shape=(1,)))
model.add(Dense(5, activation='relu'))
model.add(Dense(1, activation='linear'))

然后,播放隐藏层的数量/大小;我建议您多使用一些.

Then, play with the number/size of the hidden layers; I suggest you use a couple more.

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

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