TensorFlow2學習-3

預備知識:

  • tf.where()

條件語句真返回A,條件語句假返回B

import tensorflow as tf

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)

在這裏插入圖片描述

  • np.random.RandomState.rand()

返回一個[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)

在這裏插入圖片描述

  • np.vstack()

將兩個屬組按垂直方向疊加

import numpy as np

a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
c = np.vstack((a, b))
print(c)

在這裏插入圖片描述

  • np.mgrid[] .ravel() np.c_[]

np.mgrid[起始值:結束值:步長,起始值:結束值:步長,…]

x.ravel() 將x變爲一維數組 相當於把.前變量拉直

np.c_[] 使返回的間隔數值點配對

np.c_[數組1,數組2,…]

import numpy as np

x, y = np.mgrid[1:3:1, 2:4:0.5]
grid = np.c_[x.ravel(), y.ravel()]
print("x:", x)
print("y:", y)
print('grid:\n', grid)

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章