Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python keras LSTM的重塑变量_Python_Keras_Lstm - Fatal编程技术网

Python keras LSTM的重塑变量

Python keras LSTM的重塑变量,python,keras,lstm,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()]

我想让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)
Exmaple from 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.reformate(dictionary,(n_patterns,seq_length,1))
,它输出
无法将大小为1的数组重塑为形状(775100,1)
。您能帮我做些什么吗。

这里的问题是示例代码中的
dataX
不应该被
字典
取代,而是由数据中的
n_模式
样本列表取代,每个样本都应该是长度
seq_length
的子序列,其项应该是长度为
vocab_size
的一个热向量

通常会以类似的方式创建这样的数据集(调整
seq_length
,并调整
range
的第三个参数以适应口味):

您可能还想考虑使用一个SET而不是一个计数器,用于<代码> BuudgDeasDATET< < /C> >,这将导致这个函数替代:

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):


你把所有的单词都指定为数字是什么意思?这些只是单词的整数索引吗?字典是:'the':0','and':1','to':2,我在dataX.append([keras.utils.to_category(Dictionary[word],num_classes=vocab_size)行中得到一个error-KeyError:'Alice'(文本中的第一个单词),用于训练_数据[I:I+seq_length]]中的单词。
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)
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)