標籤:

TensorFlow筆記

tf.one_hot()使用

官方文檔

tf.one_hot(indices, depth, on_value=None, off_value=None,axis=None, dtype=None, name=None)

Args:

  • indices: A Tensor ofn indices. #輸入的tensor
  • depth: A scalarn defining the depth of the one hot dimension. # one_hot形式的深度
  • on_value: A scalarn defining the value to fill in output when indices[j] = i. (default: 1) # one_hot形式中以on_value值表示該位置為真值。默認值為1.實際上就是使用on_value來代替one_hot形式中的1.
  • off_value: A scalarn defining the value to fill in output when indices[j] != i. (default: 0) # 與on_value相反,默認值為0.實際上就是使用off_value來代替one_hot形式中的0.
  • axis: The axisn to fill (default: -1, a new inner-most axis). # 和 轉置 相關的數據
  • dtype: The datan type of the output tensor.

舉例:

import tensorflow as tf nindices2 = [0,2,-1,1] na = tf.one_hot(indices2, depth=3, on_value=None, off_value=None, axis=None, dtype=None, name=None)nb = tf.one_hot(indices2, depth=3, on_value=None, off_value=None, axis=1, dtype=None, name=None)nsess = tf.Session()nu = sess.run(b)nv = sess.run(c)n nprint a nprint u nprint bnprint vn

輸出結果:

a的結果n Tensor("one_hot_56:0", shape=(4, 3), dtype=float32)nu的結果n[[ 1. 0. 0.]n [ 0. 0. 1.]n [ 0. 0. 0.]n [ 0. 1. 0.]]nb的結果nTensor("one_hot_57:0", shape=(3, 4), dtype=float32)nv的結果n[[ 1. 0. 0. 0.]n [ 0. 0. 0. 1.]n [ 0. 1. 0. 0.]]n

tensor形式的轉換:

import tensorflow as tf nindices = [[3,1], [5,2], [0,7], [7,9]] nnc = tf.one_hot(indices, depth=10, on_value=None, off_value=None, axis=None, dtype=None, name=None)nw = sess.run(a)nnprint c nprint wn

輸出結果nc的結果nTensor("one_hot_55:0", shape=(4, 2, 10), dtype=float32)nw的結果n[[[ 0. 0. 0. 1. 0. 0. 0. 0. 0. 0.]n [ 0. 1. 0. 0. 0. 0. 0. 0. 0. 0.]]nn [[ 0. 0. 0. 0. 0. 1. 0. 0. 0. 0.]n [ 0. 0. 1. 0. 0. 0. 0. 0. 0. 0.]]nn [[ 1. 0. 0. 0. 0. 0. 0. 0. 0. 0.]n [ 0. 0. 0. 0. 0. 0. 0. 1. 0. 0.]]nn [[ 0. 0. 0. 0. 0. 0. 0. 1. 0. 0.]n [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 1.]]]nntf.squeeze()使用n官方文檔:nntf.squeeze(input, squeeze_dims=None, name=None)nArgs:nninput: A Tensor. The input to squeeze. # 需要被squeeze的tensornnsqueeze_dims: An optional list of ints. Defaults to []. If specified, only squeezes thedimensions listed. The dimension index starts at 0. It is an error to squeeze a dimension that is not 1. # 進行squeeze的[index1,index2],默認是[ ]。nn注意:1.只能squeeze 輸入的 張量(tensor)中維度為1的那一個部分nn2.如果不說明squeeze_dims,則默認將張量中所有維度為1的進行squeezennname: A name for the operation (optional).nn舉例:nn現在有一個shape=[1,1,2]的 張量(tensor),張量名為 A,nn那麼只能輸入:nntf.squeeze(A,[0]) --> shape=[1,2]ntf.squeeze(A,[1]) --> shape=[1,2]ntf.squeeze(A,[0,1]) --> shape=[2,]n在本例中:A的index分別為[0,1,-1]。老樣子,-1表示最後一個nn因為最後一個的維度是2.所以不能使用tf.squeeze()n n

tf.squeeze()使用

官方文檔:

tf.squeeze(input, squeeze_dims=None, name=None)

Args:

input: A Tensor. The input to squeeze. # 需要被squeeze的tensor

squeeze_dims: An optional list of ints. Defaults to []. If specified, only squeezes thedimensions listed. The dimension index starts at 0. It is an error to squeeze a dimension that is not 1. # 進行squeeze的[index1,index2],默認是[ ]。

注意:

1.只能squeeze 輸入的 張量(tensor)中維度為1的那一個部分

2.如果不說明squeeze_dims,則默認將張量中所有維度為1的進行squeeze

name: A name for the operation (optional).

舉例:

現在有一個shape=[1,1,2]的 張量(tensor),張量名為 A,n

那麼只能輸入:nntf.squeeze(A,[0]) --> shape=[1,2]ntf.squeeze(A,[1]) --> shape=[1,2]ntf.squeeze(A,[0,1]) --> shape=[2,]n在本例中:A的index分別為[0,1,-1]。老樣子,-1表示最後一個nn因為最後一個的維度是2.所以不能使用tf.squeeze()n

推薦閱讀:

學習筆記TF029:實現進階卷積網路
播客類今日頭條誕生記:TensorFlow成共享秘方?
tf.reset_default_graph
TensorFlow 官方文檔中文版 [W3Cschool]

TAG:TensorFlow |