您的位置:首頁>正文

教程|深度學習指導——利用TF建立神經網路模型(上)

本文提供了代碼, 代碼並不完整, 但都會指導你如何一步步地去達到自己的 目的。 代碼很簡單, 瞭解機器學習的都能跟隨指導去完成任務。 目前相關文章都是為機器學習入門者而寫。

import pickle as p

import numpy as np

import tensorflow as tf

import random

import lab1_utils as utils

import tensorflow as tf

import numpy as np

import matplotlib.pyplot as plt

%matplotlib inline

TensorFlow入門

什麼是計算圖?

TensorFlow中的一切操作都是建立一個計算圖。 什麼是計算圖? 它只是一系列以某種順序發生的數學運算。 這裡是一個簡單的計算圖的例子:

此圖需要2個輸入(a, b)並計算輸出(e)。 圖中的每個節點都是一個操作, 它接受一些輸入, 執行一些計算, 並將其輸出傳遞給另一個節點。

我們可以通過以下方式在TensorFlow中創建此計算圖:

a = tf.placeholder(tf.float32)

b = tf.placeholder(tf.float32)

c = tf.add(a, b)

d = tf.sub(b, 1)

e = tf.mul(c, d)

Tensorflow使用tf.placeholder()來處理模型的輸入。 這就像在餐廳預訂: 餐廳為5人預訂了一個地方, 但你和你朋友可以自由任選其中座位。 tf.placeholder()允許你指定某些即將輸入的任意形狀和類型的輸入。 只有當你運行計算圖時, 你才能提供此輸入資料的值。 你可以這樣運行這個簡單的計算圖:

with tf.Session() as session:

a_data, b_data = 3.0, 6.0

feed_dict = {a: a_data, b: b_data}

output = session.run([e], feed_dict=feed_dict)

print(output) # 45.0

我們使用feed_dict將實際輸入資料傳遞到圖形中。 我們使用session.run()從圖中的結點c獲取輸出。 因為e在圖的結尾, 所以結束運行整個圖並返回數字45 - cool!

在Tensorflow中實現神經網路

我們可以使用計算圖定義TensorFlow中的神經網路。 這是一個非常簡單的神經網路的例子(單層感知器):

此圖接受輸入(x)並計算輸出(out)。 它與我們在分類演算法中學到的一樣, out = sigmoid(W * x + b)。

我們可以通過以下方式在TensorFlow中創建此計算圖:

n_input_nodes = 2

n_output_nodes = 1

x = tf.placeholder(tf.float32, (None, n_input_nodes))

W = tf.Variable(tf.ones((n_input_nodes, n_output_nodes)), dtype=tf.float32)

b = tf.Variable(tf.zeros(n_output_nodes), dtype=tf.float32)

'''TODO: Define the operation for z (use tf.matmul to multiply W and x).'''

z = #todo

'''TODO: Define the operation for out (use tf.sigmoid).'''

out = #todo

要運行此圖, 我們再次使用session.run()和通過feed_dict輸入資料。

test_input = [[0.5, 0.5]]

with tf.Session() as session:

tf.global_variables_initializer().run(session=session)

feed_dict = {x: test_input}

output = session.run([out], feed_dict=feed_dict)

print(output[0]) # This should output 0.73105. If not, double-check your code above

當我們需要的時候, 我們還可以設置tf.Variable的值。 下面是一個例子, 我們自己設置tf.Variable的值。 我們創建了一個分類資料集, 供你使用, 並讓你能夠查看決策邊界隨模型參數(權重和偏差)如何變化。 去嘗試獲取所有的正確資料點(綠色)!

''TODO: manually optimize weight_values and bias_value to classify points'''

# Modify weight_values, bias_value in the above code to adjust the decision boundary

# See if you can classify all the points correctly (all markers green)

weight_values = np.array([[-0.1], [0.2]]) # TODO change values and re-run

bias_value = np.array([[0.5]]) #TODO change values and re-run

# A pretty good boundary is made with:

# weight_values = np.array([[0.03], [0.12]])

# bias_value = np.array([[-0.5]])

x = tf.placeholder(tf.float32, (None, 2), name='x')

W = tf.Variable(weight_values, name='W', dtype=tf.float32)

b = tf.Variable(bias_value, name='b', dtype=tf.float32)

z = tf.matmul(x, W) + b

out = tf.sigmoid(z)

data = np.array([[2, 7], [1, 7], [3, 1], [3, 3], [4, 3], [4, 6], [6, 5], [7, 7], [7, 5], [2, 4], [2, 2]])

y = np.array([1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0])

with tf.Session() as session:

tf.global_variables_initializer().run(session=session)

utils.classify_and_plot(data, y, x, out, session)

下一篇文章我將介紹如何使用TensorFlow進行情感分析!

同類文章
Next Article
喜欢就按个赞吧!!!
点击关闭提示