tf

維度相關

定義

a = tf.Variable([[1,-1,3]], shape=(1,3))

<tf.Variable 'Variable:0' shape=(1, 3) dtype=int32, numpy=array([[ 1, -1, 3]], dtype=int32)>

a只能是一維矩陣(即向量),shape是(3,)而不是(3,1) 也不能是(1,3)

a = tf.Variable([0,-1,3], shape=(3,))
b =  tf.Variable([-1,5,6], shape=(3,))
c = tf.Variable([[7,8],[9,10],[10,-1]], shape=(3,2))
a,b,c

 (<tf.Variable 'Variable:0' shape=(3,) dtype=int32, numpy=array([ 0, -1,  3], dtype=int32)>,
 <tf.Variable 'Variable:0' shape=(3,) dtype=int32, numpy=array([-1,  5,  6], dtype=int32)>,
 <tf.Variable 'Variable:0' shape=(3, 2) dtype=int32, numpy=array([[ 7,  8],[ 9, 10],[10, -1]], dtype=int32)>)

tf.expand_dims

維度擴展,將之前的維度加一,一維變二維,二維變三維,以此類推。

a之前是一維形狀是(3,),現在a_1是兩位形狀如下(x,x),因爲是0維度,故0索引的應該是1,1索引的是原來的3,即(1,3)

a_1 = tf.expand_dims(a,0)

<tf.Tensor: shape=(1, 3), dtype=int32, numpy=array([[ 0, -1,  3]], dtype=int32)> 

a之前是一維形狀是(3,),現在a_1是兩位形狀如下(x,x),因爲是1維度,故1索引的應該是1,0索引的是原來的3,即(3,1);c之前是一維形狀是(3,2),現在c_1是兩位形狀如下(x,x,x),因爲是1維度,故1索引的應該是1,0索引的是原來的3,2索引的應該是原來的2,即(3,1,2)

a_1 = tf.expand_dims(a,1)
c_1 = tf.expand_dims(c,1)
a_1, c_1

(<tf.Tensor: shape=(3, 1), dtype=int32, numpy=array([[ 0],[-1],[ 3]], dtype=int32)>,
 <tf.Tensor: shape=(3, 1, 2), dtype=int32, numpy=array([[[ 7,  8]],[[ 9, 10]],[[10, -1]]], dtype=int32)>) 

a_1 = tf.expand_dims(a,1)
b_1 = tf.expand_dims(b,1)
c_1 = c
a_1, b_1,c_1

(<tf.Tensor: shape=(3, 1), dtype=int32, numpy=array([[ 0],[-1],[ 3]], dtype=int32)>,
 <tf.Tensor: shape=(3, 1), dtype=int32, numpy=array([[-1],[ 5],[ 6]], dtype=int32)>,
 <tf.Variable 'Variable:0' shape=(3, 2) dtype=int32, numpy=array([[ 7,  8],[ 9, 10],[10, -1]],dtype=int32)>) 

tf.concat

連接,將張量依照制定的維度連接。

a_1的維度是(3,1),b_1的維度是(3,1),指定0,即索引0的長度相加(除索引0以外其餘的長度必須一致),即c的維度與a_1和b_1相同,形狀是(6,1),

c = tf.concat([a_1,b_1], 0)
c

<tf.Tensor: shape=(6, 1), dtype=int32, numpy=array([[ 0],[-1],[ 3],[-1],[ 5],[ 6]], dtype=int32)> 

 a_1的維度是(3,1),b_1的維度是(3,1),指定1,即索引1的長度相加(除索引1以外其餘的長度必須一致),即c的維度與a_1和b_1相同,形狀是(3,2)

c = tf.concat([a_1,b_1], 1)
c

<tf.Tensor: shape=(3, 2), dtype=int32, numpy=array([[ 0, -1],[-1,  5],[ 3,  6]], dtype=int32)> 

c = tf.concat([a_1,b_1,c_1], 1)
c

<tf.Tensor: shape=(3, 4), dtype=int32, numpy=array([[ 0, -1,  7,  8],[-1,  5,  9, 10],[ 3,  6, 10, -1]], dtype=int32)> 

tf.not_equal

not_equal(
    x,
    y,
    name=None
)

轉自https://www.w3cschool.cn/tensorflow_python/tensorflow_python-h6852fqf.html

 返回 (x! = y) 元素的真值。注意:NotEqual 支持廣播。

參數:

  • x:張量.必須是下列類型之一:half,float32,float64,uint8,int8,int16,int32,int64,complex64,quint8,qint8,qint32,string,bool,complex128.
  • y:張量.必須與 x 具有相同的類型.
  • name:操作的名稱(可選).

