Notes on understanding TensorFlow - Chapter 3.2 data carrier: tensor


In order to define the mathematical expression more conveniently and describe the mathematical model more accurately, TF uses Tensor to represent all data uniformly. In the actual calculation formula, that is, the transformation process of the expression, the data in the expression corresponding to the model is carried by the Tensor. Provided by TF Tensor and SparseTensor has two tensor abstractions, which represent dense data and sparse data respectively. The latter aims to reduce the memory occupation of high-dimensional sparse data.

Tensor: tensor

TF Tensor has strong data expression ability, which is reflected not only in its abstract description of High dimensional data, but also in its support for diversified data type s.

Data typeTF data typeexplain
DT_HALFtf.float16Semi precision floating point number
DT_FLOATtf.float32Single-precision floating-point
DT_DOUBLEtf.float64Double precision floating point number
DT_BFLOAT16tf.bfloat16Truncate floating point number
DT_INT8tf.int88-bit signed integer
DT_INT16tf.int1616 bit signed integer
DT_INT32tf.int3232-bit signed integer
DT_INT64tf.int6464 bit signed integer
DT_UINT8tf.uint88-bit unsigned integer
DT_UINT16tf.uint1616 bit unsigned integer
DT_STRINGtf.stringcharacter string
DT_BOOLtf.boolBoolean value
DT_COMPLEX64tf.complex64Single precision complex number
DT_COMPLEX128tf.complex128Double precision complex
DT_QINT8tf.qint8Quantized 8-bit signed integer
DT_QINT16tf.qint16Quantized 16 bit signed integer
DT_QINT32tf.qint32Quantized 32-bit signed integer
DT_QUINT8tf.quint8Quantized 8-bit unsigned integer
DT_QUINT16tf.quint16Quantized 16 bit unsigned integer

TF tensor is a data carrier in logical definition, but it is a handle in physical implementation. It stores meta info of tensor and memory buffer pointer pointing to tensor data. This design is to realize memory reuse. When the output value of some pre operations is input to multiple post operations, there is no need to store the input value repeatedly. When a tensor is no longer dependent on any operation, TF will release the memory buffer storing the tensor. TF determines whether the memory buffer of tensor data should be released by reference counting.

Create tensor

In TF Python API, dense Tensor abstraction is Tensor class. However, in general, users do not need to use the construction method of Tensor class to create tensors directly, but indirectly through operation. Typical Tensor creation operations include constant definition operations and algebraic calculation operations.
python import tensorflow as tf a = tf.constant(1.0) b = tf.constant(2.0) c = tf.add(a, b) print(a, b, c) # output "" "Tensor("Const:0", shape=(), dtype=float32) Tensor("Const_1:0", shape=(), dtype=float32) Tensor("Add:0", shape=(), dtype=float32)" ""
The above three variables are tensors.

solve

The operation output value in the data flow graph is carried by the tensor. If you want to solve a specific tensor value, you need to create a reply and then execute the eval() method of the tensor or the run() method of the session.
python with tf.Session() as sess: print(c.eval()) print(sess.run(c)) # output "" 3.0 3.0 "" "

Member method

In addition to supporting diversified data types, TF Tensor abstraction also provides some member methods to dynamically change the tensor shape and view the post operation of the tensor.

member namefunction
eval()Take out the tensor value
get_shape()Get tensor shape
set_shape()Modify tensor shape
consumersPost operation of obtaining tensor

operation

TF provides Tensor with a large number of operations to build data flow diagram and realize algorithm model. Such as unary algebraic operation, binary algebraic operation, shape operation, protocol operation, neural network operation and conditional operation.

Examples

import tensorflow as tf
a = tf.constant([1, 1])
b = tf.constant([2, 3])
c = tf.add(a, b)
with tf.Session() as sess:
	print("a[0] = {}, a[1] = {}".format(a[0].eval(), a[1].eval()))
	print("c.name = {}".format(c.name))
	print("c.value = {}".format(c.eval()))
	print("c.shape = {}".format(c.shape))
	print("a.consumers = {}".format(a.consumers()))
	print("b.consumers = {}".format(b.consumers()))
	print("[c.op]: \n {}".format(c.op))
# output
"""
a[0] = 1, a[1] = 1
c.name = Add:0
c.value = [3 4]
c.shape = (2,)
a.consumers = [<tf.Operation 'Add' type=Add>]
b.consumers = [<tf.Operation 'Add' type=Add>]
[c.op]:
 name: "Add"
op: "Add"
input: "Const"
input: "Const_1"
attr {
 		key: "T"
 		value {
  			type: DT_INT32
 		}
}
"""

Sparse tensor: SparseTensor

TF provides a SparseTensor class specially used for processing high-dimensional sparse data. This class represents high-dimensional sparse data in the form of key value pairs, which includes indexes, values, & deny_ Shape has three attributes. Among them, indices is a tensor instance with the shape of [N, ndims], N represents the number of non-zero elements, and ndims represents the order of tensor. For example, when indices = [[0,2], [1,3], [2,1]] (N=2, ndims = 2), it means that the elements with indexes of [0,2], [1,3], [2,1] in the coefficient tensor of order 2 are non-zero. Values is a tensor object with the shape [N], which is used to save the value of the specified element in indexes. dense_shape is a tensor instance with the shape [ndims], which represents the shape of the sparse tensor corresponding to the dense tensor.

establish

To create a spark tensor in TF, you usually directly use the construction method of SparseTensor class.

import tensorflow as tf
sp = tf.SparseTensor(indices=[[0, 2], [1, 3], [2, 1]],
					values = [1, 3, 2],
					dense_shape = [3, 4])
with tf.Session() as sess:
	print(sp.eval())
	dense = tf.sparse_tensor_to_dense(sp)
	print(dense.eval())
"""
SparseTensorValue(indices=array([[0, 2],
      [1, 3],
      [2, 1]]), values=array([1, 3, 2], dtype=int32), dense_shape=array([3, 4]))
[[0 0 1 0]
[0 0 0 3]
	[0 2 0 0]]
"""

operation

TF provides some special operations for spark sensor, so that users can handle SparseTensor like Tensor. It mainly includes: conversion operation, algebraic operation, set operation and reduction operation.

Examples

import tensorflow as tf
sp = tf.SparseTensor(indices=[[0, 2], [1, 3], [2, 1]],
					values = [1, 3, 2],
					dense_shape = [3, 4])
reduce_x = [tf.sparse_reduce_sum(x),
			tf.sparse_reduce_sum(x, axis=1),
			tf.sparse_reduce_sum(x, axis=1, keep_dims=True),
			tf.sparse_reduce_sum(x, axis=[0, 1])]
with tf.session() as sess:
	dense = tf.sparse_tensor_to_dense(sp)
	print(dense.eval())
	print(sess.run(reduce_x))
# output
"""
dense_shape=array([3, 4]))
[[0 0 1 0]
[0 0 0 3]
	[0 2 0 0]]
	
[6, array([1, 3, 2], dtype=int32), array([[1],
      [3],
      [2]], dtype=int32), 6]
"""

Keywords: Machine Learning TensorFlow Deep Learning

Added by soadlink on Fri, 28 Jan 2022 05:05:25 +0200