TensorFlow是一個開源軟件庫,用於各種機器學習與數據處理任務。TensorFlow最初由谷歌公司的大腦團隊開發,用於Google的研究和生產,於2015年11月9日在Apache 2.0開源許可證下發佈。

在Python中需要首先導入包:

import tensorflow as tf

概念 編輯

  • TensorFlow是採用數據流圖(data flow graphs)來描述了計算的過程。
  • 圖中的節點(Nodes)被稱之為 op (operation 的縮寫),用來表示施加的計算任務,但也可以表示數據輸入(feed in)的起點/輸出(push out)的終點,或者是讀取/寫入持久變量(persistent variable)的終點。一個 op 獲得 0 個或多個 tensor,執行計算產生 0 個或多個張量tensor,每個tensor是一個類型化的多維數組。
    • 源 op (source op)不需要任何輸入,例如 常量 (Constant)。
  • 線(edges)則表示在節點間相互聯繫的多維數據數組(即張量)。在Python中,節點賦值給一個名字,該名字對應的類型為ensorflow.python.framework.ops.Tensor,即表示這個節點輸出的張量。一旦輸入端的所有張量準備好,節點將被分配到各種計算設備完成異步並行計算。
  • 為了進行計算,圖必須在被稱之為 會話 (Session) 的上下文 (context) 中執行。Session 將圖的 op 分發到諸如 CPU 或 GPU 之類的設備上,同時提供執行 op 的方法。這些方法執行後,將產生的 tensor 返回。
  • 變量 (Variable) 用於維護狀態
  • feed機制可以用一個 tensor 值臨時替換圖中的任意節點的輸出tensor;還可以直接插入一個 tensor。feed數據作為 Session 對象的run() 調用的參數。feed 只在調用它的方法內有效,方法結束,feed 就會消失。
  • fetch:為了取回操作的輸出內容,使用 Session 對象的 run() 調用執行圖時,傳入一些(節點的輸出)tensor,,則run()返回其計算結果。在 Python 語言中, tf.Session的run返回的tensor是 numpy.ndarray 對象;在 C 和 C++ 語言中,返回的 tensor 是 tensorflow::Tensor 實例。Session需要close()釋放。

交互式運行環境 編輯

可以使用 InteractiveSession 代替 Session 類,使用 Tensor.eval() 和 Operation.run() 方法代替 Session.run().

# 进入一个交互式 TensorFlow 会话.
import tensorflow as tf
sess = tf.InteractiveSession()

x = tf.Variable([1.0, 2.0])
a = tf.constant([3.0, 3.0])

# 使用初始化器 initializer op 的 run() 方法初始化 'x' 
x.initializer.run()

# 增加一个减法 sub op, 从 'x' 减去 'a'. 运行减法 op, 输出结果 
sub = tf.subtract(x, a)
print (sub.eval())

TensorBoard可視化數據流圖 編輯

import tensorflow as tf

a = tf.constant(2, name="a")
b = tf.constant(2, name="b")
x = tf.add(a, b, name="add") 
with tf.Session() as sess:
    writer = tf.summary.FileWriter('D:\\tmp', sess.graph) # if you prefer creating your writer using session's graph
    print(sess.run(x))
    writer.close()

然後在cmd運行程序

$ python3 [my_program.py] 
$ tensorboard --logdir="\tmp" --port 6006

在瀏覽器打開

http://ThisComputerName:6006/

成員函數 編輯

常量 編輯

num = tf.constant(2, name="num")
# constant of 1d tensor (vector)
a = tf.constant([2, 2], name="vector")
# constant of 2x2 tensor (matrix)
b = tf.constant([[0, 1], [2, 3]], name="matrix")
# create a tensor of shape and all elements are zeros
tf.zeros([2, 3], tf.int32) ==> [[0, 0, 0], [0, 0, 0]]
# create a tensor of shape and type (unless type is specified) as the input_tensor but all elements are zeros. input_tensor [[0, 1], [2, 3], [4, 5]]
tf.zeros_like(input_tensor) ==> [[0, 0], [0, 0], [0, 0]]
# create a tensor of shape and all elements are ones
tf.ones([2, 3], tf.int32) ==> [[1, 1, 1], [1, 1, 1]]
# input_tensor is [[0, 1], [2, 3], [4, 5]]
tf.ones_like(input_tensor) ==> [[1, 1], [1, 1], [1, 1]]
# create a tensor filled with a scalar value.
tf.fill([2, 3], 8) ==> [[8, 8, 8], [8, 8, 8]]
#tf.lin_space(start, stop, num, name=None)
# create a sequence of num evenly-spaced values are generated beginning at start. If num > 1, the values in the sequence increase by (stop - start) / (num - 1), so that the last one is exactly stop. comparable to but slightly different from numpy.linspace
tf.lin_space(10.0, 13.0, 4, name="linspace") ==> [10.0 11.0 12.0 13.0]
#tf.range([start], limit=None, delta=1, dtype=None, name='range')
# create a sequence of numbers that begins at start and extends by increments of delta up to but not including limit. slight different from range in Python
# 'start' is 3, 'limit' is 18, 'delta' is 3
tf.range(start, limit, delta) ==> [3, 6, 9, 12, 15]
# 'start' is 3, 'limit' is 1,  'delta' is -0.5
tf.range(start, limit, delta) ==> [3, 2.5, 2, 1.5]
# 'limit' is 5
tf.range(limit) ==> [0, 1, 2, 3, 4]

