tf.gather:用乙個一維的索引陣列,將張量中對應索引的向量提取出來
1import
tensorflow as tf
23 a = tf.variable([[1,2,3,4,5], [6,7,8,9,10], [11,12,13,14,15]])
4 index_a = tf.variable([0,2])
56 b = tf.variable([1,2,3,4,5,6,7,8,9,10])
7 index_b = tf.variable([2,4,6,8])89
with tf.session() as sess:
10sess.run(tf.global_variables_initializer())
11print
(sess.run(tf.gather(a, index_a)))
12print
(sess.run(tf.gather(b, index_b)))
1314
#[[ 1 2 3 4 5]15#
[11 12 13 14 15]]
1617
#[3 5 7 9]
np.stack()
axis引數指定新軸在結果尺寸中的索引。指定的索引,是新結果shape的第n個位置的值。例如,如果axis=0
,它將是第乙個維度,如果axis=-1
,它將是最後乙個維度。
>>> a = np.array([1, 2, 3])>>> b = np.array([2, 3, 4])
>>>a.shape
(3,)
>>>b.shape
(3,)
>>> np.stack((a, b), axis=0).shape
(2, 3)
>>> np.stack((a, b), axis=1).shape
(3, 2)
tf.stack()和np.stack()道理差不多
tf.concat是沿某一維度拼接shape相同的張量,拼接生成的新張量維度不會增加。而tf.stack是在新的維度上拼接,拼接後維度加1
import
tensorflow as tf
a
=
tf.variable([[
1
,
2
,
3
,
4
,
5
], [
6
,
7
,
8
,
9
,
10
], [
11
,
12
,
13
,
14
,
15
]])
index_a
=
tf.variable([
0
,
2
])
b
=
tf.variable([
1
,
2
,
3
,
4
,
5
,
6
,
7
,
8
,
9
,
10
])
index_b
=
tf.variable([
2
,
4
,
6
,
8
])
with tf.session() as sess:
sess.run(tf.global_variables_initializer())
print
(sess.run(tf.gather(a, index_a)))
print
(sess.run(tf.gather(b, index_b)))
# [[ 1 2 3 4 5]
# [11 12 13 14 15]]
# [3 5 7 9]
tensorflow常用函式
1.variable 主要在於一些可訓練變數 trainable variables 比如模型的權重 weights 或者偏執值 bias 1 宣告時,必須提供初始值 2 在真實訓練時,其值是會改變的,自然事先需要指定初始值 weights tf.variable tf.random normal ...
TensorFlow常用函式
使用tensorflow計算流程 1 prepare train data 2 define model and graph 3 choose optimizer 4 create a session to run import tensorflow as tf 1.tf.reduce mean i...
Tensorflow常用函式
w1 tf.variable tf.random normal 2,3 sttdev 1,seed 1 生成矩陣的均值為0,方差為2。使用seed 1指定隨機種子,可以保證每次執行得到的結果相同。sess.run w1.initializer 初始化w1 sess.run tf.initiable ...