首页 > 解决方案 > n_jobs = -1 等效于 keras

问题描述

我最近开始学习深度学习。在使用 n_jobs = -1 的 sklearn 库的机器学习中,我所有的 cpu 内核都被使用,这加快了网格搜索。现在我正在尝试在训练数据上拟合一个 rnn 模型,这需要很多时间。有什么方法可以加快训练速度。

# Initialising the RNN
regressor = Sequential()

# Adding the first LSTM layer and some Dropout regularisation
regressor.add(LSTM(units = 50, return_sequences = True, input_shape = (X_train.shape[1], 7)))
regressor.add(Dropout(0.2))

# Adding a second LSTM layer and some Dropout regularisation
regressor.add(LSTM(units = 50, return_sequences = True))
regressor.add(Dropout(0.2))

# Adding a third LSTM layer and some Dropout regularisation
regressor.add(LSTM(units = 50, return_sequences = True))
regressor.add(Dropout(0.2))

# Adding a fourth LSTM layer and some Dropout regularisation
regressor.add(LSTM(units = 50))
regressor.add(Dropout(0.2))

# Adding the output layer
regressor.add(Dense(units = 1))

# Compiling the RNN
regressor.compile(optimizer = 'adam', loss = 'mean_squared_error')

# Fitting the RNN to the Training set
regressor.fit(X_train, y_train, epochs = 100, batch_size = 32,shuffle=False)

标签: python-3.xparallel-processingkerasscikit-learncpu-usage

解决方案


使用以下内容:

config = tf.ConfigProto(device_count{"CPU": <NB CPU>})
keras.backend.tensorflow_backend.set_session(tf.Session(config=config))

推荐阅读