gpt4 book ai didi

python - 如何在 GPU 上使用 Keras?

转载 作者:太空狗 更新时间:2023-10-30 02:53:26 24 4
gpt4 key购买 nike

我已经成功安装了带 GPU 的 TensorFlow。当我运行以下脚本时,我得到了这个结果:

from tensorflow.python.client import device_lib
print(device_lib.list_local_devices())

C:\tf_jenkins\workspace\rel-win\M\windows-gpu\PY\36\tensorflow\core\platform\cpu_feature_guard.cc:140]
Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 2018-03-26
Found device 0 with properties: name: GeForce GTX 970 major: 5 minor: 2 memoryClockRate(GHz): 1.253 pciBusID: 0000:01:00.0 totalMemory: 4.00GiB freeMemory: 3.31GiB 2018-03-26 11:47:03.186046: I C:\tf_jenkins\workspace\rel-win\M\windows-gpu\PY\36\tensorflow\core\common_runtime\gpu\gpu_device.cc:1312]
Adding visible gpu devices: 0 2018-03-26 11:47:04.062049: I C:\tf_jenkins\workspace\rel-win\M\windows-gpu\PY\36\tensorflow\core\common_runtime\gpu\gpu_device.cc:993]
Creating TensorFlow device (/device:GPU:0 with 3043 MB memory) -> physical GPU (device: 0, name: GeForce GTX 970, pci bus id: 0000:01:00.0, compute capability: 5.2) [name: "/device:CPU:0" device_type: "CPU" memory_limit: 268435456 locality { } incarnation: 8082333747214375667 , name: "/device:GPU:0" device_type: "GPU" memory_limit: 3190865920 locality { bus_id: 1 } incarnation: 1190887510488091263 physical_device_desc: "device: 0, name: GeForce GTX 970, pci bus id: 0000:01:00.0, compute capability: 5.2" ]

例如,如果我在 Keras 中运行 CNN,它会自动使用 GPU 吗?或者我是否必须编写一些代码来强制 Keras 使用 GPU?

例如,对于 MNIST 数据集,我将如何使用 GPU?

model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3),
activation='relu',
input_shape=input_shape))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation='softmax'))

model.compile(loss=keras.losses.categorical_crossentropy,
optimizer=keras.optimizers.Adadelta(),
metrics=['accuracy'])

model.fit(x_train, y_train,
batch_size=batch_size,
epochs=epochs,
verbose=1,
validation_data=(x_test, y_test))

最佳答案

您不必明确告诉 Keras 使用 GPU。如果 GPU 可用(并且从您的输出中我可以看到是这种情况),它将使用它。

您还可以通过查看模型训练期间 GPU 的使用情况来凭经验检查这一点:如果您使用的是 Windows 10,则只需打开任务管理器并查看“性能”选项卡(参见 here ).

关于python - 如何在 GPU 上使用 Keras?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49488614/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com