不像Numpy或者Python其他序列,TensorFlow序列不能迭代

for _ in np.linspace(0, 10, 4): # OK
for _ in tf.linspace(0.0, 10.0, 4): # TypeError: 'Tensor' object is not iterable.
for _ in range(4): # OK
for _ in tf.range(4): # TypeError: 'Tensor' object is not iterable.

生成隨機常量:

  • tf.random_normal
  • tf.truncated_normal
  • tf.random_uniform
  • tf.random_shuffle
  • tf.random_crop
  • tf.multinomial
  • tf.random_gamma
  • tf.set_random_seed

變量 編輯

  • tf.Variable(initial_value=None, trainable=True, collections=None, validate_shape=True, caching_device=None, name=None, variable_def=None, dtype=None, expected_shape=None, import_scope=None) 如果檢測到命名衝突,系統會自己處理。 每次都創建新對象
  • tf.get_variable(name, shape=None, dtype=None, initializer=None, regularizer=None, trainable=True, collections=None, caching_device=None, partitioner=None, validate_shape=True, custom_getter=None) 如果檢測到命名衝突,系統會報錯。因而,需要跨作用域共享變量的時候,應該使用tf.get_variable()
  • tf.global_variables_initializer 初始化全局變量

tf.assign(variableName,value) 賦值給變量

數學運算 編輯

各種除法

import tensorflow as tf
a = tf.constant([2, 2], name='a')
b = tf.constant([[0, 1], [2, 3]], name='b')
with tf.Session() as sess:
   #print(sess.run(tf.div(b, a)))             #  ⇒ [[0 0] [1 1]] 过时了
   print(sess.run(tf.divide(b, a)))          #  ⇒ [[0. 0.5] [1. 1.5]]
   print(sess.run(tf.truediv(b, a)))         #  ⇒ [[0. 0.5] [1. 1.5]]
   print(sess.run(tf.floordiv(b, a)))        #  ⇒ [[0 0] [1 1]]
   #print(sess.run(tf.realdiv(b, a)))         #  ⇒ # Error: only works for real values
   print(sess.run(tf.truncatediv(b, a)))     #  ⇒ [[0 0] [1 1]]
   print(sess.run(tf.floor_div(b, a)))       #  ⇒ [[0 0] [1 1]]
tf.add(x, y, name=None) 求和
tf.sub(x, y, name=None) 減法
tf.mul(x, y, name=None) 乘法
tf.div(x, y, name=None) 除法
tf.mod(x, y, name=None) 取模
tf.abs(x, name=None) 求絕對值
tf.neg(x, name=None) 取負 (y = -x).
tf.sign(x, name=None) 返回符號 y = sign(x) = -1 if x < 0; 0 if x == 0; 1 if x > 0.
tf.inv(x, name=None) 取反
tf.square(x, name=None) 計算平方 (y = x * x = x^2).
tf.round(x, name=None) 捨入最接近的整數 # 『a』is [0.9, 2.5, 2.3, -4.4], tf.round(a)則[ 1.0, 3.0, 2.0, -4.0 ]
tf.sqrt(x, name=None) 開根號 (y = \sqrt{x} = x^{1/2}).
tf.pow(x, y, name=None) 冪次方
  1. tensor 『x』 is [[2, 2], [3, 3]]
  2. tensor 『y』 is [[8, 16], [2, 3]]

tf.pow(x, y) 則 [[256, 65536], [9, 27]]

tf.exp(x, name=None) 計算e的次方
tf.log(x, name=None) 計算log,一個輸入計算e的ln,兩各輸入以第二輸入為基
tf.maximum(x, y, name=None) 返回最大值 (x > y ? x : y)
tf.minimum(x, y, name=None) 返回最小值 (x < y ? x : y)
tf.cos(x, name=None) 三角函數cosine
tf.sin(x, name=None) 三角函數sine
tf.tan(x, name=None) 三角函數tan
tf.atan(x, name=None) 三角函數ctan

數據類型轉換Casting

