【发布时间】:2025-12-04 03:10:01
【问题描述】:
我正在尝试使用书籍作为输入来创建用于字符识别和预测的 RNN。在我的本地机器上运行每个 epoch 需要几分钟,所以我尝试在 GCP 上运行它。
在 Google Cloud Platform 上执行我的代码时出现以下错误。但是当我使用 Spyder3 在本地机器上尝试时,代码正在正常执行。
# Character Prediction using RNN
# Small LSTM Network to Generate Text for Alice in Wonderland
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
# load ascii text and covert to lowercase
filename = "Alice in Wonderland.txt"
raw_text = open(filename).read()
raw_text = raw_text.lower()
# create mapping of unique chars to integers
chars = sorted(list(set(raw_text)))
char_to_int = dict((c, i) for i, c in enumerate(chars))
# summarize the loaded data
n_chars = len(raw_text)
n_vocab = len(chars)
print ("Total Characters: ", n_chars)
print ("Total Vocab: ", n_vocab)
# prepare the dataset of input to output pairs encoded as integers
seq_length = 100
X_train = []
y_train = []
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]
X_train.append([char_to_int[char] for char in seq_in])
y_train.append(char_to_int[seq_out])
n_patterns = len(X_train)
print ("Total Patterns: ", n_patterns)
# reshape X to be [samples, time steps, features]
X = numpy.reshape(X_train, (len(X_train), seq_length, 1))
# normalize
X = X / float(n_vocab)
# one hot encode the output variable
y = np_utils.to_categorical(y_train)
# define the LSTM model
model = Sequential()
model.add(LSTM(256, input_shape=(X.shape[1], X.shape[2])))
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}.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=20, batch_size=128, callbacks=callbacks_list)
创建 LSTM 时发生错误,如下行:
model.add(LSTM(256, input_shape=(X.shape[1], X.shape[2])))
这是错误:
文件“/root/anaconda3/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py”,第 2957 行,在 rnn 最大迭代次数=输入长度)
TypeError: while_loop() 得到了一个意外的关键字参数“maximum_iterations”
【问题讨论】:
标签: python keras google-cloud-platform lstm rnn