Back

Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in AI and Deep Learning by (50.2k points)

I want to make a tensor of 0.9 of a specific shape. In tensorflow there is this command:

tf.ones_like()

So I put there the shape and I have my vector of ones. I want to do the same but with other value like 0.9, so I want to put the shape like in tf.ones_like but I want my tensor with this number 0.9

How can I do it?

1 Answer

0 votes
by (108k points)

A tensor is a vector or matrix of n-dimensions that represents all types of data. All the values in a tensor are having the same data type known as shape. The shape is the dimension of any matrix.

To create a tensor, you can use tf.constant().

Here is the syntax for the same:

tf.constant(value, dtype, name = "")

The arguments are as follows:

-value: Value of n dimension to define the tensor. Optional

- dtype: Define the type of data:    

   - tf.string: String variable    

   - tf.float32: Float variable    

   - tf.int16: Integer variable

- "name": Name of the tensor.

To create a tensor of dimension 0, run the following code:

r1 = tf.constant(1, tf.int16) 

print(r1)

You can define a tensor with decimal values or with a string by changing the type of data.

 # Decimal

r1_decimal = tf.constant(0.9, tf.float32)

print(r1_decimal)

# String

r1_string = tf.constant("any name", tf.string)

print(r1_string)

The following fill operation creates a tensor of shape dims and fills it with value.

tf.fill(dims,value,name=None)

Now for the specified shape, When you print the tensor, TensorFlow guesses the shape. However, you can get the shape of the tensor with the shape property.

m_shape = tf.constant(0.9, tf.float32) 

m_shape.shape

Hope this helps!

Interested in learning Artificial Intelligence? Click to learn more artificial intelligence course.

Welcome to Intellipaat Community. Get your technical queries answered by top developers!

28.4k questions

29.7k answers

500 comments

94k users

Browse Categories

...