操作 描述
tf.string_to_number(string_tensor, out_type=None, name=None) 字符串轉為數字
tf.to_double(x, name=』ToDouble’) 轉為64位浮點類型–float64
tf.to_float(x, name=』ToFloat』) 轉為32位浮點類型–float32
tf.to_int32(x, name=』ToInt32』) 轉為32位整型–int32
tf.to_int64(x, name=』ToInt64』) 轉為64位整型–int64
tf.cast(x, dtype, name=None) 將x或者x.values轉換為dtype
  1. tensor a is [1.8, 2.2], tf.cast(a, tf.int32) ==> [1, 2] # dtype=tf.int32

形狀操作Shapes and Shaping

操作 描述
tf.shape(input, name=None) 返回數據的shape
  1. 『t』 is [[[1, 1, 1], [2, 2, 2]], [[3, 3, 3], [4, 4, 4]]]

shape(t) ==> [2, 2, 3]

tf.size(input, name=None) 返回數據的元質數量
  1. 『t』 is [[[1, 1, 1], [2, 2, 2]], [[3, 3, 3], [4, 4, 4]]]]

size(t) ==> 12

tf.rank(input, name=None) 返回tensor的rank

注意:此rank不同於矩陣的rank, tensor的rank表示一個tensor需要的索引數目來唯一表示任何一個元素 也就是通常所說的 「order」, 「degree」或」ndims」

  1. ’t』 is [[[1, 1, 1], [2, 2, 2]], [[3, 3, 3], [4, 4, 4]]]
  2. shape of tensor 『t』 is [2, 2, 3]

rank(t) ==> 3 tf.reshape(tensor, shape, name=None) 改變tensor的形狀

  1. tensor 『t』 is [1, 2, 3, 4, 5, 6, 7, 8, 9]
  2. tensor 『t』 has shape [9]

reshape(t, [3, 3]) ==> [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

  1. 如果shape有元素[-1],表示在該維度打平至一維
  2. -1 將自動推導得為 9:

reshape(t, [2, -1]) ==> [[1, 1, 1, 2, 2, 2, 3, 3, 3], [4, 4, 4, 5, 5, 5, 6, 6, 6]] tf.expand_dims(input, dim, name=None) 插入維度1進入一個tensor中

  1. 該操作要求-1-input.dims()
  2. 『t』 is a tensor of shape [2]

shape(expand_dims(t, 0)) ==> [1, 2] shape(expand_dims(t, 1)) ==> [2, 1] shape(expand_dims(t, -1)) ==> [2, 1] <= dim <= input.dims()

   切片与合并(Slicing and Joining)

操作 描述 tf.slice(input_, begin, size, name=None) 對tensor進行切片操作 其中size[i] = input.dim_size(i) - begin[i] 該操作要求 0 <= begin[i] <= begin[i] + size[i] <= Di for i in [0, n]

  1. 』input』 is
  2. [[[1, 1, 1], [2, 2, 2]],[[3, 3, 3], [4, 4, 4]],[[5, 5, 5], [6, 6, 6]]]

tf.slice(input, [1, 0, 0], [1, 1, 3]) ==> [[[3, 3, 3]]] tf.slice(input, [1, 0, 0], [1, 2, 3]) ==> [[[3, 3, 3], [4, 4, 4]]] tf.slice(input, [1, 0, 0], [2, 1, 3]) ==> [[[3, 3, 3]], 5, 5, 5] tf.split(split_dim, num_split, value, name=’split』) 沿着某一維度將tensor分離為num_split tensors

  1. 『value’ is a tensor with shape [5, 30]
  2. Split 『value’ into 3 tensors along dimension 1

split0, split1, split2 = tf.split(1, 3, value) tf.shape(split0) ==> [5, 10] tf.concat(concat_dim, values, name=』concat』) 沿着某一維度連結tensor t1 = [[1, 2, 3], [4, 5, 6]] t2 = [[7, 8, 9], [10, 11, 12]] tf.concat(0, [t1, t2]) ==> [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]] tf.concat(1, [t1, t2]) ==> [[1, 2, 3, 7, 8, 9], [4, 5, 6, 10, 11, 12]] 如果想沿着tensor一新軸連結打包,那麼可以: tf.concat(axis, [tf.expand_dims(t, axis) for t in tensors]) 等同於tf.pack(tensors, axis=axis) tf.pack(values, axis=0, name=』pack』) 將一系列rank-R的tensor打包為一個rank-(R+1)的tensor

  1. 『x』 is [1, 4], 『y』 is [2, 5], 『z』 is [3, 6]

pack([x, y, z]) => [[1, 4], [2, 5], [3, 6]]

  1. 沿着第一維pack

pack([x, y, z], axis=1) => [[1, 2, 3], [4, 5, 6]] 等價於tf.pack([x, y, z]) = np.asarray([x, y, z]) tf.reverse(tensor, dims, name=None) 沿着某維度進行序列反轉 其中dim為列表,元素為bool型,size等於rank(tensor)

  1. tensor 『t』 is

[[[[ 0, 1, 2, 3],

  1. [ 4, 5, 6, 7],
  1. [ 8, 9, 10, 11]],
  2. [[12, 13, 14, 15],
  3. [16, 17, 18, 19],
  4. [20, 21, 22, 23]]]]
  5. tensor 『t』 shape is [1, 2, 3, 4]
  6. 『dims』 is [False, False, False, True]

reverse(t, dims) ==> [[[[ 3, 2, 1, 0], [ 7, 6, 5, 4], [ 11, 10, 9, 8]], [[15, 14, 13, 12], [19, 18, 17, 16], [23, 22, 21, 20]]]] tf.transpose(a, perm=None, name=’transpose’) 調換tensor的維度順序 按照列表perm的維度排列調換tensor順序, 如為定義,則perm為(n-1…0)

  1. 『x』 is [[1 2 3],[4 5 6]]

tf.transpose(x) ==> [[1 4], [2 5],[3 6]]

  1. Equivalently

tf.transpose(x, perm=[1, 0]) ==> [[1 4],[2 5], [3 6]] tf.gather(params, indices, validate_indices=None, name=None) 合併索引indices所指示params中的切片 tf.gather tf.one_hot (indices, depth, on_value=None, off_value=None, axis=None, dtype=None, name=None) indices = [0, 2, -1, 1] depth = 3 on_value = 5.0 off_value = 0.0 axis = -1

  1. Then output is [4 x 3]:

output = [5.0 0.0 0.0] // one_hot(0) [0.0 0.0 5.0] // one_hot(2) [0.0 0.0 0.0] // one_hot(-1) [0.0 5.0 0.0] // one_hot(1) 矩陣相關運算 操作 描述 tf.diag(diagonal, name=None) 返回一個給定對角值的對角tensor

  1. 『diagonal』 is [1, 2, 3, 4]

tf.diag(diagonal) ==> [[1, 0, 0, 0] [0, 2, 0, 0] [0, 0, 3, 0] [0, 0, 0, 4]] tf.diag_part(input, name=None) 功能與上面相反 tf.trace(x, name=None) 求一個2維tensor足跡,即對角值diagonal之和 tf.transpose(a, perm=None, name=’transpose’) 調換tensor的維度順序 按照列表perm的維度排列調換tensor順序, 如為定義,則perm為(n-1…0)

  1. 『x』 is [[1 2 3],[4 5 6]]

tf.transpose(x) ==> [[1 4], [2 5],[3 6]]

  1. Equivalently

tf.transpose(x, perm=[1, 0]) ==> [[1 4],[2 5], [3 6]] tf.matmul(a, b, transpose_a=False, transpose_b=False, a_is_sparse=False, b_is_sparse=False, name=None) 矩陣相乘 tf.matrix_determinant(input, name=None) 返回方陣的行列式 tf.matrix_inverse(input, adjoint=None, name=None) 求方陣的逆矩陣,adjoint為True時,計算輸入共軛矩陣的逆矩陣 tf.cholesky(input, name=None) 對輸入方陣cholesky分解, 即把一個對稱正定的矩陣表示成一個下三角矩陣L和其轉置的乘積的分解A=LL^T tf.matrix_solve(matrix, rhs, adjoint=None, name=None) 求解tf.matrix_solve(matrix, rhs, adjoint=None, name=None) matrix為方陣shape為[M,M],rhs的shape為[M,K],output為[M,K] 複數操作 操作 描述 tf.complex(real, imag, name=None) 將兩實數轉換為複數形式

  1. tensor 『real』 is [2.25, 3.25]
  2. tensor imag is [4.75, 5.75]

tf.complex(real, imag) ==> [[2.25 + 4.75j], [3.25 + 5.75j]] tf.complex_abs(x, name=None) 計算複數的絕對值,即長度。

  1. tensor 『x』 is [[-2.25 + 4.75j], [-3.25 + 5.75j]]

tf.complex_abs(x) ==> [5.25594902, 6.60492229] tf.conj(input, name=None) 計算共軛複數 tf.imag(input, name=None) tf.real(input, name=None) 提取複數的虛部和實部 tf.fft(input, name=None) 計算一維的離散傅里葉變換,輸入數據類型為complex64 歸約計算(Reduction) 操作 描述 tf.reduce_sum(input_tensor, reduction_indices=None, keep_dims=False, name=None) 計算輸入tensor元素的和,或者安照reduction_indices指定的軸進行求和

  1. 『x』 is [[1, 1, 1]
  2. [1, 1, 1]]

