我想在Anaconda的GPU上运行TF2.0中的TF1.X程序,所以我对代码做了一些修改,但我会在下面的最后一行得到一个错误。
train_dataset = tf.data.Dataset.from_tensor_slices((X_train, y_train))
train_dataset = train_dataset.apply(tf.data.experimental.shuffle_and_repeat(10000, seed=0))
train_dataset = train_dataset.batch(self.batchsize)
train_dataset = train_dataset.apply(tf.data.experimental.prefetch_to_device(device))错误消息是
Traceback (most recent call last):
File "train.py", line 77, in <module>
reward = manager.get_rewards(ModelGenerator, state_space.parse_state_space_list(action))
File "C:\Users\user\tf\tf2.0_gpu_test\manager.py", line 83, in get_rewards
train_dataset = train_dataset.apply(tf.data.experimental.prefetch_to_device(device))
File "C:\Users\user\AppData\Local\Continuum\anaconda3\envs\tf_env\lib\site-packages\tensorflow_core\python\data\ops\dataset_ops.py", line 1369, in apply
dataset = transformation_func(self)
File "C:\Users\user\AppData\Local\Continuum\anaconda3\envs\tf_env\lib\site-packages\tensorflow_core\python\data\experimental\ops\prefetching_ops.py", line 54, in _apply_fn
copy_to_device(target_device=device)).prefetch(buffer_size)
File "C:\Users\user\AppData\Local\Continuum\anaconda3\envs\tf_env\lib\site-packages\tensorflow_core\python\data\ops\dataset_ops.py", line 1369, in apply
dataset = transformation_func(self)
File "C:\Users\user\AppData\Local\Continuum\anaconda3\envs\tf_env\lib\site-packages\tensorflow_core\python\data\experimental\ops\prefetching_ops.py", line 78, in _apply_fn
source_device=source_device).with_options(options)
File "C:\Users\user\AppData\Local\Continuum\anaconda3\envs\tf_env\lib\site-packages\tensorflow_core\python\data\experimental\ops\prefetching_ops.py", line 102, in __init__
self._source_device = ops.convert_to_tensor(source_device)
File "C:\Users\user\AppData\Local\Continuum\anaconda3\envs\tf_env\lib\site-packages\tensorflow_core\python\framework\ops.py", line 1184, in convert_to_tensor
return convert_to_tensor_v2(value, dtype, preferred_dtype, name)
File "C:\Users\user\AppData\Local\Continuum\anaconda3\envs\tf_env\lib\site-packages\tensorflow_core\python\framework\ops.py", line 1242, in convert_to_tensor_v2
as_ref=False)
File "C:\Users\user\AppData\Local\Continuum\anaconda3\envs\tf_env\lib\site-packages\tensorflow_core\python\framework\ops.py", line 1296, in internal_convert_to_tensor
ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
File "C:\Users\user\AppData\Local\Continuum\anaconda3\envs\tf_env\lib\site-packages\tensorflow_core\python\framework\constant_op.py", line 286, in _constant_tensor_conversion_function
return constant(v, dtype=dtype, name=name)
File "C:\Users\user\AppData\Local\Continuum\anaconda3\envs\tf_env\lib\site-packages\tensorflow_core\python\framework\constant_op.py", line 227, in constant
allow_broadcast=True)
File "C:\Users\user\AppData\Local\Continuum\anaconda3\envs\tf_env\lib\site-packages\tensorflow_core\python\framework\constant_op.py", line 235, in _constant_impl
t = convert_to_eager_tensor(value, ctx, dtype)
File "C:\Users\user\AppData\Local\Continuum\anaconda3\envs\tf_env\lib\site-packages\tensorflow_core\python\framework\constant_op.py", line 96, in convert_to_eager_tensor
return ops.EagerTensor(value, ctx.device_name, dtype)
RuntimeError: Can't copy Tensor with type string to device /job:localhost/replica:0/task:0/device:GPU:0.如果有人能帮我那就太好了。
发布于 2020-05-15 21:45:05
我能够在tensorflow-gpu==2.0.0版本中重现这个问题,如下所示-
代码-
import tensorflow_hub as hub
import tensorflow as tf
print(tf.__version__)
embed = hub.load("https://tfhub.dev/google/universal-sentence-encoder-large/5")
with tf.device('GPU:0'):
embeddings = embed(["The quick brown fox jumps over the lazy dog."])
print(embeddings)输出-
2.0.0
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
<ipython-input-4-e3758851cab1> in <module>()
7 with tf.device('GPU:0'):
8
----> 9 embeddings = embed(["The quick brown fox jumps over the lazy dog."])
10
11 print(embeddings)
11 frames
/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/framework/constant_op.py in convert_to_eager_tensor(value, ctx, dtype)
94 dtype = dtypes.as_dtype(dtype).as_datatype_enum
95 ctx.ensure_initialized()
---> 96 return ops.EagerTensor(value, ctx.device_name, dtype)
97
98
RuntimeError: Can't copy Tensor with type string to device /job:localhost/replica:0/task:0/device:GPU:0.然而,这个问题似乎在tensorflow版本的2.2.0中得到了解决。
代码-
import tensorflow_hub as hub
import tensorflow as tf
print(tf.__version__)
embed = hub.load("https://tfhub.dev/google/universal-sentence-encoder-large/5")
with tf.device('GPU:0'):
embeddings = embed(["The quick brown fox jumps over the lazy dog."])
print("I Ran Successfully")输出-
2.2.0
I Ran Successfully你能把你的tensorflow版本升级到2.2.0吗?
希望这能回答你的问题。快乐学习。
https://stackoverflow.com/questions/59353119
复制相似问题