**
一 Tensorflow的类型
**
--int, float, double
--bool
--tring
In [1]: import tensorflow as tf
In [2]: import numpy as np
In [3]: tf.constant(1)#常量1,其数据可以更改
Out[3]: <tf.Tensor: id=0, shape=(), dtype=int32, numpy=1>
In [4]: tf.constant(1.)#常量1.0,数据可以更改,为float型
Out[4]: <tf.Tensor: id=2, shape=(), dtype=float32, numpy=1.0>
In [5]: tf.constant(2.2,dtype=tf.int32)#强制将float型设置为int型,会报错
TypeError: Cannot convert provided value to EagerTensor. Provided value: 2.2 Requested dtype: int32
In [6]: tf.constant(2.,dtype=tf.double)#将float可以强制设置为double型
Out[6]: <tf.Tensor: id=5, shape=(), dtype=float64, numpy=2.0>
In [7]: tf.constant([True,False])#bool型变量
Out[7]: <tf.Tensor: id=7, shape=(2,), dtype=bool, numpy=array([ True, False])>
In [8]: tf.constant('hello world!')#string类型变量
Out[8]: <tf.Tensor: id=9, shape=(), dtype=string, numpy=b'hello world!'>
**
二 Tensorflow的常用属性
**
--a.device#查看变量在哪个设备上
--aa = a.gpu()#将cpu上的变量a复制到gpu上
--aa = a.cpu()#将gpu上的变量a复制到cpu上
--a.shape#返回变量a的形状
--a.ndim#返回变量a的维度
--a.numpy()#将tensor数据类型转换为numpy
In [1]: import tensorflow as tf
In [2]: import numpy as np
In [3]: with tf.device("cpu"):a=tf.constant([1])
In [4]: a.device#返回当前变量所在设备的名字
Out[4]: '/job:localhost/replica:0/task:0/device:CPU:0'
In [5]: with tf.device("gpu"):b=tf.constant([1.])#未安装gpu会报错
RuntimeError: Error copying tensor to device: /job:localhost/replica:0/task:0/device:GPU:0. /job:localhost/replica:0/task:0/device:GPU:0 unknown device.
In [6]: aa = a.gpu()#a在cpu上面,可以通过gpu()方法将变量a复制到gpu上面,此处会报错
...: ,因为未安装gpu设备
RuntimeError: Error copying tensor to device: GPU:0. GPU:0 unknown device.
In [7]: a.numpy()#将tensor数据类型转换为numpy数据类型
Out[7]: array([1], dtype=int32)
In [8]: a.shape #返回一个tensor的形状
Out[8]: TensorShape([1])
In [9]: a.ndim#返回一个tensor的维度
Out[9]: 1
In [10]: tf.rank(a)#返回一个tensor张量的秩
Out[10]: <tf.Tensor: id=3, shape=(), dtype=int32, numpy=1>
**
三 检查对象的数据类型
**
–isinstance(a,tf.Tensor) #判断a是否为tensor
– tf.is_tensor(b)#判断b是否为tensor
–a.dtype#返回数据类型
In [1]: import tensorflow as tf
In [2]: import numpy as np
In [3]: a = tf.constant([1.])#float32类型的tensor
In [4]: b = tf.constant([True,False])#bool类型的tensor
In [5]: c = tf.constant('Hello World')#string类型的tensor
In [6]: d = np.arange(4) #numpy类型
In [7]: isinstance(a,tf.Tensor) #判断a是否为tensor
Out[7]: True
In [8]: tf.is_tensor(b)#判断b是否为tensor
Out[8]: True
In [9]: tf.is_tensor(d) #判断d是否为tensor
Out[9]: False
In [10]: a.dtype,b.dtype,c.dtype#返回数据类型
Out[10]: (tf.float32, tf.bool, tf.string)
In [11]: a.dtype == tf.float32
Out[11]: True
In [12]: c.dtype == tf.string
Out[12]: True
**
四 数据类型的相互转换
**
–aa = tf.convert_to_tensor(a,dtype = tf.int32) #将numpy转换为tensor并指定数据类型为int32
–aaa = tf.cast(aa,dtype = tf.double)#张量数据类型转换为double
In [1]: import tensorflow as tf
In [2]: import numpy as np
In [3]: a = np.arange(5)#a为numpy类型的数组,其dtype为int64
In [4]: a
Out[4]: array([0, 1, 2, 3, 4])
In [5]: a.dtype
Out[5]: dtype('int64')
In [6]: aa = tf.convert_to_tensor(a)#将numpy转换为tensor,其dtype与a默认一致
In [7]: aa
Out[7]: <tf.Tensor: id=0, shape=(5,), dtype=int64, numpy=array([0, 1, 2, 3, 4])>
In [9]: aa = tf.convert_to_tensor(a,dtype = tf.int32) #指定数据类型为int32
In [10]: aa
Out[10]: <tf.Tensor: id=2, shape=(5,), dtype=int32, numpy=array([0, 1, 2, 3, 4], dtype=int32)>
In [12]: tf.cast(aa,dtype=tf.float32)#张量数据类型转换为float32
Out[12]: <tf.Tensor: id=4, shape=(5,), dtype=float32, numpy=array([0., 1., 2., 3., 4.], dtype=float32)>
In [13]: aaa = tf.cast(aa,dtype = tf.double)#张量数据类型转换为double
In [14]: aaa
Out[14]: <tf.Tensor: id=6, shape=(5,), dtype=float64, numpy=array([0., 1., 2., 3., 4.])>
In [15]: tf.cast(aaa,dtype=tf.int32) #张量数据类型转换成int32
Out[15]: <tf.Tensor: id=8, shape=(5,), dtype=int32, numpy=array([0, 1, 2, 3, 4], dtype=int32)>
In [16]: b = tf.constant([0,1]) #int32类型的tensor
In [17]: b
Out[17]: <tf.Tensor: id=10, shape=(2,), dtype=int32, numpy=array([0, 1], dtype=int32)>
In [20]: bb = tf.cast(b,dtype=tf.bool) #转换为bool类型
In [21]: bb
Out[21]: <tf.Tensor: id=14, shape=(2,), dtype=bool, numpy=array([False, True])>
In [22]: tf.cast(bb,dtype=tf.int32) #转换回int32类型
Out[22]: <tf.Tensor: id=16, shape=(2,), dtype=int32, numpy=array([0, 1], dtype=int32)>
—tf.Variable()类型
In [1]: import tensorflow as tf
In [2]: import numpy as np
In [3]: a =tf.range(5)
In [4]: a
Out[4]: <tf.Tensor: id=3, shape=(5,), dtype=int32, numpy=array([0, 1, 2, 3, 4], dtype=int32)>
In [5]: b = tf.Variable(a)#对tensor类型a再进行Variable属性的包装
In [6]: b.dtype
Out[6]: tf.int32
In [7]: b.name
Out[7]: 'Variable:0'
In [8]: b=tf.Variable(a,name='input_data')
In [9]: b.name
Out[9]: 'input_data:0'
In [10]: b.trainable
Out[10]: True
In [11]: isinstance(b,tf.Tensor)
Out[11]: False
In [13]: isinstance(b,tf.Variable)
Out[13]: True
In [14]: tf.is_tensor(b)
Out[14]: True
In [15]: b.numpy()#Variable转换为numpy
Out[15]: array([0, 1, 2, 3, 4], dtype=int32)