Introduction to TensorFlow basic data types

Introduction to TensorFlow basic data types

preface

Note in advance: TensorFlow 2.0 is used in this article Version 5.0. In TensorFlow2 After 0, many things in TensorFlow have changed, which is different from 1 The X version has many differences. On the whole, TensorFlow has become more concise, including Session(), placeholders and so on. Grammatically, it is closer to pytoch. Therefore, some insiders believe that TensorFlow 2 X and pytorch, just spend the main experience on one, and the other can be easily started.

I data type

Overall, TensorFlow data types can be divided into two parts: basic data types and Tensor types

1.1 basic data type

First, TensorFlow supports the following basic data types:

Integer signedint8: 8-bit integer; int16: 16 bit integer; int32: 32-bit integer; int64: 64 bit integer.
Integer unsigneduint8: 8-bit unsigned integer; uint16: 16 bit unsigned integer; uint32: 32-bit unsigned integer; uint64: 64 bit unsigned integer
float float16: 16 bit floating point number; float32: 32-bit floating point number; Float64: 64 bit floating point number. double: equivalent to float64.
character stringstring: string
Booleanbool: Boolean
Plural typetf.complex64: 64 bit complex number. tf.complex128: 128 bit complex

1.2 container type

Container types are not unique to TensorFlow. For example, numpy has an array, python itself has a list, and so on. In TensorFlow, it is: TF Tensor.

In numpy, in fact, the optimization of data operation has been done on the basis of the original Python. However, numpy is unable to adapt to operations such as big data and deep learning. In reality, GPU has the characteristics of multi-threaded operation, so the overall speed is much faster than CPU. But numpy just can't interact with GPU. Numpy also has no automatic derivation method. It will be very troublesome if it involves gradient descent and so on. In TensorFlow, Tensor type is introduced, which can be well linked with GPU, and is more suitable for big data and deep learning.

Next, let's introduce this tensor type

1.3 Tensor type (tensor)

Tensor as a whole has the following types:

  • Scalar: represents scalar
  • Vector: representative vector
  • Matrix: matrix

In TensorFlow, data with a dimension greater than 2 is often referred to as Tensor

II basic operation

2.1 creating Tensor

For example, we can create Constant: Although this word is translated as a Constant, it is still different from const int a = 10 in other high-level languages. Because const in other high-level languages cannot be modified once it is specified. But TensorFlow is allowed. For example, you write one

a = tf.constant(1)
a = tf.constant(2.)

Don't worry, it won't be wrong.

import tensorflow as tf
In [2]: tf.constant(1) # Create an int constant

Out[2]: <tf.Tensor: shape=(), dtype=int32, numpy=1>
In [4]: tf.constant(1.) # Create floating point constant
Out[4]: <tf.Tensor: shape=(), dtype=float32, numpy=1.0>

In [5]: tf.constant(2.,dtype=tf.double) # You can specify the data type
Out[5]: <tf.Tensor: shape=(), dtype=float64, numpy=2.0>

In [6]: tf.constant([True,False]) # Create bool type constant
Out[6]: <tf.Tensor: shape=(2,), dtype=bool, numpy=array([ True, False])>

In [7]: tf.constant('hello world') #Create character constant
Out[7]: <tf.Tensor: shape=(), dtype=string, numpy=b'hello world'>

2.2 Tensor properties

In [9]: with tf.device("cpu"):
   ...:     a = tf.constant([1])
   ...:

In [10]: with tf.device("gpu"):
    ...:     b = tf.range(4)
    ...:

In [11]: a.device
Out[11]: '/job:localhost/replica:0/task:0/device:CPU:0'

In [12]: b.device
Out[12]: '/job:localhost/replica:0/task:0/device:GPU:0'

In [13]: aa = a.gpu() # Switch to gpu operation. This method is likely to be deleted in the later TensorFlow version

In [15]: aa.device
Out[15]: '/job:localhost/replica:0/task:0/device:GPU:0'

In [16]: bb = b.cpu() # Switch to cpu operation. This method is likely to be deleted in later versions
In [17]: bb.device
Out[17]: '/job:localhost/replica:0/task:0/device:CPU:0'

