当前位置:网站首页>Data types in tensorflow

Data types in tensorflow

2022-06-23 07:17:00 Wanderer001

Reference resources  TensorFlow Data types in - cloud + Community - Tencent cloud

One 、Python Primitive types

    TensorFlow To accept the Python Own native data types , for example Python Boolean type in , Numerical data type ( Integers , Floating point numbers ) And string type . The single value will be converted to 0 D tensor ( Scalar ), The list value will be converted to 1 D tensor ( vector ), The list set list will be converted to 2 D tensor ( matrix ) wait , The following example comes from TensorFlow for Machine Intelligence.

t_0 = 19 # Treated as a 0-d tensor, or "scalar"

tf.zeros_like(t_0) # ==> 0

tf.ones_like(t_0) # ==> 1


t_1 = [b"apple", b"peach", b"grape"] # treated as a 1-d tensor, or "vector"

tf.zeros_like(t_1)                   # ==> [b'' b'' b'']

tf.ones_like(t_1)                    # ==> TypeError

t_2 = [[True, False, False],

       [False, False, True],

       [False, True, False]]         # treated as a 2-d tensor, or "matrix"

tf.zeros_like(t_2)                   # ==> 3x3 tensor, all elements are False

tf.ones_like(t_2)                    # ==> 3x3 tensor, all elements are True

Two 、TensorFlow Primitive types

It's like Numpy equally ,TensorFlow It also has its own data type , Will you be in TensorFlow You can see things like tf.int32, tf.float32 In addition to these , There are also some interesting data types, such as tf.bfloat, tf.complex, tf.quint. Here is the whole TensorFlow data type , The screenshot from tf.DType

3、 ... and 、Numpy data type

      You may have noticed Numpy and TensorFlow There are many similarities .TensorFlow At the beginning of design, I hope to be able to communicate with Numpy It has a good integration effect .Numpy Software packages have now become the universal language of data science .

    TensorFlow Many data types are also based on Numpy Of , in fact , If you make np.int32==tf.int32 Will return True. You can also pass it directly Numpy Data types are given directly to TensorFlow Medium ops.

tf.ones([2, 2], np.float32) ==> [[1.0 1.0], [1.0 1.0]]

Please remember , Our good friends tf.Session.run(), The required input object is a Tensor But its output is a Numpy Array . in fact , On most occasions , You can use... At the same time TensorFlow The type and Numpy type .

 

原网站

版权声明
本文为[Wanderer001]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206230621347028.html