tensor and operation


A Tensor is a multi-dimensional array. Similar to NumPy ndarray objects。
维度:一维直接写个数,二维使用【行,列】 多维 [n,m,k,…]

1. 创建张量

# 1. tf.constant(张量内容,dtype=数据类型(可选))   ==》用户创建常量
a = tf.constant(4)  # tf.Tensor(4, shape=(), dtype=int32) # 标量包含单个值,但没有“轴”。
b = tf.constant([1,5],dtype=tf.int32)  # tf.Tensor([1 5], shape=(2,), dtype=int32)
c = tf.constant([[1, 2],
                 [3, 4],
                 [5, 6]], dtype=tf.float16)
   
"""
tf.Tensor(
[[1. 2.]
 [3. 4.]
 [5. 6.]], shape=(3, 2), dtype=float16)
"""

# 2. 将numpy的数据类型转换为tensor数据类型 
d = np.arange(0,5)
d = tf.convert_to_tensor(a,dtype=tf.int32)
# You can convert a tensor to a NumPy array either using np.array or the tensor.numpy method:
d.numpy()


tf.zeros([2,3])   # tf.zeros(维度) 创建全为0的张量 
tf.ones([2,3])    # tf.ones(维度) 创建全为1的张量
tf.fill([2,2],9)  # tf.fill(维度,制定值) 创建全为指定值的张量  

1.1 打印张量信息

# 通过shpe、dtype、numpy、ndim、size获得张量的形状、类型、值、维度、元素个数
print(c.shape)
print(c.dtype)
print(c.numpy())
print(c.ndim)
print(tf.size(c).numpy())

1.2 切片

# 一维张量切片,同list
rank_1_tensor = tf.constant([0, 1, 1, 2, 3, 5, 8, 13, 21, 34])
print("From 4 to the end:", rank_1_tensor[4:].numpy())

# 高维张量
x = tf.constant([[1, 2],
                             [3, 4],
                             [5, 6]], dtype=tf.float16)

# Get row and column tensors
print("Second row:", x[1, :].numpy())
print("Second column:", x[:, 1].numpy())
print("Last row:", x[-1, :].numpy())
print("First item in last column:", x[0, -1].numpy())
print("Skip the first row:")
print(x[1:, :].numpy(), "\n")

1.3 张量运算

a = tf.constant([[1, 2],
                 [3, 4]])

b = tf.constant([[1, 1],
                 [1, 1]]) # Could have also said `tf.ones([2,2])`

print(tf.add(a, b),'\n')            # a + b
print(tf.subtract(a,b),'\n')        # a - b
print(tf.multiply(a, b),'\n')       # a * b
print(tf.matmul(a, b),'\n')         # a @ b
print(tf.divide(a,b),'\n')

# 平方、次方、开方:tf.square(),tf.pow(),tf.sqrt()

2. 张量的相关操作

2.1 排序和最值函数

2.1.1 排序函数

  1. tf.sort() 返回排序后的tensor
  2. tf.argsort() 返回一个张量的下标,它沿一个轴给出它的排序顺序。
  3. tf.math.top_k() 返回排序后topk个元素组成的tensor

当对多维Tensor进行排序时,可以通过axis参数指定需要排序的维度,默认axis默认值为-1,也就是对最后一维进行排序。

2.1.2 最值函数

  1. tf.reduce_max() 计算张量维度上元素的最大值
  2. tf.reduce_min()
  3. tf.reduce_mean() 计算张量沿着指定维度的平均值
  4. tf.reduce_sum(张量名,axis=操作轴)
  5. tf.argmax()
  6. tf.argmin()

注:axis=0 表示沿第一维的方向(求所有行的值中对应的值)

不指定维度时,获取整个Tensor的最值,通过axis参数可以对指定维度求最值

# 排序函数

b = tf.random.uniform([3, 3], minval=1, maxval=10,dtype=tf.int32)
tf.argsort(b,axis=0)   # 返回的张量中,每一个元素表示b中原来元素在该行中的索引。
# 注意:top_k()方法只能对最后一维度进行排序。
tf.math.top_k(b, 2).values   # values  返回值, indexs 返回index

