【TensorFlow】神经网络优化过程中常用的函数

tf.where

条件语句真返回A,条件语句假返回B

tf.where(条件语句,真返回A,假返回B)
a=tf.constant([1,2,3,1,1])
b=tf.constant([0,1,3,4,5])
c=tf.where(tf.greater(a,b), a, b) # 若a>b,返回a对应位置的元素,否则返回b对应位置的元素
print("c:",c)

运行结果

c:tf.Tensor([1 2 3 4 5], shape=(5,), dtype=int32)

np.random.RandomState.rand()

返回一个[0,1)之间的随机数

np.random.RandomState.rand(维度)# 维度为空,返回标量
import numpyas 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.vstack

将两个数组按垂直方向叠加

np.vstack(数组1,数组2)
import numpyas np
a = np.array([1,2,3])
b = np.array([4,5,6])
c = np.vstack((a,b))
print("c:\n",c)
运行结果:
c:
[[1 2 3]
[4 5 6]]

网格

np.mgrid[ ]

返回间隔数值点,可同时返回多组。

其范围[起始值,结束值)

np.mgrid[ 起始值: 结束值: 步长,起始值: 结束值: 步长,]

x.ravel( )

将x变为一维数组,“把.前变量拉直”

np.c_[ ]

使返回的间隔数值点配对

np.c_[ 数组1,数组2,… ]
import numpy as np
import tensorflow as tf

# 生成等间隔数值点
x, y = np.mgrid[1:3:1, 2:4:0.5]
# 将x, y拉直,并合并配对为二维张量,生成二维坐标点
grid = np.c_[x.ravel(), y.ravel()]
print("x:\n", x)
print("y:\n", y)
print("x.ravel():\n", x.ravel())
print("y.ravel():\n", y.ravel())
print('grid:\n', grid)

运行结果

x:
 [[1. 1. 1. 1.]
 [2. 2. 2. 2.]]
y:
 [[2.  2.5 3.  3.5]
 [2.  2.5 3.  3.5]]
x.ravel():
 [1. 1. 1. 1. 2. 2. 2. 2.]
y.ravel():
 [2.  2.5 3.  3.5 2.  2.5 3.  3.5]
grid:
 [[1.  2. ]
 [1.  2.5]
 [1.  3. ]
 [1.  3.5]
 [2.  2. ]
 [2.  2.5]
 [2.  3. ]
 [2.  3.5]]

Process finished with exit code 0

猜你喜欢

转载自blog.csdn.net/qq_45654306/article/details/112731881
今日推荐