【问题标题】:tensorflow has no profit of GPU in google colabtensorflow在google colab中没有GPU的利润
【发布时间】:2020-03-27 13:35:53
【问题描述】:

在 google Colab 中,我编写了一个 Ipython notebook,我在其中构建了一个神经网络模型,从我的 google 驱动器中获取数据并训练模型。

我的代码运行没有错误并训练模型。虽然当我使用 colab GPU 与默认 CPU 时,我没有看到任何改进。是我正确使用了GPU还是tensorflow不能使用google colab的GPU?

一些可能与这个问题相关的代码sn-ps:

import tensorflow as tf
print(tf.__version__)
device_name = tf.test.gpu_device_name()
if device_name != '/device:GPU:0':
  raise SystemError('GPU device not found')
print('Found GPU at: {}'.format(device_name))
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, BatchNormalization, Flatten, Dense, TimeDistributed, ReLU, ConvLSTM2D, Activation, Dropout, Reshape

结果:

2.0.0-alpha0
Found GPU at: /device:GPU:0

构建模型:

with tf.device("/gpu:0"):
  model = Sequential()

  #layer1
  model.add(
      TimeDistributed(
          TimeDistributed(
              Conv2D(
                  filters=4, kernel_size=(1,10), strides=(1,10), data_format="channels_last"
              )
          ), input_shape=(40, 5, 7, 100, 1), name="LLConv"
      )
  )
  model.add(TimeDistributed(BatchNormalization(axis=4), name="LBNtes"))
  model.add(TimeDistributed(ReLU(), name="LRelu"))
  #print(model.output_shape)#(None, 40, 5, 7, 10, 4)

  #layer2
  model.add(
      TimeDistributed(
          ConvLSTM2D(
              filters=4, kernel_size=(7,3), strides=(1,1),data_format="channels_last", return_sequences=True
          ), name="LConvLST"
      )
  )

  model.add(TimeDistributed(BatchNormalization(axis=4), name="LBN2"))
  model.add(TimeDistributed(Activation("tanh"), name="Ltanh"))
  #print(model.output_shape)#(None, 40, 5, 1, 8, 4)

  model.add(Reshape((40, 5, 8, 4), name="reshape"))

  #layers3
  model.add(
      ConvLSTM2D(
          filters=1, kernel_size=(4,4), strides=(1,1), data_format="channels_last", name="GConvLSTM", return_sequences=True
      )
  )
  model.add(BatchNormalization(axis=3, name="GBN"))
  model.add(Activation("tanh", name="Gtanh"))
  #print(model.output_shape)#(None, 40, 2, 5, 1)

  model.add(TimeDistributed(Flatten()))
  #print(model.output_shape)#(None, 40, 10)

  model.add(Flatten())
  #layer4
  model.add(Dense(10, name="GDense"))
  model.add(BatchNormalization(axis=-1))
  model.add(ReLU())
  model.add(Dropout(0.5))

  #layer5
  model.add(Dense(1, activation="linear"))


  model.compile(
      loss=tf.keras.losses.MeanSquaredError(),
      optimizer=tf.keras.optimizers.Nadam(lr=0.001, decay=1e-6),
      metrics=['mae', 'mse'],
  )

#model.summary()

训练模型:

EPOCHS = 300
BATCH_SIZE = 15
with tf.device("/gpu:0"):

    history = model.fit(train_features, train_labels, epochs=EPOCHS, batch_size=BATCH_SIZE, validation_data=(test_features,test_labels))

【问题讨论】:

  • 是什么型号的?请添加代码和所有参数,特别是批量大小。
  • @Omni 赞成的解决方案解释了如何在 windows 上的 tensorflow 中使用您的 gpu,我不明白这如何映射到在 google colab 中执行此操作。
  • @MatiasValdenegro 感谢您的回复,该模型是具有大量层的深度学习模型(我的问题中添加了完整模型)

标签: python tensorflow keras google-colaboratory


【解决方案1】:

确保您已安装 tensorflow-gpu。

首先在具有GPU 内核启用的新 colab 笔记本上试用。

# Uninstall tensorflow first
!pip uninstall tensorflow -y
# Install tensorflow-gpu (stable version)
!pip install tensorflow-gpu  # stable

import tensorflow as tf
# Check version
print(tf.__version__)

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

参考文献

  1. How to upgrade tensorflow with GPU on google colaboratory
  2. https://www.tensorflow.org/install/pip?lang=python3
  3. https://www.tensorflow.org/install/gpu#pip_package

更新:看起来您不再需要在 Colab 中安装 tensorflow-gpu,因为当您选择 GPU 运行时,环境会在后台安装 tensorflow-gpu根据这个视频:Using GPUs in TensorFlow, TensorBoard in notebooks, finding new datasets, & more! (#AskTensorFlow)。

【讨论】:

    【解决方案2】:

    如果您尝试通过运行 pip install tensorflow-gpu 来更新 tensorflow,则您安装的二进制文件可能未针对 Colaboratory 提供的 GPU 硬件进行调整。相反,您应该使用与 Colab 捆绑在一起的 tensorflow 版本。

    目前,此版本为 1.15,但您可以通过运行 %tensorflow_version 2.X 切换到版本 2.X。在未来的某个时候,tensorflow 2.X 将成为默认版本。

    欲了解更多信息,请参阅https://colab.research.google.com/notebooks/tensorflow_version.ipynb

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-15
      • 1970-01-01
      • 1970-01-01
      • 2019-12-14
      • 1970-01-01
      • 2021-08-14
      相关资源
      最近更新 更多