I’m following the CS20SI course and found something maybe useless but interesting.
The definition of tf.ones_like is:1
2
3
4
5
6ones_like(
tensor,
dtype=None,
name=None,
optimize=True
)
For example, given a tensor a that:1
a=tf.constant([1,2,3],[4,5,6])
The tf.ones_like can return a tensor with the same shape of a but all values are set to 1.1
b=tf.ones_like(a, tf.int16)
The ourpur of b should be:1
2[[1 1 1]
[1 1 1]]
As an accident, I found we can use the numbers instead of the data type and get the same output, for example:
1 | >>> b=tf.ones_like(a,1) |
By check the core code of tensorflow, the proto of enum data type as shown below:
1 | DT_FLOAT = 1; |
That’s why we can pass several constant numbers to the dtype. Notice that the definition of tf.ones_like’s dtype is
dtype: A type for the returned Tensor. Must be float32, float64, int8, int16, int32, int64, uint8, complex64, complex128 or bool.
Thus, the available numbers are 1,2,3,4,5,6,8,9,10,18. (7 is string)
The string of dtype can be replaced by the key value diectly for convenience. But the code becomes harder to understand.