简体   繁体   中英

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()

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM