tf建立tensor_创建Tensor

创建Tensor

* from numpy, list

* zeros, ones, fill

* random # if big dimension, random initial

* constant

* Application

numpy, list

numpy

import numpy as np

import tensorflow as tf

tf.convert_to_tensor(np.ones([2, 3]))

array([[1., 1., 1.],

[1., 1., 1.]])>

tf.convert_to_tensor(np.zeros([2, 3]))

array([[0., 0., 0.],

[0., 0., 0.]])>

list

tf.convert_to_tensor([1, 2])

tf.convert_to_tensor([1, 2.])

tf.convert_to_tensor([[1], [2.]])

array([[1.],

[2.]], dtype=float32)>

zeros, ones, fill

zeros

tf.zeros([])

tf.zeros([1])

tf.zeros([2, 2])

array([[0., 0.],

[0., 0.]], dtype=float32)>

tf.zeros([2, 3, 3])

array([[[0., 0., 0.],

[0., 0., 0.],

[0., 0., 0.]],

[[0., 0., 0.],

[0., 0., 0.],

[0., 0., 0.]]], dtype=float32)>

a = tf.constant([0])

tf.zeros_like(a) # 等同于tf.zeros(a.shape)

ones

tf.ones(1)

tf.ones([])

tf.ones([2])

tf.ones([2, 3])

array([[1., 1., 1.],

[1., 1., 1.]], dtype=float32)>

a = tf.constant([0])

tf.ones_like(a) # # 等同于tf.ones(a.shape)

fill

tf.fill([2, 2], 0)

array([[0, 0],

[0, 0]], dtype=int32)>

tf.fill([2, 2], 0)

array([[0, 0],

[0, 0]], dtype=int32)>

tf.fill([2, 2], 1)

array([[1, 1],

[1, 1]], dtype=int32)>

tf.fill([2, 2], 9)

array([[9, 9],

[9, 9]], dtype=int32)>

random

# 正态分布,均值为1,方差为1

tf.random.normal([2, 2], mean=1, stddev=1)

array([[1.0804566, 0.9318387],

[1.0620257, 0.6907253]], dtype=float32)>

tf.random.normal([2, 2])

array([[-0.06452972, 0.05704789],

[ 0.82857376, 0.71619517]], dtype=float32)>

# 截断的正态分布,

tf.random.truncated_normal([2, 2], mean=0, stddev=1)

array([[ 0.19161457, -0.820383 ],

[ 0.43668088, -0.3798696 ]], dtype=float32)>

如下图所示为截断正态分布,截掉红色部分,对新的正态分布重新采样。因为sigmoid的和新的正态分布不冲突的地方的区域,对于sigmoid函数来说是近似于平滑的直线,梯度为0,因此会有梯度消失。

o_01-%E6%88%AA%E6%96%AD%E6%AD%A3%E6%80%81%E5%88%86%E5%B8%83.jpg

# 均匀分布

tf.random.uniform([2, 2], minval=0, maxval=1)

array([[0.01481438, 0.15071952],

[0.5599004 , 0.59821343]], dtype=float32)>

tf.random.uniform([2, 2], minval=0, maxval=100, dtype=tf.int32)

array([[51, 9],

[10, 14]], dtype=int32)>

打乱idx后,a和b的索引不变

idx = tf.range(10)

idx = tf.random.shuffle(idx)

idx

a = tf.random.normal([10, 784])

b = tf.random.uniform([10], maxval=10, dtype=tf.int32)

b

a = tf.gather(a, idx)

b = tf.gather(b, idx)

b

constant

tf.constant(1)

tf.constant([1])

tf.constant([1, 2.])

tf.constant([[1, 2], [3., 2]])

array([[1., 2.],

[3., 2.]], dtype=float32)>

loss计算

无bias的loss

out = tf.random.uniform([4, 10])

out

array([[0.67733276, 0.2267617 , 0.21761227, 0.28679788, 0.68864655,

0.21349418, 0.5646602 , 0.8294822 , 0.22094071, 0.20246148],

[0.7940483 , 0.86402774, 0.78399694, 0.80085063, 0.01357341,

0.11889946, 0.89162886, 0.755934 , 0.8058628 , 0.40188062],

[0.115659 , 0.30951428, 0.39866602, 0.5358803 , 0.9163326 ,

0.47557557, 0.9397205 , 0.3110628 , 0.49839914, 0.34321547],

[0.5563061 , 0.78829396, 0.52705276, 0.29077685, 0.35033226,

0.9630101 , 0.338771 , 0.6301584 , 0.7393383 , 0.7073529 ]],

dtype=float32)>

y = tf.range(4)

y = tf.one_hot(y, depth=10)

y

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)

loss

loss = tf.reduce_mean(loss)

loss

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值