Tensorflow数据类型

tensorflow数据类型tf.Tensor:

int,float,double,bool,string

import  tensorflow as tf 

eg:

tf.constant(1)
<tf.Tensor: shape=(), dtype=int32, numpy=1>
tf.constant(1.)
<tf.Tensor: shape=(), dtype=float32, numpy=1.0>
tf.constant(2., dtype = tf.double)
<tf.Tensor: shape=(), dtype=float64, numpy=2.0>
tf.constant([True, False])
<tf.Tensor: shape=(2,), dtype=bool, numpy=array([ True, False])>
tf.constant('hello world')
<tf.Tensor: shape=(), dtype=string, numpy=b'hello world'>
b = tf.range(4)
<tf.Tensor: shape=(4,), dtype=int32, numpy=array([0, 1, 2, 3])>
b.numpy()
array([0, 1, 2, 3])
b.ndim
1
tf.rank(b)
<tf.Tensor: shape=(), dtype=int32, numpy=1>
a = tf.rank(tf.ones([3,4,2]))
<tf.Tensor: shape=(), dtype=int32, numpy=3>
isinstance(a, tf.Tensor)
True
tf.is_tensor(b)
True
b.dtype == tf.int32/tf.float32/tf.bool....

数据类型转换numpy等<>Tensor

a = numpy.arange(5)
array([0, 1, 2, 3, 4])
a.dtype
dtype('int32')
aa = tf.convert_to_tensor(a)
<tf.Tensor: shape=(5,), dtype=int32, numpy=array([0, 1, 2, 3, 4])>
aa = tf.convert_to_tensor(a, dtype = tf.int32)
<tf.Tensor: shape=(5,), dtype=int32, numpy=array([0, 1, 2, 3, 4])>
tf.convert_to_tensor(numpy.ones([2, 3]))
<tf.Tensor: shape=(2, 3), dtype=float64, numpy=
array([[1., 1., 1.],
       [1., 1., 1.]])>
tf.convert_to_tensor(numpy.zeros([2, 3]))
<tf.Tensor: shape=(2, 3), dtype=float64, numpy=
array([[0., 0., 0.],
       [0., 0., 0.]])>
tf.convert_to_tensor([2, 3])
<tf.Tensor: shape=(2,), dtype=int32, numpy=array([2, 3])>
tf.convert_to_tensor([2., 3])
tf.convert_to_tensor([[2], [3.]])
<tf.Tensor: shape=(2, 1), dtype=float32, numpy=
array([[2.],
       [3.]], dtype=float32)>

Tensor数据类型内部转换:

tf.cast(aa, dtype = tf.float32 )
<tf.Tensor: shape=(5,), dtype=float32, numpy=array([0., 1., 2., 3., 4.], dtype=float32)>
aaa = tf.cast(aa, dtype = tf.double )
<tf.Tensor: shape=(5,), dtype=float64, numpy=array([0., 1., 2., 3., 4.])>
tf.cast(aaa, dtype = tf.int32)
<tf.Tensor: shape=(5,), dtype=int32, numpy=array([0, 1, 2, 3, 4])>
b = tf.constant([0, 1])
bb = tf.cast(b, dtype=tf.bool)
<tf.Tensor: shape=(2,), dtype=bool, numpy=array([False,  True])>
tf.cast(bb, dtype = tf.int32)
<tf.Tensor: shape=(2,), dtype=int32, numpy=array([0, 1])>
a = tf.ones([])
<tf.Tensor: shape=(), dtype=float32, numpy=1.0>
a.numpy()
1.0
int(a)
1
float(a)
1.0

Variable类型

a = tf.range(5)
<tf.Tensor: shape=(5,), dtype=int32, numpy=array([0, 1, 2, 3, 4])>
b = tf.Variable(a, name = 'input_name')
<tf.Variable 'input_name:0' shape=(5,) dtype=int32, numpy=array([0, 1, 2, 3, 4])>
b.name
'input_name:0'
b.trainable
True
tf.is_tensor(b)
True
isinstance(b, tf.Variable)
True
b.numpy()
array([0, 1, 2, 3, 4])

tf.zeros

tf.zeros([])
<tf.Tensor: shape=(), dtype=float32, numpy=0.0>
tf.zeros([1])
<tf.Tensor: shape=(1,), dtype=float32, numpy=array([0.], dtype=float32)>
tf.zeros([1, 6])
a = tf.zeros([1, 6, 5])
<tf.Tensor: shape=(1, 6, 5), dtype=float32, numpy=
array([[[0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0.]]], dtype=float32)>
tf.zeros_like(a)
<tf.Tensor: shape=(1, 6, 5), dtype=float32, numpy=
array([[[0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0.]]], dtype=float32)>

tf.ones

tf.ones(1)
<tf.Tensor: shape=(1,), dtype=float32, numpy=array([1.], dtype=float32)>
tf.ones([])
<tf.Tensor: shape=(), dtype=float32, numpy=1.0>
tf.ones([2])
<tf.Tensor: shape=(2,), dtype=float32, numpy=array([1., 1.], dtype=float32)>
a = tf.ones([2,3])
tf.ones_like(a)
<tf.Tensor: shape=(2, 3), dtype=float32, numpy=
array([[1., 1., 1.],
       [1., 1., 1.]], dtype=float32)>

tf.fill

tf.fill([2,2], 0)
<tf.Tensor: shape=(2, 2), dtype=int32, numpy=
array([[0, 0],
       [0, 0]])>

