How can I copy a variable in tensorflow

前端 未结 4 953
清歌不尽
清歌不尽 2020-12-24 13:19

In numpy I can create a copy of the variable with numpy.copy. Is there a similar method, that I can use to create a copy of a Tensor in TensorFlow?

4条回答
  •  一向
    一向 (楼主)
    2020-12-24 14:19

    In TF2 : tf.identity() will do the good deed for you. Recently I encountered some problems using the function in google colab. In case that's why you're here, this will be helping you.

    Error : 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 Identity: No unary variant device copy function found for direction: 1 and Variant type_index: tensorflow::data::(anonymous namespace)::DatasetVariantWrapper [Op:Identity]

    #Erroneous code
    tensor1 = tf.data.Dataset.from_tensor_slices([[[1], [2]], [[3], [4]]])
    tensor2 = tf.identity(tensor1)
    
    #Correction
    tensor1 = tf.data.Dataset.from_tensor_slices([[[1], [2]], [[3], [4]]])
    with tf.device('CPU'): tensor2 = tf.identity(tensor1)
    

提交回复
热议问题