cw-delli-bird
cw-delli-bird

Reputation: 11

using tensorflow fill method to create a tensor of certain datatype

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

Answers (2)

Salvador Dali
Salvador Dali

Reputation: 222571

You can either provide the value of the datatype you want your resulting tensor to be or to cast a tensor afterwards.

  1. tf.fill((3, 3), 0.0) # will be a float 32
  2. tf.cast(tf.fill((3, 3)), tf.float32) # also float 32

The first one is better because you use less operations in the graph

Upvotes: 1

P-Gn
P-Gn

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

Related Questions