【问题标题】:Tensorflow 2.0-GPU Windows is running training code on the CPUTensorFlow 2.0-GPU Windows 正在 CPU 上运行训练代码
【发布时间】:2020-01-16 11:56:04
【问题描述】:

目前我在 Tensorflow 2.0 上使用。这是我的电脑:

  • CPU:i5-4690 3.5Ghz。
  • 内存:16GB。
  • GPU:NVIDIA GeForce 1050Ti 4GB。
  • 操作系统:Windows 10 专业版 64 位。
  • CUDA 10.0 和 cuDNN 7.4。

我的项目是一个使用 ResNet50CIFAR100 Dataset 的图像分类项目。

我使用子类化构建网络(代码 sn-p 太长,所以我没有在这个问题上附加它)并使用 tf.data.Dataset.from_tensor_slices 加载数据:

def load_cifar100(batch_size, num_classes=100):
    (x_train, y_train), (x_test, y_test) = cifar100.load_data()

    x_train, x_test = x_train.astype('float32') / 255, x_test.astype('float32') / 255

    x_val, y_val = x_train[-10000:], y_train[-10000:]
    x_train, y_train = x_train[:-10000], y_train[:-10000]

    y_train = to_categorical(y_train, num_classes)
    y_test = to_categorical(y_test, num_classes)
    y_val = to_categorical(y_val, num_classes)

    train_dataset = tf.data.Dataset.from_tensor_slices((x_train, y_train))
    train_dataset = train_dataset.shuffle(buffer_size=1024).batch(batch_size)

    val_dataset = tf.data.Dataset.from_tensor_slices((x_val, y_val))
    val_dataset = val_dataset.batch(batch_size)

    test_dataset = tf.data.Dataset.from_tensor_slices((x_test, y_test))
    test_dataset = test_dataset.batch(batch_size)

    return train_dataset, val_dataset, test_dataset

我使用GradientTape 设置我的训练过程:

def training(x_batch, y_batch):
    with tf.GradientTape() as tape:
        logits = model(x_batch, training=True)
        loss_val = loss(y_batch, logits)

    grads = tape.gradient(loss_val, model.trainable_weights)
    optimizer.apply_gradients(zip(grads, model.trainable_weights))
    train_acc_metric(y_batch, logits)


for epoch in range(epochs):
    train_acc_metric.reset_states()
    for step, (x_batch_train, y_batch_train) in enumerate(train_dataset):
        training(x_batch_train, y_batch_train)

    train_acc = train_acc_metric.result()
    template = 'Epoch {}, Train_Acc: {}'
    print(template.format(epoch + 1,
                          train_acc))

在训练过程中,我看到我的GPU 根本不起作用 [Pic1],所有的训练过程都只是放入了CPU,即使我打开了调试配置tf.debugging.set_log_device_placement(True) 并且看起来像所有的图层已加载到GPU [Pic2]。

更新:

这是当我更改为 model.fit 函数时 GPU 的样子。每个 epoch 的训练时间都比 GradientTape 快得多:

【问题讨论】:

  • Python 似乎使用了 11% 的 GPU 和 25% 的 CPU,所以是的,它在 GPU 上运行,我没有看到问题。不要误以为 0% CPU 和 100% GPU。
  • @MatiasValdenegro 啊,当我使用model.fit 时,GPU 会运行得更多,训练过程会更快。
  • 你必须记住的一件事是,并不是所有的进程都适合在 GPU 上并行化,所以在 tensorflow 中,它自己的框架会处理这个问题。这就是为什么你会看到有些方法使用 GPU 多于 CPU,而有些方法使用 GPU 少。像 PyTorch 这样的框架不会处理这个问题,而且 PyTorch 的整体例程与 TensorFlow 有点不同。
  • @Shahryar 所以你的意思是当 Tensorflow 已经检测到 GPU 时,框架本身就会知道如何在 CPU 和 GPU 之间进行控制,而不总是只在 GPU 上运行?
  • @TrungTínTrần 是的。

标签: python tensorflow deep-learning


【解决方案1】:

在开始训练过程之前检查 Tensorflow (TF2) 是否使用 GPU 会很有帮助,方法是:

assert tf.test.is_gpu_available()
assert tf.test.is_built_with_cuda()

【讨论】:

  • 投反对票的原因?
  • 所有的返回都是 True 但还是一样,CPU 仍然运行满 1 核和 GPU 1-2%。
  • @TrungTínTrần 你的批量大小是多少?
  • batch_size=16 & epochs=100 先生
  • @TrungTínTrần 小批量可能是 GPU 使用率低的一个原因,此外(一般而言)最好尽可能使用最高可用的 API,因为它们会处理一些低级细节。但是,为了检查您的 NN 是否在 GPU 上进行训练,您必须使用上述断言。
最近更新 更多