tf.fill([2,2], 1)
<tf.Tensor: shape=(2, 2), dtype=int32, numpy=
array([[1, 1],
       [1, 1]])>

tf.fill([2,2], 8)
<tf.Tensor: shape=(2, 2), dtype=int32, numpy=
array([[8, 8],
       [8, 8]])>

Normal初始化

tf.random.normal([2,2], mean =1, stddev =1)
<tf.Tensor: shape=(2, 2), dtype=float32, numpy=
array([[-0.01399314,  0.52268547],
       [-0.09143722,  2.2820375 ]], dtype=float32)>
tf.random.normal([2,2])
<tf.Tensor: shape=(2, 2), dtype=float32, numpy=
array([[ 0.37584627, -0.83630717],
       [ 0.8441107 , -0.7528748 ]], dtype=float32)>
#截断正态分布
tf.random.truncated_normal([2,2], mean=0, stddev=1)
<tf.Tensor: shape=(2, 2), dtype=float32, numpy=
array([[-0.6675387 ,  0.69668263],
       [ 0.10024793,  0.0487113 ]], dtype=float32)>

Uniform初始化,均匀分布

tf.random.uniform([2,2], minval=0, maxval =1)
<tf.Tensor: shape=(2, 2), dtype=float32, numpy=
array([[0.13525379, 0.9151571 ],
       [0.3170817 , 0.78060853]], dtype=float32)>
tf.random.uniform([2,2], minval=1, maxval =100)
<tf.Tensor: shape=(2, 2), dtype=float32, numpy=
array([[33.672283, 42.952175],
       [23.742775, 43.31634 ]], dtype=float32)>

应用:

idx = tf.range(10)
idx = tf.random.shuffle(idx)
<tf.Tensor: shape=(10,), dtype=int32, numpy=array([2, 6, 0, 4, 3, 9, 8, 1, 5, 7])>
b = tf.random.uniform([10], maxval =10, dtype = tf.int32)
<tf.Tensor: shape=(10,), dtype=int32, numpy=array([9, 3, 6, 2, 3, 5, 2, 2, 8, 5])>
#打散后根据打散的idx,重新编排b
b = tf.gather(b,idx)
<tf.Tensor: shape=(10,), dtype=int32, numpy=array([6, 2, 9, 3, 2, 5, 8, 3, 5, 2])>

标量的典型应用:

out = tf.random.uniform([4,10])
<tf.Tensor: shape=(4, 10), dtype=float32, numpy=
array([[0.72310257, 0.17787945, 0.48778737, 0.2833053 , 0.09322917,
        0.30319488, 0.95172286, 0.01841676, 0.23138034, 0.42098236],
       [0.5976453 , 0.9707862 , 0.15076113, 0.98200226, 0.42302537,
        0.39767098, 0.56338763, 0.4590268 , 0.94290924, 0.908659  ],
       [0.99827015, 0.9411502 , 0.54296076, 0.6086023 , 0.20918679,
        0.4857303 , 0.37843978, 0.18378937, 0.8666023 , 0.97962904],
       [0.31089926, 0.8760989 , 0.74259734, 0.11853445, 0.74546754,
        0.1520915 , 0.75031424, 0.7914462 , 0.5480534 , 0.17335129]],
      dtype=float32)>

y = tf.range(4)
y = tf.one_hot(y,depth=10)
<tf.Tensor: 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)>

#平方差求和
loss = tf.keras.losses.mse(y, out)
<tf.Tensor: shape=(4,), dtype=float32, numpy=array([0.16640091, 0.39250335, 0.46289498, 0.42912656], dtype=float32)>

loss = tf.reduce_mean(loss)
<tf.Tensor: shape=(), dtype=float32, numpy=0.36273146>

向量的典型应用

net = tf.keras.layers.Dense(10)
net.build(4, 8)
net.kernel
<tf.Variable 'kernel:0' shape=(8, 10) dtype=float32, numpy=
array([[-0.19487035, -0.35632044,  0.493037  ,  0.19505286, -0.5533646 ,
         0.35682672, -0.22376922, -0.22088751,  0.4322039 ,  0.42164177],
       [-0.1608974 , -0.09800559, -0.03395259,  0.50997865, -0.11398593,
        -0.37929505,  0.24511272,  0.43068767,  0.25773418, -0.36935788],
       [ 0.28312093, -0.23296624,  0.49434614, -0.5310584 , -0.08502951,
         0.5257757 , -0.566297  , -0.5138498 ,  0.33717442, -0.16711757],
       [ 0.25932544, -0.46989506, -0.40796143, -0.3785271 , -0.45649415,
         0.4810108 ,  0.11100286, -0.06313211,  0.09695095, -0.28344342],
       [ 0.32550502, -0.14006212, -0.36269492,  0.2210418 , -0.32522437,
         0.15863937,  0.52616024,  0.33934873, -0.09300956, -0.47067016],
       [ 0.18515575, -0.0716548 ,  0.45634592, -0.37529066,  0.47605145,
         0.5186825 , -0.36287743,  0.02849567, -0.14803103,  0.23635453],
       [-0.17680448,  0.3378464 , -0.03016818,  0.07350922, -0.34339306,
         0.54447865, -0.41977853, -0.24240002, -0.41721106, -0.42745242],
       [-0.26714113, -0.28684258, -0.28709695, -0.08227292,  0.32058275,
        -0.02927589,  0.48515797, -0.3551976 , -0.4480523 ,  0.09974605]],
      dtype=float32)>

net.kias
<tf.Variable 'bias:0' shape=(10,) dtype=float32, numpy=array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], dtype=float32)>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值