返回值:

     該函數將返回一個 bool 類型的張量。

  這邊用到了廣播機制,其中c的維度是2,形狀是(3,4),這邊返回的是與c同維度同形狀的bool類型的張量,此處d的維度也是2,形狀也是(3,4),只要c中元素不爲-1,則d的對應位置的值爲True,否則爲False。

d = tf.not_equal(c,-1)
d

<tf.Tensor: shape=(3, 4), dtype=bool, numpy=array([[ True, False,  True,  True],[False,  True,  True,  True],[ True,  True,  True, False]])>) 

tf.where

tf.where(
    condition,
    x=None,
    y=None,
    name=None
)

轉自https://www.w3cschool.cn/tensorflow_python/tensorflow_python-u6l92oik.html

根據condition返回x或y中的元素. 

如果x和y都爲None,則該操作將返回condition中true元素的座標.座標以二維張量返回,其中第一維(行)表示真實元素的數量,第二維(列)表示真實元素的座標.請記住,輸出張量的形狀可以根據輸入中的真實值的多少而變化.索引以行優先順序輸出.

如果兩者都不是None,則x和y必須具有相同的形狀.如果x和y是標量,則condition張量必須是標量.如果x和y是更高級別的矢量,則condition必須是大小與x的第一維度相匹配的矢量,或者必須具有與x相同的形狀.

condition張量作爲一個可以選擇的掩碼(mask),它根據每個元素的值來判斷輸出中的相應元素/行是否應從 x (如果爲 true) 或 y (如果爲 false)中選擇.

如果condition是向量,則x和y是更高級別的矩陣,那麼它選擇從x和y複製哪個行(外部維度).如果condition與x和y具有相同的形狀,那麼它將選擇從x和y複製哪個元素.

函數參數:

  • condition:一個bool類型的張量(Tensor).
  • x:可能與condition具有相同形狀的張量;如果condition的秩是1,則x可能有更高的排名,但其第一維度必須匹配condition的大小.
  • y:與x具有相同的形狀和類型的張量.
  • name:操作的名稱(可選).

返回值:

如果它們不是None,則返回與x,y具有相同類型與形狀的張量;張量具有形狀(num_true, dim_size(condition)).

可能引發的異常:

  • ValueError:當一個x或y正好不是None.

其中d的維度是2,形狀是(3,4),這邊返回的是與c同維度的張量,其中每個元素都是bool值爲True的下標索引,即每個元素的長度是c的維度的長度,這邊是2,此處d中共有9個值爲True的元素,例如[0,0]表示索引[0][0]位置的元素是True。

e = tf.where(d)
e

<tf.Tensor: shape=(9, 2), dtype=int64, numpy=array([[0, 0],[0, 2],[0, 3],[1, 1],[1, 2],[1, 3],[2, 0],[2, 1],[2, 2]])>

tf.slice

slice( input_, begin, size, name=None )

轉自https://www.w3cschool.cn/tensorflow_python/tensorflow_python-cdj92kbd.html

從張量中提取切片。

此操作從由begin指定位置開始的張量input中提取一個尺寸size的切片。切片size被表示爲張量形狀,其中size[i]是你想要分割的input的第i維的元素的數量。切片的起始位置(begin)表示爲每個input維度的偏移量.換句話說,begin[i]是你想從中分割出來的input的“第i個維度”的偏移量。

請注意,tf.Tensor.__getitem__通常是執行切片的python方式,因爲它允許您寫foo[3:7, :-2],而不是tf.slice([3, 0], [4, foo.get_shape()[1]-2])。

begin是基於零的;size是一個基礎.如果size[i]是-1,則維度i中的所有其餘元素都包含在切片中.換句話說,這相當於設置:

size[i] = input.dim_size(i) - begin[i]

該操作要求:

0 <= begin[i] <= begin[i] + size[i] <= Di for i in [0, n]

例如:

t = tf.constant([[[1, 1, 1], [2, 2, 2]],
                 [[3, 3, 3], [4, 4, 4]],
                 [[5, 5, 5], [6, 6, 6]]])
tf.slice(t, [1, 0, 0], [1, 1, 3])  # [[[3, 3, 3]]]
tf.slice(t, [1, 0, 0], [1, 2, 3])  # [[[3, 3, 3],
                                   #   [4, 4, 4]]]
tf.slice(t, [1, 0, 0], [2, 1, 3])  # [[[3, 3, 3]],
                                   #  [[5, 5, 5]]]

