user3210986
user3210986

Reputation: 221

Run Tensorflow on CPU only

I'm trying to run the TensorFlow sample from the advanced quick start guide, but it just hangs with these three lines:

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>

I never successfully got the GPU stuff installed so I uninstalled tensorflow-gpu and reinstalled the regular tensorflow package. It looks like it's trying to run off of my GPU, but I'm not sure why and I really want to stop it.

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()

Upvotes: 0

Views: 4532

Answers (2)

Timbus Calin
Timbus Calin

Reputation: 15043

Simplest way to run on CPU: os.environ['CUDA_VISIBLE_DEVICES'] = '-1'

Upvotes: 1

user3210986
user3210986

Reputation: 221

After a lot of crying, I realized that I could use a docker image.

Upvotes: 0

Related Questions