国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

TensorFlow 新手入門

2019-11-06 06:03:45
字體:
來源:轉載
供稿:網友

剛裝上TensorFlow,還是不太會用,主要去官網還要翻墻太麻煩了。。隨手翻一下教程備用


初識TensorFlow

初期準備:

安裝好TensorFlow知道如何在Python中編程懂一點數組知識最好了解機器學習(不必要)

TensorFLow提供多種APIs,從低級到高級,滿足不同使用需求,越高級越容易學習和使用。下面的一些模型都可以用tf.contrib.learn高級API實現。

Tensors

TensorFlow最重要的數據單元就是tensor(張量)。一個tensor包括了任意維度的數組的原始值。tensor的rank代表其維度,如:

3 # a rank 0 tensor; this is a scalar with shape [][1. ,2., 3.] # a rank 1 tensor; this is a vector with shape [3][[1., 2., 3.], [4., 5., 6.]] # a rank 2 tensor; a matrix with shape [2, 3][[[1., 2., 3.]], [[7., 8., 9.]]] # a rank 3 tensor with shape [2, 1, 3]

直觀感覺有幾層[]括號就有幾個rank。而shape是從括號外向里,數,的個數。

TensorFlow 核心教程


導入TensorFlow

python下基本的導入聲明:

import tensorflow as tf

大多數的文檔都假設已經導入了tf模塊。

計算圖(The Computational Graph)

你可以把TensorFlow核心程序想成兩個獨立的模塊:

搭建計算圖運行計算圖

計算圖computational graph是一系列布置為節點圖的TensorFlow操作。每個節點將0個或更多張量作為輸入并產生一個張量作為輸出。常數是其中一個節點類型,他沒有輸入,輸出其儲存的常量,建立兩個浮點數張量node1node2

node1 = tf.constant(3.0, tf.float32)node2 = tf.constant(4.0) # also tf.float32 implicitlyPRint(node1, node2)

打印為:

Tensor("Const:0", shape=(), dtype=float32) Tensor("Const_1:0", shape=(), dtype=float32)

注意其輸出并不是3.04.0。相反,他們是節點,當被評價(when evaluated)時,就會輸出3.04.0。為了確切評價節點,我們必須用session運行一個計算圖。一個session封裝了TensorFlow運行時的控件和狀態。

下面是一個使用Session運行的例子:

sess = tf.Session()print(sess.run([node1, node2]))

得到輸出:

[3.0, 4.0]

我們可以將Tensor節點與運算(運算操作也是節點)結合搭建更為復雜的計算圖。比如我們可以將兩個常數相加:

node3 = tf.add(node1, node2)print("node3: ", node3)print("sess.run(node3): ",sess.run(node3))

輸出:

node3: Tensor("Add_2:0", shape=(), dtype=float32)sess.run(node3): 7.0

TensorFlow提供了一個名為TensorBoard的工具用來顯示計算圖,上面的計算過程可以可視化表示為下圖: add

由于輸入是常數,這張圖的輸出結果是恒定的。一個圖可以參數化為接受外部輸入,稱為placeholders(占位符),用于為之后的數據占取空間。

a = tf.placeholder(tf.float32)b = tf.placeholder(tf.float32)adder_node = a + b # + provides a shortcut for tf.add(a, b)

下面三行有點像一個函數或一個lambda,其中我們定義兩個輸入參數(a和b),然后對它們進行操作。 我們可以通過使用feed_dict參數指定為這些占位符提供具體值的Tensors,使用多個輸入來評估此圖:

print(sess.run(adder_node, {a: 3, b:4.5}))print(sess.run(adder_node, {a: [1,3], b: [2, 4]}))

輸出的結果:

7.5[ 3. 7.]

在TensorBoard,圖看起來是這樣的: add_node 我們可以使用計算圖做一些更復雜的操作:

add_and_triple = adder_node * 3.print(sess.run(add_and_triple, {a: 3, b:4.5}))

結果:

22.5

可視化之后: add_three 在機器學習中,我們通常需要一個可以接受任意輸入的模型,例如上面的模型。 為了使模型可訓練,我們需要能夠修改圖以獲得具有相同輸入的新輸出。 Variables(變量)允許我們向圖中添加可訓練的參數。 它們使用類型和初始值構造:

W = tf.Variable([.3], tf.float32)b = tf.Variable([-.3], tf.float32)x = tf.placeholder(tf.float32)linear_model = W * x + b