函數參數

  • input_:一個Tensor.
  • begin:一個int32或int64類型的Tensor.
  • size:一個int32或int64類型的Tensor.
  • name:操作的名稱(可選).

函數返回

tf.slice函數返回與input具有相同類型的Tensor。

tf.cast

cast(x, dtype, name=None)

類型轉化,將待轉換的張量,轉換成dtype類型的數據,返回與x同形狀的張量

c中只有[0][0]位置是0,轉換成bool型爲False,其餘均爲True。 

tf.cast(c,dtype=bool)

<tf.Tensor: shape=(3, 4), dtype=bool, numpy=array([[False,  True,  True,  True],[ True,  True,  True,  True],[ True,  True,  True,  True]])> 

tf.nn.embedding_lookup

tf.nn.embedding_lookup( params, ids, partition_strategy='mod', 
                        name=None, validate_indices=True, max_norm=None )

tf.nn.embedding_lookup函數的用法主要是選取一個張量裏面索引對應的元素。tf.nn.embedding_lookup(params, ids):params可以是張量也可以是數組等,id就是對應的索引,其他的參數不介紹。

 a是一維向量,形狀(3,),[1,2]表示取下標1和2位置上的元素,返回的維度與a相同,形狀是(下標列表的長度,)

e = tf.nn.embedding_lookup(a,[1,2])
a,e

(<tf.Variable 'Variable:0' shape=(3,) dtype=int32, numpy=array([ 0, -1,  3], dtype=int32)>,
 <tf.Tensor: shape=(2,), dtype=int32, numpy=array([-1,  3], dtype=int32)>) 

 c是二維向量,形狀(3,4),返回的維度是[0,2]的維度(1)+1,即返回2維矩陣,0維是[0,2]的長度即2,1維是c的-1維的長度,形狀(2,4)。

e = tf.nn.embedding_lookup(c,[0,2])
c,e

(<tf.Tensor: shape=(3, 4), dtype=int32, numpy=array([[ 0, -1,  7,  8],[-1,  5,  9, 10],[ 3,  6, 10, -1]], dtype=int32)>,
 <tf.Tensor: shape=(2, 4), dtype=int32, numpy=array([[ 0, -1,  7,  8],[ 3,  6, 10, -1]], dtype=int32)>) 

  c是二維向量,形狀(5,4), d是二維向量,形狀(2,3),返回的維度是d的維度(2)+1,即返回3維矩陣,前面是d的形狀,-1維是c的-1維的長度,形狀(2,3,4)。

c = tf.Variable([[ 0, -1,  7,  8],[-1,  5,  9, 10], [ 3,  6, 10, -1],[7,8,9,0],[8, 7,6,5]])
d = tf.reshape(tf.Variable([0,1,3,4,2,0]),shape=(2,3))
e = tf.nn.embedding_lookup(c,d)
c,d,e

(<tf.Variable 'Variable:0' shape=(5, 4) dtype=int32, numpy=array([[ 0, -1,  7,  8],[-1,  5,  9, 10],[ 3,  6, 10, -1],[ 7,  8,  9,  0],[ 8,  7,  6,  5]], dtype=int32)>,
 <tf.Tensor: shape=(2, 3), dtype=int32, numpy=array([[0, 1, 3],[4, 2, 0]], dtype=int32)>,
 <tf.Tensor: shape=(2, 3, 4), dtype=int32, numpy=array([[[ 0, -1,  7,  8],[-1,  5,  9, 10],[ 7,  8,  9,  0]],[[ 8,  7,  6,  5],[ 3,  6, 10, -1],[ 0, -1,  7,  8]]], dtype=int32)>) 

tf.nn.softmax

tf.nn.softmax(logits,axis=None,name=None,dim=None)

轉自https://www.w3cschool.cn/tensorflow_python/tf_nn_softmax.html

計算softmax激活。(棄用的參數)

有些參數已被棄用。它們將在將來的版本中刪除。更新說明:不推薦使用dim,而是使用axis。

此函數執行相當於:

softmax = tf.exp(logits) / tf.reduce_sum(tf.exp(logits), axis)

參數:

  • logits:一個非空的Tensor。必須是下列類型之一:half, float32,float64。
  • axis:將在其上執行維度softmax。默認值爲-1,表示最後一個維度。
  • name:操作的名稱(可選)。
  • dim:axis的已棄用的別名。

返回:

一個Tensor,與logits具有相同的類型和shape。

可能引發的異常:

  • InvalidArgumentError:如果logits爲空或axis超出logits的最後一個維度。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章