排序函式tf.sort()
用法:
tf.sort
( values,
axis=-1
, direction=
'ascending'
, name=
none
)
引數說明:
排序的座標tf.argsort()
返回張量的索引,該張量給出沿軸的排序順序。
用法:
tf.argsort
( values,
axis=-1
, direction=
'ascending'
, stable=
false
, name=
none
)
示例:
1)一維tensor
# 打亂序列
a = tf.random.shuffle(tf.
range(5
))# numpy=array([4, 3, 2, 0, 1])
tf.sort(a, direction =
'descending'
)# 按降序排列
out[90]
:,), dtype=int32, numpy=array([4
,3,2
,1,0
])>
tf.argsort(a, direction =
'descending'
)# 降序後,每個元素對應的位置
out[91]
:,), dtype=int32, numpy=array([0
,1,2
,4,3
])>
idx = tf.argsort(a, direction =
'descending'
)tf.gather(a, idx)
out[93]
:,), dtype=int32, numpy=array([4
,3,2
,1,0
])>
2)多維tensor
a = tf.random.uniform([3
,3], maxval =
10, dtype = tf.int32)
##array([[2, 1, 5],
## [1, 7, 5],
## [3, 6, 3]])>
tf.sort(a)
# 預設公升序排列
out[97]
:,3), dtype=int32, numpy=
array([[
1,2,
5],[
1,5,
7],[
3,3,
6]])
>
tf.sort(a, direction =
'descending'
)out[98]
:,3), dtype=int32, numpy=
array([[
5,2,
1],[
7,5,
1],[
6,3,
3]])
>
idx = tf.argsort(a)
array([[
1,0,
2],[
0,2,
1],[
0,2,
1]])
>
返回最內層一維(也就是最後一維)的前k個最大的元素,以及它所對應的索引。
tf.math.top_k
(input
, k=1,
sorted
=true
, name=
none
)
示例:
a = tf.constant([[
4,6,
8],[
9,4,
7],[
4,5,
1]])
res.indices # 排序後的前兩個位置索引
out[
105]
:,2), dtype=int32, numpy=
array([[
2,1]
,[0,
2],[
1,0]
])>
res.values # 排序後的前兩個的值
out[
106]
:,2), dtype=int32, numpy=
array([[
8,6]
,[9,
7],[
5,4]
])>
Tensorflow2 0張量生成
tensorflow 1 tensor就是張量,多維陣列,多維列表,階表示張量的維數 dimension 維數階 名字例子 0 d標量scalar s 1 2 3 1 d向量vector v 1,2,3 2 d矩陣matrix m 1,2,3 4,5,6 7,8,9 n dn 張量tensor t ...
tensorflow2 1安裝指南
開啟anconda prompt 建立conda虛擬環境 用create n 新建乙個名叫tf2.1的環境用python3.7版本 conda create n tf2.1 python 3.7 進入tensorflow2.1環境 conda activate tf2.1 安裝英偉達的sdk10.1...
tensorflow犯錯記錄1(張量使用)
舉例 1 少了,號分隔符 錯誤 結果如下 張量的形狀 import tensorflow as tf tens1 tf.constant 1,2,2 2,2,3 3,5,6 5,4,3 7,0,1 9,1,9 11,12,7 1,3,14 name tens1 語句中包含 或 括號,中間換行的就不需...