tensorflow2.0 Series (1): tensor

The next few blog will introduce tensorflow are basic concepts: tensor, operation, session and graph. We wanted to know is how tensor should be defined? What operating operation tensor there? What is to calculate the flow diagram, how to build? How tensor calculation of flow in the flow chart, the realization of training and forecasting models?

tensor: tensor

The most basic data structure tensorflow is tensor, tensor. First we look at how to define a tensor. It is the tensor nature multidimensional array, similar to numpy tf.Tensor the ndarray, also need to define the data types and dimensions. However, tensor operations tf.Tensor defined may be accelerated in a GPU (tensorflow-gpu version if any).

Can freely switch between multidimensional arrays ndarray and tensorflow tensor tensor Numpy, for example:

tensor = tf.convert_to_tensor([2,3,4], dtype=tf.float64)
tensor
Out[74]: <tf.Tensor: id=251, shape=(3,), dtype=float64, numpy=array([2., 3., 4.])>
numpy_array = tensor.numpy()
numpy_array
Out[76]: array([2., 3., 4.])

There are three basic tensor properties: tag label, the dimensions and dimension data type data type.

Tensor data types

tf.dtypes module (Module1) defines the classes and functions associated with the data type. Python Module (module), is a file containing Python Python object definitions and statements (definitions and statements). File name is the module name with the suffix .py, inside the module, the module name is stored in the global variable in __name__, is a string, the module can be referenced directly by __name__ to the module name.
module in order to reuse some objects, such as classes, functions, and these objects is defined in a .py file, or to a plurality of larger works to tailor .py easy to maintain files, each file .py It is a module.
Documentation tf.dtypes modules: https: //tensorflow.google.cn/api_docs/python/tf/dtypes

Dtype class

tf.dtypes included in a class: to Dtype, tensor can be defined to support data types, as shown below:

function

tf.as_type (): defines the type of object Dtype

Instructions:

tf.dtypes.as_dtype(type_value)

type_value tf.Dtype may be defined data types may be enumerated by tf.Dtype types.

x = tf.constant([1.8, 2.2], dtype=tf.float32)
xint = tf.dtypes.as_type(tf.int32) # xint是tf.int32类型的Dtype对象

Tf.int32 get xint this type of Dtype objects.

tf.dtypes.cast (): The tensor mapping / projections for new types

The operation support basic data types defined Dtype tensorflow. When the complex type (complex64, complex128) is converted to a solid type, returns only the real part of x. When converting real type complex type (complex64, complex128), the imaginary part of the return value is set to zero. Here to match the behavior of complex types of treatment and numpy. And tf.cast () function is the same function.

tf.dtypes.cast(x, dtype, name=None)

Then the above example:

xx = tf.dtypes.cast(x, tf.int32)  
 xxx = tf.cast(x,tf.int32)

Export

xxx
Out[67]: <tf.Tensor: id=248, shape=(2,), dtype=int32, numpy=array([1, 2], dtype=int32)>

xx
Out[68]: <tf.Tensor: id=242, shape=(2,), dtype=int32, numpy=array([1, 2], dtype=int32)>

tf.dtypes.complex (): converts a real number to a complex two

Given a real tensor represents the real part of the complex, a tensor imag represents an imaginary part of the complex, the operation returns in the form of a plurality of elements, wherein a represents the real part, b represents imag portion. Input real and imag tensor must have the same shape. And tf.cast () function is the same function.

rx = tf.constant([2.25, 3.25])
ix = tf.constant([4.75, 5.75])
cx = tf.complex(rx, ix)  # [[2.25 + 4.75j], [3.25 + 5.75j]]
cxx = tf.dtypes.complex(rx,ix)

The resulting cx and cxx as:

cxx
Out[61]: <tf.Tensor: id=246, shape=(2,), dtype=complex64, numpy=array([2.25+4.75j, 3.25+5.75j], dtype=complex64)>

cx
Out[62]: <tf.Tensor: id=245, shape=(2,), dtype=complex64, numpy=array([2.25+4.75j, 3.25+5.75j], dtype=complex64)>

tf.dtypes.saturate_cast (): the value is converted to a saturated security dtype

The input value to the projected specified data type, data overflow occurs in the projection, appropriately clamping (clamping) operations, to ensure that the correct data conversion.
tf.dtypes.saturate_cast (value, dtype, name = None)

Tensor of four types

