TensorFlow(二)
TensorFlow
控制流
tf.cond
a=tf.constant(2)
b=tf.constant(3)
x=tf.constant(4)
y=tf.constant(5)
z = tf.multiply(a, b)
result = tf.cond(x < y, lambda: tf.add(x, z), lambda: tf.square(y))
with tf.Session() as session:
print(result.eval())
tf.case
decode_png = lambda :tf.image.decode_png(image_tensor, channels) decode_jpg = lambda :tf.image.decode_jpeg(image_tensor, channels) decoder = { tf.equal(image_ext, '.png'): decode_png, tf.equal(image_ext, '.jpg'): decode_jpg} image_tensor = tf.case(decoder, default = decode_png, exclusive = True)
TFLite
Tensorflow原始碼中自帶的toco工具,可用於生成一個可供TensorFlow Lite框架使用的tflite檔案。
程式碼:
https://github.com/tensorflow/tensorflow/tree/master/tensorflow/contrib/lite/toco
參考:
https://www.jianshu.com/p/fa204a54a956
生成TFLite模型檔案
https://mp.weixin.qq.com/s/eSczqqyzh4PZomJL4saxug
出門問問:使用TensorFlow Lite在嵌入式端部署熱詞檢測模型
玩轉TensorFlow Lite:有道雲筆記實操案例分享
https://mp.weixin.qq.com/s/lNP9WdzSWE4FjB_-Sjc2aA
TensorFlow Lite for Android初探
Broadcast
Broadcast是一種填充元素以使運算元的形狀相匹配的操作。例如,對一個[3,2]的張量和一個[3,1]的張量相加在TF中是合法的,TF會使用預設的規則將[3,1]的張量填充為[3,2]的張量,從而使操作能夠執行下去。
參考:
https://www.cnblogs.com/yangmang/p/7125458.html
numpy陣列廣播
https://blog.csdn.net/LoseInVain/article/details/78763303
TensorFlow中的廣播Broadcast機制
TensorFlow Serving
TensorFlow Serving是一個用於機器學習模型serving的高效能開源庫。它可以將訓練好的機器學習模型部署到線上,使用gRPC作為介面接受外部呼叫。更加讓人眼前一亮的是,它支援模型熱更新與自動模型版本管理。
程式碼:
https://github.com/tensorflow/serving
TensorFlow Serving實際上是TensorFlow Extended (TFX)的一部分:
https://tensorflow.google.cn/tfx
TFX還包括了Data Validation、Transform和Model Analysis等方面的功能。
參考:
https://zhuanlan.zhihu.com/p/23361413
TensorFlow Serving嚐嚐鮮
http://www.cnblogs.com/xuchenCN/p/5888638.html
tensorflow serving
https://mp.weixin.qq.com/s/iqvpX6QuBEmF_UK9RMu9eQ
TensorFlow Serving入門
https://mp.weixin.qq.com/s/TL87BY3DdP1bolc0Sxkahg
gRPC客戶端建立和呼叫原理解析
https://zhuanlan.zhihu.com/p/30628048
遠端通訊協議:從CORBA到gRPC
https://mp.weixin.qq.com/s/b569est_LpcxsoTNWXcfog
TensorFlow Extended幫你快速落地專案
https://mp.weixin.qq.com/s/qOy9fR8Zd3SufvsMmLpoGg
使用TensorFlow Serving優化TensorFlow模型
https://mp.weixin.qq.com/s/IPwOZKvDsONegyIuwkG6bQ
將深度學習模型部署為web應用有多難?答案自己找
https://mp.weixin.qq.com/s/7nugWFKtD-C6cpwm2TyvdQ
手把手教你如何部署深度學習模型
op的C++實現
有的時候為了將Tensorflow的op移植到其他平臺,需要找到相應op的cpu實現。比如space_to_batch這個op,它的實現在:
core/kernels/spacetobatch_op.cc
簡單的op一般找到這裡就可以了,但space_to_batch還要更深一層:
core/kernels/spacetobatch_functor.cc
一般XXX_impl.cc或者XXX_functor.cc才是op實現真正所在的位置。
此外,TFlite的實現往往更加簡單:
tensorflow/contrib/lite/kernels/internal/reference/reference_ops.h
TensorFlow.js
https://mp.weixin.qq.com/s/dqMS4NjmNYs7IFHm8uFM8w
TensorFlow釋出面向JavaScript開發者的機器學習框架TensorFlow.js
https://zhuanlan.zhihu.com/p/35181413
TensorFlow.js人臉識別—玩轉吃豆豆小遊戲
https://mp.weixin.qq.com/s/ebLHZAG8H78TsZUKSzAtIw
TF官方部落格:基於TensorFlow.js框架的瀏覽器實時姿態估計
https://mp.weixin.qq.com/s/z6p4A4DfCuK8IBGVGwrtLQ
如何利用TensorFlow.js部署簡單的AI版“你畫我猜”影象識別應用
https://mp.weixin.qq.com/s/NO_XY-JmTpIkoC-fpkZ-qg
在瀏覽器上也能訓練神經網路?TensorFlow.js帶你玩遊戲~
Eager Execution
TensorFlow的Eager Execution可立即評估操作,無需構建圖:操作會返回具體的值,而不是構建以後再執行的計算圖。這也就是所謂的動態圖計算的概念。
參考:
https://mp.weixin.qq.com/s/Yp2zE85VCx8q67YXvuw5qw
TensorFlow引入了動態圖機制Eager Execution
https://github.com/ZhuanZhiCode/TensorFlow-Eager-Execution-Examples
Eager Execution的程式碼示例
https://mp.weixin.qq.com/s/By_GKPtY6xr8MwkWA6frzA
TensorFlow的動態圖工具Eager怎麼用?這是一篇極簡教程
https://mp.weixin.qq.com/s/Lvd4NfLg0Lzivb4BingV7w
Tensorflow Eager Execution入門指南
https://mp.weixin.qq.com/s/q6bJfCV5kU8BzvWjOXkCDg
簡單粗暴TensorFlow Eager教程
https://mp.weixin.qq.com/s/zz8XCykJ6jxbE5J4YwAkEA
一招教你使用tf.keras和eager execution解決複雜問題
Estimator
Estimator是一個非常高階的API,其抽象等級甚至在Keras之上。
Estimator主要包括以下部分:
1.初始化。定義網路結構。
2.train。
3.evaluate。
4.predict。
TensorFlow已經包含了一些預置的Estimator。例如:BoostedTreesClassifier、DNNClassifier、LinearClassifier等。具體可參見:
https://tensorflow.google.cn/api_docs/python/tf/estimator
參考:
https://mp.weixin.qq.com/s/a68brFJthczgwiFoUBh30A
TensorFlow資料集和估算器介紹
細節
執行session.run(out)
,會在終端列印out的值,但執行res = session.run(out)
則不會。
tensorflow的程式中,在main函式下,都是使用tf.app.run()來啟動。檢視原始碼可知,該函式是用來處理flag解析,然後執行main函式。
https://blog.csdn.net/lujiandong1/article/details/53262612
tensorflow中的tf.app.run()
TF提供了一套專門的IO函式:tf.gfile。主要優點在於:對於寫檔案來說,open操作直到真的需要寫的時候才執行。
遷移學習的時候,有的時候需要保持某幾層的權值,在後續訓練中不被改變。這時,可以在建立Variable時,令trainable=false。
sparse_softmax_cross_entropy_with_logits和softmax_cross_entropy_with_logits的區別在於:後者的label是一個one hot的tensor,而前者label直接用對應分類的index表示就行了。
blog
http://www.jianshu.com/u/eaec1fc422e9
一個TF的blog
http://blog.csdn.net/u012436149
一個TensorFlow+PyTorch的blog
我的TensorFlow實踐
MNIST+Softmax
程式碼:
https://github.com/antkillerfarm/antkillerfarm_crazy/tree/master/python/ml/tensorflow/hello_mnist.py
MNIST+CNN
程式碼:
https://github.com/antkillerfarm/antkillerfarm_crazy/tree/master/python/ml/tensorflow/hello_cnn.py
第一個例子中,我對CPU的計算能力還沒有切膚之痛,但在這裡使用CPU差不多要花半個小時時間。。。