为什么我的CNN模型在本地CPU上的训练时间比托管选项少?

2024-04-19 20:54:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我是一个全新的深度学习,我一直在遵循一些教程,主要是使用托管Jupyter笔记本(Azure和Colaboratory)。我正处于一个阶段,我希望开始在我自己的神经网络上进行实验;然而,我有点困惑,我应该在哪里训练我的keras模型。为了做出决定,我在几个不同的地方运行了下面的模型,总之,我的i5 6500 CPU排在第二位,这让我感到非常困惑。更令人困惑的是,用8个虚拟CPU运行googlecloud计算比用我的CPU运行要慢。我还没有尝试我的GTX1060GPU;然而,似乎有理由认为,它将执行甚至优于我的CPU。为什么我会得到这些结果?人们通常在哪里训练他们的ML模型?我的结果如下。

from keras.datasets import mnist
from keras.preprocessing.image import load_img, array_to_img
from keras.utils.np_utils import to_categorical
from keras.models import Sequential
from keras.layers import Dense

image_height, image_width = 28, 28
(x_train, y_train), (x_test, y_test) = mnist.load_data()

x_train = x_train.reshape(60000, image_height * image_width)
x_test = x_test.reshape(10000, image_height * image_width)

x_train = x_train.astype('float32')
x_test = x_test.astype('float32')

x_train /= 255.0
x_test /= 255.0

y_train = to_categorical(y_train, 10)
y_test = to_categorical(y_test, 10)

model = Sequential()
model.add(Dense(512, activation='relu', input_shape=(784,)))
model.add(Dense(512, activation='relu'))
model.add(Dense(10, activation='softmax'))

model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])

history = model.fit(x_train, y_train, epochs=2, validation_data=(x_test, y_test))

我在以下位置尝试了上面的代码片段。下面是每个时代的时间。你知道吗

  • 我的i5 6500 CPU:20s
  • 带CPU的实验室笔记本:27s
  • 带GPU的协作笔记本:8s(预计)
  • 带TPU的协作笔记本:26s
  • 带CPU的Azure笔记本电脑:60秒
  • 谷歌云计算Jupyterlab:4vCPUs:36s

enter image description hereenter image description here

  • 谷歌云计算Jupyterlab:8vCPUs:40s

enter image description hereenter image description here

不幸的是,用GPU运行Google云计算需要我升级我的免费帐户,所以我没有尝试。你知道吗


Tags: tofrom模型testimageimportmodel笔记本