Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the type of a Tensor?

I'm looking for something similar to the effects of:

x.get_shape()

that will give the type of x. Is there is any function for this?

like image 233
CGTheLegend Avatar asked Mar 13 '17 07:03

CGTheLegend


People also ask

How do you find the type of tensor?

We can access the data type of a tensor using the ". dtype" attribute of the tensor. It returns the data type of the tensor.

What is tensor datatype?

Tensors are multi-dimensional arrays with a uniform type (called a dtype ). You can see all supported dtypes with names(tf$dtypes) . If you're familiar with R array or NumPy, tensors are (kind of) like R or NumPy arrays. All tensors are immutable: you can never update the contents of a tensor, only create a new one.

How do you find the shape of a tensor PyTorch?

To get the shape of a tensor as a list in PyTorch, we can use two approaches. One using the size() method and another by using the shape attribute of a tensor in PyTorch.


2 Answers

You can use get_shape() to get the shape of a tensorflow variable.

>>> x = tf.Variable(tf.random_normal([256, 100]))
>>> x.get_shape()
(256, 100)

You can use dtype property to get the type of a tensorflow variable.

>>> x = tf.Variable(tf.random_normal([256, 100]))
>>> x.dtype
<dtype: 'float32_ref'>

You can use as_numpy_dtype property of dtype to convert from tf.dtype to numpy dtype.

>>> x = tf.Variable(tf.random_normal([256, 100]))
>>> x.dtype.as_numpy_dtype
<class 'numpy.float32'>
like image 118
umutto Avatar answered Oct 19 '22 09:10

umutto


To get the type you can do

x.dtype
like image 31
Miriam Farber Avatar answered Oct 19 '22 11:10

Miriam Farber