Functions in commonly used numpy libraries used in conjunction with tensorflow

"""
tf.where() 条件语句为真返回A,条件语句假返回B
tf.where(条件语句,真返回A,假返回B)
"""
import tensorflow as tf
import numpy as np
a=tf.constant([2,2,3,7,9])
b=tf.constant([1,1,3,4,5])#张量维度必须相同
#b=tf.constant([1,2,3,4,5,6])#会报错,因为比第一个张量多了一个元素
c=tf.where(tf.greater(a,b),a,b)#若a>b,返回a对应位置的元素,否则返回b对应位置的元素
print('c:',c)
#np.random.RandomState.rand()返回一个[0,1)之间的随机数
#np.random.RandomState.rand(维度) 维度为空,返回标量
rdm=np.random.RandomState(seed=116)#seed=常数,每次生成随机数相同
a=rdm.rand()
b=rdm.rand(2,3)#返回2维度为2行3列的随机数矩阵
print('a:',a)
print('b:',b)
#np.vstack()将两个数组按垂直方向叠加(而不是相加) np.vstack(数组1,数组2)
a=np.array([1,2,3])
b=np.array([4,5,6])
c=np.vstack((a,b))
print('c:',c)
#np.mgrid[]  mgrid返回若干组维度相同的等差数组
#np.mgrid[起始值:结束之:步长,起始值:结束值:步长,....][起始值 结束值)起始值和结束值是左闭右开
x,y=np.mgrid[3:5:1,2:6:0.5]
print(f'x={x},y={y}')
#x.ravel() 将x变为一维数组,相当于把x拉直
x1=x.ravel()
y1=y.ravel()
print(f'ravel()后的x:{x1},ravel()后的:{y1}')
#np.c_[]使返回的间隔数值点配对
grid=np.c_[x1,y1]
print(grid)

The results are as follows:
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_45187794/article/details/108053459