chiphuyen / stanford-tensorflow-tutorials

This repository contains code examples for the Stanford's course: TensorFlow for Deep Learning Research.
http://cs20.stanford.edu
MIT License
10.32k stars 4.32k forks source link

Why do tf.zeros_like and tf.ones_like not return a tensor with all elements set to zero/one when passing a list of strings? #108

Closed yiminglin-ai closed 5 years ago

yiminglin-ai commented 6 years ago

Hi Chip, The following snipet seems to quite confusing to me:

t_1 = [b"apple", b"peach", b"grape"]    # 1-d arrays are treated like 1-d tensors
tf.zeros_like(t_1)                              # ==> [b'' b'' b'']
tf.ones_like(t_1)                               # ==> TypeError: Expected string, got 1 of type 'int' instead.

I understand that a list of strings is a 1-d array that is treated like 1-d tensors in Tensorflow, but I feel like tf.zeros_like(t_1) should return a tensor with a tensor of [0,0,0] since t_1 is of length 3? Also, I do not understand why ones_like and zeros_like treat t_1 differently? Could you tell me tf.zeros_like and tf.ones_like are returning the results like above?

Thank you very much.