print(b.numpy)
print(tf.reduce_max(b, axis=0), '\n')
"""
<tf.Tensor: id=3, shape=(3, 3), dtype=int32, numpy=
array([[1, 9, 2],
       [3, 2, 6],
       [9, 7, 6]], dtype=int32)>>
tf.Tensor([9 9 6], shape=(3,), dtype=int32) 
"""

b = tf.random.uniform([3, 4, 2], minval=1, maxval=10, dtype=tf.int32)

print(b.numpy)

print(tf.reduce_max(b, axis=0), '\n')
"""
<tf.Tensor: id=3, shape=(3, 4, 2), dtype=int32, numpy=
array([[[9, 8],
        [4, 5],
        [8, 1],
        [3, 3]],

       [[6, 2],
        [8, 7],
        [5, 3],
        [7, 6]],

       [[7, 4],
        [7, 2],
        [9, 4],
        [5, 8]]], dtype=int32)>>
tf.Tensor(
[[9 8]         # [9,6,5] 中的最大值为9,[8,2,4]中最大值为8
 [8 7]
 [9 4]
 [7 8]], shape=(4, 2), dtype=int32) 
"""

3. 张量补齐操作

3.1 向量扩展

  1. tf.pad( tensor,paddings, mode=‘CONSTANT’,name=None)。填充函数,padings 代表每一维填充多少行/列
  2. tf.broadcast_to() 将原始矩阵成倍增加
  3. tf.expand_dims() 增加维度
a =[[2,3,4],[5,6,7]]
print(tf.pad(a,[[1,1],[2,2]],"CONSTANT"))

输出

[[0, 0, 0, 0, 0, 0, 0],
 [0, 0, 2, 3, 4, 0, 0],
 [0, 0, 5, 6, 7, 0, 0],
 [0, 0, 0, 0, 0, 0, 0]]

注:[1,1]是在pad里是第一个,代表第一维即矩阵的行,左边的1代表上方放一行0,右边的1代表下方放一行0
同理,2,2顺序是第二个,代表对列操作,左边的2代表在左边放两列0,右边2代表在右边放两列0

x = tf.constant([[1, 2, 3]])   # Shape (1, 3,)
y = tf.broadcast_to(x, [2, 3])

3.2 向量拼接

  1. tf.concat() :不增加新的维度

  2. tf.tile():不增加新的维度

tf.tile()可以沿着某个维度对tensor进行指定倍数的复制,需要注意的是,tile不能增加tensor的维度,即tensor本身有几个维度,那么它还是几个维度,那么如果想要升维扩展呢,可以借助tf.reshape()

  1. tf.stack() 根据张量列表,拼接张量,增加一个新的维度,扩充维度,

Stacks a list of rank-R tensors into one rank-(R+1) tensor.This is the opposite of unstack. The numpy equivalent is np.stack

x = tf.constant([1, 2, 3])
tf.broadcast_to(x, [3, 3])

t=[[2,3,4],[5,6,7]]
tf.pad(t,[[1,1],[2,2]],"CONSTANT")

a = tf.constant([[1,2,3],[4,5,6]], tf.int32)
b = tf.constant([2,2], tf.int32)
tf.tile(a, b)

a = tf.constant([1, 2, 3])
# tf.tile(a, [2]) # 维度需要用[]指明是几维张量

a = tf.reshape(a, [1, 3]) # a变成了二维张量,(1,3)一行三列
tf.tile(a,[2,2])    # 表示第一维度和第二维度分别复制两次
t1 = [[1, 2, 3], [4, 5, 6]]
t2 = [[7, 8, 9], [10, 11, 12]]
tf.concat([t1, t2], axis = 1)

x = tf.constant([1, 4])
y = tf.constant([2, 5])
z = tf.constant([3, 6])
tf.stack([x, y, z])  # [[1, 4], [2, 5], [3, 6]] (Pack along first dim.)
tf.stack([x, y, z], axis=1)  # [[1, 2, 3], [4, 5, 6]]

3.3 减少向量维度:

  1. tf.squeeze() 从张量中移除尺寸为1的维数。
# 't' is a tensor of shape [1, 2, 1, 3, 1, 1]
tf.shape(tf.squeeze(t))  # [2, 3]

