TensorFlow基本概念之张量

张量:Tensor

在TensorFlow中,张量是数据流图上的数据载体,tensorflow中的tensor就是张量的意思,使用张量统一表示所有数据。
张量可以看做是0阶标量,1阶向量和2阶矩阵在高维空间的推广。
张量与常见数据实体的关系如图所示:

阶数数据实体python样例
0标量scalar = 1
1向量vector =[1,2,3]
2矩阵matirx=[[1,2],[3,4]]
3数据立方tensor=[[[1,2],[3,4]],[[5,6],[7,8]]]
nn阶张量
1.打印hello world
import tensorflow as tf
hello = tf.constant("hello world")
sess = tf.Session()
print(sess.run(hello))
2.张量支持的数据类型

张量支持多种数据类型,除了浮点数、整数、字符串、布尔等类型外,还支持复数和量化整数类型,主要的数据类型如下:

TensorFlow数据类型说明
tf.float16半精度浮点数
tf.float32单精度浮点数
tf.float64双精度浮点数
tf.bfloat16裁短浮点数
tf.int88位有符号整数
tf.int1616位有符号整数
tf.int3232位有符号整数
tf.int6464位有符号整数
tf.uint8位无符号整数
tf.uint1616位无符号整数
tf.string字符串
tf.bool布尔值
tf.complex64单精度复数
tf.complex128双精度复数
tf.qint8量化的8位有符号整数
tf.qint16量化的16位有符号整数
tf.qint32量化的32位有符号整数
tf.quint量化的8位无符号整数
tf.quint16量化的16位无符号整数
3.创建张量
a = tf.constant(1.0)
print(a)

输出结果

<tf.Tensor 'Const_2:0' shape=() dtype=float32>

shape=() 表示是标量

d = tf.constant([1.0,2.0])
print(d)

Tensor("Const_5:0", shape=(2,), dtype=float32)

shape=(2,)说明张量是一维数组,并且长度为2

d = tf.constant([[1.0,2.0],[3.0,4.0]])
print(d)
Tensor("Const_7:0", shape=(2, 2), dtype=float32)

shape=(2, 2)说明是二维矩阵,并且行数和列数都为2

d = tf.constant([[[1.0,2.0],[3.0,4.0]],[[5.0,6.0],[7.0,8.0]]])
print(d)
Tensor("Const_9:0", shape=(2, 2, 2), dtype=float32)

shape=(2, 2, 2)说明是三维向量,第一个2可以当做是块数,第二个2是行数,第三个2是列数,也就是有两块两行两列的矩阵
张量的属性

属性名称功能说明
dtype张量的数据类型
name张量在数据流图中的名称
graph张量所属的数据流图
op生成该张量的前置操作
shape张量传输数据的形状
value_index张量在前置操作所有输出值的索引
4.计算张量
a = tf.constant(1.0)
b = tf.constant(2.0)
c = tf.add(a,b)
with tf.Session() as sess:
    print(c.eval())
    print(sess.run([a,b,c]))
    print(c.get_shape())
3.0
[1.0, 2.0, 3.0]
()

张量的成员方法

方法名功能说明
eval取出张量的值
get_shape获取张量的形状
set_shape修改张量的形状
consumers获取张量的后置操作
a = tf.constant([1,1])
b = tf.constant([2,3])
c = tf.add(a,b)
with tf.Session() as sess:
    print(a[0].eval(),a[1].eval())
    print(c.name)
    print(c.eval())
    print(c.shape)
    print(a.consumers())
    print(c.op)
1 1
Add_1:0
[3 4]
(2,)
[<tf.Operation 'Add_1' type=Add>, <tf.Operation 'strided_slice' type=StridedSlice>, <tf.Operation 'strided_slice_1' type=StridedSlice>]
name: "Add_1"
op: "Add"
input: "Const_12"
input: "Const_13"
attr {
  key: "T"
  value {
    type: DT_INT32
  }
}

张量的操作

操作类型典型操作
一元代数操作abs,neg,invert
二元代数操作add,multiply,sub
形状操作chip,reshape,slice,shuffle
归约操作reduce_mean,reduce_sum
神经网络操作conv,pool,softmax,relu
条件操作cond
5.稀疏张量 SparseTensor

创建

sp = tf.SparseTensor(indices=[[0,2],[1,3]],values=[1,2],dense_shape=[3,4])
'''

稀疏张量sp的键值对如下:
[0,2]:1
[1,3]:2
形状等价于:
[[0,0,1,0]
 [0,0,0,2]
 [0,0,0,0]
]

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

稀疏张量的操作

操作类型典型操作
转换操作sparse_to_dense,sparse_to_indicator,sparse_merge
代数操作sparse_add,sparse_softmax,sparse_tensor_dense_matmul,sparse_maxium
几何操作sparse_concat,sparse_reorder,sparse_split,sparse_transpose
归约操作sparse_reduce_sum,sparse_reduce_sum_sparse
x = tf.SparseTensor(indices=[[0,0],[0,2],[0,1]],values=[1,1,1],dense_shape=[2,3])
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:
    print(sess.run(reduce_x))
[3, array([3, 0], dtype=int32), array([[3],
       [0]], dtype=int32), 3]

张量的基础部分差不多就这些了。


  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
TensorFlow是一个开源的机器学习框架,由Google开发并维护。它被广泛用于构建和训练各种机器学习模型,包括神经网络。TensorFlow基本概念包括: 1. 张量(Tensor):TensorFlow的基本数据单位是张量,可以看作是多维数组。张量可以是标量(0维张量)、向量(1维张量)、矩阵(2维张量)或更高维的数组。张量TensorFlow用于表示输入数据、模型参数和计算结果。 2. 计算图(Computation Graph):TensorFlow使用计算图来描述模型的计算过程。计算图是由一系列节点(Node)和边(Edge)组成的有向无环图。节点表示操作(如加法、乘法、激活函数等),边表示数据流动的方向。 3. 变量(Variable):在TensorFlow,变量用于存储模型的参数,并且在训练过程可以更新。变量在计算图是持久存在的,并且可以跨多个计算图共享。 4. 会话(Session):TensorFlow使用会话来执行计算图的操作。会话负责分配计算资源、管理变量和执行操作。 5. 损失函数(Loss Function):损失函数用于衡量模型在训练过程的预测结果与真实标签之间的差异。通过最小化损失函数,可以使模型逐步优化,提高预测的准确性。 6. 优化器(Optimizer):优化器用于更新模型的参数,使损失函数的值最小化。常见的优化器包括梯度下降(Gradient Descent)、Adam、Adagrad等。 这些是TensorFlow基本概念,了解它们可以帮助理解和使用TensorFlow进行机器学习模型的开发和训练。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值