tensorflow2.0学习笔记第二章第一节
2.1预备知识
# 条件判断tf.where(条件语句,真返回A,假返回B) import tensorflow as tf a = tf.constant([1,2,3,1,1]) b = tf.constant([0,1,2,4,5]) c = tf.where(tf.greater(a,b),a,b) # 返回张量中比较大的元素 print(c)
tf.Tensor([1 2 3 4 5], shape=(5,), dtype=int32)
# 返回[0,1)之间的随机数 import numpy as np rdm = np.random.RandomState(seed=1) # seed=常数,每次生成的随机数相同 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]]
# np.stack((数组一,数组二))将两个数组按垂直方向叠加 a = np.array([1,2,3]) b = np.array([4,5,6]) c = np.vstack((a,b)) print(c)
[[1 2 3] [4 5 6]]
# np.mgrid[起始值:结束值:步长,起始值:结束值:步长]输出一个两行四列的张量 # x.ravel() 将x展平为一维数组 # np.c_[数组1,数组2,。。。] 是返回的间隔数值点配对 x,y = np.mgrid[-3:3:1,-3:3:1] grid = np.c_[x.ravel(),y.ravel()] print("x:",x) print("y:",y) print("grid:",grid)
x: [[-3 -3 -3 -3 -3 -3] [-2 -2 -2 -2 -2 -2] [-1 -1 -1 -1 -1 -1] [ 0 0 0 0 0 0] [ 1 1 1 1 1 1] [ 2 2 2 2 2 2]] y: [[-3 -2 -1 0 1 2] [-3 -2 -1 0 1 2] [-3 -2 -1 0 1 2] [-3 -2 -1 0 1 2] [-3 -2 -1 0 1 2] [-3 -2 -1 0 1 2]] grid: [[-3 -3] [-3 -2] [-3 -1] [-3 0] [-3 1] [-3 2] [-2 -3] [-2 -2] [-2 -1] [-2 0] [-2 1] [-2 2] [-1 -3] [-1 -2] [-1 -1] [-1 0] [-1 1] [-1 2] [ 0 -3] [ 0 -2] [ 0 -1] [ 0 0] [ 0 1] [ 0 2] [ 1 -3] [ 1 -2] [ 1 -1] [ 1 0] [ 1 1] [ 1 2] [ 2 -3] [ 2 -2] [ 2 -1] [ 2 0] [ 2 1] [ 2 2]]