一、常见数据类型的载体,在python语言中list是一个非常灵活的数据载体,在list中间可以添加任何类型的数据比如:[1,1.2,"hellow",(1,2)],他们分别是整形,浮点型,字符型,元组。可以随意添加、删除,类似于链表的概念。
二、为了解决大数据的吞吐,提高存储效率,我们可以用numpy来存储相同类型的数据 np.array 是专门用来解决同类型的数据载体,可以很方便的存储比如[64,224,224,3]这样的图片数据,并对其进行一些处理比如转置、加减、乘除之类。但是numpy出现在深度学习之前,是一个科学计算库,没有很好地GPU计算支持也不能够支持自动求导这个时候TensorFlow就应运而生。
三、TensorFlow在功能方面比numpy稍微偏重一点,更加偏重于神经网络的计算,但它的一些基本方面比如拼接、分裂和numpy非常类似,为了减少初学者的难度,TensorFlow的API和numpy的API的命名都是刻意的相近的。
在TensorFlow中的数据载体叫tf.tensor.
what is tensor
- scalar: 1.1 2.2 5 这些称之为标量 ,它的维度(dimension)=0
- vector: [1.1],[1.1,2.2,......] 这些称之为向量,dim=1
- matrix: [[1.1,2.2],[3.3,4.4,5.5],[6.6,7.7]] 这称为矩阵,dim=2,
- Tensor:dim>2
- 在工程上以上的数据类型除了scalar都可以叫做Tensor。
https://playground.tensorflow.org
很多的数据,经过很多的操作,从输入到输出完成一个flow的过程。这就是TensorFlow的由来。
TF is computing lib
- int ,float, double
- bool
- string
我们来看创建不同类型的数据。
In [2]: import tensorflow as tf
G:\Users\hasee\Anaconda3\lib\site-packages\h5py\__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
from ._conv import register_converters as _register_converters
In [3]: tf.constant(1)
2019-07-25 16:37:02.809147: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
Out[3]: <tf.Tensor: id=0, shape=(), dtype=int32, numpy=1>
In [4]: tf.constant(1.)
Out[4]: <tf.Tensor: id=2, shape=(), dtype=float32, numpy=1.0>
In [5]: tf.constant(2.2, dtype=tf.int32)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-5-cf6ddbb50ed9> in <module>()
----> 1 tf.constant(2.2, dtype=tf.int32)
G:\Users\hasee\Anaconda3\lib\site-packages\tensorflow\python\framework\constant_op.py in constant(value, dtype, shape, name)
243 """
244 return _constant_impl(value, dtype, shape, name, verify_shape=False,
--> 245 allow_broadcast=True)
246
247
G:\Users\hasee\Anaconda3\lib\site-packages\tensorflow\python\framework\constant_op.py in _constant_impl(value, dtype, shape, name, verify_shape, allow_broadcast)
251 ctx = context.context()
252 if ctx.executing_eagerly():
--> 253 t = convert_to_eager_tensor(value, ctx, dtype)
254 if shape is None:
255 return t
G:\Users\hasee\Anaconda3\lib\site-packages\tensorflow\python\framework\constant_op.py in convert_to_eager_tensor(value, ctx, dtype)
108 return ops.EagerTensor(
109 value, handle, device, dtype, tensor)
--> 110 t = ops.EagerTensor(value, handle, device, dtype)
111 scalar_cache[cache_key] = t
112 return t
TypeError: Cannot convert provided value to EagerTensor. Provided value: 2.2 Requested dtype: int32
In [6]: tf.constant(2.2, dtype=tf.double)
Out[6]: <tf.Tensor: id=5, shape=(), dtype=float64, numpy=2.2>
In [7]: tf.constant([True, False])
Out[7]: <tf.Tensor: id=7, shape=(2,), dtype=bool, numpy=array([ True, False])>
In [2]: tf.constant('hellow,world')
Out[2]: <tf.Tensor: id=0, shape=(), dtype=string, numpy=b'hellow,world'>
tf.constant 的含义是创建一个常量, 可以理解为一个普通的tensor,这个常量的类型是根据你给的参数决定的,在定义的时候可以将float升级为double型但不可以将float降为int型。
将tensor转化为numpy,ndim是获得它的维度,rank获得变量的详细信息
In [3]: b=tf.range(4)
In [4]: b.numpy()
Out[4]: array([0, 1, 2, 3])
In [5]: b.shape
Out[5]: TensorShape([4])
In [6]: b.ndim
Out[6]: 1
In [7]: tf.rank(b)
Out[7]: <tf.Tensor: id=7, shape=(), dtype=int32, numpy=1>
检查一个变量是不是tensor,和它的数据类型
In [8]: tf.is_tensor(b)
Out[8]: True
In [9]: b.dtype
Out[9]: tf.int32
numpy型数据转化成tensor型数据的两种方法:一种是tf.conver_to_tensor()
另一种是cast(,)
import numpy as np
In [13]: a=np.arange(5)
In [14]: a.dtype
Out[14]: dtype('int32')
In [15]: aa=tf.convert_to_tensor(a)
In [16]: aa
Out[16]: <tf.Tensor: id=9, shape=(5,), dtype=int32, numpy=array([0, 1, 2, 3, 4])>
In [17]: aa=tf.convert_to_tensor(a,dtype=tf.int64)
In [18]: aa
Out[18]: <tf.Tensor: id=11, shape=(5,), dtype=int64, numpy=array([0, 1, 2, 3, 4], dtype=int64)>
In [19]: tf.cast(aa,dtype=tf.float32)
Out[19]: <tf.Tensor: id=13, shape=(5,), dtype=float32, numpy=array([0., 1., 2., 3., 4.], dtype=float32)>
In [20]: aaa=tf.cast(aa,dtype=tf.double)
In [21]: aaa
Out[21]: <tf.Tensor: id=15, shape=(5,), dtype=float64, numpy=array([0., 1., 2., 3., 4.])>
In [22]: tf.cast(aaa,dtype=tf.int32)
Out[22]: <tf.Tensor: id=17, shape=(5,), dtype=int32, numpy=array([0, 1, 2, 3, 4])>
常见的bool转换成int
b=tf.constant([0,1])
In [4]: b
Out[4]: <tf.Tensor: id=0, shape=(2,), dtype=int32, numpy=array([0, 1])>
In [5]: tf.cast(b,dtype=tf.bool)
Out[5]: <tf.Tensor: id=2, shape=(2,), dtype=bool, numpy=array([False, True])>
In [6]: bb=tf.cast(b,dtype=tf.bool)
In [7]: bb
Out[7]: <tf.Tensor: id=4, shape=(2,), dtype=bool, numpy=array([False, True])>
tf.cast(bb,tf.int32)
Out[10]: <tf.Tensor: id=6, shape=(2,), dtype=int32, numpy=array([0, 1])>
tf.variable
专门神经网络的参数所设定的数据类型,为了更好地跟踪变量的信息,求导,和更新
In [11]: a=tf.range(5)
In [12]: b=tf.Variable(a)
In [13]: b.name
Out[13]: 'Variable:0'
In [14]: b.trainable
Out[14]: True
In [15]: tf.is_tensor(b)
Out[15]: True