TensorFlow之张量
1 张量(Tensor) 【类似于numpy中的数组】
TensorFlow 的张量就是一个 n 维数组, 类型为tf.Tensor。Tensor具有以下两个重要的属性
- type:数据类型
- shape:形状(阶)
形状有0阶、1阶、2阶….
tensor1 = tf.constant(4.0)
tensor2 = tf.constant([1, 2, 3, 4])
linear_squares = tf.constant([[4], [9], [16], [25]], dtype=tf.int32)
print(tensor1.shape)
# 0维:() 1维:(10, ) 2维:(3, 4) 3维:(3, 4, 5)
1.3 创建张量的指令
- 固定值张量
示例代码:
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
import tensorflow as tf
zeros = tf.zeros([3, 5])
print(zeros)
print('#' * 50)
zeros_like = tf.zeros_like(zeros)
print(zeros_like)
print('#' * 50)
one = tf.ones([3, 4])
print(one)
print('#' * 50)
ones_like = tf.ones_like(zeros)
print(ones_like)
print('#' * 50)
- 随机值张量
- 其它特殊的创建张量的op
- tf.Variable
- tf.placeholder
示例代码:
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
import tensorflow as tf
one = tf.ones([3, 4])
print(one)
print('#' * 50)
cast_one = tf.cast(one, tf.int32)
print(cast_one)
TensorFlow的张量具有两种形状变换,动态形状和静态形状
- tf.reshape
- tf.set_shape
关于动态形状和静态形状必须符合以下规则
- 静态形状
- 转换静态形状的时候,1-D到1-D,2-D到2-D,不能跨阶数改变形状
- 对于已经固定的张量的静态形状的张量,不能再次设置静态形状
- 动态形状
- tf.reshape()动态创建新张量时,张量的元素个数必须匹配
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
def tensor_demo():
"""
张量的介绍
:return:
"""
a = tf.constant(value=30.0, dtype=tf.float32, name="a")
b = tf.constant([[1, 2], [3, 4]], dtype=tf.int32, name="b")
a2 = tf.constant(value=30.0, dtype=tf.float32, name="a2")
c = tf.placeholder(dtype=tf.float32, shape=[2, 3, 4], name="c")
sum = tf.add(a, a2, name="my_add")
print(a, a2, b, c)
print(sum)
# 获取张量属性
print("a的图属性:\n", a.graph)
print("b的名字:\n", b.name)
print("a2的形状:\n", a2.shape)
print("c的数据类型:\n", c.dtype)
print("sum的op:\n", sum.op)
# 获取静态形状
print("b的静态形状:\n", b.get_shape())
# 定义占位符
a_p = tf.placeholder(dtype=tf.float32, shape=[None, None])
b_p = tf.placeholder(dtype=tf.float32, shape=[None, 10])
c_p = tf.placeholder(dtype=tf.float32, shape=[3, 2])
# 获取静态形状
print("a_p的静态形状为:\n", a_p.get_shape())
print("b_p的静态形状为:\n", b_p.get_shape())
print("c_p的静态形状为:\n", c_p.get_shape())
# 形状更新
# a_p.set_shape([2, 3])
# 静态形状已经固定部分就不能修改了
# b_p.set_shape([10, 3])
# c_p.set_shape([2, 3])
# 静态形状已经固定的部分包括它的阶数,如果阶数固定了,就不能跨阶更新形状
# 如果想要跨阶改变形状,就要用动态形状
# a_p.set_shape([1, 2, 3])
# 获取静态形状
print("a_p的静态形状为:\n", a_p.get_shape())
print("b_p的静态形状为:\n", b_p.get_shape())
print("c_p的静态形状为:\n", c_p.get_shape())
# 动态形状
# c_p_r = tf.reshape(c_p, [1, 2, 3])
c_p_r = tf.reshape(c_p, [2, 3])
# 动态形状,改变的时候,不能改变元素的总个数
# c_p_r2 = tf.reshape(c_p, [3, 1])
print("动态形状的结果:\n", c_p_r)
# print("动态形状的结果2:\n", c_p_r2)
return None
if __name__ == '__main__':
tensor_demo()
运行结果:
Tensor("a:0", shape=(), dtype=float32) Tensor("a2:0", shape=(), dtype=float32) Tensor("b:0", shape=(2, 2), dtype=int32) Tensor("c:0", shape=(2, 3, 4), dtype=float32)
Tensor("my_add:0", shape=(), dtype=float32)
a的图属性:
b的名字:
b:0
a2的形状:
()
c的数据类型:
sum的op:
name: "my_add"
op: "Add"
input: "a"
input: "a2"
attr {
key: "T"
value {
type: DT_FLOAT
}
}
b的静态形状:
(2, 2)
a_p的静态形状为:
(?, ?)
b_p的静态形状为:
(?, 10)
c_p的静态形状为:
(3, 2)
a_p的静态形状为:
(?, ?)
b_p的静态形状为:
(?, 10)
c_p的静态形状为:
(3, 2)
动态形状的结果:
Tensor("Reshape:0", shape=(2, 3), dtype=float32)
1.5 张量的数学运算
- 算术运算符
- 基本数学函数
- 矩阵运算
- reduce操作
- 序列索引操作
详细请参考: https://www.tensorflow.org/versions/r1.8/api_guides/python/math_ops
这些API使用,具体参考文档