栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

简明的tensorflow2(tensorflow tensor)

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

简明的tensorflow2(tensorflow tensor)

1.基础知识
张量时具有统一类型(称为dtype)的多维数组。
张量与np.arrays有一定的相似性。
就像python的数值和字符串一样,所有的张量都是不可变的:永远无法更新张量的内容,只能创建新的张量。
标量(0秩张量):标量包含单个值,但没有轴。
向量(1秩张量):像一个值列表,向量有1个轴。
矩阵(2秩张量):有两个轴。

#标量
rank_0_tensor = tf.constant(4)
print(rank_0_tensor)
#标量
rank_1_tensor = tf.constant([2.1,3.2,4.3])
print(rank_1_tensor)
#矩阵
rank_2_tensor = tf.constant([[1,2],[3,4],[5,6]],dtype = tf.float16)
print(rank_2_tensor)
#包含3个轴的张量
rank_3_tensor = tf.constant([
  [[0, 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]],])
print(rank_3_tensor)```

```python
tf.Tensor(4, shape=(), dtype=int32)
tf.Tensor([2.1 3.2 4.3], shape=(3,), dtype=float32)
tf.Tensor(
[[1. 2.]
 [3. 4.]
 [5. 6.]], shape=(3, 2), dtype=float16)
tf.Tensor(
[[[ 0  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]]], shape=(3, 2, 5), dtype=int32)


3轴张量的不同表现形式

张量通常包含浮点型和整型数据,但还有其他数据类型,包括:复杂的数值,字符串。
可以对张量执行基本数学运算,包括加法、逐元素乘法和矩阵乘法。

形状简介
张量有形状。
形状:张量的每个轴的长度(元素数量)。
秩:张量轴数。标量的秩为0,向量的秩为1,矩阵的秩为2。
轴或维度:张量的一个特殊维度。
大小:张量的总项数,即乘积形状向量。
张量和tf.TensorShape对象提供了方便的属性访问。

rank_4_tensor = tf.zeros([3,2,4,5])
print("type of every element:",rank_4_tensor.dtype) #dtype
print("number of dimensions:",rank_4_tensor.ndim)#维度
print("shape of tensor:",rank_4_tensor.shape)
print("elements along axis 0 of tensor:",rank_4_tensor.shape[0])
print("elements along the last axis of tensor:",tf.size(rank_4_tensor).numpy())
Type of every element: 
Number of dimensions: 4
Shape of tensor: (3, 2, 4, 5)
Elements along axis 0 of tensor: 3
Elements along the last axis of tensor: 5
Total number of elements (3*2*4*5):  120

轴一般按照从全局到局部的顺序进行排序:首先是批次轴,随后是空间维度,最后是每个位置的特征。
典型轴顺序

索引
单轴索引
索引从0开始编制
负索引表示按倒序编制索引
冒号:用于切片 start:stop:step

rank_1_tensor = tf.constant([0, 1, 1, 2, 3, 5, 8, 13, 21, 34])
print("First:", rank_1_tensor[0].numpy())
print("Second:", rank_1_tensor[1].numpy())
print("Last:", rank_1_tensor[-1].numpy())

print("Everything:", rank_1_tensor[:].numpy())
print("Before 4:", rank_1_tensor[:4].numpy())
print("From 4 to the end:", rank_1_tensor[4:].numpy())
print("From 2, before 7:", rank_1_tensor[2:7].numpy())
print("Every other item:", rank_1_tensor[::2].numpy())
print("Reversed:", rank_1_tensor[::-1].numpy())
first:0
second:1
last:34

Everything: [ 0  1  1  2  3  5  8 13 21 34]
Before 4: [0 1 1 2]
From 4 to the end: [ 3  5  8 13 21 34]
From 2, before 7: [1 2 3 5 8]
Every other item: [ 0  1  3  8 21]
Reversed: [34 21 13  8  5  3  2  1  1  0]

多轴索引
对于高秩张量的每个单独的轴,遵循与单轴完全相同的规则。

rank_2_tensor = tf.constant([[1. 2.]
							 [3. 4.]
							 [5. 6.]])
Everything: [ 0  1  1  2  3  5  8 13 21 34]
Before 4: [0 1 1 2]
From 4 to the end: [ 3  5  8 13 21 34]
From 2, before 7: [1 2 3 5 8]
Every other item: [ 0  1  3  8 21]
Reversed: [34 21 13  8  5  3  2  1  1  0]
Second row: [3. 4.]
Second column: [2. 4. 6.]
Last row: [5. 6.]
First item in last column: 2.0
Skip the first row:
[[3. 4.]
 [5. 6.]]

操作形状

dtypes详解
使用Tensor.dtype属性可以检查tf.Tensor的数据类型。
从python对象创建tf.tensor时可以选择指定数据类型。
数据类型可以相互转换。
tf.cast()函数的作用是执行 tensorflow 中张量数据类型转换,比如读入的图片如果是int8类型的,一般在要在训练前把图像的数据格式转换为float32。

cast定义:cast(x, dtype, name=None)
第一个参数 x: 待转换的数据(张量)
第二个参数 dtype: 目标数据类型
第三个参数 name: 可选参数,定义操作的名称

import tensorflow as tf
 
t1 = tf.Variable([1,2,3,4,5])
t2 = tf.cast(t1,dtype=tf.float32)
 
print 't1: {}'.format(t1)
print 't2: {}'.format(t2)
 
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    sess.run(t2)
    print t2.eval()
    # print(sess.run(t2))
t1: 
t2: Tensor("Cast:0", shape=(5,), dtype=float32)
[ 1.  2.  3.  4.  5.]


the_f64_tensor = tf.constant([2.2, 3.3, 4.4], dtype=tf.float64)
the_f16_tensor = tf.cast(the_f64_tensor, dtype=tf.float16)
# Now, let's cast to an uint8 and lose the decimal precision
the_u8_tensor = tf.cast(the_f16_tensor, dtype=tf.uint8)
print(the_u8_tensor)
tf.Tensor([2 3 4], shape=(3,), dtype=uint8)

广播
在一定条件下,对一组张量执行组合运算时,为了适应大张量,会对小张量进行扩展。
最简单和最常见的例子是尝试将张量和标量相乘或相加。这种情况下会对标量进行广播,使其变成与其他参数相同的形状。

t1: 
t2: Tensor("Cast:0", shape=(5,), dtype=float32)
[ 1.  2.  3.  4.  5.]
tf.Tensor([2 4 6], shape=(3,), dtype=int32)
tf.Tensor([2 4 6], shape=(3,), dtype=int32)
tf.Tensor([2 4 6], shape=(3,), dtype=int32)

在大多数情况下,广播的时间和空间效率更高,因为广播运算不会在内存中具体化扩展的张量。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/772893.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号