我正在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
答案 0 :(得分:1)
根据github discussion,有两种方法可以解决该问题:
卸载tensorflow并安装tensorflow的降级版本
pip uninstall tensorflow
pip uninstall tensorflow-gpu
pip install tensorflow==1.8.0
pip install tensorflow-gpu==1.8.0
如果您拥有超过1个GPU
export CUDA_VISIBLE_DEVICES='0'