使用多个GPU进行Keras训练并追溯错误“AttributeError:'NoneType'对象没有属性'update'”

时间:2017-12-15 19:59:55

标签: keras lstm tensorflow-gpu

我在仅限CPU的计算机上尝试过我的代码并且运行正常。但是在我更改为GPU版本后,弹出错误“AttributeError:'NoneType'对象没有属性'update'”。有人帮我解决了吗?

我的部分源代码如下:

from keras.utils import multi_gpu_model
import tensorflow as tf`
...
def train_validation_test_model(model, X_tr, Y_tr, X_ts, Y_ts, filepath):
    print('-----------------------------------------------------------------------')
    print('Training...')
    X_mag_tr = 20 * np.log10(np.abs(X_tr))
    X_mag_ts = 20 * np.log10(np.abs(X_ts))
    EarlyStop = callbacks.EarlyStopping(monitor = 'val_loss', patience = 10, verbose = 0, mode = 'min')
    CheckPoint = callbacks.ModelCheckpoint(filepath, monitor = 'val_loss', save_best_only = True, mode = 'min')
    opti = optimizers.Adadelta(lr=1.0, rho=0.95, epsilon=1e-08, clipnorm=1.)
    model.compile(loss='categorical_crossentropy', optimizer=opti, metrics=['accuracy'])
    history = model.fit(x = X_mag_tr, y = Y_tr, batch_size = batch_size, epochs = 500, verbose = 2,
                    callbacks = [EarlyStop, CheckPoint],
                    validation_data = (X_mag_ts, Y_ts),
                    shuffle = True)
    return history

def My_Model():
    model = Sequential()
    model.add(BatchNormalization(batch_input_shape = (None, time_step, input_size)))
    model.add(LSTM(units = cell_size,
                   batch_input_shape = (None, time_step, input_size),
                   return_sequences = True,
                   stateful = False, kernel_initializer = Init.orthogonal(), name = 'LSTM_1'))
    model.add(BatchNormalization()
    model.add(LSTM(units = cell_size,
                   return_sequences = True,
                   stateful = False, kernel_initializer = Init.orthogonal(), name = 'LSTM_2'))
    model.add(Dropout(0.5))
    model.add(TimeDistributed(Dense(output_size, kernel_initializer = Init.lecun_uniform(),
                                    activation = 'softmax', name = 'Output_Layer')))
    return model

X, Y = load_dataset()
idx = np.random.permutation(X.shape[0]) # shuffle the dataset
X = X[idx, :, :]
Y = Y[idx, :, :]
...
with tf.device("/cpu:0"):
    model = My_Model()
pmodel = multi_gpu_model(model, gpus = 2)
X_train, Y_train, X_test, Y_test = generate_2sets(X, Y, fold+1, n_fold)
History_ = train_validation_test_model(pmodel, X_train, Y_train, X_test, 
Y_test, model_path)
...

然后我收到了以下Tracback信息:

Traceback (most recent call last):
  File "C:/XXX/Python/XXX/Mynet.py", line 226, in <module>
    History_ = train_validation_test_model(pmodel, X_train, Y_train, X_test, Y_test, model_path)
  File "C:/XXX/Python/XXX/Mynet.py", line 148, in train_validation_test_model
    shuffle = True)
  File "C:\Program Files\Python35\lib\site-packages\keras\engine\training.py", line 1657, in fit
    validation_steps=validation_steps)
  File "C:\Program Files\Python35\lib\site-packages\keras\engine\training.py", line 1233, in _fit_loop
    callbacks.on_epoch_end(epoch, epoch_logs)
  File "C:\Program Files\Python35\lib\site-packages\keras\callbacks.py", line 73, in on_epoch_end
    callback.on_epoch_end(epoch, logs)
  File "C:\Program Files\Python35\lib\site-packages\keras\callbacks.py", line 415, in on_epoch_end
    self.model.save(filepath, overwrite=True)
  File "C:\Program Files\Python35\lib\site-packages\keras\engine\topology.py", line 2556, in save
    save_model(self, filepath, overwrite, include_optimizer)
  File "C:\Program Files\Python35\lib\site-packages\keras\models.py", line 108, in save_model
    'config': model.get_config()
  File "C:\Program Files\Python35\lib\site-packages\keras\engine\topology.py", line 2397, in get_config
    return copy.deepcopy(config)
  File "C:\Program Files\Python35\lib\copy.py", line 155, in deepcopy
    y = copier(x, memo)
  File "C:\Program Files\Python35\lib\copy.py", line 243, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)
  File "C:\Program Files\Python35\lib\copy.py", line 155, in deepcopy
    y = copier(x, memo)
  File "C:\Program Files\Python35\lib\copy.py", line 218, in _deepcopy_list
    y.append(deepcopy(a, memo))
  File "C:\Program Files\Python35\lib\copy.py", line 155, in deepcopy
    y = copier(x, memo)
  File "C:\Program Files\Python35\lib\copy.py", line 243, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)
  File "C:\Program Files\Python35\lib\copy.py", line 155, in deepcopy
    y = copier(x, memo)
  File "C:\Program Files\Python35\lib\copy.py", line 243, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)
  File "C:\Program Files\Python35\lib\copy.py", line 155, in deepcopy
    y = copier(x, memo)
  File "C:\Program Files\Python35\lib\copy.py", line 223, in _deepcopy_tuple
    y = [deepcopy(a, memo) for a in x]
  File "C:\Program Files\Python35\lib\copy.py", line 223, in <listcomp>
    y = [deepcopy(a, memo) for a in x]
  File "C:\Program Files\Python35\lib\copy.py", line 155, in deepcopy
    y = copier(x, memo)
  File "C:\Program Files\Python35\lib\copy.py", line 223, in _deepcopy_tuple
    y = [deepcopy(a, memo) for a in x]
  File "C:\Program Files\Python35\lib\copy.py", line 223, in <listcomp>
    y = [deepcopy(a, memo) for a in x]
  File "C:\Program Files\Python35\lib\copy.py", line 182, in deepcopy
    y = _reconstruct(x, rv, 1, memo)
  File "C:\Program Files\Python35\lib\copy.py", line 297, in _reconstruct
    state = deepcopy(state, memo)
  File "C:\Program Files\Python35\lib\copy.py", line 155, in deepcopy
    y = copier(x, memo)
  File "C:\Program Files\Python35\lib\copy.py", line 243, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)
  File "C:\Program Files\Python35\lib\copy.py", line 182, in deepcopy
    y = _reconstruct(x, rv, 1, memo)
  File "C:\Program Files\Python35\lib\copy.py", line 306, in _reconstruct
    y.__dict__.update(state)
AttributeError: 'NoneType' object has no attribute 'update'

我使用的平台是Windows 10. tensorflow的版本是1.4.0,keras的版本是2.1.2,它的后端是tensorflow。 GPU为GTX 1080 Ti,驱动程序版本为385.28; CUDA 8.0.61win10; cuDNN-8.0-windows10-x64的V6.0; Python 3.5.2。

0 个答案:

没有答案