Python – tensorflow.fill()

TensorFlow is open-source Python library designed by Google to develop Machine Learning models and deep learning  neural networks. 

fill() is used to generate a tensor having scalar value.

Syntax: tensorflow.fill( dims, value, name)

Parameters:

  • dims: It is 1-D sequence of dtype int32 or int64 with non-negative integers representing the shape of the resulting Tensor.
  • value:  It is the value to be filled.
  • name(optional):  It defines the name of the operation. 

Returns: It returns a Tensor of shape dim.



Raises:

  • InvalidArgumentError: This error is raised when dim contains negative values.
  • NotFoundError: This error is raised when dim contains non-integer values.

Example 1:

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Importing the library
import tensorflow as tf
  
# Initializing the input
dim = [4, 5]
value = 5
  
# Printing the input
print('dim: ', dim)
print('value: ', value)
  
# Calculating result
res = tf.fill(dim, value)
  
# Printing the result
print('res: ', res)

chevron_right


Output:


dim:  [4, 5]
value:  5
res:  tf.Tensor(
[[5 5 5 5 5]
 [5 5 5 5 5]
 [5 5 5 5 5]
 [5 5 5 5 5]], shape=(4, 5), dtype=int32)

Example 2:

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Importing the library
import tensorflow as tf
  
# Initializing the input
dim = [4, 2, 5]
value = 5
  
# Printing the input
print('dim: ', dim)
print('value: ', value)
  
# Calculating result
res = tf.fill(dim, value)
  
# Printing the result
print('res: ', res)

chevron_right


Output:


dim:  [4, 2, 5]
value:  5
res:  tf.Tensor(
[[[5 5 5 5 5]
  [5 5 5 5 5]]

 [[5 5 5 5 5]
  [5 5 5 5 5]]

 [[5 5 5 5 5]
  [5 5 5 5 5]]

 [[5 5 5 5 5]
  [5 5 5 5 5]]], shape=(4, 2, 5), dtype=int32)




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.