TensorFlow - Tensor理解与使用

作者:flyfish1986
原文地址:https://blog.csdn.net/flyfish1986/article/details/72809248

如何理解TensorFlow中的tensor
tensor 张量
英 [‘tensə; -sɔː] 美 [‘tɛnsɚ]

What is a Tensor?

Tensors are simply mathematical objects that can be used to describe
physical properties, just like scalars and vectors. In fact tensors
are merely a generalisation of scalars and vectors; a scalar is a zero
rank tensor, and a vector is a first rank tensor.

张量是简单的数学对象,可以用来描述物理性质,就像标量和向量一样。实际上,张量仅仅是标量和向量的一般化;标量是零阶张量,而向量是一阶张量。( rank 与 order通用 )

The rank (or order) of a tensor is defined by the number of directions
(and hence the dimensionality of the array) required to describe it.
For example, properties that require one direction (first rank) can be
fully described by a 3×1 column vector, and properties that require
two directions (second rank tensors), can be described by 9 numbers,
as a 3×3 matrix. As such, in general an nth rank tensor can be
described by 3n coefficients.

一个张量的秩(或阶)是由方向数(以及该数组的维数)来定义的。例如,属性,需要一个方向(第一阶)可以完全描述由一个3×1列向量,属性需要两个方向(二阶张量),可以由9数字,描述为一个3×3的矩阵。因此,一般来说,第n阶张量可以用3的n次方个系数来描述。

以下引用自《TensorFlow:实战Google深度学习框架》

概念

从TensorFlow的名字就可以看出张量(tensor)是一个很重的概念。在TensorFlow程序中所有的数据都通过张量的形式来表示。从功能的角度看,张量可以被理解为多维数组。其中零阶张量表示标量(scalar)也就是一个数;一阶张量为向量,也就是一维数组;n阶张量可以理解为一个n维数组。但张量的实现并不是直接采用数组的形式,它只是对TensorFlow中运算结果的引用。在张量中并没有保存数字,它保存的是如何得到这些数字的计算过程。

向量加法代码说明

import tensorflow as tf 
a=tf.constant([1.0,2.0],name='a') 
b=tf.constant([2.0,3.0],name='b') 
result=tf.add(a,b,name='add') print(result

输出 Tensor(“add:0”, shape=(2,), dtype=float32)
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

一个张量主要保存三个属性:名字(name)、维度(shape)和类型(type)。

张量的命名就可以通过“node:str_output”的形式来给出。其中node为节点的名称,str_output表示当前张量来自节点的第几个输出。比如上面代码打出来的“add:0”就说明result这个张量是计算节点”add”输出的第一个结果(编号从零开始)。
张量的第二个属性是张量的维度(shape)。这个属性描述了一个张量的维度信息。 张量的第三个属性是类型(type),每个张量会有唯一的类型。

使用

张量的使用分两类。 1 对中间计算结果的引用。当一个计算包含很多计算结果时,使用张量可以很大的提高代码可读性

代码对比

 #使用张量记录中间结果 
 a=tf.constant([1.0,2.0],name='a')
 b=tf.constant([2.0,3.0],name='b') 
 result=a+b

#直接结计算向量的和,可能性差 
result=tf.constant([1.0,2.0],name='a')+ tf.constant([2.0,3.0],name='b')
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

2当计算图构造完成之后,张量可以来获得计算结果,也就是得到真实的数字。虽然张量本身没有存储具体的数字,但可以通过会话session得到这些具体的数字。

C++使用 Tensor的一些示例
class Tensor的源码目录是
\tensorflow\core\framework\tensor.h
python里边的tensor是numpy ndarry,C++里面使用了第三方库Eigen库的封装-class Tensor


代码在
eigen\unsupported\Eigen\CXX11\src\Tensor

Eigen库的unsupported部分没有官方支持。
CHECK fails表示在编译时就已经发现了错误

typedef float T;
    Tensor T1(DT_FLOAT, TensorShape({}));



    Tensor my_mat(DT_FLOAT, TensorShape({ 3, 5 }));
    // built with Shape{rows: 3, cols: 5}

    auto mat1 = my_mat.matrix<T>();    // 2D Eigen::Tensor, 3 x 5.
    auto mat2 = my_mat.tensor<T, 2>(); // 2D Eigen::Tensor, 3 x 5.
    //auto mat3 = my_mat.matrix<int32>();// CHECK fails as type mismatch.
    //auto vec1 = my_mat.vec<T>();       // CHECK fails as my_mat is 2D.
    //auto vec2 = my_mat.tensor<T, 3>(); // CHECK fails as my_mat is 2D.


// Tensor my_ten(...built with Shape{planes: 4, rows: 3, cols: 5}...)
    Tensor my_ten(DT_FLOAT, TensorShape({4, 3, 5}));

    // 1D Eigen::Tensor, size 60:
    auto flat = my_ten.flat<T>();

    // 2D Eigen::Tensor 12 x 5:
    auto inner = my_ten.flat_inner_dims<T>();

    // 2D Eigen::Tensor 4 x 15:
    auto outer1 = my_ten.shaped<T, 2>({ 4, 15 });

     CHECK fails, bad num elements:
    //auto outer2 = my_ten.shaped<T, 2>({ 4, 8 });

    // 3D Eigen::Tensor 6 x 5 x 2:
    auto weird = my_ten.shaped<T, 3>({ 6, 5, 2 });

     CHECK fails, type mismatch:
    //auto bad = my_ten.flat<int32>();
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值