當調用tf.constant時,常量被初始化,它們的值永遠不會改變。 相比之下,當調用tf.Variable時,變量不會被初始化。 要初始化TensorFlow程序中的所有變量,必須顯式調用特殊操作,如下所示:

init = tf.global_variables_initializer()sess.run(init)

重要的是理解init是TensorFlow子圖的句柄,它初始化所有的全局變量。 直到我們調用sess.run,變量是未初始化的。

由于x是一個占位符,我們可以同時對多個值進行輸入:

print(sess.run(linear_model, {x:[1,2,3,4]}))

結果為:

[ 0. 0.30000001 0.60000002 0.90000004]

我們已經創建了一個模型,但無法評價其性能。為了評估訓練數據的模型,需要一個占位符y來提供所需的值,我們需要寫一個損失函數。 損失函數測量當前模型與提供的數據之間的距離。 我們將使用用于線性回歸的標準損失模型,其將當前模型和提供的數據之間的增量的平方求和。 用linear_model - y創建一個向量,其中每個元素是對應的示例的誤差增量。 我們調用tf.square來平方誤差。 然后,我們對所有平方誤差求和,創建一個單一的標量,使用tf.reduce_sum抽象所有示例的錯誤:

y = tf.placeholder(tf.float32)squared_deltas = tf.square(linear_model - y)loss = tf.reduce_sum(squared_deltas)print(sess.run(loss, {x:[1,2,3,4], y:[0,-1,-2,-3]}))

結果為:

23.66

我們可以通過將Wb的值重新賦值為-1和1的完美值來手動改進。變量初始化為提供給tf.Variable的值,但可以使用類似tf.assign的操作來更改。 例如,W = -1b = 1是我們模型的最佳參數。 我們可以相應地改變W和b:

fixW = tf.assign(W, [-1.])fixb = tf.assign(b, [1.])sess.run([fixW, fixb])print(sess.run(loss, {x:[1,2,3,4], y:[0,-1,-2,-3]}))

最終的輸出為0:

0.0

我們人為猜測了Wb的“完美”值,但機器學習的整個要點是自動找到正確的模型參數。 我們將在下一節中說明如何完成這一任務。

tf.train API


機器學習的完整討論超出了本教程的范圍。 然而,TensorFlow提供了優化器,其緩慢地改變每個變量以便最小化損失函數。 最簡單的優化器是梯度下降。 它根據相對于該變量的損失導數的大小來修改每個變量。 一般來說,人工計算符號導數是繁瑣的并且容易出錯。 因此,TensorFlow可以使用函數tf.gradients自動產生僅給出模型描述的導數。 為了簡單起見,優化器通常會為您執行此操作。 例如,

optimizer = tf.train.GradientDescentOptimizer(0.01)train = optimizer.minimize(loss)sess.run(init) # reset values to incorrect defaults.for i in range(1000): sess.run(train, {x:[1,2,3,4], y:[0,-1,-2,-3]})print(sess.run([W, b]))

輸出的最終結果:

[array([-0.9999969], dtype=float32), array([ 0.99999082], dtype=float32)]

現在我們已經做了實際的機器學習! 雖然做這個簡單的線性回歸不需要太多的TensorFlow核心代碼,但是更復雜的模型和方法來將數據輸入到模型中需要更多的代碼。 因此,TensorFlow為通用模式、結構和功能提供了更高級別的抽象。 我們將在下一節中學習如何使用這些抽象。

完整程序

import numpy as npimport tensorflow as tf# Model parametersW = tf.Variable([.3], tf.float32)b = tf.Variable([-.3], tf.float32)# Model input and outputx = tf.placeholder(tf.float32)linear_model = W * x + by = tf.placeholder(tf.float32)# lossloss = tf.reduce_sum(tf.square(linear_model - y)) # sum of the squares# optimizeroptimizer = tf.train.GradientDescentOptimizer(0.01)train = optimizer.minimize(loss)# training datax_train = [1,2,3,4]y_train = [0,-1,-2,-3]# training loopinit = tf.global_variables_initializer()sess = tf.Session()sess.run(init) # reset values to wrongfor i in range(1000): sess.run(train, {x:x_train, y:y_train})# evaluate training accuracycurr_W, curr_b, curr_loss = sess.run([W, b, loss], {x:x_train, y:y_train})print("W: %s b: %s loss: %s"%(curr_W, curr_b, curr_loss))

運行后結果:

W: [-0.9999969] b: [ 0.99999082] loss: 5.69997e-11

其可視化圖: visual

tf.contrib.learn