4. 张量索引

  1. tf.gather() 根据指定轴,按照index切分tensor【是选择同一维度的元素组成新的矩阵】

  2. tf.gather_nd() 取出对应位置的元素 【可以选择不同维度的元素】
    tf.gather和tf.gather_nd都是从tensor中取出index标注的部分,不同之处在于,gather一般只使用一个index来标注,而gather_nd可以使用多个index。

  3. tf.boolean_mask()

    • Apply boolean mask to tensor. 按照mask中的true和false对张量进行切片
  4. tf.slice() 张量切片

    • tf.slice(input_, begin, size, name=None) 从原始输入input数据中选择以begin开始的尺度大小为size的切片
  5. tf.where()

    • 用法1: tf.where()返回一个布尔张量中真值的位置。对于非布尔型张量,非0的元素都判为True。
    • 用户2: tf.where(input_tensor, a,b) 它的作用是:对于张量a,如果input_tensor对应位置的元素为True,则张量a中的该位置处元素保留,反之由张量b中相应位置的元素来代替。
  6. tf.split() 将张量切分成不同组张量

# tf.boolean_mask()
tensor = [[1, 2], [3, 4], [5, 6]] # 2-D example
mask = np.array([True, False, True])
tf.boolean_mask(tensor, mask)

# tf.where()
test_a=tf.constant([[1,2,3],[0,0,6]])

# 返回的是二维张量,第一个维度表示非零的个数,第二维为 dimensions。
tf.where(test_a)    # 0:表示第一维: 0,1,2 表示第一行非零的index 1:表示第二维,2表示第二行的非零index

tf.where([True, False, False, True], [1,2,3,4], [100])

# tf.gather
c2 = tf.constant([[1, 1, 1],
                  [2, 2, 2],
                  [3, 3, 3],
                  [4, 4, 4],
                  [5, 5, 5]], shape=[5, 3])
g2 = tf.gather(c2, indices=[1, 4], axis=0) # 获取在第1维度上的,索引为1和4的值

print(g2)  # tf.Tensor([[2 2 2][5 5 5]], shape=(2, 3), dtype=int32)

# tf.gather_nd()
a = tf.constant([[1,  2,  3,  4,  5],
                 [6,  7,  8,  9,  10],
                 [11, 12, 13, 14, 15]])

index_a1 = tf.constant([[0, 2], [0, 4], [2, 2]])  # 随便选几个
index_a2 = tf.constant([0, 1])  # 0行1列的元素——2
index_a3 = tf.constant([[0], [1]])  # [第0行,第1行]
index_a4 = tf.constant([0])  # 第0行
print(tf.gather_nd(a, index_a1))
print(tf.gather_nd(a, index_a2))
print(tf.gather_nd(a, index_a3))
print(tf.gather_nd(a, index_a4))

5. 创建张量

5.1 创建特定value的张量

  • tf.eye() 创建单位矩阵
  • tf.fill() 创建一个填充标量值的张量
  • tf.ones() 创建一个全为1的张量
  • tf.zeros() 创建一个全为0的张量
  • tf.one_like()创建一个张量,其形状与输入的形状相同。
  • tf.zeros_like()

5.2 初始化张量

  • tf.random_normal_initializer() 按照正态分布初始化张量
  • tf.random_uniform_initializer() 按照均匀分布初始化张量

5.3 张量处理

  • tf.clip_by_value() Clips tensor values to a specified min and max.
  • tf.cast() 强制tensor转换该数据类型
  • tf.reshape() 变换向量维度
  • tf.one_hot(代转换数据,depth=classes) 独热编码 one-hot encoding
reshaped = tf.reshape(x, [1, 3])
print(tf.reshape(x, [-1]))   # A `-1` passed in the `shape` argument says "Whatever fits".

indices = [0, 1, 2]
depth = 3
tf.one_hot(indices, depth)  # output: [3 x 3]
# [[1., 0., 0.],
#  [0., 1., 0.],
#  [0., 0., 1.]]

6. 相关函数

