【问题标题】:Run Tensorflow on CPU only仅在 CPU 上运行 TensorFlow
【发布时间】:2019-11-26 00:32:47
【问题描述】:

我正在尝试运行高级快速入门指南中的 TensorFlow 示例,但它只是挂起以下三行:

2019-11-25 18:24:37.609515: I tensorflow/core/platform/profile_utils/cpu_utils.cc:94] CPU Frequency: 3503035000 Hz
2019-11-25 18:24:37.610314: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x2b714c0 executing computations on platform Host. Devices:
2019-11-25 18:24:37.610351: I tensorflow/compiler/xla/service/service.cc:175]   StreamExecutor device (0): <undefined>, <undefined>

我从来没有成功安装 GPU 的东西,所以我卸载了 tensorflow-gpu 并重新安装了常规的 tensorflow 包。 看起来它正试图耗尽我的 GPU,但我不知道为什么,我真的想阻止它。

from __future__ import absolute_import, division, print_function, unicode_literals

import tensorflow as tf

from tensorflow.keras.layers import Dense, Flatten, Conv2D
from tensorflow.keras import Model

mnist = tf.keras.datasets.mnist

(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train, x_test = x_train /255.0, x_test / 255.0

x_train = x_train[..., tf.newaxis]
x_test = x_test[..., tf.newaxis]

train_ds = tf.data.Dataset.from_tensor_slices((x_train, y_train)).shuffle(10000).batch(32)

class MyCoolModel(Model):
    def __init__(self):
        super(MyCoolModel, self).__init__()
        self.conv1 = Conv2D(32, 3, activation='relu')
        self.flatten = Flatten()
        self.d1 = Dense(128, activation='relu')
        self.d2 = Dense(10, activation='softmax')

    def call(self, x):
        x = self.conv1(x)
        x = self.flatten(x)
        x = self.d1(x)
        return self.d2(x)

model = MyCoolModel()

loss_object = tf.keras.losses.SparseCategoricalCrossentropy()

optimizer = tf.keras.optimizers.Adam()

train_loss = tf.keras.metrics.Mean(name='train_loss')
train_accuracy = tf.keras.metrics.SparseCategoricalAccuracy(name='train_accuracy')

test_loss = tf.keras.metrics.Mean(name='test_loss')
test_accuracy = tf.keras.metrics.SparseCategoricalAccuracy(name='test_accuracy')

@tf.function
def train_step(images, labels):
    with tf.GradientTape() as tape:
        predictions = model(images)
        loss = loss_object(labels, predictions)

        gradients = tape.gradient(loss, model.trainable_variables)
        optimizer.apply_gradients(zip(gradients, model.trainable_variables))

        train_loss(loss)
        train_accuracy(labels, predictions)

@tf.function
def test_step(images, labels):
    predictions = model(images)
    t_loss = loss_object(labels, predictions)

    test_loss(t_loss)
    test_accuracy(labels, predictions)

EPOCHS = 5

for epoch in range(EPOCHS):
    for images, labels in train_ds:
        train_step(images, labels)

    for test_images, test_labels in test_ds:
        test_step(test_images, test_labels)

    template = 'Epoch {}, Loss: {}, Accuracy: {}, Test Loss: {}, Test Accuracy: {}'
    print(template.format(epoch+1, train_loss.result(), train_accuracy.result()*100, test_loss.result(), test_accuracy.result()*100))

    train_loss.reset_states()
    train_accuracy.reset_states()
    test_loss.reset_states()
    test_accuracy.reset_states()

【问题讨论】:

  • 没有任何效果。也不清楚他们是否有同样的问题。
  • 您是否尝试重新启动。有时有些东西会留在 tensorflow 大脑中。
  • 我刚试过重启,还是不行。
  • 这是什么结果:os.environ['CUDA_VISIBLE_DEVICES'] = '-1' if tf.test.gpu_device_name(): print('GPU found') else: print("No GPU found")

标签: python tensorflow keras


【解决方案1】:

在 CPU 上运行的最简单方法:os.environ['CUDA_VISIBLE_DEVICES'] = '-1'

【讨论】:

    【解决方案2】:

    在哭了很久之后,我意识到我可以使用 docker 镜像。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-11-24
      • 2018-01-20
      • 2016-10-06
      • 2018-11-23
      • 2019-12-11
      • 2018-04-06
      • 1970-01-01
      相关资源
      最近更新 更多