TensorFlow of Tensor objects

Tensor translated into Chinese means tensor, tf.Tensor object is a handle to the data object. Data objects include input variables and constants, and the output data of the computing nodes. After all common types of data required in the Python language into Tensor objects TensorFlow in order to use the compute nodes TensorFlow framework.

Tensor translated into Chinese means tensor , zero-dimensional representation of tensor is a scalar , one-dimensional tensor is a vector , is represented by two-dimensional tensor matrix . TensorFlow, the training convolution neural network model, common Tensor dimension is four-dimensional. Shape of the common four-dimensional Tensor [batch, height, width, channels ], i.e., a height Batch number input picture, the output characteristic of FIG network layer, width and number of channels.

In the TensorFlow, Tensor object can store any dimension tensor, FIG involved in the calculation are Tensor data objects in front of the variable and constant learning Tensor objects belong. Tensor objects often one computing node operation (Operation objects, abbreviated op) output, in fact, the input data may be taken as the output of op. Tensor object concept is easier to understand, only the data it can be seen in FIG.

Code:

import tensorflow as tf

data = [[1, 2], [3, 4]]
# 定义变量Tensor
A_tf = tf.Variable(data, name='A')
# 定义常量Tensor
B_tf = tf.constant(data, name='B')

# 根据Tensor的名称获取Tensor
A_tmp = tf.get_default_graph().get_tensor_by_name('A:0')
B_tmp = tf.get_default_graph().get_tensor_by_name('B:0')
# 将查找得到的Tensor对象做矩阵乘法
C_tf = tf.matmul(A_tf, B_tf)

# 将查找到的Tensor对象打印
print("Tensor named 'A:0' :", A_tmp)
print("Tensor named 'B:0' :", B_tmp)
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    A_v, B_v, C_v = sess.run([A_tmp, B_tmp, C_tf])
    print("\n Tensor named 'A:0' value:\n", A_v)
    print("\n Tensor named 'B:0' value:\n", B_v)
    print("\n Matuml output:\n", C_v)

 

The code (TensorFlow changed from the original version of 1.15.0 2.0.0) , the first acquired line 10-11 from FIG Tensor corresponding to an object in accordance with the specified name; Tensor 13 about to find the object returned to do matrix multiplication; Tensor data output of each of the 21-23 line will be printed. After performing the above code, the following output:

Tensor named 'A:0' : Tensor("A:0", shape=(2, 2), dtype=int32_ref)
Tensor named 'B:0' : Tensor("B:0", shape=(2, 2), dtype=int32)

 Tensor named 'A:0' value:
 [[1 2]
 [3 4]]

 Tensor named 'B:0' value:
 [[1 2]
 [3 4]]

 Matuml output:
 [[ 7 10]
 [15 22]]

 

发布了105 篇原创文章 · 获赞 17 · 访问量 11万+

Guess you like

Origin blog.csdn.net/qq_38890412/article/details/104085994