TensorFlow深度学习入门笔记(四)一些基本函数

写在前面

学习建议:以下学习过程中有不理解可以简单查找下资料,但不必纠结(比如非得深究某一个函数等),尽量快速的学一遍,不求甚解无妨。多实操代码,不能只复制代码,或者感觉懂了就只看。熟能生巧,我亦无他,唯手熟尔

今天介绍一些基础函数及其用法,基本全是代码,一些解释都放在代码的注释里了。直接看代码吧,记得在你本地跑一下看哦

代码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:  
shape:  [2, 3] ,type:  
'''

代码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深度学习入门笔记(四)一些基本函数)