Reputation: 11
I am trying to use the tf.fill()
method to create a tensor of different data types(float16,float32,float64) similar to what you can do with numpy.full()
. would tf.constant() be a suitable substitution? or should I create my fill values to be of the data type I want them to be then plug it into the value holder inside tf.fill()
Upvotes: 1
Views: 3458
Reputation: 222571
You can either provide the value of the datatype you want your resulting tensor to be or to cast a tensor afterwards.
tf.fill((3, 3), 0.0)
# will be a float 32tf.cast(tf.fill((3, 3)), tf.float32)
# also float 32The first one is better because you use less operations in the graph
Upvotes: 1
Reputation: 24591
You can use fill
. However, the resulting type will depend on the value
argument, you cannot control the resulting type with an explicit dtype
argument. That is indeed a bit different from most other tensorflow operators.
tf.fill([2, 3], 9) # tensor with dtype=int23
tf.fill([2, 3], 9.0) # tensor with dtype=float32
# more explicit
tf.fill([2, 3], np.float64(9)) # tensor with dtype=float64
Upvotes: 2