1. 程式人生 > >TensorFlow深度學習入門筆記(四)一些基本函數

TensorFlow深度學習入門筆記(四)一些基本函數

.com com pre http 今天 重用 模型 use max

關註公眾號“從機器學習到深度學習那些事”獲取更多最新資料

寫在前面

學習建議:以下學習過程中有不理解可以簡單查找下資料,但不必糾結(比如非得深究某一個函數等),盡量快速的學一遍,不求甚解無妨。多實操代碼,不能只復制代碼,或者感覺懂了就只看。熟能生巧,我亦無他,唯手熟爾

今天介紹一些基礎函數及其用法,基本全是代碼,一些解釋都放在代碼的註釋裏了。直接看代碼吧,記得在你本地跑一下看哦

代碼1

#tensor.get_shape() 獲取tensor的shape,就是維度這些
#tensor.get_shape().as_list(),把shape轉換成列表形式
# 上面這兩個函數不常直接用,在理解TensorFlow的用法等有幫助,就先列在這裏了
# a = tf.constant([[1, 2, 3], [4, 5, 6]], dtype=tf.float32, name=a) with tf.Session() as sess: print(a.eval()) print("shape: ", a.get_shape(), ",type: ", type(a.get_shape())) print("shape: ", a.get_shape().as_list(), ",type: ", type(a.get_shape().as_list())) ‘‘‘ 輸出如下 [[1. 2. 3.] [4. 5. 6.]] shape: (2, 3) ,type: <class ‘tensorflow.python.framework.tensor_shape.TensorShape‘> shape: [2, 3] ,type: <class ‘list‘>
‘‘‘

代碼2

#tf.argmax
#tf.argmax(input, dimension, name=None) returns the index with the largest value across dimensions of a tensor.
# 上面註釋是英文的,翻譯下就是,tf.argmax()這個函數輸入的張量中,沿著指定維度中最大的一個值的索引,可以這樣理解,0就是行,1就是列,3等更大的就是沿著更好維度算,下面的例子理解下。
# 註意,返回的是索引號
a = tf.constant([[1, 6, 5], [2, 3, 4]])
with tf.Session() as sess:
   
print(a.eval()) print("argmax over axis 0") print(tf.argmax(a, 0).eval()) print("argmax over axis 1") print(tf.argmax(a, 1).eval()) ‘‘‘ [[1 6 5] [2 3 4]] argmax over axis 0 [1 0 0] argmax over axis 1 [1 2] ‘‘‘

代碼3

#tf.reduce_sum
#tf.reduce_sum(input_tensor, reduction_indices=None, keep_dims=False, name=None) computes the sum of elements across dimensions of a tensor. Unless keep_dims is true, the rank of the tensor is reduced by 1 for each entry in reduction_indices. If keep_dims is true, the reduced dimensions are retained with length 1. If reduction_indices has no entries, all dimensions are reduced, and a tensor with a single element is returned
# 大概翻譯下,就是求和,計算張量的各維度上的元素的和。還有兩個參數, keep_dims和reduction_indices,下面代碼執行並理解下
a = tf.constant([[1, 1, 1], [2, 2, 2]])
with tf.Session() as sess:
   print(a.eval())
   print("reduce_sum over entire matrix")
   print(tf.reduce_sum(a).eval())
   print("reduce_sum over axis 0")
   print(tf.reduce_sum(a, 0).eval())
   print("reduce_sum over axis 0 + keep dimensions")
   print(tf.reduce_sum(a, 0, keep_dims=True).eval())
   print("reduce_sum over axis 1")
   print(tf.reduce_sum(a, 1).eval())
   print("reduce_sum over axis 1 + keep dimensions")
   print(tf.reduce_sum(a, 1, keep_dims=True).eval())
‘‘‘
輸出:
[[1 1 1]
[2 2 2]]
reduce_sum over entire matrix
9
reduce_sum over axis 0
[3 3 3]
reduce_sum over axis 0 + keep dimensions
[[3 3 3]]
reduce_sum over axis 1
[3 6]
reduce_sum over axis 1 + keep dimensions
[[3]
[6]]
‘‘‘

