首页 > 解决方案 > Tensorflow:不同时期的训练速度和 CPU 使用率

问题描述

我正在一个包含大约 8000 个句子的句子分类数据集上训练一个简单的 Bi-LSTM 模型。我提取了关于模型的代码片段,如下所示。我在不同的时期随机且不一致地经历了训练减速和加速。这很奇怪,因为在我的本地机器或公司服务器上,这并没有发生而是在我的 AWS 服务器上。我想知道在不同时期随机导致训练减速的原因是什么?我可以确认没有其他 CPU 或内存密集型进程同时运行。我的程序是唯一的。

代码

input_shapes=Input(shape=(100, 300))
embedding = Embedding(...)(input_shapes)
lstm = Bidirectional(LSTM(units=100, return_sequences=False))(embedding)
dense = Dense(600)(lstm) 
final = Dense(num_of_classes, activation="softmax")(dense) 
model = Model(inputs=input_shapes, outputs=final)
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])

training_generator = data_generator(...) #my own method to generate batch on the fly
training_steps_per_epoch = round(len(X_train) / 100
model.fit_generator(training_generator, steps_per_epoch=training_steps_per_epoch, epochs=20)
...

配置

OS: Ubuntu 18.04.2 LTS
CPU: 8 Cores, Mem: 32G
conda 4.4.10
tensorflow 1.10.0
theano 0.9.0
scikit learn 0.19.0
keras 2.0.9
fasttext 0.9.1
nltk 3.2.5 (stopwords, wordnet)
pydot 1.2.3

观察/问题:在不同的时期,训练速度会减慢并随机加快,在此期间观察到不同的 CPU 使用率。例如,在 60 秒的 epoch 中,我注意到 CPU 使用率约为 300%。但是当它花费 <10s 时,CPU 使用率为 600%。但是,正如我上面提到的,在 AWS 服务器上的整个这段时间内,根本没有其他用户进程。我的 AWS 服务器是专门为此实验创建的。而且这只发生在我的 AWS 服务器上,而不是我的本地机器或公司服务器上。查看不同时期的观察时间截图

截屏

在此处输入图像描述

标签: tensorflowkeras

解决方案


推荐阅读