当我将张量拷贝到GPU时,会报错。
InternalError: Failed copying input tensor from /job:localhost/replica:0/task:0/device:CPU:0 to /job:localhost/replica:0/task:0/device:GPU:0 in order to run _EagerConst: Dst tensor is not initialized
这种情况也发生在你的环境是GPU的环境,执行代码时他默认也会帮你拷贝张量到GPU. 有人说是显存不足导致的,这是原因之一。
这里讲解决办法:
import numpy as np
import tensorflow as tf
x = np.random.randn(60000,28,28)
device_name = '/GPU:0' if tf.config.list_physical_devices('GPU') else '/CPU:0'
with tf.device(device_name):
x = tf.convert_to_tensor(x)
这个张量很大,显存不够直接拷贝会报错。Failed copying input tensor
多加一步就能解决,先放到CPU再放到GPU
import numpy as np
import tensorflow as tf
x = np.random.randn(60000,28,28)
device_name = '/CPU:0' # 强制使用 CPU
with tf.device(device_name):
x = tf.convert_to_tensor(x)
device_name = '/GPU:0' if tf.config.list_physical_devices('GPU') else '/CPU:0'
with tf.device(device_name):
x = tf.convert_to_tensor(x)
以下是我遇到的实际问题,只需要将张量先拷贝到CPU再拷贝到GPU就能解决。
import tensorflow as tf
from tensorboard.plugins.hparams import api as hp
fashion_mnist = tf.keras.datasets.fashion_mnist
(x_train, y_train),(x_test, y_test) = fashion_mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
device_name = '/CPU:0' # 强制使用 CPU
with tf.device(device_name):
x_train = tf.convert_to_tensor(x_train)
x_test = tf.convert_to_tensor(x_test)
y_train = tf.convert_to_tensor(y_train)
y_test = tf.convert_to_tensor(y_test)
device_name = '/GPU:0' if tf.config.list_physical_devices('GPU') else '/CPU:0'
with tf.device(device_name):
x_train = tf.convert_to_tensor(x_train)
x_test = tf.convert_to_tensor(x_test)
y_train = tf.convert_to_tensor(y_train)
y_test = tf.convert_to_tensor(y_test)