简体   繁体   English

使用Keras进行整数序列预测

[英]Integer Series prediction using Keras

I'm trying to code a RNN model that will predict the next number in the integer series. 我正在尝试编写一个RNN模型,该模型将预测整数序列中的下一个数字。 The model loss gets smaller with each epoch, but the predictions never get quite accurate. 模型损失在每个时期都会变小,但是预测永远不会变得非常准确。 I've tried many train set sizes and numbers of epochs, but my predicted value is always off from the expected by few digits. 我已经尝试了许多训练集大小和时期数,但我的预测值始终与预期值相差几位数。 Can you give me some hints what to improve or what I'm doing wrong? 您能否给我一些提示,以改善或我做错了什么? This is the code: 这是代码:

from keras.models import Sequential
from keras.layers import Dense, Dropout, LSTM
from keras.callbacks import ModelCheckpoint
from keras.utils import np_utils
from keras import metrics
import numpy as np

training_length = 10000
rnn_size = 512
hm_epochs = 30

def generate_sequence(length=10):
    step = np.random.randint(0,50)
    first_element = np.random.randint(0,10)
    first_element = 0
    l_ist = [(first_element + (step*i)) for i in range(length)]
    return l_ist

training_set = []

for _ in range(training_length):
    training_set.append(generate_sequence(10))

feature_set = [i[:-1] for i in training_set]

label_set = [i[-1:] for i in training_set]

X = np.reshape(feature_set,(training_length, 9, 1))
y = np.array(label_set)


model = Sequential()
model.add(LSTM(rnn_size, input_shape = (X.shape[1], X.shape[2]), return_sequences = True))
model.add(Dropout(0.2))
model.add(LSTM(rnn_size))
model.add(Dropout(0.2))
model.add(Dense(y.shape[1], activation='linear'))
model.compile(loss='mse', optimizer='rmsprop', metrics=['accuracy'])

filepath="checkpoint_folder/weights-improvement.hdf5"
checkpoint = ModelCheckpoint(filepath, monitor='loss', verbose=1, save_best_only=True, mode='min')
callbacks_list = [checkpoint]

model.fit(X,y,epochs=hm_epochs, callbacks=callbacks_list)

Effects: 效果:

After 30 epochs (Loss: 66.39): 30个纪元后(亏损:66.39):

1 Sequence: [0, 20, 40, 60, 80, 100, 120, 140, 160]Expected: [180] || 1顺序:[0,20,40,60,80,100,120,140,160]预期:[180] || Got: [181.86118] 得到了:[181.86118]

2 Sequence: [0, 11, 22, 33, 44, 55, 66, 77, 88]Expected: [99] || 2顺序:[0,11,22,33,44,55,66,77,88]预期:[99] || Got: [102.17369] 得到了:[102.17369]

3 Sequence: [0, 47, 94, 141, 188, 235, 282, 329, 376]Expected: [423] || 3顺序:[0,47,94,141,188,235,282,329,376]预计:[423] || Got: [419.1763] 得到了:[419.1763]

4 Sequence: [0, 47, 94, 141, 188, 235, 282, 329, 376]Expected: [423] || 4顺序:[0,47,94,141,188,235,282,329,376]预期:[423] || Got: [419.1763] 得到了:[419.1763]

5 Sequence: [0, 4, 8, 12, 16, 20, 24, 28, 32]Expected: [36] || 5序列:[0,4,8,12,16,20,24,28,32]预期:[36] || Got: [37.506496] 得到了:[37.506496]

6 Sequence: [0, 48, 96, 144, 192, 240, 288, 336, 384]Expected: [432] || 6序列:[0、48、96、144、192、240、288、336、384]预期:[432] || Got: [425.0569] 得到了:[425.0569]

7 Sequence: [0, 28, 56, 84, 112, 140, 168, 196, 224]Expected: [252] || 7顺序:[0,28,56,84,112,140,168,196,224]预计:[252] || Got: [253.60233] 得到了:[253.60233]