tf.contrib.learn是一個高級TensorFlow庫,它簡化了機器學習的機制,包括以下內容:

運行訓練運行評價管理數據集管理輸入

tf.contrib.learn定義了許多常見的模型。

基本用法

注意,線性回歸程序用tf.contrib.learn變得更簡單:

import tensorflow as tf# NumPy is often used to load, manipulate and preprocess data.import numpy as np# Declare list of features. We only have one real-valued feature. There are many# other types of columns that are more complicated and useful.features = [tf.contrib.layers.real_valued_column("x", dimension=1)]# An estimator is the front end to invoke training (fitting) and evaluation# (inference). There are many predefined types like linear regression,# logistic regression, linear classification, logistic classification, and# many neural network classifiers and regressors. The following code# provides an estimator that does linear regression.estimator = tf.contrib.learn.LinearRegressor(feature_columns=features)# TensorFlow provides many helper methods to read and set up data sets.# Here we use `numpy_input_fn`. We have to tell the function how many batches# of data (num_epochs) we want and how big each batch should be.x = np.array([1., 2., 3., 4.])y = np.array([0., -1., -2., -3.])input_fn = tf.contrib.learn.io.numpy_input_fn({"x":x}, y, batch_size=4, num_epochs=1000)# We can invoke 1000 training steps by invoking the `fit` method and passing the# training data set.estimator.fit(input_fn=input_fn, steps=1000)# Here we evaluate how well our model did. In a real example, we would want# to use a separate validation and testing data set to avoid overfitting.estimator.evaluate(input_fn=input_fn)

運行后輸出:

{'global_step': 1000, 'loss': 1.9650059e-11}

自定義模型

tf.contrib.learn不會只能運行其預定義的模型。 假設我們想創建一個未內置到TensorFlow中的自定義模型。 我們仍然可以保留tf.contrib.learn的數據集,饋送,訓練等的高級抽象。 為了說明,我們將演示如何使用我們的低級TensorFlow API的知識來實現我們自己的等效模型到LinearRegressor。 要定義與tf.contrib.learn一起使用的自定義模型,我們需要使用tf.contrib.learn.Estimatortf.contrib.learn.LinearRegressor實際上是tf.contrib.learn.Estimator的子類。 而不是子類別Estimator,我們只是提供Estimator一個函數model_fn,告訴tf.contrib.learn如何評估預測,訓練步驟和損失。 代碼如下:

import numpy as npimport tensorflow as tf# Declare list of features, we only have one real-valued featuredef model(features, labels, mode): # Build a linear model and predict values W = tf.get_variable("W", [1], dtype=tf.float64) b = tf.get_variable("b", [1], dtype=tf.float64) y = W*features['x'] + b # Loss sub-graph loss = tf.reduce_sum(tf.square(y - labels)) # Training sub-graph global_step = tf.train.get_global_step() optimizer = tf.train.GradientDescentOptimizer(0.01) train = tf.group(optimizer.minimize(loss), tf.assign_add(global_step, 1)) # ModelFnOps connects subgraphs we built to the # appropriate functionality. return tf.contrib.learn.ModelFnOps( mode=mode, predictions=y, loss= loss, train_op=train)estimator = tf.contrib.learn.Estimator(model_fn=model)# define our data setx=np.array([1., 2., 3., 4.])y=np.array([0., -1., -2., -3.])input_fn = tf.contrib.learn.io.numpy_input_fn({"x": x}, y, 4, num_epochs=1000)# trainestimator.fit(input_fn=input_fn, steps=1000)# evaluate our modelprint(estimator.evaluate(input_fn=input_fn, steps=10))

運行后輸出:

{'loss': 5.9819476e-11, 'global_step': 1000}

注意自定義model()函數的內容與下層API的手動模型訓練循環非常相似。


用慣了caffe,感覺TensorFlow有點麻煩呢

TensorFlow Develop


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 阳谷县| 揭东县| 潍坊市| 怀宁县| 右玉县| 南汇区| 睢宁县| 霍山县| 闻喜县| 长垣县| 通海县| 绍兴市| 武穴市| 庄浪县| 隆德县| 彩票| 陇川县| 贵州省| 禹州市| 芜湖市| 民勤县| 桂阳县| 鞍山市| 衡阳市| 新龙县| 宝坻区| 宜宾县| 房山区| 长宁区| 揭阳市| 招远市| 天柱县| 惠水县| 十堰市| 永州市| 出国| 灵宝市| 遂宁市| 百色市| 杂多县| 天气|