tf.reduce_sum(x) ==> 6 tf.reduce_sum(x, 0) ==> [2, 2, 2] tf.reduce_sum(x, 1) ==> [3, 3] tf.reduce_sum(x, 1, keep_dims=True) ==> [[3], [3]] tf.reduce_sum(x, [0, 1]) ==> 6 tf.reduce_prod(input_tensor, reduction_indices=None, keep_dims=False, name=None) 計算輸入tensor元素的乘積,或者安照reduction_indices指定的軸進行求乘積 tf.reduce_min(input_tensor, reduction_indices=None, keep_dims=False, name=None) 求tensor中最小值 tf.reduce_max(input_tensor, reduction_indices=None, keep_dims=False, name=None) 求tensor中最大值 tf.reduce_mean(input_tensor, reduction_indices=None, keep_dims=False, name=None) 求tensor中平均值 tf.reduce_all(input_tensor, reduction_indices=None, keep_dims=False, name=None) 對tensor中各個元素求邏輯』與』

  1. 『x』 is
  2. [[True, True]
  3. [False, False]]

tf.reduce_all(x) ==> False tf.reduce_all(x, 0) ==> [False, False] tf.reduce_all(x, 1) ==> [True, False] tf.reduce_any(input_tensor, reduction_indices=None, keep_dims=False, name=None) 對tensor中各個元素求邏輯』或』 tf.accumulate_n(inputs, shape=None, tensor_dtype=None, name=None) 計算一系列tensor的和

  1. tensor 『a』 is [[1, 2], [3, 4]]
  2. tensor b is [[5, 0], [0, 6]]

tf.accumulate_n([a, b, a]) ==> [[7, 4], [6, 14]] tf.cumsum(x, axis=0, exclusive=False, reverse=False, name=None) 求累積和 tf.cumsum([a, b, c]) ==> [a, a + b, a + b + c] tf.cumsum([a, b, c], exclusive=True) ==> [0, a, a + b] tf.cumsum([a, b, c], reverse=True) ==> [a + b + c, b + c, c] tf.cumsum([a, b, c], exclusive=True, reverse=True) ==> [b + c, c, 0]

分割(Segmentation) 操作 描述 tf.segment_sum(data, segment_ids, name=None) 根據segment_ids的分段計算各個片段的和 其中segment_ids為一個size與data第一維相同的tensor 其中id為int型數據,最大id不大於size c = tf.constant([[1,2,3,4], [-1,-2,-3,-4], [5,6,7,8]]) tf.segment_sum(c, tf.constant([0, 0, 1])) ==>[[0 0 0 0] [5 6 7 8]] 上面例子分為[0,1]兩id,對相同id的data相應數據進行求和, 並放入結果的相應id中, 且segment_ids只升不降 tf.segment_prod(data, segment_ids, name=None) 根據segment_ids的分段計算各個片段的積 tf.segment_min(data, segment_ids, name=None) 根據segment_ids的分段計算各個片段的最小值 tf.segment_max(data, segment_ids, name=None) 根據segment_ids的分段計算各個片段的最大值 tf.segment_mean(data, segment_ids, name=None) 根據segment_ids的分段計算各個片段的平均值 tf.unsorted_segment_sum(data, segment_ids, num_segments, name=None) 與tf.segment_sum函數類似, 不同在於segment_ids中id順序可以是無序的 tf.sparse_segment_sum(data, indices, segment_ids, name=None) 輸入進行稀疏分割求和 c = tf.constant([[1,2,3,4], [-1,-2,-3,-4], [5,6,7,8]])

  1. Select two rows, one segment.

tf.sparse_segment_sum(c, tf.constant([0, 1]), tf.constant([0, 0])) ==> 0 0 0 0 對原data的indices為[0,1]位置的進行分割, 並按照segment_ids的分組進行求和 序列比較與索引提取(Sequence Comparison and Indexing) 操作 描述 tf.argmin(input, dimension, name=None) 返回input最小值的索引index tf.argmax(input, dimension, name=None) 返回input最大值的索引index tf.listdiff(x, y, name=None) 返回x,y中不同值的索引 tf.where(input, name=None) 返回bool型tensor中為True的位置

  1. 『input』 tensor is
  2. [[True, False]
  3. [True, False]]
  4. 『input』 有兩個』True’,那麼輸出兩個坐標值.
  5. 『input』的rank為2, 所以每個坐標為具有兩個維度.

where(input) ==> [[0, 0], [1, 0]] tf.unique(x, name=None) 返回一個元組tuple(y,idx),y為x的列表的唯一化數據列表, idx為x數據對應y元素的index

  1. tensor 『x』 is [1, 1, 2, 4, 4, 4, 7, 8, 8]

y, idx = unique(x) y ==> [1, 2, 4, 7, 8] idx ==> [0, 0, 1, 2, 2, 2, 3, 4, 4] tf.invert_permutation(x, name=None) 置換x數據與索引的關係

  1. tensor x is [3, 4, 0, 2, 1]

invert_permutation(x) ==> [2, 4, 3, 0, 1] 神經網絡(Neural Network)

   激活函数(Activation Functions)

