12.2.创建张量
from pprint import pprint
import tensorflow as tf
Linux 5.4.0-74-generic
Python 3.9.5 @ GCC 7.3.0
Latest build date 2021.06.21
tensorflow version: 2.5.0
创建张量
tf.constant(value, dtype=None, shape=None, name='Const')
创建标量
scalar = tf.constant(1.2)
# 查看 scalar 的类型
print(type(scalar))
# 判断是否为 tensor
print(tf.is_tensor(scalar))
print(scalar)
<class 'tensorflow.python.framework.ops.EagerTensor'>
True
tf.Tensor(1.2, shape=(), dtype=float32)
创建向量和矩阵
vector = tf.constant([1.2, 1, 2], name="vanilla")
pprint(vector)
print()
matrix = tf.constant([[1, 2], [3, 4]])
matrix = tf.constant([1, 2, 3, 4], shape=(2, 2))
pprint(matrix)
<tf.Tensor: shape=(3,), dtype=float32, numpy=array([1.2, 1. , 2. ],
dtype=float32)>
<tf.Tensor: shape=(2, 2), dtype=int32, numpy=
array([[1, 2],
[3, 4]], dtype=int32)>
创建更高维度的张量
tensor = tf.constant([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
print(tensor)
tf.Tensor(
[[[1 2]
[3 4]]
[[5 6]
[7 8]]], shape=(2, 2, 2), dtype=int32)
tf.range(start, limit=None, delta=1, dtype=None, name='range')
tf.range(start=-5, limit=5, delta=2, dtype=None, name='test')
<tf.Tensor: shape=(5,), dtype=int32, numpy=array([-5, -3, -1, 1, 3], dtype=int32)>
tf.zeros(shape, dtype=dtypes.float32, name=None)
print(tf.zeros((2, 3)))
tf.Tensor(
[[0. 0. 0.]
[0. 0. 0.]], shape=(2, 3), dtype=float32)
tf.zeros_like(input, dtype=None, name=None)
print(tf.zeros_like((2, 3)))
tf.Tensor([0 0], shape=(2,), dtype=int32)
tf.ones(shape, dtype=dtypes.float32, name=None)
print(tf.ones((2, 3)))
tf.Tensor(
[[1. 1. 1.]
[1. 1. 1.]], shape=(2, 3), dtype=float32)
tf.ones_like(input, dtype=None, name=None)
print(tf.ones_like((2, 3)))
tf.Tensor([1 1], shape=(2,), dtype=int32)
tf.fill(dims, value, name=None)
print(tf.fill(dims=[], value=8)) # 标量
print(tf.fill(dims=(2, 3), value=2))
tf.Tensor(8, shape=(), dtype=int32)
tf.Tensor(
[[2 2 2]
[2 2 2]], shape=(2, 3), dtype=int32)
tf.eye(num_rows, num_columns=None, batch_shape=None, dtype=dtype.float32, name=None)
print(tf.eye(4), "\n")
print(tf.eye(4, 2), "\n")
print(tf.eye(2, 2, (2, 3)), "\n")
print(tf.eye(2, 2, (2, 3)).shape)
tf.Tensor(
[[1. 0. 0. 0.]
[0. 1. 0. 0.]
[0. 0. 1. 0.]
[0. 0. 0. 1.]], shape=(4, 4), dtype=float32)
tf.Tensor(
[[1. 0.]
[0. 1.]
[0. 0.]
[0. 0.]], shape=(4, 2), dtype=float32)
tf.Tensor(
[[[[1. 0.]
[0. 1.]]
[[1. 0.]
[0. 1.]]
[[1. 0.]
[0. 1.]]]
[[[1. 0.]
[0. 1.]]
[[1. 0.]
[0. 1.]]
[[1. 0.]
[0. 1.]]]], shape=(2, 3, 2, 2), dtype=float32)
(2, 3, 2, 2)
tf.tile(input, multiples, name=None)
a = tf.constant([[1, 2, 3], [4, 5, 6]], tf.int32)
b = tf.constant([1, 2], tf.int32)
tf.tile(a, b)
<tf.Tensor: shape=(2, 6), dtype=int32, numpy=
array([[1, 2, 3, 1, 2, 3],
[4, 5, 6, 4, 5, 6]], dtype=int32)>
c = tf.constant([2, 1], tf.int32)
tf.tile(a, c)
<tf.Tensor: shape=(4, 3), dtype=int32, numpy=
array([[1, 2, 3],
[4, 5, 6],
[1, 2, 3],
[4, 5, 6]], dtype=int32)>
d = tf.constant([2, 2], tf.int32)
tf.tile(a, d)
<tf.Tensor: shape=(4, 6), dtype=int32, numpy=
array([[1, 2, 3, 1, 2, 3],
[4, 5, 6, 4, 5, 6],
[1, 2, 3, 1, 2, 3],
[4, 5, 6, 4, 5, 6]], dtype=int32)>
tf.repeat(input, repeats, axis=None, name=None)
a = tf.constant([[1, 2], [3, 4]], tf.int32)
print(tf.repeat(a, 2))
print(tf.repeat(a, (2, 2, 3, 1)))
tf.Tensor([1 1 2 2 3 3 4 4], shape=(8,), dtype=int32)
tf.Tensor([1 1 2 2 3 3 3 4], shape=(8,), dtype=int32)
tf.convert_to_tensor(value, dtype=None, dtype_hint=None, name=None)
tf.convert_to_tensor([1, 2])
<tf.Tensor: shape=(2,), dtype=int32, numpy=array([1, 2], dtype=int32)>
tf.one_hot(indices, depth, on_value=None, off_value=None, axis=None, dtype=None, name=None)
拼接张量
tf.concat(values, axis, name='concat')
a = tf.ones((2, 3))
b = tf.zeros((2, 3))
print(tf.concat([a, b], axis=0))
print("shape", tf.concat([a, b], axis=0).shape)
tf.Tensor(
[[1. 1. 1.]
[1. 1. 1.]
[0. 0. 0.]
[0. 0. 0.]], shape=(4, 3), dtype=float32)
shape (4, 3)
tf.stack(values, axis=0, name='stack')
a = tf.ones((2, 3))
b = tf.zeros((2, 3))
tf.stack([a, b], axis=0)
print("shape", tf.stack([a, b], axis=0))
shape tf.Tensor(
[[[1. 1. 1.]
[1. 1. 1.]]
[[0. 0. 0.]
[0. 0. 0.]]], shape=(2, 2, 3), dtype=float32)
tf.tuple(tensors, control_inputs=None, name=None)
a = tf.ones((2, 3))
b = tf.zeros((2, 3))
print(tf.tuple([a, b]))
[<tf.Tensor: shape=(2, 3), dtype=float32, numpy=
array([[1., 1., 1.],
[1., 1., 1.]], dtype=float32)>, <tf.Tensor: shape=(2, 3),
dtype=float32, numpy=
array([[0., 0., 0.],
[0., 0., 0.]], dtype=float32)>]
拆分张量
tf.unstack(value, num=None, axis=0, name='unstack')
a = tf.ones((2, 3))
b = tf.zeros((2, 3))
c = tf.stack([a, b], axis=0)
print(tf.unstack(c, axis=0))
[<tf.Tensor: shape=(2, 3), dtype=float32, numpy=
array([[1., 1., 1.],
[1., 1., 1.]], dtype=float32)>, <tf.Tensor: shape=(2, 3),
dtype=float32, numpy=
array([[0., 0., 0.],
[0., 0., 0.]], dtype=float32)>]
tf.split(value, num_or_size_splits, axis=0, num=None, name='split')
a = tf.ones((2, 3))
b = tf.zeros((2, 3))
c = tf.concat([a, b], axis=0)
print(tf.split(c, num_or_size_splits=2, axis=0))
[<tf.Tensor: shape=(2, 3), dtype=float32, numpy=
array([[1., 1., 1.],
[1., 1., 1.]], dtype=float32)>, <tf.Tensor: shape=(2, 3),
dtype=float32, numpy=
array([[0., 0., 0.],
[0., 0., 0.]], dtype=float32)>]
获取张量属性信息
tf.shape(input, out_type=tf.int32, name=None)
a = tf.ones((2, 3))
print(tf.shape(a))
tf.Tensor([2 3], shape=(2,), dtype=int32)
tf.shape_n(input, out_type=tf.int32, name=None)
a = tf.ones((2, 3))
b = tf.zeros((2, 4))
print(tf.shape_n([a, b]))
[<tf.Tensor: shape=(2,), dtype=int32, numpy=array([2, 3],
dtype=int32)>, <tf.Tensor: shape=(2,), dtype=int32, numpy=array([2,
4], dtype=int32)>]
tf.size(input, out_type=tf.int32, name=None)
a = tf.ones((2, 3))
print(tf.size(a))
tf.Tensor(6, shape=(), dtype=int32)
tf.rank(input, name=None)
a = tf.ones((2, 3, 5))
print(tf.rank(a))
tf.Tensor(3, shape=(), dtype=int32)