In [18]: b.numpy() # Convert from Tensor to Numpy
Out[18]: array([0, 1, 2, 3])

In [19]: b.ndim # View data dimensions
Out[19]: 1

In [20]: tf.rank(b) # View data information
Out[20]: <tf.Tensor: shape=(), dtype=int32, numpy=1>

In [21]: tf.rank(tf.ones([3,4,2]))
Out[21]: <tf.Tensor: shape=(), dtype=int32, numpy=3>

2.2 Tensor attribute discrimination

In [23]: a = tf.constant([1.]) # Create floating point type constant

In [24]: b = tf.constant([True,False]) # Create bool type constant

In [25]: c = tf.constant("hello world!")# Create string type constant

In [27]: import numpy as np

In [28]: d = np.arange(4)# Create numpy type

In [29]: isinstance(a,tf.Tensor) # Judge whether a is Tensor type
Out[29]: True

In [30]: tf.is_tensor(b) # is_tensor is more recommended
Out[30]: True

In [31]: tf.is_tensor(d)
Out[31]: False

In [32]: a.dtype,b.dtype,c.dtype # View data type
Out[32]: (tf.float32, tf.bool, tf.string)

In [33]: a.dtype==tf.float32
Out[33]: True

In [34]: c.dtype==tf.string
Out[34]: True

2.3 data type conversion

In [35]: a = np.arange(5)

In [36]: a.dtype
Out[36]: dtype('int32')

In [37]: aa = tf.convert_to_tensor(a)

In [38]: aa
Out[38]: <tf.Tensor: shape=(5,), dtype=int32, numpy=array([0, 1, 2, 3, 4])>

In [39]: a
Out[39]: array([0, 1, 2, 3, 4])

In [40]: aa = tf.convert_to_tensor(a,dtype=tf.int64)

In [41]: aa
Out[41]: <tf.Tensor: shape=(5,), dtype=int64, numpy=array([0, 1, 2, 3, 4], dtype=int64)>

In [42]: tf.cast(aa,dtype=tf.float32)
Out[42]: <tf.Tensor: shape=(5,), dtype=float32, numpy=array([0., 1., 2., 3., 4.], dtype=float32)>

In [43]: aaa = tf.cast(aa,dtype=tf.double)

In [44]: aaa
Out[44]: <tf.Tensor: shape=(5,), dtype=float64, numpy=array([0., 1., 2., 3., 4.])>

In [45]: tf.cast(aaa,dtype=tf.int32)
Out[45]: <tf.Tensor: shape=(5,), dtype=int32, numpy=array([0, 1, 2, 3, 4])>

# Conversion between bool and int
In [46]: b = tf.constant([0,1])

In [47]: tf.cast(b,dtype=tf.bool)
Out[47]: <tf.Tensor: shape=(2,), dtype=bool, numpy=array([False,  True])>

In [48]: bb = tf.cast(b,dtype=tf.bool)

In [49]: bb
Out[49]: <tf.Tensor: shape=(2,), dtype=bool, numpy=array([False,  True])>

In [50]: tf.cast(bb,tf.int32)
Out[50]: <tf.Tensor: shape=(2,), dtype=int32, numpy=array([0, 1])>

2.4 Variable type

Most Variable operations in TensorFlow are initialized and implemented through variables. At the same time, the derivation operation in TensorFlow is also a direct benchmarking Variable type, so this type is very important in TensorFlow. The Variable method is as follows:

tf.Variable.init(initial_value, trainable=True, collections=None, validate_shape=True, name=None)

The meanings of these parameters are as follows:

In [51]: a = tf.range(5)

In [52]: b = tf.Variable(a)

In [53]: b.dtype
Out[53]: tf.int32

In [54]: b.name
Out[54]: 'Variable:0'

In [55]: b = tf.Variable(a,name='input_data')

In [56]: b.name
Out[56]: 'input_data:0'

In [57]: tf.is_tensor(b)
Out[57]: True

In [58]: b.numpy()
Out[58]: array([0, 1, 2, 3, 4])
    
In [59]: b.trainable
Out[59]: True

Keywords: TensorFlow

Added by El Ornitorrico on Wed, 02 Feb 2022 09:06:33 +0200