操作 描述 tf.nn.relu(features, name=None) 整流函數:max(features, 0) tf.nn.relu6(features, name=None) 以6為閾值的整流函數:min(max(features, 0), 6) tf.nn.elu(features, name=None) elu函數,exp(features) - 1 if < 0,否則features Exponential Linear Units (ELUs) tf.nn.softplus(features, name=None) 計算softplus:log(exp(features) + 1) tf.nn.dropout(x, keep_prob, noise_shape=None, seed=None, name=None) 計算dropout,keep_prob為keep機率 noise_shape為噪聲的shape tf.nn.bias_add(value, bias, data_format=None, name=None) 對value加一偏置量 此函數為tf.add的特殊情況,bias僅為一維, 函數通過廣播機制進行與value求和, 數據格式可以與value不同,返回為與value相同格式 tf.sigmoid(x, name=None) y = 1 / (1 + exp(-x)) tf.tanh(x, name=None) 雙曲線切線激活函數

   卷积函数(Convolution)

操作 描述 tf.nn.conv2d(input, filter, strides, padding, use_cudnn_on_gpu=None, data_format=None, name=None) 在給定的4D input與 filter下計算2D卷積 輸入shape為 [batch, height, width, in_channels] tf.nn.conv3d(input, filter, strides, padding, name=None) 在給定的5D input與 filter下計算3D卷積 輸入shape為[batch, in_depth, in_height, in_width, in_channels]

   池化函数(Pooling)

操作 描述 tf.nn.avg_pool(value, ksize, strides, padding, data_format=』NHWC』, name=None) 平均方式池化 tf.nn.max_pool(value, ksize, strides, padding, data_format=』NHWC』, name=None) 最大值方法池化 tf.nn.max_pool_with_argmax(input, ksize, strides, padding, Targmax=None, name=None) 返回一個二維元組(output,argmax),最大值pooling,返回最大值及其相應的索引 tf.nn.avg_pool3d(input, ksize, strides, padding, name=None) 3D平均值pooling tf.nn.max_pool3d(input, ksize, strides, padding, name=None) 3D最大值pooling

   数据标准化(Normalization)

操作 描述 tf.nn.l2_normalize(x, dim, epsilon=1e-12, name=None) 對維度dim進行L2範式標準化 output = x / sqrt(max(sum(x**2), epsilon)) tf.nn.sufficient_statistics(x, axes, shift=None, keep_dims=False, name=None) 計算與均值和方差有關的完全統計量 返回4維元組,*元素個數,*元素總和,*元素的平方和,*shift結果 參見算法介紹 tf.nn.normalize_moments(counts, mean_ss, variance_ss, shift, name=None) 基於完全統計量計算均值和方差 tf.nn.moments(x, axes, shift=None, name=None, keep_dims=False) 直接計算均值與方差

   损失函数(Losses)

操作 描述 tf.nn.l2_loss(t, name=None) output = sum(t ** 2) / 2

   分类函数(Classification)

操作 描述 tf.nn.sigmoid_cross_entropy_with_logits (logits, targets, name=None)* 計算輸入logits, targets的交叉熵 tf.nn.softmax(logits, name=None) 計算softmax softmax[i, j] = exp(logits[i, j]) / sum_j(exp(logits[i, j])) tf.nn.log_softmax(logits, name=None) logsoftmax[i, j] = logits[i, j] - log(sum(exp(logits[i]))) tf.nn.softmax_cross_entropy_with_logits (logits, labels, name=None) 計算logits和labels的softmax交叉熵 logits, labels必須為相同的shape與數據類型 tf.nn.sparse_softmax_cross_entropy_with_logits (logits, labels, name=None) 計算logits和labels的softmax交叉熵 tf.nn.weighted_cross_entropy_with_logits (logits, targets, pos_weight, name=None) 與sigmoid_cross_entropy_with_logits()相似, 但給正向樣本損失加了權重pos_weight

   符号嵌入(Embeddings)

操作 描述 tf.nn.embedding_lookup (params, ids, partition_strategy=’mod』, name=None, validate_indices=True) 根據索引ids查詢embedding列表params中的tensor值 如果len(params) > 1,id將會安照partition_strategy策略進行分割 1、如果partition_strategy為」mod」, id所分配到的位置為p = id % len(params) 比如有13個ids,分為5個位置,那麼分配方案為: [[0, 5, 10], [1, 6, 11], [2, 7, 12], [3, 8], [4, 9]] 2、如果partition_strategy為」div」,那麼分配方案為: [[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10], [11, 12]] tf.nn.embedding_lookup_sparse(params, sp_ids, sp_weights, partition_strategy=’mod』, name=None, combiner=’mean』) 對給定的ids和權重查詢embedding 1、sp_ids為一個N x M的稀疏tensor, N為batch大小,M為任意,數據類型int64 2、sp_weights的shape與sp_ids的稀疏tensor權重, 浮點類型,若為None,則權重為全』1』

   循环神经网络(Recurrent Neural Networks)

