Tensorflow2-数据类型

数据载体

  • list: 一个非常灵活的数据载体,可以添加任何类型数据类型,随意的添加删除管理 但是对于图片型数据消耗内存空间过大,且读取写入效率低
  • np.array: 专门用于解决同类型数据的运算例如同样大小的图片[64,32,32,3]
  • tf.tensor: 在神经网络上的功能比numpy更优秀。

Tensor

scalar 标量 : 1.1

vecter 向量 : [1.1]、[1.1,2.2,…]

matrix : [[1.1,2.2],[5.5,6.6]] (1.1,2.2为一行)

tensor : rank > 2 几乎代表神经网络当中所有的数据类型

Tensorflow中基本数据类型

int,float,double,bool,string

数据类型变量的创建,叫constant是因为在前期版本是常量,但现在它是变量

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
In [4]: tf.constant(1)
Out[4]: <tf.Tensor: shape=(), dtype=int32, numpy=1>

In [5]: tf.constant(1.1)
Out[5]: <tf.Tensor: shape=(), dtype=float32, numpy=1.1>

In [6]: tf.constant(2.2,dtype = tf.int32)
TypeError: Cannot convert 2.2 to EagerTensor of dtype int32

In [7]: tf.constant(2.,dtype = tf.double)
Out[7]: <tf.Tensor: shape=(), dtype=float64, numpy=2.0>

In [8]: tf.constant([True,False])
Out[8]: <tf.Tensor: shape=(2,), dtype=bool, numpy=array([ True, False])>

In [9]: tf.constant('hello')
Out[9]: <tf.Tensor: shape=(), dtype=string, numpy=b'hello'>

Tensor属性

device属性

查看当前Tensor所在设备的名字

1
2
3
4
In [2]: with tf.device("cpu"): #通过cpu创建
...: a = tf.constant([1])
In [3]: a.device
Out[3]: '/job:localhost/replica:0/task:0/device:CPU:0'

将Tensor在cpu转移至gpu 只需要 aa = a.gpu() aa就在gpu上

两个Tensor在不同设备上不能顺利进行运算

Tensor->numpy

1
2
3
In [8]: b = tf.range(4)
In [9]: b.numpy()
Out[9]: array([0, 1, 2, 3], dtype=int32)

Tensor维度 ndim

1
2
3
4
5
6
In [12]: c= tf.constant([[1,2],[3,4]])
In [13]: c.ndim
Out[13]: 2 #返回数字

In [14]: tf.rank(c)
Out[14]: <tf.Tensor: shape=(), dtype=int32, numpy=2> #返回Tensor

TensorShape

1
2
In [19]: c.shape
Out[19]: TensorShape([2, 2])

确认是否为Tensor

1
2
3
4
5
In [20]: tf.is_tensor(c) #一般使用这种方法
Out[20]: True

In [29]: isinstance(c,tf.Tensor)
Out[29]: True

查看类型

1
2
3
4
5
6
In [24]: a = tf.constant([1.])
In [25]: b = tf.constant([True,False])
In [26]: c = tf.constant('hello')

In [30]: a.dtype,b.dtype,c.dtype
Out[30]: (tf.float32, tf.bool, tf.string)

数字类型转换

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
In [4]: a = np.arange(5)
In [5]: a.dtype
Out[5]: dtype('int64')
In [6]: aa = tf.convert_to_tensor(a)
#<tf.Tensor: shape=(5,), dtype=int64, numpy=array([0, 1, 2, 3, 4])>
In [8]: aa = tf.convert_to_tensor(a,dtype=tf.int32)
#<tf.Tensor: shape=(5,), dtype=int32, numpy=array([0, 1, 2, 3, 4], dtype=int32)>

In [10]: tf.cast(aa,dtype = tf.float32) #Tensor aa 从整型转换为 float32
Out[10]: <tf.Tensor: shape=(5,), dtype=float32, numpy=array([0., 1., 2., 3., 4.], dtype=float32)>

In [13]: aaa = tf.cast(aa,dtype = tf.double)
#<tf.Tensor: shape=(5,), dtype=float64, numpy=array([0., 1., 2., 3., 4.])>

In [15]: tf.cast(aaa,dtype = tf.int32)
Out[15]: <tf.Tensor: shape=(5,), dtype=int32, numpy=array([0, 1, 2, 3, 4], dtype=int32)>

bool类型转换

1
2
3
4
5
6
In [16]: b = tf.constant([0,1])
In [17]: tf.cast(b,dtype=tf.bool)
Out[17]: <tf.Tensor: shape=(2,), dtype=bool, numpy=array([False, True])>

In [18]: bb=tf.cast(b,dtype = tf.int32)
#<tf.Tensor: shape=(2,), dtype=int32, numpy=array([0, 1], dtype=int32)>

tf.Variable类型

专门为神经网络设计的参数,针对可优化的参数,例如线性回归中的w和b,将Tensor包成Variable

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
In [20]: a = tf.range(5)
In [21]: b = tf.Variable(a,name = 'input_data')
In [22]: b.dtype
Out[22]: tf.int32
In [23]: b.name
Out[23]: 'input_data:0'
In [31]: b.trainable
Out[31]: True

In [27]: isinstance(b,tf.Tensor) #所以不推荐使用
Out[27]: False
In [29]: isinstance(a,tf.Tensor)
Out[29]: True
In [30]: tf.is_tensor(b)
Out[30]: True

在运行当中Tensor一般在GPU当中,但是如果要在cpu进行一些控制逻辑,则需要将Tensor转换为numpy类型

打赏
  • 版权声明: 本博客所有文章除特别声明外,均采用 Apache License 2.0 许可协议。转载请注明出处!

请我喝杯咖啡吧~

支付宝
微信