Tensorflow深度学习框架最重要的加速计算功能,就是通过在cuda上定义Tensor类型数据,利用GPU对神经网络进行计算加速。本文主要介绍Tensorflow2.0的一些Tensor张量数据类型的操作。
注:Tensorflow1.X语法繁琐复杂,各版本之间兼容性极差,相差一个小版本写的代码就极有可能无法运行,在tf2.0以后版本API偏向Keras风格,更易使用,且兼容性问题有所改善。
1 tf2.0与numpy的相互转换
复制代码
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
30
31
32import tensorflow as tf import numpy as np #numpy转tf #in: tf.convert_to_tensor(np.ones((2,2)), tf.float32) # tf.constant()等同 #out: <tf.Tensor: id=2, shape=(2, 2), dtype=float32, numpy= array([[1., 1.], [1., 1.]], dtype=float32)> #in: a = tf.convert_to_tensor(np.ones((2,2)), tf.float32) tf.zeros(a.shape) # 复制Tensor的形状,内容全为0 #out: <tf.Tensor: id=6, shape=(2, 2), dtype=float32, numpy= array([[0., 0.], [0., 0.]], dtype=float32)> #in: tf.fill(a.shape, 3) #将数值全部填充为3 #out: <tf.Tensor: id=12, shape=(2, 2), dtype=int32, numpy= array([[3, 3], [3, 3]])> #tf转numpy #in: a.numpy() # .numpy()方法可以直接将tensor转换为numpy数组array #out: array([[1., 1.], [1., 1.]], dtype=float32)
2 tf张量Tensor初始化
复制代码
1
2
3
4
5
6
7
8# 初始化, 正态分布初始化 #in: tf.random.normal([2, 2], mean=0, stddev=1) #out: <tf.Tensor: id=18, shape=(2, 2), dtype=float32, numpy= array([[ 1.1078316 , 0.23817614], [-0.43512696, -0.14037803]], dtype=float32)>
复制代码
1
2
3
4
5
6
7
8# 截断正态分布初始化,将梯度弥散的区域截断 #in: tf.random.truncated_normal([2, 2], mean=0, stddev=1) #out: <tf.Tensor: id=24, shape=(2, 2), dtype=float32, numpy= array([[-1.0143591 , 0.30591688], [ 0.25293255, -0.91047406]], dtype=float32)>
复制代码
1
2
3
4
5
6
7
8# 均值初始化,在区间等概率地均匀地初始化数组 #in: tf.random.uniform([2, 2], minval=0, maxval=1) #out: <tf.Tensor: id=31, shape=(2, 2), dtype=float32, numpy= array([[0.0710001 , 0.18758869], [0.6249238 , 0.20907605]], dtype=float32)>
复制代码
1
2
3
4
5
6# 创建一个(1,10)区间的整型Tensor #in: tf.range(10) #out: <tf.Tensor: id=35, shape=(10,), dtype=int32, numpy=array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])>
3 Tensor张量操作
3.1 shuffle打乱
复制代码
1
2
3
4
5
6
7# 打乱操作 #in: x = tf.range(10) tf.random.shuffle(x) #out: <tf.Tensor: id=40, shape=(10,), dtype=int32, numpy=array([1, 5, 6, 3, 0, 7, 8, 2, 4, 9])>
3.2 one-hot编码
复制代码
1
2
3
4
5
6
7
8
9
10
11# 转为one-hot编码 #in: y = tf.range(4) tf.one_hot(y, depth=10) #out: <tf.Tensor: id=55, shape=(4, 10), dtype=float32, numpy= array([[1., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [0., 1., 0., 0., 0., 0., 0., 0., 0., 0.], [0., 0., 1., 0., 0., 0., 0., 0., 0., 0.], [0., 0., 0., 1., 0., 0., 0., 0., 0., 0.]], dtype=float32)>
3.3 维度变换
3.3.1 reshape
复制代码
1
2
3
4
5
6
7
8
9
10
11#初始化 students = tf.random.normal([5, 30, 10], mean=0, stddev=1) #in: tf.reshape(students, [5, -1]).shape #out: TensorShape([5, 300]) #in: tf.reshape(students, [5, 50, 6]).shape #out: TensorShape([5, 50, 6])
3.3.2 转置
复制代码
1
2
3
4
5
6# 维度转置 #in: tf.transpose(students, [1, 0, 2]).shape #[1,0,2]为现维度数据在原维度的索引号 #out: TensorShape([30, 5, 10])
3.3.3 增加维度
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14# 增加维度 #in: tf.expand_dims(students, axis=0).shape #在0维度前增加一个维度 #out: TensorShape([1, 5, 30, 10]) #in: tf.expand_dims(students, axis=2).shape #在2维度前增加一个维度 #out: TensorShape([5, 30, 1, 10]) #in: tf.expand_dims(students, axis=-1).shape #在最后增加一个维度,此处参数也可以写3 #out: TensorShape([5, 30, 10, 1])
3.3.4 减少维度
复制代码
1
2
3
4
5
6
7
8
9# 减少维度 #in: d = tf.expand_dims(students, axis=-1) tf.squeeze(d, axis=-1).shape #去除最后一个维度 #out: TensorShape([5, 30, 10]) tf.squeeze(d).shape # 默认去除所有1的维度
最后
以上就是开心超短裙最近收集整理的关于Python深度学习入门之Tensorflow2.0张量操作的全部内容,更多相关Python深度学习入门之Tensorflow2内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复