操作 描述 tf.nn.rnn(cell, inputs, initial_state=None, dtype=None, sequence_length=None, scope=None) 基於RNNCell類的實例cell建立循環神經網絡 tf.nn.dynamic_rnn(cell, inputs, sequence_length=None, initial_state=None, dtype=None, parallel_iterations=None, swap_memory=False, time_major=False, scope=None) 基於RNNCell類的實例cell建立動態循環神經網絡 與一般rnn不同的是,該函數會根據輸入動態展開 返回(outputs,state) tf.nn.state_saving_rnn(cell, inputs, state_saver, state_name, sequence_length=None, scope=None) 可儲存調試狀態的RNN網絡 tf.nn.bidirectional_rnn(cell_fw, cell_bw, inputs, initial_state_fw=None, initial_state_bw=None, dtype=None, sequence_length=None, scope=None) 雙向RNN, 返回一個3元組tuple (outputs, output_state_fw, output_state_bw)

   — tf.nn.rnn简要介绍— 
   cell: 一个RNNCell实例 
   inputs: 一个shape为[batch_size, input_size]的tensor 
   initial_state: 为RNN的state设定初值,可选 
   sequence_length:制定输入的每一个序列的长度,size为[batch_size],值范围为[0, T)的int型数据 
   其中T为输入数据序列的长度 
   @ 
   @针对输入batch中序列长度不同,所设置的动态计算机制 
   @对于在时间t,和batch的b行,有 
   (output, state)(b, t) = ? (zeros(cell.output_size), states(b, sequence_length(b) - 1)) : cell(input(b, t), state(b, t - 1))
   求值网络(Evaluation)

操作 描述 tf.nn.top_k(input, k=1, sorted=True, name=None) 返回前k大的值及其對應的索引 tf.nn.in_top_k(predictions, targets, k, name=None) 返回判斷是否targets索引的predictions相應的值 是否在在predictions前k個位置中, 返回數據類型為bool類型,len與predictions同

   监督候选采样网络(Candidate Sampling)

對於有巨大量的多分類與多標籤模型,如果使用全連接softmax將會佔用大量的時間與空間資源,所以採用候選採樣方法僅使用一小部分類別與標籤作為監督以加速訓練。 操作 描述 Sampled Loss Functions tf.nn.nce_loss(weights, biases, inputs, labels, num_sampled, num_classes, num_true=1, sampled_values=None, remove_accidental_hits=False, partition_strategy=’mod』, name=』nce_loss』) 返回noise-contrastive的訓練損失結果 tf.nn.sampled_softmax_loss(weights, biases, inputs, labels, num_sampled, num_classes, num_true=1, sampled_values=None, remove_accidental_hits=True, partition_strategy=’mod』, name=’sampled_softmax_loss』) 返回sampled softmax的訓練損失 參考- Jean et al., 2014第3部分 Candidate Samplers tf.nn.uniform_candidate_sampler(true_classes, num_true, num_sampled, unique, range_max, seed=None, name=None) 通過均勻分佈的採樣集合 返回三元tuple 1、sampled_candidates 候選集合。 2、期望的true_classes個數,為浮點值 3、期望的sampled_candidates個數,為浮點值 tf.nn.log_uniform_candidate_sampler(true_classes, num_true, num_sampled, unique, range_max, seed=None, name=None) 通過log均勻分佈的採樣集合,返回三元tuple tf.nn.learned_unigram_candidate_sampler (true_classes, num_true, num_sampled, unique, range_max, seed=None, name=None) 根據在訓練過程中學習到的分佈狀況進行採樣 返回三元tuple tf.nn.fixed_unigram_candidate_sampler(true_classes, num_true, num_sampled, unique, range_max, vocab_file=」, distortion=1.0, num_reserved_ids=0, num_shards=1, shard=0, unigrams=(), seed=None, name=None) 基於所提供的基本分佈進行採樣 保存與恢復變量 操作 描述 類tf.train.Saver(Saving and Restoring Variables) tf.train.Saver.__init__(var_list=None, reshape=False, sharded=False, max_to_keep=5, keep_checkpoint_every_n_hours=10000.0, name=None, restore_sequentially=False, saver_def=None, builder=None) 創建一個存儲器Saver var_list定義需要存儲和恢復的變量 tf.train.Saver.save(sess, save_path, global_step=None, latest_filename=None, meta_graph_suffix=’meta』, write_meta_graph=True) 保存變量 tf.train.Saver.restore(sess, save_path) 恢復變量 tf.train.Saver.last_checkpoints 列出最近未刪除的checkpoint 文件名 tf.train.Saver.set_last_checkpoints(last_checkpoints) 設置checkpoint文件名列表 tf.train.Saver.set_last_checkpoints_with_time(last_checkpoints_with_time) 設置checkpoint文件名列表和時間戳

