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 type | TF data type | explain |
---|---|---|
DT_HALF | tf.float16 | Semi precision floating point number |
DT_FLOAT | tf.float32 | Single-precision floating-point |
DT_DOUBLE | tf.float64 | Double precision floating point number |
DT_BFLOAT16 | tf.bfloat16 | Truncate floating point number |
DT_INT8 | tf.int8 | 8-bit signed integer |
DT_INT16 | tf.int16 | 16 bit signed integer |
DT_INT32 | tf.int32 | 32-bit signed integer |
DT_INT64 | tf.int64 | 64 bit signed integer |
DT_UINT8 | tf.uint8 | 8-bit unsigned integer |
DT_UINT16 | tf.uint16 | 16 bit unsigned integer |
DT_STRING | tf.string | character string |
DT_BOOL | tf.bool | Boolean value |
DT_COMPLEX64 | tf.complex64 | Single precision complex number |
DT_COMPLEX128 | tf.complex128 | Double precision complex |
DT_QINT8 | tf.qint8 | Quantized 8-bit signed integer |
DT_QINT16 | tf.qint16 | Quantized 16 bit signed integer |
DT_QINT32 | tf.qint32 | Quantized 32-bit signed integer |
DT_QUINT8 | tf.quint8 | Quantized 8-bit unsigned integer |
DT_QUINT16 | tf.quint16 | Quantized 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 name | function |
---|---|
eval() | Take out the tensor value |
get_shape() | Get tensor shape |
set_shape() | Modify tensor shape |
consumers | Post 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] """