8 Sequence: [0, 18, 36, 54, 72, 90, 108, 126, 144]Expected: [162] || 8顺序:[0、18、36、54、72、90、108、126、144]预期:[162] || Got: [163.538] 得到了:[163.538]

9 Sequence: [0, 19, 38, 57, 76, 95, 114, 133, 152]Expected: [171] || 9顺序:[0,19,38,57,76,95,114,133,152]预期:[171] || Got: [173.77933] 得到了:[173.77933]

10 Sequence: [0, 1, 2, 3, 4, 5, 6, 7, 8]Expected: [9] || 10序列:[0,1,2,3,4,5,6,7,8]预期:[9] || Got: [9.577981] 得到了:[9.577981]

... ...

After 100 epochs (Loss: 54.81): 100个纪元后(亏损:54.81):

1 Sequence: [0, 20, 40, 60, 80, 100, 120, 140, 160] Expected: [180] || 1顺序:[0,20,40,60,80,100,120,140,160]预期值:[180] || Got: [181.03535] 得到了:[181.03535]

2 Sequence: [0, 11, 22, 33, 44, 55, 66, 77, 88] Expected: [99] || 2顺序:[0,11,22,33,44,55,66,77,88]预期值:[99] || Got: [99.15022] 得到了:[99.15022]

3 Sequence: [0, 47, 94, 141, 188, 235, 282, 329, 376] Expected: [423] || 3顺序:[0,47,94,141,188,235,282,329,376]预期值:[423] || Got: [423.7969] 得到了:[423.7969]

4 Sequence: [0, 47, 94, 141, 188, 235, 282, 329, 376] Expected: [423] || 4序列:[0,47,94,141,188,235,282,329,376]预期值:[423] || Got: [423.7969] 得到了:[423.7969]

5 Sequence: [0, 4, 8, 12, 16, 20, 24, 28, 32] Expected: [36] || 5序列:[0,4,8,12,16,20,24,28,32]预期值:[36] || Got: [34.47298] 得到了:[34.47298]

6 Sequence: [0, 48, 96, 144, 192, 240, 288, 336, 384] Expected: [432] || 6序列:[0,48,96,144,192,240,288,336,384]预期值:[432] || Got: [432.73163] 得到了:[432.73163]

7 Sequence: [0, 28, 56, 84, 112, 140, 168, 196, 224] Expected: [252] || 7顺序:[0,28,56,84,112,140,168,196,224]预期值:[252] || Got: [251.55792] 得到了:[251.55792]

8 Sequence: [0, 18, 36, 54, 72, 90, 108, 126, 144] Expected: [162] || 8顺序:[0,18,36,54,72,90,108,126,144]预期值:[162] || Got: [164.81227] 得到了:[164.81227]

9 Sequence: [0, 19, 38, 57, 76, 95, 114, 133, 152] Expected: [171] || 9序列:[0,19,38,57,76,95,114,133,152]预期值:[171] || Got: [172.6425] 得到了:[172.6425]

10 Sequence: [0, 1, 2, 3, 4, 5, 6, 7, 8] Expected: [9] || 10序列:[0、1、2、3、4、5、6、7、8]预期值:[9] || Got: [8.837313] 得到了:[8.837313]

Did you try it with a longer sequence? 您是否尝试了更长的顺序? LSTM is not required, because the dependency is not very long. 不需要LSTM,因为依赖性不是很长。 You can give a try with another variant of RNN. 您可以尝试使用RNN的另一个变体。

Regarding your examples your input sequence is just (x 2x 3x) etc. This is not a problem for recurrent neural networks. 关于示例,您的输入序列仅为(x 2x 3x)等。对于递归神经网络来说,这不是问题。 You want to learn a calculation policy, not long term dependecies in features. 您想学习一种计算策略,而不是功能的长期依赖。 RNNs are very powerful and can find very complex pattern, but for this they are not the right tool. RNN非常强大,可以找到非常复杂的模式,但为此,它们不是正确的工具。

To solve such a problem you can take a look at evolutionary algorithms. 为了解决这个问题,您可以看一下进化算法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM