首页 > 解决方案 > 重塑 keras LSTM 的变量

问题描述

我想制作可以学习我将提供的文本的 LSTM。首先,我创建了获取训练数据的函数。

def read_data(filename):
    with open(filename) as f:
        content = f.readlines()
    content = [x.strip() for x in content]
    content = [word for i in range(len(content)) for word in content[i].split()]
    content = np.array(content)
    return content
training_data = read_data(filename)
print("Loaded training data...")

之后,我有一个分配所有单词编号的功能。

def build_dataset(words):
    count = collections.Counter(words).most_common()
    dictionary = dict()
    for word, _ in count:
        dictionary[word] = len(dictionary)
    reverse_dictionary = dict(zip(dictionary.values(), dictionary.keys()))
    return dictionary, reverse_dictionary
dictionary, reverse_dictionary = build_dataset(training_data)
vocab_size = len(dictionary)

来自字典变量的示例是'the': 0, 'and': 1, 'to': 2, 我找到了一些 LSTM 的示例代码

# 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)
# 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.2))
model.add(LSTM(256))
model.add(Dropout(0.2))
model.add(Dense(y.shape[1], activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam')
# define the checkpoint
filepath="weights-improvement-{epoch:02d}-{loss:.4f}-bigger.hdf5"
checkpoint = ModelCheckpoint(filepath, monitor='loss', verbose=1, save_best_only=True, mode='min')
callbacks_list = [checkpoint]
# fit the model
model.fit(X, y, epochs=50, batch_size=64, callbacks=callbacks_list)

我不明白我必须为重塑做什么。我的 seq 长度是 3,但是我如何确定模式的数量,据我所知 dataX 应该是单词的向量。当我这样做时X = np.reshape(dictionary, (n_patterns, seq_length, 1)),它会输出cannot reshape array of size 1 into shape (775,100,1)。你能帮我做什么吗。

标签: pythonkeraslstm

解决方案


这里的问题是,dataX在示例代码中不应替换为dictionary,而是由n_patterns数据中的样本列表替换,每个样本都应该是 length 的子序列seq_length,其中的术语应该是 length 的单热向量vocab_size

通常以与此类似的方式制作这样的数据集(adjustseq_lengthrangeto taste 的第三个参数):

seq_length=50
dataX=[]
dataY=[]
for i in range(0,len(training_data)-seq_length-1,3):
    dataX.append([keras.utils.to_categorical(dictionary[word],num_classes=vocab_size) for word in training_data[i:i+seq_length]])
    dataY.append(keras.utils.to_categorical(dictionary[training_data[i+seq_length]],num_classes=vocab_size))

您可能还需要考虑使用 set 而不是计数器 for build_dataset,这将导致此函数:

def build_dataset(words):
    dictionary = dict()
    for word in set(words):
        dictionary[word] = len(dictionary)
    reverse_dictionary = dict(zip(dictionary.values(), dictionary.keys()))
    return dictionary, reverse_dictionary
dictionary, reverse_dictionary = build_dataset(training_data)
vocab_size = len(dictionary)

因此,将它们放在一起,您的最终代码可能是(进行一些调整以使其适合 LSTM):

def read_data(filename):
    with open(filename) as f:
        content = f.readlines()
    content = [x.strip() for x in content]
    content = [word for i in range(len(content)) for word in content[i].split()]
    content = np.array(content)
    return content
training_data = read_data(filename)
print("Loaded training data...")

def build_dataset(words):
    dictionary = dict()
    for word in set(words):
        dictionary[word] = len(dictionary)
    reverse_dictionary = dict(zip(dictionary.values(), dictionary.keys()))
    return dictionary, reverse_dictionary
dictionary, reverse_dictionary = build_dataset(training_data)
vocab_size = len(dictionary)

seq_length=50
dataX=[]
dataY=[]
for i in range(0,len(training_data)-seq_length-1,3):
    dataX.append([keras.utils.to_categorical(dictionary[word],num_classes=vocab_size) for word in training_data[i:i+seq_length]])
    dataY.append(keras.utils.to_categorical(dictionary[training_data[i+seq_length]],num_classes=vocab_size))

n_patterns=len(dataX)
# reshape X to be [samples, time steps, features]
X = numpy.reshape(dataX, (n_patterns, seq_length, vocab_size))
# reshape Y
y = numpy.reshape(dataY, (n_patterns, vocab_size))
# define the LSTM model
model = keras.Sequential()
model.add(LSTM(256, input_shape=(seq_length,vocab_size), return_sequences=True))
model.add(Dropout(0.2))
model.add(LSTM(256))
model.add(Dropout(0.2))
model.add(Dense(y.shape[1], activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam')
# define the checkpoint
filepath="weights-improvement-{epoch:02d}-{loss:.4f}-bigger.hdf5"
checkpoint = ModelCheckpoint(filepath, monitor='loss', verbose=1, save_best_only=True, mode='min')
callbacks_list = [checkpoint]
# fit the model
model.fit(X, y, epochs=50, batch_size=64, callbacks=callbacks_list)

推荐阅读