在学习深度学习等知识之前,首先得了解著名的框架TensorFlow里面的一些基础知识,下面首先看一下这个框架的一些基本用法。
import tensorflow as tf a = 3 # Python中普通的变量创建方式 # Create a variable. w = tf.Variable([[ 0.5 , 1.0 ]]) # tensorflow创建变量方式 x = tf.Variable([[ 2.0 ], [ 1.0 ]]) y = tf.matmul(w, x) # 矩阵内积 变量的操作 print (y) # tensor 里面没有具体的值 # variables have to be explicitly initialized before you can run Ops # 初始化全局变量 w,x,y init_op = tf.global_variables_initializer() # 计算图 with tf.Session() as sess: sess.run(init_op) print (y. eval ()) # 通过这种方式打印具体的值
得到的结果是:
Tensor("MatMul_2:0", shape=(1, 1), dtype=float32) [[2.]]
通过上面可以看出,只是简单的一个矩阵的乘法,我们就写了这么多的代码,看起来比较麻烦,但是没有办法,要用这个框架就必须按照它的用法去用,但是在用这个框架来写深度学习里面的代码,那就不是很复杂了。上面的代码展示了TensorFlow框架的基本用法,导入库、变量定义、初始化变量、Session操作、然后才能进行具体的操作。
下面学习一下TensorFlow框架中一些函数的用法,可以和numpy库中的一些函数对比着学习。
from numpy import int32 # float32 在TensorFlow最好使用这种格式 # 创建都是0的矩阵 tf.zeros([ 3 , 4 ], int32) # ==> [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]] tensor = tf.constant([[ 1 , 2 , 3 ], [ 4 , 5 , 6 ]]) # 矩阵格式相似 tf.zeros_like(tensor) # ==> [[0, 0, 0], [0, 0, 0]] # 矩阵元素都为1 tf.ones([ 2 , 3 ], int32) # ==> [[1, 1, 1], [1, 1, 1]] tf.ones_like(tensor) # ==> [[1, 1, 1], [1, 1, 1]] # Constant 1-D Tensor populated with value list. # 创建一个常量,必须使用这种方式 tensor = tf.constant([ 1 , 2 , 3 , 4 , 5 , 6 , 7 ]) # => [1 2 3 4 5 6 7] # Constant 2-D tensor populated with scalar value -1. # 创建二维矩阵常量 tensor = tf.constant( - 1.0 , shape = [ 2 , 3 ]) # => [[-1. -1. -1.] # [-1. -1. -1.]] # 创建间隔矩阵 tf.linspace( 10.0 , 12.0 , 3 , name = "linspace" ) # => [ 10.0 11.0 12.0] # 'start' is 3 # 'limit' is 18 # 'delta' is 3 # tf.range(start, limit, delta) tf. range ( 3 , 18 , 3 ) # ==> [3, 6, 9, 12, 15]
可以看出TensorFlow里面一些函数和numpy里面的用法差不多,下面看看TensorFlow中随机数的一些用法。
# 高斯分布的均值矩阵 指定均值和方差 norm = tf.random_normal([ 2 , 3 ], mean = - 1 , stddev = 4 ) # Shuffle the first dimension of a tensor c = tf.constant([[ 1 , 2 ], [ 3 , 4 ], [ 5 , 6 ]]) # shuffle操作 shuff = tf.random_shuffle(c) # Each time we run these ops, different results are generated # 要执行这些操作的方法。推荐使用上面With结构 sess = tf.Session() print (sess.run(norm)) print (sess.run(shuff))
运行得到的结果是
[[-2.4004993 5.356218 0.51297414] [-4.353016 2.234075 -4.2948236 ]] [[1 2] [3 4] [5 6]]
下面来看一个使用TensorFlow完成打印0到4之间的数字这样的一个小栗子,在原生Python中很简单,主要看看在TensorFlow中的用法。
# 打印0到4之间的的值 state = tf.Variable( 0 ) # 初始化常量0 new_value = tf.add(state, tf.constant( 1 )) # 执行加1操作 update = tf.assign(state, new_value) # 将new_value赋给state # Session计算块 with tf.Session() as sess: sess.run(tf.global_variables_initializer()) print (sess.run(state)) for _ in range ( 3 ): sess.run(update) print (sess.run(state))
得到的结果是
0 1 2 3
下面再来看看在创建变量时将numpy里面的格式转换为tensor格式,但是并不推荐使用这种方法
import numpy as np a = np.zeros(( 3 , 3 )) # 将numpy里面的格式转换为tensor格式,并不推荐使用这种方法 # 推荐使用上面创建变量的方法 ta = tf.convert_to_tensor(a) with tf.Session() as sess: print (sess.run(ta))
得到的结果是
[[0. 0. 0.] [0. 0. 0.] [0. 0. 0.]]
下面再来看看TensorFlow中占位符的用法
# 创建占位符,用的时候再具体赋值。 input1 = tf.placeholder(tf.float32) input2 = tf.placeholder(tf.float32) output = tf.multiply(input1, input2) # 矩阵元素相乘 with tf.Session() as sess: print (sess.run([output], feed_dict = {input1:[ 7. ], input2:[ 2. ]}))
得到的结果是
[array([14.], dtype=float32)]
总结,这篇博文包含了TensorFlow框架中一些常见的用法,但是肯定很多细节没有写全,只是写了一些大概的用法留作以后查看。