TensorFlow2——张量Tensors

张量是具有统一类型(dtype)的多维数组。所有张量都是不可变的,不能更新张量的内容,只能创建一个新的张量。

 

1、基础

创建基本的张量

1)“标量”或“秩—0”张量。没有axes

# This will be an int32 tensor by default; see "dtypes" below.
rank_0_tensor = tf.constant(4)
print(rank_0_tensor)
# tf.Tensor(4, shape=(), dtype=int32)

2)“向量”或“秩—1”张量。向量有1-axes:

rank_1_tensor = tf.constant([2.0, 3.0, 4.0])
print(rank_1_tensor)
# tf.Tensor([2. 3. 4.], shape=(3,), dtype=float32)

3)“矩阵”或“秩—2”张量。有2-axes:

rank_2_tensor = tf.constant([[1, 2],
                             [3, 4],
                             [5, 6]], dtype=tf.float16)
print(rank_2_tensor)
# tf.Tensor(
# [[1. 2.]
#  [3. 4.]
#  [5. 6.]], shape=(3, 2), dtype=float16)

在这里插入图片描述4)张量可以有更多的axes,例如一个3-axes张量:

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)
# 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-axes张量直观的表示有下面三种方式:
在这里插入图片描述

张量转换为NumPy数组

可以使用 np.array 或者 tensor.numpy 方法将张量转换为NumPy数组:

rank_2_tensor = tf.constant([[1, 2],
                             [3, 4],
                             [5, 6]], dtype=tf.float16)
np.array(rank_2_tensor)
rank_2_tensor.numpy()

张量数学运算

可以对张量进行基本的数学运算,包括加法、元素相乘和矩阵相乘(分别有两种方法):

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

# element-wise addition
print(tf.add(a, b), "\n")
print(a + b, "\n")
# tf.Tensor(
# [[2 3]
#  [4 5]], shape=(2, 2), dtype=int32) 

# element-wise multiplication
print(tf.multiply(a, b), "\n")
print(a * b, "\n")
# tf.Tensor(
# [[1 2]
#  [3 4]], shape=(2, 2), dtype=int32) 

# matrix multiplication
print(tf.matmul(a, b), "\n")
print(a @ b, "\n")
# tf.Tensor(
# [[3 3]
#  [7 7]], shape=(2, 2), dtype=int32) 

张量的操作(ops)

可以获取张量中的最大值、最大值对应的位置,以及计算softmax:

c = tf.constant([[4.0, 5.0], [10.0, 1.0]])

# Find the largest value
print(tf.reduce_max(c))
# Find the index of the largest value
print(tf.argmax(c))
# Compute the softmax
print(tf.nn.softmax(c))

# tf.Tensor(10.0, shape=(), dtype=float32)
# tf.Tensor([1 0], shape=(2,), dtype=int64)
# tf.Tensor(
# [[2.6894143e-01 7.3105860e-01]
#  [9.9987662e-01 1.2339458e-04]], shape=(2, 2), dtype=float32)

 

2、形状(shapes)

Shape:张量的每个维度的长度。
Rank:张量维数。标量的秩为0,向量的秩为1,矩阵的秩为2。
Axis 或 Dimension:张量的特定维度。
Size:张量中项的总数。

rank_4_tensor = tf.zeros([3, 2, 4, 5])
print("Type of every element:", rank_4_tensor.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:", rank_4_tensor.shape[-1])
print("Total number of elements (3*2*4*5): ", tf.size(rank_4_tensor).numpy())
# Type of every element: <dtype: 'float32'>
# 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

在这里插入图片描述
轴通常按从全局到局部的顺序排列:
在这里插入图片描述
 

3、索引

单轴索引

  1. 索引从0开始
  2. 负数索引从末尾倒数
  3. 冒号:用于切片 start:stop:step
rank_1_tensor = tf.constant([0, 1, 1, 2, 3, 5, 8, 13, 21, 34])
print(rank_1_tensor.numpy())
print("First:", rank_1_tensor[0].numpy())
print("Second:", rank_1_tensor[1].numpy())
print("Last:", rank_1_tensor[-1].numpy())
# [ 0  1  1  2  3  5  8 13 21 34]
# First: 0
# Second: 1
# Last: 34

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())
# 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]], dtype=tf.float16)
print(rank_2_tensor.numpy())
print(rank_2_tensor[1, 1].numpy())
# [[1. 2.]
#  [3. 4.]
#  [5. 6.]]
# 4.0

