Python 如何处理LSTM无法学习的情况(不断做出同样的错误预测)

Python 如何处理LSTM无法学习的情况(不断做出同样的错误预测),python,machine-learning,time-series,keras,lstm,Python,Machine Learning,Time Series,Keras,Lstm,我试图使用LSTM神经网络来创作一首歌曲。基本上,这是基于一个文本生成器(尝试在查看一系列字符后预测下一个字符),但它尝试预测注释,而不是字符 用作输入的midi文件的结构(Y轴是音高或音符值,而X轴是时间): 这是预测的音符值: 我设定了一个50的纪元,但似乎LSTM的损失率没有减少,大部分时间它的损失率没有提高 我怀疑这是因为有大量的特定注释(在本例中为注释值65),这使得LSTM在训练阶段变懒,每次都预测65 我觉得这是LSTM和基于时间序列的学习算法中的一个常见问题。我如何解决这样

我试图使用LSTM神经网络来创作一首歌曲。基本上,这是基于一个文本生成器(尝试在查看一系列字符后预测下一个字符),但它尝试预测注释,而不是字符

用作输入的midi文件的结构(Y轴是音高或音符值,而X轴是时间):

这是预测的音符值:

我设定了一个50的纪元,但似乎LSTM的损失率没有减少,大部分时间它的损失率没有提高

我怀疑这是因为有大量的特定注释(在本例中为注释值65),这使得LSTM在训练阶段变懒,每次都预测65

我觉得这是LSTM和基于时间序列的学习算法中的一个常见问题。我如何解决这样的问题?如果我提到的不是问题,那么问题是什么,我该如何解决

如果您需要,我将使用以下代码进行培训:

import numpy 
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import Dropout
from keras.layers import LSTM
from keras.callbacks import ModelCheckpoint
from keras.utils import np_utils

seq_length = 100

read_path = '../matrices/input/world-is-mine/world-is-mine-y-0.npy' 


raw_text = numpy.load(read_path)


# create mapping of unique chars to integers, and a reverse mapping
chars = sorted(list(set(raw_text)))
char_to_int = dict((c,i) for i,c in enumerate(chars))

n_chars = len(raw_text)
n_vocab = len(chars)


# prepare the dataset of input to output pairs encoded as integers
dataX = []
dataY = []

# dataX is the encoding version of the sequence
# dataY is an encoded version of the next prediction
for i in range(0, n_chars - seq_length, 1):
    seq_in = raw_text[i:i + seq_length]
    seq_out = raw_text[i+seq_length]
    dataX.append([char_to_int[char] for char in seq_in])
    dataY.append(char_to_int[seq_out])


n_patterns = len(dataX)
print "Total Patterns: ", n_patterns

# reshape X to be [samples, time steps, features]
X = numpy.reshape(dataX, (n_patterns, seq_length,1))

# normalize
X = X/float(n_vocab)

# one hot encode the output variable
y = np_utils.to_categorical(dataY)

print 'X: ', X.shape
print 'Y: ', y.shape


# define the LSTM model
model = Sequential()
model.add(LSTM(256, input_shape=(X.shape[1], X.shape[2]), return_sequences=True))
#model.add(Dropout(0.05))
model.add(LSTM(256))
#model.add(Dropout(0.05))
model.add(Dense(y.shape[1], activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam')

# There is no test dataset. We are modeling the entire training dataset to learn the probability of each character in a sequence.
# We are interested in a generalization of the dataset that minimizes the chosen loss function
# We are seeking a balance between generalization of the dataset and overfitting but short of memorization

# define the check point
filepath="../checkpoints/weights-{epoch:02d}-{loss:.4f}.hdf5"
checkpoint = ModelCheckpoint(filepath, monitor='loss', verbose=1, save_best_only=True, mode='min')
callbacks_list = [checkpoint]

model.fit(X,y, nb_epoch=50, batch_size=64, callbacks=callbacks_list)

我没有处理音乐数据的经验。根据我对文本数据的经验,这似乎是一个不合适的模型。增加具有不同注释值的训练数据集可以克服欠拟合问题。似乎培训示例不足以学习音符变化。例如,对于char语言模型,1MB数据太小,无法训练合理的LSTM模型。另外,试着先用较小的序列长度(比如20)进行训练。在训练数据有限的情况下,较小的序列长度比较长的序列长度更容易学习。

您可能太过合适了。。你的样本量是多少?正如@Minato所说,这听起来像是过度拟合。这实际上是欠拟合,而不是过度拟合,因为他的模型只是学习了信号的预期值,而不是任何训练数据特征。你是否尝试过更长时间的训练?65和任何其他值之间的点数是多少?你正在训练长度为100的序列,也许它不足以捕捉音符中的变化?为什么不在整个信号机上训练呢?总的来说,这似乎有点困难(因为LSTM可能必须记住所有内容,因为这是离散的、极短的信号,没有大量重复/relations@lejlot还要多长时间?我觉得如果我添加更多的历元不会有多大区别,因为大多数历元都无法改进和学习。如果将大小增加到300 b以上,我真的无法改进因为我得到了某种错误[文件“model.py”,第57行,在y=np_utils.to_category(dataY)ValueError:zero size数组到reduce操作最大值,没有标识]