【问题标题】:Error while running on GCP: unexpected keyword argument 'maximum_iterations'在 GCP 上运行时出错:意外的关键字参数“maximum_iterations”
【发布时间】: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


    【解决方案1】:

    尝试升级您的tensorflow。问题将得到解决。

    CPU:pip install –upgrade tensorflow
    GPU:pip install –upgrade tensorflow-gpu
    

    【讨论】:

      【解决方案2】:

      我在本地机器上运行时遇到了类似的问题。以下是我遵循的步骤

      我的 conda 环境名称是 TESTENV

      1. 使用

        登录或进入您的 conda 环境
         source activate TESTENV
        
      2. 检查conda环境中是否已经安装了pip,否则安装它

         conda install pip
        
      3. 安装 TensorFlow 1.4.1 版

        pip install --ignore-installed --upgrade https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-1.4.1-py2-none-any.whl

      4. 安装 Keras 2.1.2 版

        conda install keras=2.1.2

      【讨论】:

      • 遇到了同样的问题。正在运行 tensorflow 版本 1.4.0 并使用 pip 安装了最新的 Keras,但必须卸载它并重新安装 Keras 版本 2.1.2
      【解决方案3】:
      conda remove keras*
      conda install keras-gpu==2.1.6
      

      解决了我同样的问题。

      【讨论】: