第四百一十六節,Tensorflow簡介與安裝html
TensorFlow是什麼python
Tensorflow是一個Google開發的第二代機器學習系統,克服了第一代系統DistBelief僅能開發神經網絡算法、難以配置、依賴Google內部硬件等侷限性,應用更加普遍,而且提升了靈活性和可移植性,速度和擴展性也有了大幅提升。字面上理解,TensorFlow就是以張量(Tensor)在計算圖(Graph)上流動(Flow)的方式的實現和執行機器學習算法的框架。具備如下特色:git
Google第一代分佈式機器學習框架DistBelief在內部大規模使用後沒有選擇開源,而第二代TensorFlow於2015年11月在GitHub上開源,並在持續快速開發迭代中。TensorFlow最先由Google Brain的工程師開發,設計初衷是加速機器學習的研究,並快速地將研究原型轉化爲產品。Google選擇開源TensorFlow的緣由很簡單:第一是但願藉助社區的力量,你們一塊兒完善TensorFlow。第二是回饋社區,Google但願讓這個優秀的工具獲得更多的應用,提升學術界和工業界使用機器學習的效率。
自從2015年11月開源以來,TensorFlow迅速在衆多的機器學習框架中脫穎而出,在Github上得到了最多的Star
Google第一代分佈式機器學習框架DistBelief在內部大規模使用後沒有選擇開源,而第二代TensorFlow於2015年11月在GitHub上開源,並在持續快速開發迭代中。TensorFlow最先由Google Brain的工程師開發,設計初衷是加速機器學習的研究,並快速地將研究原型轉化爲產品。Google選擇開源TensorFlow的緣由很簡單:第一是但願藉助社區的力量,你們一塊兒完善TensorFlow。第二是回饋社區,Google但願讓這個優秀的工具獲得更多的應用,提升學術界和工業界使用機器學習的效率。github
自從2015年11月開源以來,TensorFlow迅速在衆多的機器學習框架中脫穎而出,在Github上得到了最多的Star.算法
安裝 pip install tensorflow服務器
安裝時會安裝如下依賴markdown
absl-py-0.4.1
astor-0.7.1 gast-0.2.0
grpcio-1.14.2
markdown-2.6.11
numpy-1.14.5
protobuf-3.6.1
setuptools-39.1.0
six-1.11.0
tensorboard-1.10.0
termcolor-1.1.0
werkzeug-0.14.1網絡
設置變量框架
import tensorflow as tf # python建立變量 a = 3 # tensorflow建立一個變量Variable # 建立橫向量 w = tf.Variable([[0.5, 1.0]]) # 建立豎向量 x = tf.Variable([[4.0], [1.0]]) # 橫向量乘以豎向量matmul y = tf.matmul(w, x) # 全局變量初始化global_variables_initializer init_op = tf.global_variables_initializer() with tf.Session() as sess: # 執行計算run sess.run(init_op) # 打印結果eval
[[ 2.]]
注意:若是PyCharm提示如下信息,加兩行代碼便可
2018-09-10 21:02:19.937491: I T:\src\github\tensorflow\tensorflow\core\platform\cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
#默認爲0:輸出全部log信息
#設置爲1:進一步屏蔽INFO信息
#設置爲2:進一步屏蔽WARNING信息dom
#設置爲3:進一步屏蔽ERROR信息
import tensorflow as tf import os os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' # python建立變量 a = 3 # tensorflow建立一個變量Variable # 建立橫向量 w = tf.Variable([[0.5, 1.0]]) # 建立豎向量 x = tf.Variable([[4.0], [1.0]]) # 橫向量乘以豎向量matmul y = tf.matmul(w, x) # 全局變量初始化global_variables_initializer init_op = tf.global_variables_initializer() with tf.Session() as sess: # 執行計算run sess.run(init_op) # 打印結果eval print(y.eval())
tensorflow不少操做跟numpy有些相似的
tf.zeros([3, 4], int32) ==> [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
tf.zeros_like(tensor) ==> [[0, 0, 0], [0, 0, 0]]
tf.ones([2, 3], int32) ==> [[1, 1, 1], [1, 1, 1]]
tf.ones_like(tensor) ==> [[1, 1, 1], [1, 1, 1]]
tensor = tf.constant([1, 2, 3, 4, 5, 6, 7]) => [1 2 3 4 5 6 7]
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]
tf.range(start, limit, delta) ==> [3, 6, 9, 12, 15]
隨機
import tensorflow as tf import os os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' # 生成的值服從具備指定平均值和標準誤差的正態分佈 norm = tf.random_normal([2, 3], mean=-1, stddev=4) # 洗牌 c = tf.constant([[1, 2], [3, 4], [5, 6]]) shuff = tf.random_shuffle(c) # 每一次執行結果都會不一樣 sess = tf.Session() print(sess.run(norm)) print(sess.run(shuff))
[[-5.58110332 0.84881377 7.51961231] [ 3.27404118 -7.22483826 7.70631599]] [[5 6] [1 2] [3 4]]
循環加1
import tensorflow as tf import os os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' state = tf.Variable(0) # 每初始化一次加1 # add加 new_value = tf.add(state, tf.constant(1)) # assign從新賦值 update = tf.assign(state, new_value) with tf.Session() as sess: # 初始化 sess.run(tf.global_variables_initializer()) print(sess.run(state)) # 循環3次 for _ in range(3): sess.run(update) print(sess.run(state))
0 1 2 3
加減乘除
import tensorflow as tf import os os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' a = tf.constant(5.0) b = tf.constant(10.0) ts1 = tf.constant(8.0) ts2 = tf.constant(9.0) x = tf.add(a, b, name="add") y = tf.div(a, b, name="divide") # (1)加法+ ts_add1 = tf.add(ts1, ts2, name=None) ts_add2 = ts1 + ts2 # 兩者等價 # (2)減法- ts_sub1 = tf.subtract(ts1, ts2, name=None) ts_sub2 = ts1 - ts2 # 兩者等價 # (3)乘法* ts_mul1 = tf.multiply(ts1, ts2, name=None) ts_mul2 = ts1 * ts2 # (4)除法/ ts_div1 = tf.divide(ts1, ts2, name=None) ts_div2 = tf.div(ts1, ts2, name=None) # div 支持 broadcasting(即shape可不一樣) ts_div3 = ts1 / ts2 with tf.Session() as sess: print("a =", sess.run(a)) print("b =", sess.run(b)) print("a + b =", sess.run(x)) print("a/b =", sess.run(y)) print("ts_sub1", sess.run(ts_sub1))
a = 5.0
b = 10.0
a + b = 15.0
a/b = 0.5
ts_sub1 -1.0
挖坑
import tensorflow as tf import os os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' # 理解爲挖一個32的坑 input1 = tf.placeholder(tf.float32) input2 = tf.placeholder(tf.float32) # 兩個坑相乘 output = tf.multiply(input1, input2) with tf.Session() as sess: # 分別向兩個坑填數據feed_dict print(sess.run([output], feed_dict={input1: [7.], input2: [2.]}))
[array([ 14.], dtype=float32)]