TF学习:Tensorflow基础案例、经典案例集合——基于python编程代码的实现
生活随笔
收集整理的這篇文章主要介紹了
TF学习:Tensorflow基础案例、经典案例集合——基于python编程代码的实现
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
TF學習:Tensorflow基礎案例、經典案例集合——基于python編程代碼的實現
?
目錄
Tensorflow的使用入門
1、TF:使用Tensorflow輸出一句話
2、TF實現加法
3、TF實現乘法
4、TF實現計算功能
5、TF:Tensorflow完成一次線性函數計算
Tensorflow的基礎案例
1、TF根據三維數據擬合平面
Tensorflow的經典案例
?
?
?
相關文章
TF學習——DL框架之Tensorflow:Tensorflow的簡介、安裝、使用方法之詳細攻略
?
Tensorflow的使用入門
1、TF:使用Tensorflow輸出一句話
#TF:使用Tensorflow輸出一句話 import tensorflow as tf import numpy as npgreeting = tf.constant('Hello Google Tensorflow!') sess = tf.Session() #啟動一個會話 result = sess.run(greeting) #使用會話執行greeting計算模塊 print(result) sess.close() #關閉會話,這是一種顯式關閉會話的方式?
2、TF實現加法
張量和圖的兩種方式實現:聲明兩個常量 a 和 b,并定義一個加法運算。先定義一張圖,然后運行它,
# -*- coding: utf-8 -*-#1、張量和圖的兩種方式實現:聲明兩個常量 a 和 b,并定義一個加法運算。先定義一張圖,然后運行它, import tensorflow as tf import os import numpy as np os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' #T1 a=tf.constant([1,0,1,4]) b=tf.constant([ 1 , 0 , 0 , 4 ]) result=a+b sess=tf. Session () print (sess.run(result)) sess.close #T2 with tf.Session() as sess:a=tf.constant([ 1 , 0 , 1 , 4 ])b=tf.constant([ 1 , 0 , 0 , 4 ])result=a+bprint (sess.run(result))#2、常量和變量 #TensorFlow 中最基本的單位是常量(Constant)、變量(Variable)和占位符(Placeholder)。常量定義后值和維度不可變,變量定義后值可變而維度不可變。在神經網絡中,變量一般可作為儲存權重和其他信息的矩陣,而常量可作為儲存超參數或其他結構信息的變量。下面我們分別定義了常量與變量 #聲明了不同的常量(tf.constant()) a = tf.constant( 2 , tf.int16) #聲明了不同的整數型數據 b = tf.constant( 4 , tf.float32) #聲明了不同的浮點型數據 c = tf.constant( 8 , tf.float32) #聲明了不同的變量(tf.Variable()) d = tf. Variable ( 2 , tf.int16) e = tf. Variable ( 4 , tf.float32) f = tf. Variable ( 8 , tf.float32) g = tf.constant(np.zeros(shape=( 2 , 2 ), dtype=np.float32))#聲明結合了 TensorFlow 和 Numpy h = tf.zeros([ 11 ], tf.int16) #產生全是0的矩陣 i = tf.ones([ 2 , 2 ], tf.float32) #產生全是 1的矩陣 j = tf.zeros([ 1000 , 4 , 3 ], tf.float64) k = tf. Variable (tf.zeros([ 2 , 2 ], tf.float32)) l = tf. Variable (tf.zeros([ 5 , 6 , 5 ], tf.float32))#聲明一個 2 行 3 列的變量矩陣,該變量的值服從標準差為 1 的正態分布,并隨機生成 w1=tf.Variable(tf.random_normal([2,3],stddev=1,seed=1)) #TensorFlow 還有 tf.truncated_normal() 函數,即截斷正態分布隨機數,它只保留 [mean-2*stddev,mean+2*stddev] 范圍內的隨機數 #案例應用:應用變量來定義神經網絡中的權重矩陣和偏置項向量 weights = tf.Variable(tf.truncated_normal([256 * 256, 10])) biases = tf. Variable (tf.zeros([10])) print (weights.get_shape().as_list()) print (biases.get_shape().as_list())?
3、TF實現乘法
Tensorflow之session會話的使用,定義兩個矩陣,兩種方法輸出2個矩陣相乘的結果
import tensorflow as tfmatrix1 = tf.constant([[3, 20]]) matrix2 = tf.constant([[6], [100]]) product = tf.matmul(matrix1, matrix2) # method 1,常規方法 sess = tf.Session() result = sess.run(product) print(result) sess.close() # # method 2,with方法 # with tf.Session() as sess: # # result2 = sess.run(product) # print(result2)?
4、TF實現計算功能
TF:Tensorflow定義變量+常量,實現輸出計數功能
輸出結果
代碼設計
#TF:Tensorflow定義變量+常量,實現輸出計數功能import tensorflow as tfstate = tf.Variable(0, name='Parameter_name_counter') #print(state.name) one = tf.constant(1) new_value = tf.add(state, one) update = tf.assign(state, new_value)init = tf.global_variables_initializer() with tf.Session() as sess: sess.run(init) for _ in range(8):sess.run(update)print(sess.run(state))?
5、TF:Tensorflow完成一次線性函數計算
#TF:Tensorflow完成一次線性函數計算 #思路:TF像搭積木一樣將各個不同的計算模塊拼接成流程圖,完成一次線性函數的計算,并在一個隱式會話中執行。 matrix1 = tf.constant([[3., 3.]]) #聲明matrix1為TF的一個1*2的行向量 matrix2 = tf.constant([[2.],[2.]]) #聲明matrix2為TF的一個2*1的列向量 product = tf.matmul(matrix1, matrix2) #兩個算子相乘,作為新算例 linear = tf.add(product, tf.constant(2.0)) #將product與一個標量2求和拼接.作為最終的linear算例#直接在會話中執行linear算例,相當于將上面所有的單獨算例拼接成流程圖來執行 with tf.Session() as sess:result = sess.run(linear)print(result)?
?
Tensorflow的基礎案例
1、TF根據三維數據擬合平面
Python 程序生成了一些三維數據, 然后用一個平面擬合它.
import tensorflow as tf import numpy as np# 使用 NumPy 生成假數據(phony data), 總共 100 個點. x_data = np.float32(np.random.rand(2, 100)) # 隨機輸入 y_data = np.dot([0.100, 0.200], x_data) + 0.300# 構造一個線性模型 # b = tf.Variable(tf.zeros([1])) W = tf.Variable(tf.random_uniform([1, 2], -1.0, 1.0)) y = tf.matmul(W, x_data) + b# 最小化方差 loss = tf.reduce_mean(tf.square(y - y_data)) optimizer = tf.train.GradientDescentOptimizer(0.5) train = optimizer.minimize(loss)# 初始化變量 init = tf.initialize_all_variables()# 啟動圖 (graph) sess = tf.Session() sess.run(init)# 擬合平面 for step in xrange(0, 201):sess.run(train)if step % 20 == 0:print step, sess.run(W), sess.run(b)# 得到最佳擬合結果 W: [[0.100 0.200]], b: [0.300]?
?
Tensorflow的經典案例
后期更新……
?
?
?
?
?
總結
以上是生活随笔為你收集整理的TF学习:Tensorflow基础案例、经典案例集合——基于python编程代码的实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 成功解决Instructions for
- 下一篇: Py之matplotlib:python