There are four main types of tensor, tf.constant, tf.Variable, tf.placeholder, tf.sparse.SparseTensor. If you do not specify a tensor of dtype and name when creating the tensor, tf will automatically automatically match based on value data types.

Constant type: tf.constant ()

tf.constant () method calls the basic function is:
tf.constant(value, type, name='')
For example:


const1 = tf.constant(1, tf.int16, name='tensor1')
print('const1:')
print(const1)

const2 = tf.constant(np.pi, tf.float32, name='tensor2')
print('const2:')
print(const2)

const3 = tf.constant('tensorflow-2.0', tf.string, name='tensor3')
print('const3:')
print(const3)

const4 = tf.constant([[10,12],[23,45],[45,6]])
print('const4')
print(const4)
print('shape of const4: '+str(const4.shape))# 查看tensor的维度

get:

const1:
tf.Tensor(1, shape=(), dtype=int16)
const2:
tf.Tensor(3.1415927, shape=(), dtype=float32)
const3:
tf.Tensor(b'tensorflow-2.0', shape=(), dtype=string)
const4
tf.Tensor(
[[10 12]
 [23 45]
 [45  6]], shape=(3, 2), dtype=int32)
shape of const4: (3, 2)

Variable type: tf.Variable ()

Variable is a class of tensorflow, which encapsulates many operations, referred ops, so the first letter capitalized. tensorflow of op is the first letter is lowercase, for example, is a constant operation, abbreviated as op, so constant is the first letter lowercase.

Create a variable of type tensor is very simple, such as:

w = tf.Variable(<initial-value>, name=<optional-name>)

Note that in tf2.0, the variables in the creation of the initial value should be assigned directly, rather than re-use the initializer. tf.Variable types of variables after creating, you need to initialize, and execute in session session.


# Launch the graph in a session.
with tf.compat.v1.Session() as sess:
    # Run the variable initializer.
    sess.run(w.initializer)
    # ...you now can run ops that use the value of 'w'...

In tf 1.x, use the tf.compat.v1.get_variable () function to create


tf.compat.v1.get_variable(name='',values,dtype,initializer)

is the tensor dimension values, the variable initializer of a type tensor initialization method, e.g. zeros_initializer.


var1 = tf.compat.v1.get_variable('var1', [1,2], dtype=tf.int32, initializer=tf.zeros_initializer)
print(var1)

var2 = tf.Variable([1,2],name='var2')
print(var2)

get

<tf.Variable 'var1:0' shape=(1, 2) dtype=int32, numpy=array([[0, 0]], dtype=int32)>
 <tf.Variable 'var2:0' shape=(2,) dtype=int32, numpy=array([1, 2], dtype=int32)>

Placeholder Type: tf.placeholder ()

In tf 1.x, create a placeholder type tensor need to use tf.placeholder () function. Now, this function is called is modified to tf.compat.v1.placeholder ().

tf.compat.v1.placeholder(
    dtype,
    shape=None,
    name=None
)

Placeholder type tensor is mainly used to create a "feed to the" model data and the tag of this type may be considered to be null tensor predefined variable, when creating the data and the computation graph of a percent of label input this placeholder data structure definitions, after starting the session, loaded to the actual data.
Here is a simple example, draw a sine curve with tensorflow it.

import numpy as np
import matplotlib.pyplot as plt

x_data = np.array([np.linspace(0,2*np.pi,100)]).transpose()

with tf.Graph().as_default():
    x = tf.compat.v1.placeholder(tf.float32, shape=(100,1))
    y = tf.sin(x)
    with tf.compat.v1.Session() as sess:
        sin_x = sess.run(y, feed_dict={x:x_data})
        
plt.plot(x_data,sin_x)

get
Here Insert Picture Description

Sparse tensor: tf.sparse.SparseTensor ()

Sparse tensor, tensor by definition is in most of the data is 0, only sporadic data on the position is not 0. In order to save storage space, as long as the preservation of the non-zero data location and data itself on it. So sparse tensor can be created using the following method:

s_tensor = tf.sparse.SparseTensor(indices=[[0, 0], [1, 2]], values=[1, 2], dense_shape=[3, 4])
with tf.compat.v1.Session() as sess:
    print(sess.run(s_tensor))

Output:

SparseTensorValue(indices=array([[0, 0],
       [1, 2]]), values=array([1, 2], dtype=int32), dense_shape=array([3, 4]))
Published 111 original articles · won praise 118 · views 280 000 +

Guess you like

Origin blog.csdn.net/happyhorizion/article/details/103833899