赞
踩
在TensorFlow中的数据载体叫tf.tensor.
https://playground.tensorflow.org
很多的数据,经过很多的操作,从输入到输出完成一个flow的过程。这就是TensorFlow的由来。
TF is computing lib
我们来看创建不同类型的数据。
- 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
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。