6.1 tf.random

  1. tf.random.normal()
  2. tf.random.uniform()
  3. tf.random.set_seed() 设置全局随机种子
  4. tf.random.shuffle() 按照第一维打乱张量
  5. tf.random.truncated_normal() 截断正态分布

6.2 tf.math

  • tf.math.argmax() 返回张量中最大值的index
  • tf.math.equal() Returns the truth value of (x == y) element-wise.
  • tf.math.greater() Returns the truth value of (x > y) element-wise.
  • tf.math.less() Returns the truth value of (x < y) element-wise.
  • tf.math.multiply() Returns an element-wise x * y.
  • tf.math.pow()

6.3 最常用的函数

  1. tf.math.reduce_mean()
  2. tf.math.reduce_max()
  3. tf.math.reduce_min()
  4. tf.math.reduce_sum()
  5. tf.math.top_k()

6.5 tf.sparse

Sometimes, your data is sparse, like a very wide embedding space. TensorFlow supports tf.sparse.SparseTensor and related operations to store sparse data efficiently.

稀疏张量相关的操作运算需要添加tf.sparse.

  1. tf.sparse.SparseTensor(indices,values,dense_shape) 张量的稀疏表示
  2. tf.sparse.to_dense() Converts a SparseTensor into a dense tensor.
# Sparse tensors store values by index in a memory-efficient manner
sparse_tensor = tf.sparse.SparseTensor(indices=[[0, 0], [1, 2]],
                                       values=[1, 2],
                                       dense_shape=[3, 4])
print(sparse_tensor, "\n")

# You can convert sparse tensors to dense
print(tf.sparse.to_dense(sparse_tensor))

稠密矩阵转为稀疏矩阵的方法

# 1.生成一个0,1矩阵a表示稠密矩阵
a = tf.constant([[1, 0, 1], [0, 1, 0], [0, 0, 1]], dtype=tf.float32)

# 2.定义一个0值
zero = tf.constant(0, dtype=tf.float32)

# 3.将矩阵a等于0的地方置为False,反之True
where = tf.not_equal(a, zero)

# 4.获取矩阵a中非0元素的索引
indices = tf.where(where)

# 5.获取矩阵a中非0元素的值
vals = tf.gather_nd(a, indices)

# 6.将稠密矩阵a转为稀疏矩阵s
s = tf.sparse.SparseTensor(indices, vals, dense_shape=a.shape)
print(s)

6.6 tf.strings

  1. tf.strings.join() 连接两个字符串

  2. tf.strings.to_hash_bucket() 该方法可以把每个字(词)进行hashing,编码到具体的对应索引上。

  3. tf.strings.to_number()

# Tensors can be strings, too here is a scalar string.
scalar_string_tensor = tf.constant("Gray wolf")

# Some basic functions with strings can be found in tf.strings, including tf.strings.split.

print(tf.strings.split(scalar_string_tensor, sep=" ")) # You can use split to split a string into a set of tensors

text = tf.constant("1 10 100")
print(tf.strings.to_number(tf.strings.split(text, " ")))


# str 字符串
# num 词表大小
tf.strings.to_hash_bucket_fast(str, num)

7. 使用GPU 加速

Many TensorFlow operations are accelerated using the GPU for computation

import time

def time_matmul(x):
    start = time.time()
    for loop in range(10):
        tf.matmul(x,x)
    
    result = time.time - start
    print("10 loops: {:0.2f}ms".format(1000*result))

# Froce execution on CPU
print("On CPU:")
with tf.device("CPU:"):
    x = tf.random.uniform([1000, 1000])
    assert x.device.endswith("CPU:0")
    time_matmul(x)

# Force execution on GPU 
if tf.config.list_physical_devices("GPU"):
    print("ON GPU:")
    with tf.device("GPU:0"):
        x = tf.random.uniform([1000, 1000])
        assert x.device.endswith('GPU:0')
        time_matmul(x)

Apply transformations

Use the transformations functions like map, batch, and shuffle to apply transformations to dataset records.

ds_tensors = tf.data.Dataset.from_tensor_slices([1, 2, 3, 4, 5, 6])
ds_tensors = ds_tensors.map(tf.square).shuffle(2).batch(2)

ds_file = ds_file.batch(2)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值