設備 編輯

with tf.device("/gpu:0"): 指定一个计算设备,作用域中的代码在该设备上执行。

例子 編輯

例:Hello World。

import tensorflow as tf
hw = tf.constant("Hello World")
with tf.Session() as sess:
 print(sess.run(hw))

例:兩個矩陣相乘。

import tensorflow as tf

# Build a dataflow graph.
c = tf.constant([[1.0, 2.0], [3.0, 4.0]])
d = tf.constant([[1.0, 1.0], [0.0, 1.0]])
e = tf.matmul(c, d)

# Construct a `Session` to execute the graph.
with tf.Session() as sess:
  # Execute the graph and store the value that `e` represents in `result`.
  result = sess.run(e)

print(result)

例:使用Feeding在執行時傳入參數

import tensorflow as tf

# Build a dataflow graph.
c = tf.constant([[1.0, 2.0], [3.0, 4.0]])
d = tf.constant([[1.0, 1.0], [0.0, 1.0]])
e = tf.matmul(c, d)

# Construct a `Session` to execute the graph.
sess = tf.Session()

# Execute the graph and store the value that `e` represents in `result`.
result = sess.run(e,feed_dict={c:[[0.0, 0.0], [3.0, 4.0]]})
print(result)
sess.close()

TensorFlow的一大特色是其圖中的節點可以是帶狀態的。

例:使用佔位符 <source lang="python"> input_placeholder = tf.placeholder(tf.int32) sess = tf.Session() print (sess.run(input_placeholder, feed_dict={input_placeholder: 2})) sess.close() </syntaxhighlight>

例:使用變量、給變量賦值

使用tf.get_variable()創建變量。tf.get_variable() 的前兩個參數是必需的,其餘參數是可選的。tf.get_variable(name,shape)。name 是一個唯一標識這個變量對象的字符串。它必須相對於全局圖是唯一的,所以要明了你使用過的所有命名,確保沒有重複。shape 是與張量形狀對應的整數數組,按順序每個維度只有一個整數。一個 3x8 矩陣形狀是 [3, 8]。要創建一個純量,就需要使用形狀為 [] 的空列表。有兩種將值放入變量的方法:初始化器和 tf.assign()。初始化器應該把聲明(tf.constant_initializer)與執行初始化(tf.global_variables_initializer)兩種節點配合使用。

import tensorflow as tf
count_variable = tf.get_variable("count", [])
zero_node = tf.constant(0.)
assign_node = tf.assign(count_variable, zero_node)
sess = tf.Session()
sess.run(assign_node)
print (sess.run(count_variable))
 
const_init_node = tf.constant_initializer(0.)
count_variable1 = tf.get_variable("count1", [], initializer=const_init_node)
init = tf.global_variables_initializer()
sess.run(init)
print (sess.run(count_variable1))
sess.close()

例子:tf.Variable() 和tf.get_variable()的區別 <source lang="python"> import tensorflow as tf

with tf.variable_scope("scope1"):

   w1 = tf.get_variable("w1", shape=[])
   w2 = tf.Variable(0.0, name="w2")

with tf.variable_scope("scope1", reuse=True):

   w1_p = tf.get_variable("w1", shape=[])
   w2_p = tf.Variable(1.0, name="w2")

print(w1 is w1_p, w2 is w2_p)

  1. 輸出
  2. True False

</syntaxhighlight>

例:帶狀態的圖

import tensorflow as tf

# Build a dataflow graph.
count = tf.Variable([0],trainable=False);
init_op = tf.global_variables_initializer()
update_count = count.assign_add(tf.constant([2]))

# Construct a `Session` to execute the graph.
sess = tf.Session()
sess.run(init_op)

for step in range(10):
    result = sess.run(update_count)
    print("step %d: count = %g" % (step,result))

sess.close()

例:梯度計算

import tensorflow as tf

# Build a dataflow graph.
filename_queue = tf.train.string_input_producer(['1.txt'],num_epochs=1)
reader = tf.TextLineReader()
key,value = reader.read(filename_queue)
num = tf.decode_csv(value,record_defaults=[[0]])
x = tf.Variable([0])
loss = x * num
grads = tf.gradients([loss],x)
grad_x = grads[0]

def train_fn(sess):
  train_fn.counter += 1
  result = sess.run(grad_x)
  print("step %d: grad = %g" % (train_fn.counter,result))

train_fn.counter = 0

sv = tf.train.Supervisor()
tf.train.basic_train_loop(sv,train_fn)

假設1.txt的內容為:

3
2
5
8

那麼上述程序的輸出應該為:

step 1: grad = 3
step 2: grad = 2
step 3: grad = 5
step 4: grad = 8