Home > Net >  Interconversion and difference
Interconversion and difference

Time:09-17

I have two types of tensors.

1) <class 'tensorflow.python.ops.resource_variable_ops.ResourceVariable'>
2) <class 'tensorflow.python.framework.ops.Tensor'>

What is the difference between the two? Can interconversion be done? In particular if I want to convert the second type to first type. Thanks

CodePudding user response:

A tf.Tensor is basically a multidimensional array of elements, while a tf.ResourceVariable is pretty much like a fancier tf.Variable. I think they were used in Tensorflow 1, but in Tf2 got removed, I can't even find them in the documentation (are you using tf1?).

Let's just talk about variables, that can be used similarly. The tf.Variable is a wrapper around a tensor, so you can initialize the variable using the tensor, as we will see below.

Having a tf.Variable (here to learn more) adds further capabilities to the tensor. An important distinction is that the tf.Variable maintains state across multiple runs. The variable is pretty much used to represent the trainable parameters of the model, while the tensor is used as array to feed to the network.

So as I was saying, a tf.Variable stores a persistent tensor and can be initialized using that tf.Tensor like this:

variable = tf.Variable(my_tensor)
  • Related