简体   繁体   English

神经网络 Keras 的预测不起作用

[英]Prediction by Neural network Keras is not working

I want to write a NN program for recognition using Keras.我想使用 Keras 编写一个用于识别的 NN 程序。

I have used 2 sets of data for training:我使用了两组数据进行训练:

toyX = [1, 2, 3, 4, 5, 6, 7, 8]
toyX2 = [18, 17, 16, 15, 14, 13, 12, 11].

After training with toyX and then toyX2 , the output of model.predict(toyX) is [[0.56053144 1.0758346 1.7890009 ]] .toyXtoyX2训练后, model.predict(toyX)的 output 为[[0.56053144 1.0758346 1.7890009 ]] However, it should have been [6, 11, 14] .但是,它应该是[6, 11, 14]

Should I add more layers or change parameters to improve prediction?我应该添加更多层或更改参数以改进预测吗? Which parameters I should change?我应该更改哪些参数?

Please help me to solve this problem.请帮我解决这个问题。

from keras.models import Sequential
from keras.optimizers import Adam
from keras.layers import Conv1D, MaxPooling1D
from keras.layers import Dense, Flatten
from keras.layers import Dropout
import numpy as np

model = Sequential()
model.add(Conv1D(filters=64, kernel_size=3, activation='relu', input_shape=(8, 1)))
model.add(Conv1D(filters=64, kernel_size=3, activation='relu'))
model.add(Dropout(0.5))
model.add(MaxPooling1D(pool_size=2))
model.add(Flatten())
model.add(Dense(100, activation='relu'))
#model.add(Dense(3, activation='softmax'))
model.add(Dense(3))
#model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
model.compile(loss='mean_squared_error', optimizer='adam', metrics=['accuracy'])
print(model.summary())


toyX = np.array([1, 2, 3, 4, 5, 6, 7, 8]).reshape(1, 8, 1)
toyX2 = np.array([18, 17, 16, 15, 14, 13, 12, 11]).reshape(1, 8, 1)
#print (toyX.shape)
toyY = np.array([6, 11, 14]).reshape(1, 3)
#print (toyY.shape)
toyY2 = np.array([1, 2, 3]).reshape(1, 3) # if flatten is active

model.fit(toyX, toyY, epochs = 1000, verbose = 0)
model.fit(toyX2, toyY2, epochs = 1000, verbose = 0)

print (model.predict(toyX))

This phenomenon is called catastrophic forgetting of neural networks.这种现象被称为神经网络的灾难性遗忘。

You can read a paper on this: https://arxiv.org/pdf/1708.02072.pdf您可以阅读有关此的论文: https://arxiv.org/pdf/1708.02072.pdf

Your toyX and toyX2 has a completely different distribution.您的toyXtoyX2具有完全不同的分布。 When you re-train your model with ToyX2 for 1000 epochs, your model has completely forgotten the mapping from toyX to toyY .当您使用 ToyX2 重新训练 model 1000 个 epoch 时,您的 model 完全忘记了从toyXtoyY的映射。

You should train only for very few epochs with a really small learning rate if you want to make sure the knowledge of previous training stays or just mix both of the sets and re-train.如果您想确保保留先前训练的知识,或者只是混合两个集合并重新训练,您应该只训练非常小的学习率的极少数时期。

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

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