Tensorflow2.0学习笔记-常用函数(二)

常用函数(二)

6.enumerate枚举函数

enumerate是python的内建函数,他可以遍历整个数组,组合为:索引,元素。常在for循环中使用。

seq = ['one', 'two', 'there']
for i, element in enumerate(seq):
    print(i, element)
#输出结果
0 one
1 two
2 there
7.one_hot独热码函数

函数介绍:

tf.one_hottf.onehot(indices ,#表示输入的数据
                    depth,#分类数
                    on_value=None,#默认值为1,可输入值进行替换
                    off_value=None,#默认值为0,可输入值进行替换,其中,on_value和off_value的数据类型要相同。
                    axis=None,#用于输出shape的选择
                    dtype=None,#默认值为tf.float32
                    name=None)

函数可以将数据转换成独热码形式的张量进行输出,输出规则如下。

#当输入为N维张量,输出为N+1维张量,多出的维度由axis的值决定,默认值为-1
#当输入为标量时,输出为一个长度为depth的向量
classes = 3
labels = 2
output = tf.one_hot(labels, depth=classes, off_value=2)
#输出结果
tf.Tensor([2 2 1], shape=(3,), dtype=int32)

#当输入为一个长度为features的向量时,axis=-1,输出为features*depth
classes = 3
labels = (1, 0, 2, -1)
output = tf.one_hot(labels, depth=classes, axis=-1)
#输出结果
tf.Tensor(
[[0. 1. 0.] #对位置0的onehot编码
 [1. 0. 0.] #对位置1的onehot编码
 [0. 0. 1.] #对位置2的onehot编码
 [0. 0. 0.]] #对位置3的onehot编码
 , shape=(4, 3), dtype=float32)
 
#axis=0,输出depth*features
classes = 3
labels = [1, 0, 2, -1]
output = tf.one_hot(labels, depth=classes, axis=0)
#输出结果
tf.Tensor(
[[0. 1. 0. 0.]#对位置0的onehot编码
 [1. 0. 0. 0.]#对位置1的onehot编码
 [0. 0. 1. 0.]]#对位置2的onehot编码
 , shape=(3, 4), dtype=float32)

#当输入是一个[batch, features]的矩阵,axis=-1时(默认),输出batch * features * depth的矩阵
#axis=1时,输出batch * depth * features的矩阵
#axis=0时,输出depth * batch * features的矩阵
classes = 3
labels = [[1, 2],
          [0, -1]]
output = tf.one_hot(labels, depth=classes, axis=0)
#输出结果:
tf.Tensor(
[[[0. 0.]
  [1. 0.]]

 [[1. 0.]
  [0. 0.]]

 [[0. 1.]
  [0. 0.]]], shape=(3, 2, 2), dtype=float32)
8.nn.softmax概率分布函数

使N个输入的N个输出符合概率分布

Input = tf.constant([0., 1., 3.])
output = tf.nn.softmax(Input)
#输出结果
tf.Tensor([0.04201007 0.1141952  0.8437947 ], shape=(3,), dtype=float32)
9.assign自更新函数

自更新函数所操作的量,必须时被Variable标记的可训练的量。

w = tf.Variable(4)
w.assign_sub(1)
print(w)
w.assign_add(2)
print(w)

#输出结果
<tf.Variable 'Variable:0' shape=() dtype=int32, numpy=3>
<tf.Variable 'Variable:0' shape=() dtype=int32, numpy=5>
10.argmax返回最大索引函数

可以返回沿指定维度寻找最大值的索引,tf.argmax(张量,axis=操作轴)

test = tf.constant([[1, 2, 3], [2, 3, 4], [5, 4, 3], [8, 7, 2]])
print(test)
print(tf.argmax(test, axis=1)) # 横向
print(tf.argmax(test, axis=0)) # 纵向
#输出结果
tf.Tensor(
[[1 2 3]
 [2 3 4]
 [5 4 3]
 [8 7 2]], shape=(4, 3), dtype=int32)
tf.Tensor([2 2 0 0], shape=(4,), dtype=int64)
tf.Tensor([3 3 1], shape=(3,), dtype=int64)
11.tf.where条件函数

使用 tf.where(条件语句, A, B)函数进行条件判断,如果条件为真,则返回A,否则,则返回B

a = tf.constant([1, 2, 3, 1, 1])
b = tf.constant([0, 1, 3, 4, 5])
# 使用greater函数对a,b 中的元素进行比较,如果a大则返回真,否则为假
c = tf.where(tf.greater(a, b), a, b)
print("c:", c)
#输出结果:
c: tf.Tensor([1 2 3 4 5], shape=(5,), dtype=int32)
12.np.random.RandomState函数

可以使用np.random.RandomState.rand()产生区间为[0, 1)之间的随机数

rdm = np.random.RandomState(seed=1) 
a = rdm.rand()     # 括号内表示维度,维度为空返回一个随机标量
b = rdm.rand(2, 3)  # 返回维度为2行3列的随机数矩阵

print("a:", a)
print("b:", b)
#输出结果:
a: 0.417022004702574
b: [[7.20324493e-01 1.14374817e-04 3.02332573e-01]
 [1.46755891e-01 9.23385948e-02 1.86260211e-01]]
13.np.vstack数组叠加函数

使用np.vstack()函数可以将两个数组进行垂直方向的叠加

a1 = tf.constant([1, 2, 3])
b = np.array([4, 5, 6])
c = np.vstack((a1.numpy(), b))
print("c:", c)
#输出结果
c: [[1 2 3]
 [4 5 6]]
14.np.mgrid[ ], .ravel()和np_c[ ]函数

np.mgrid[起始值:结束值:步长,起始值:结束值:步长,…]
x.ravel() 将x拉升为1维数组
np_c[数组1,数组2],将两个数组中的数值点进行配对

z = np.mgrid[1:3:1, 3:4:0.5]
x = z[0]
y = z[1]
print("z:", z)
grid = np.c_[x.ravel(), y.ravel()]
print("x:", x)
print("y:", y)
print("grid:\n", grid)
#输出结果
z: [[[1.  1. ]
  [2.  2. ]]

 [[3.  3.5]
  [3.  3.5]]]
x: [[1. 1.]
 [2. 2.]]
y: [[3.  3.5]
 [3.  3.5]]
grid:
 [[1.  3. ]
 [1.  3.5]
 [2.  3. ]
 [2.  3.5]]

Process finished with exit code 0
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值