代碼4

‘‘‘
tf.get_variable,是用來獲取或者創建一個變量的函數,它需要用一個初始化函數,根據給的shape初始化相同shape的tensor。初始化函數有很多種,前面一篇中也有介紹過,各種隨機初始化,常數初始化等,
# 看下面例子
tf.get_variable(name, shape=None, dtype=None, initializer=None, trainable=True) is used to get or create a variable instead of a direct call to tf.Variable. It uses an initializer instead of passing the value directly, as in tf.Variable. An initializer is a function that takes the shape and provides a tensor with that shape. Here are some initializers available in TensorFlow:
?tf.constant_initializer(value) initializes everything to the provided value,
?tf.random_uniform_initializer(a, b) initializes uniformly from [a, b],
?tf.random_normal_initializer(mean, stddev) initializes from the normal distribution with the given mean and standard deviation.
‘‘‘

my_initializer = tf.random_normal_initializer(mean=0, stddev=0.1)
v = tf.get_variable(v, shape=[2, 3], initializer=my_initializer)
with tf.Session() as sess:
   tf.initialize_all_variables().run()
   print(v.eval())

"""
[[ 0.14729649 -0.07507571 -0.00038549]
[-0.02985961 -0.01537443  0.14321376]]
"""

代碼5

my_initializer = tf.random_normal_initializer(mean=0, stddev=0.1)
v = tf.get_variable(v, shape=[2, 3], initializer=my_initializer)


# tf.variable_scope 這個函數是管理變量的命名空間的,這個也是方便tensorboard中能可視化,使模型邏輯流程等更容易理解等
# tf.variable_scope(scope_name) manages namespaces for names passed to tf.get_variable.
with tf.variable_scope(layer1):
   w = tf.get_variable(v, shape=[2, 3], initializer=my_initializer)
   print(w.name)

with tf.variable_scope(layer2):
   w = tf.get_variable(v, shape=[2, 3], initializer=my_initializer)
   print(w.name)
‘‘‘
layer1/v:0
layer2/v:0
‘‘‘

代碼6

‘‘‘
reuse_variables
上面的代碼只能運行一次,就是因為這個reuse_variables,
用這個scope.reuse_variables()來獲取(重用)之前創建的變量,而不是再創建一個新的
Note that you should run the cell above only once. If you run the code above more than once, an error message will be printed out: "ValueError: Variable layer1/v already exists, disallowed.". This is because we used tf.get_variable above, and this function doesn‘t allow creating variables with the existing names. We can solve this problem by using scope.reuse_variables() to get preivously created variables instead of creating new ones.
‘‘‘
my_initializer = tf.random_normal_initializer(mean=0, stddev=0.1)
v = tf.get_variable(v, shape=[2, 3], initializer=my_initializer)

with tf.variable_scope(layer1):
   w = tf.get_variable(v, shape=[2, 3], initializer=my_initializer)
   # print(w.name)

with tf.variable_scope(layer2):
   w = tf.get_variable(v, shape=[2, 3], initializer=my_initializer)
   # print(w.name)

with tf.variable_scope(layer1, reuse=True):
   w = tf.get_variable(v)   # Unlike above, we don‘t need to specify shape and initializer
   print(w.name)

   # or
with tf.variable_scope(layer1) as scope:
   scope.reuse_variables()
   w = tf.get_variable(v)
   print(w.name)

小結

今天主要是貼代碼了,沒有過多的文字解釋,一些解釋也盡量放在代碼註釋中,這樣在本地自己電腦上運行也方便理解。初學的話代碼還是要自己敲一遍,自己敲的過程中肯定會遇到一些問題,這就是反復學習的過程,還是不要省。
今天代碼中的英文依然沒有抹去,盡量看一下,順便學下英語也不錯。主要是因為有些英文文檔不好翻譯過來反而不好理解,相信這種情況以後也會遇到。所以可以先學點。

本篇完

技術分享圖片

TensorFlow深度學習入門筆記(四)一些基本函數