# Get row and column tensors
print("Second row:", rank_2_tensor[1, :].numpy())
print("Second column:", rank_2_tensor[:, 1].numpy())
print("Last row:", rank_2_tensor[-1, :].numpy())
print("First item in last column:", rank_2_tensor[0, -1].numpy())
print("Skip the first row:")
print(rank_2_tensor[1:, :].numpy(), "\n")
# 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.]] 

 

4、操作形状

转化为列表:

var_x = tf.Variable(tf.constant([[1], [2], [3]]))
print(var_x.shape)
# (3, 1)

# You can convert this object into a Python list, too
print(var_x.shape.as_list())
# [3, 1]

改变形状:

reshaped = tf.reshape(var_x, [1, 3])
print(var_x.shape)
print(reshaped.shape)
# (3, 1)
# (1, 3)

合并或拆分相邻轴(或添加/删除1):
对于3x2x5张量,整形到(3x2)x5或3x(2x5)都是合理的,因为切片不会混合。

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(tf.reshape(rank_3_tensor, [-1]))
# 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=(30,), dtype=int32)

print(tf.reshape(rank_3_tensor, [3*2, 5]), "\n")
print(tf.reshape(rank_3_tensor, [3, -1]))
# 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=(6, 5), dtype=int32) 
# 
# 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, 10), dtype=int32)

在这里插入图片描述
 

5、数据类型

检查 tf.Tensor 的数据类型可使用 Tensor.dtype 。

当创建 tf.Tensor 时可以选择指定数据类型。如果不指定,则根据数据自动选择默认的数据类型:整数–> tf.int32;浮点数–> tf.float32。

 

6、不规则张量

一个沿着某一轴的元素数目可变的张量叫做“不规则张量”。对于不完整的数据,使用 tf.ragged.RaggedTensor。

ragged_list = [
    [0, 1, 2, 3],
    [4, 5],
    [6, 7, 8],
    [9]]

ragged_tensor = tf.ragged.constant(ragged_list)
print(ragged_tensor)
print(ragged_tensor.shape)
# <tf.RaggedTensor [[0, 1, 2, 3], [4, 5], [6, 7, 8], [9]]>
# (4, None)

在这里插入图片描述
 

7、字符串张量

tf.string 是一种数据类型,也就是说,可以用张量将数据表示为字符串(可变长度字节数组)。

标量字符串张量:

scalar_string_tensor = tf.constant("Gray wolf")
print(scalar_string_tensor)
# tf.Tensor(b'Gray wolf', shape=(), dtype=string)

字符串向量:

tensor_of_strings = tf.constant(["Gray wolf",
                                 "Quick brown fox",
                                 "Lazy dog"])
print(tensor_of_strings)
# tf.Tensor([b'Gray wolf' b'Quick brown fox' b'Lazy dog'], shape=(3,), dtype=string)

字符串分解(tf.strings.split):

print(tf.strings.split(scalar_string_tensor, sep=" "))
print(tf.strings.split(tensor_of_strings))
# tf.Tensor([b'Gray' b'wolf'], shape=(2,), dtype=string)
# <tf.RaggedTensor [[b'Gray', b'wolf'], [b'Quick', b'brown', b'fox'], [b'Lazy', b'dog']]>

字符串转数字(tf.string.to_number):

text = tf.constant("1 10 100")
print(tf.strings.to_number(tf.strings.split(text, " ")))
# tf.Tensor([  1.  10. 100.], shape=(3,), dtype=float32)

 

8、稀疏张量

TensorFlow 支持 tf.sparse.SparseTensor ,以及有效存储稀疏数据的相关操作。
在这里插入图片描述

sparse_tensor = tf.sparse.SparseTensor(indices=[[0, 0], [1, 2]],
                                       values=[1, 2],
                                       dense_shape=[3, 4])
print(sparse_tensor, "\n")
# SparseTensor(indices=tf.Tensor(
# [[0 0]
#  [1 2]], shape=(2, 2), dtype=int64), 
#  values=tf.Tensor([1 2], shape=(2,), dtype=int32), 
#  dense_shape=tf.Tensor([3 4], shape=(2,), dtype=int64))

# We can convert sparse tensors to dense
print(tf.sparse.to_dense(sparse_tensor))
# tf.Tensor(
# [[1 0 0 0]
#  [0 0 2 0]
#  [0 0 0 0]], shape=(3, 4), dtype=int32)

 

9、参考资料

https://tensorflow.google.cn/guide/tensor?hl=zh_cn#sparse_tensors

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值