【问题标题】:Running Spyder code on GPU instead of CPU on Ubuntu在 GPU 而不是 Ubuntu 上的 CPU 上运行 Spyder 代码
【发布时间】:2020-07-26 13:26:35
【问题描述】:

我正在使用 Spyder 在具有 GPU 的机器上创建深度学习模型我发现它正在 CPU 上工作并且我的代码运行了很长时间。首先我下载了​​ tensorflow-GPU 但我不知道如何开始在 GPU 上工作。

我使用了 { with tf.device("cpu"): } 但是当我在终端上编写 nvidia-smi 时,我发现没有正在运行的进程。

我也用过 {import os os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"] = ""
},但它不起作用。

如何让我的 Spyder 代码在 GPU 而不是 Ubuntu 上的 cpu 上运行?

任何帮助将不胜感激。

代码:

def createModel():
   with tf.device("cpu"):
        input_shape=(1, 22, 5, 3844)
        model = Sequential()
        model.add(Conv3D(16, (22, 5, 5), strides=(1, 2, 2), padding='same',activation='relu',data_format= "channels_first", input_shape=input_shape))

        model.add(keras.layers.MaxPooling3D(pool_size=(1, 2, 2),data_format= "channels_first",  padding='same'))

        model.add(BatchNormalization())
        model.add(Conv3D(32, (1, 3, 3), strides=(1, 1,1), padding='same',data_format= "channels_first",  activation='relu'))#incertezza se togliere padding

        model.add(keras.layers.MaxPooling3D(pool_size=(1,2, 2),data_format= "channels_first", ))
        model.add(BatchNormalization())
        model.add(Conv3D(64, (1,3, 3), strides=(1, 1,1), padding='same',data_format= "channels_first",  activation='relu'))#incertezza se togliere padding
        model.add(keras.layers.MaxPooling3D(pool_size=(1,2, 2),data_format= "channels_first",padding='same' ))
        model.add(BatchNormalization())
        model.add(Dense(64, input_dim=64,kernel_regularizer=regularizers.l2(0.0001), activity_regularizer=regularizers.l1(0.0001)))
        model.add(Flatten())
        model.add(Dropout(0.5))
        model.add(Dense(256, activation='sigmoid'))
        model.add(Dropout(0.5))
        model.add(Dense(2, activation='softmax'))
        opt_adam = keras.optimizers.Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0)
        model.compile(loss='categorical_crossentropy', optimizer=opt_adam, metrics=['accuracy'])
    return model

【问题讨论】:

  • 你能指定你使用的显卡吗?也许您的问题只是技术问题。还要确保你安装了 tensorflow 的 gpu 版本。
  • @AbdullahDeliogullari 我在终端写 nvidia-smi 我得到了这个输出 NVIDIA-SMI 440.82 Driver Version: 440.82 CUDA Version: 10.2
  • 好的,这意味着它不是技术问题。可能有 2 个不同的 tensorflow 版本导致了这个问题。

标签: python tensorflow deep-learning gpu nvidia


【解决方案1】:

根据github discussion,有两种方法可以解决这个问题:

  1. 卸载tensorflow并安装降级版本的tensorflow

    pip uninstall tensorflow
    pip uninstall tensorflow-gpu
    pip install tensorflow==1.8.0
    pip install tensorflow-gpu==1.8.0
    
  2. 如果你有超过 1 个 GPU

    export CUDA_VISIBLE_DEVICES='0'
    

【讨论】:

  • 非常感谢您的帮助。我将尝试卸载并安装 tensorflow-gpu==2.0.0。我希望这会奏效。
  • 我认为你应该将你的 tensorflow 降级到 1.8.0。 2.0.0 版仍然可能存在问题。尝试后,请告诉我此解决方案是否有效。
  • 非常感谢您的帮助。非常感谢您的解决方案。它对我有用。
  • 安装 tensorflow-gpu 1.15.8 时可以在服务器的 CPU 上工作吗??我想比较一下我的代码的速度。
  • 如果你按照我的回答分别安装 cpu 和 gpu 版本的 tensorflow,你可以同时使用它们并将 tf.device 更改为 cpu
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-09-02
  • 2020-01-13
  • 1970-01-01
  • 1970-01-01
  • 2020-07-18
  • 2021-11-26
  • 2011-10-02
相关资源
最近更新 更多