python - 重塑 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)
。你能帮我做什么吗。
解决方案
这里的问题是,dataX
在示例代码中不应替换为dictionary
,而是由n_patterns
数据中的样本列表替换,每个样本都应该是 length 的子序列seq_length
,其中的术语应该是 length 的单热向量vocab_size
。
通常以与此类似的方式制作这样的数据集(adjustseq_length
和range
to 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)
推荐阅读
- c++ - 如何在没有任何其他修改的情况下在 C++ 中“漂亮地打印”一个 XML 字符串?
- makefile - 在 make 中使用过滤器功能时出现意外结果
- npm - 如何更改通过电子框架生成的.exe的文件版本、产品和版权值
- rest - 在 JWT 的情况下,SPA 和移动应用程序是否会针对同一端点进行身份验证?
- javascript - Material-ui 主题父组件但不是同类型子组件
- javascript - Three.js:TypeError:无法读取未定义的属性“0”
- json - 参数类型“未来”不能分配给参数类型“列表”
' - c++ - Minio:为什么我的 PUT 请求失败了,即使 GET 成功了?
- excel - Power Query M 为导入的 json 中的每个嵌套记录添加行
- python - 试图优化我的复杂函数以在多项式时间内执行