【機器學習】搭建神經網路筆記

Bonstoppo發表於2018-12-25

一、簡單寫一個迴歸方程

import tensorflow as tf
import numpy as np

#creat data
x_data = np.random.rand(100).astype(np.float32)#在x中生成隨機數,隨機數以np的float32型別展示
y_data= x_data * 0.1 + 0.3 #基本的函式

# create tensorflow structure start#

Weights= tf.Variable(tf.random_uniform([1] , -1.0 , 1.0))#初始化Weights(權重)的張量,均勻分佈
biases = tf.Variable(tf.zeros([1]))#初始化biases(偏移量)張量,一維的資料

y = Weights * x_data + biases #依據的Weight和biases兩個建立一個模型
lost = tf.reduce_mean(tf.square(y - y_data))#lost的數值為求得的是(y-y.data)^2的平均值
optimizer = tf.train.GradientDescentOptimizer(0.5)#梯度下降優化器,範圍為0.5
train = optimizer.minimize(lost)
init = tf.global_variables_initializer();

# create tensorflow structure end #

sess = tf.Session()#建立訪問
sess.run(init) #執行

for step in range(201):
    sess.run(train)
    if(step % 20) == 0:
        print(step , sess.run(Weights), sess.run(biases))

 

二、tensorflow的會話機制:Session

#Session的兩種寫法
import tensorflow as tf

martix1 = tf.constant([[3 , 3]])
martix2 = tf.constant([[2],
                       [2]])
product = tf.matmul(martix1 , martix2)

# #method 1
# sess = tf.Session()#Session記得要大寫
# result = sess.run(product)
# print(result)
# sess.close()

#method 2
with tf.Session() as sess:
    result2 = sess.run(product)
    print(result2)

 

三、tensorflow的初始化機制:Variable

#Variable:建立變數
import tensorflow as tf

state = tf.Variable(0 , name = 'counter')
# print(state.name)

one = tf.constant(1)

new_value = tf.add(state , one)
update = tf.assign(state , new_value)#assign:轉讓編制;將new_value賦值給state,return state

#使用tf.global_variables_initializer()新增節點用於初始化所有的變數。
#在你構建完整個模型並在會話中載入模型後,執行這個節點。c231
init = tf.global_variables_initializer()#初始化模型

with tf.Session() as sess:
    sess.run(init)
    for _ in range(3):
        sess.run(update)
        print(sess.run(state))

 

四、placeholder

#placeholder:在執行的時候再去給我的值,而不是一開始就先賦值。
import tensorflow as tf

input1 = tf.placeholder(tf.float32)
input2 = tf.placeholder(tf.float32)

output = tf.multiply(input1 , input2)

with tf.Session() as sess:
    print(sess.run(output , feed_dict = {input1:[7.] , input2:[2.]}))#將feed_dict的數值傳入output

 

五、搭建一個神經網路

#定義一個新增層
#建造神經網路
%matplotlib qt5
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt

#新增激勵函式
def add_layer(inputs, in_size, out_size, activation_function = None):
    with tf.name_scope('layer'):
        Weights = tf.Variable(tf.random_normal([in_size , out_size]) , name = 'W')
    with tf.name_scope('biases'):
        biases = tf.Variable(tf.zeros([1 , out_size]) + 0.1 , name = 'b')#初始化讓所有的數值都是0.1
    with tf.name_scope('Wx_plus_b'):
        Wx_plus_b = tf.matmul(inputs , Weights) +  biases #矩陣的乘法,表示式
    if activation_function is None:    #沒有激勵的話直接輸出
        outputs = Wx_plus_b
    else:
        outputs = activation_function(Wx_plus_b)   #有激勵的話就做激勵
    return outputs


#定義資料形式
x_data = np.linspace(-1 , 1 , 300)[: , np.newaxis]#300行有300個例子
noise = np.random.normal(0 , 0.05 , x_data.shape)#形成一些噪點
y_data = np.square(x_data) - 0.5 + noise

with tf.name_scope('inputs'):
    xs = tf.placeholder(tf.float32 , [None , 1] , name = 'x_input')#傳進來的數值
    ys = tf.placeholder(tf.float32 , [None , 1] , name = 'y_input')#傳進來的數值   ?????

l1 = add_layer(xs , 1 , 10 , activation_function = tf.nn.relu)#隱藏層,10個因子
prediction = add_layer(l1 , 10 , 1 , activation_function = None)#輸出層
with tf.name_scope('loss'):
    loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys - prediction) , reduction_indices = [1]))#求誤差
with tf.name_scope('train'):
    train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss)#梯度下降演算法,學習效率0.1 

init = tf.global_variables_initializer()
sess = tf.Session()
writer = tf.summary.FileWriter("logs/" , sess.graph)
sess.run(init)

fig = plt.figure()
ax = fig.add_subplot(1 , 1 , 1)
ax.scatter(x_data , y_data)
plt.ion()
plt.show()
#plt.ioff()

for i in range(1000):
    sess.run(train_step , feed_dict = {xs:x_data , ys:y_data})
    if i % 50 == 0:
        try:
            ax.lines.remove(lines[0])
        except Exception:
            pass
        #print(sess.run(loss , feed_dict = {xs:x_data , ys:y_data}))
        prediction_value = sess.run(prediction , feed_dict = {xs : x_data})
        lines = ax.plot(x_data , prediction_value , 'r-' , lw = 5)
        plt.pause(0.1)
        

 

相關文章