张量是具有统一类型(dtype)的多维数组。所有张量都是不可变的,不能更新张量的内容,只能创建一个新的张量。
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)
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)
可以使用 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)
可以获取张量中的最大值、最大值对应的位置,以及计算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)
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:
# 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
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.]]
转化为列表:
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)
检查 tf.Tensor 的数据类型可使用 Tensor.dtype 。
当创建 tf.Tensor 时可以选择指定数据类型。如果不指定,则根据数据自动选择默认的数据类型:整数–> tf.int32;浮点数–> tf.float32。
一个沿着某一轴的元素数目可变的张量叫做“不规则张量”。对于不完整的数据,使用 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)
#
# (4, None)
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.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)
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)
https://tensorflow.google.cn/guide/tensor?hl=zh_cn#sparse_tensors