一 TensorFlow的安裝
- 首先安裝numpy雁刷,需要最新版
pip install numpy
- 之后安裝Tensorflow
pip install tensorflow
二 簡(jiǎn)單的TensorFlow例子
- 讓計(jì)算機(jī)自己預(yù)測(cè)y = 0.3x + b
import tensorflow as tf
import numpy as np
# create data
x_data = np.random.rand(100).astype(np.float32)#初始化100以內(nèi)的值
y_data = x_data*0.1 + 0.3#根據(jù)隨機(jī)的x值計(jì)算y值
### create tensorflow structure start ###
#這個(gè)是線性回歸 y = Wx + b 其中W可能不是一個(gè)具體的數(shù)字,可能是一個(gè)矩陣,b為偏移量
Weights = tf.Variable(tf.random_uniform([2], -1.0, 1.0))#[1]是生成一個(gè)一維的數(shù),范圍在-0.1與1之間
biases = tf.Variable(tf.zeros([1]))#生成一個(gè)一維為0的數(shù)字
y = Weights*x_data + biases#這個(gè)是根據(jù)計(jì)算得到的曲線 反復(fù)計(jì)算用來學(xué)習(xí)
loss = tf.reduce_mean(tf.square(y-y_data))#計(jì)算估計(jì)的y與原先y的誤差
optimizer = tf.train.GradientDescentOptimizer(0.5)#定義學(xué)習(xí)效率
train = optimizer.minimize(loss)#這個(gè)就是在訓(xùn)練
init = tf.initialize_all_variables()#初始化整個(gè)神經(jīng)網(wǎng)絡(luò)的結(jié)構(gòu)
### create tensorflow structure end ###
sess = tf.Session() #定義一個(gè)繪畫類似指針的東西
sess.run(init) # Very important指向了之前定義的神經(jīng)網(wǎng)絡(luò) 并且激活
for step in range(20000):
sess.run(train)#真正的指向訓(xùn)練過程
if step % 20 == 0:
print(step, sess.run(Weights), sess.run(biases))v
三 TensorFlow中的Session進(jìn)一步學(xué)習(xí)
- session其實(shí)就是負(fù)責(zé)啟動(dòng)這個(gè)神經(jīng)網(wǎng)絡(luò)的對(duì)象恭金。
- session中的run方法 如果想用到tf中的方法 變量 就要使用run指向這個(gè)對(duì)象
- 列子:兩個(gè)矩陣相乘
import tensorflow as tf
m1 = tf.constant([[2,3]]) #定義矩陣
m2 = tf.constant([[2],[3]])
product = tf.matmul(m1,m2) #這個(gè)就是矩陣相乘失乾,與numpy中的dot一樣
# sess = tf.Session()
# r = sess.run(product)
# print(r)
# sess.close()
with tf.Session() as sess: #開啟session并且運(yùn)行product
print(sess.run(product))
四 TensorFlow中的變量定義
- 使用tensorFlow中的Variable作為變量
- 使用tensorFlow中的constant做為常量
import tensorflow as tf
text1 = tf.Variable(0,'happy') #叫做happy的變量值為1
one = tf.constant(1)#常量
new = tf.add(text1,one)#這是tf中的相加
update = tf.assign(text1,new)#把new附給text1
init = tf.initialize_all_variables()#這個(gè)很重要 初始化所有表變量
with tf.Session() as sess:
sess.run(init)
for each in range(3):
sess.run(update)
print(sess.run(text1))
五 tensorFlow中的placeholder
- 這個(gè)模塊是控制負(fù)責(zé)接受外界輸入的
import tensorflow as tf
input1 = tf.placeholder(tf.float32) #定義一個(gè)input1參數(shù)
input2 = tf.placeholder(tf.float32)
update = tf.multiply(input1,input2)#這是兩個(gè)兩個(gè)數(shù)相乘
with tf.Session() as sess:
print(sess.run(update,feed_dict= {input1:0.3,input2 :0.5}))#通過feed_dict參數(shù)來輸入數(shù)據(jù),這個(gè)參數(shù)是字典的形式.
六 激勵(lì)函數(shù)
- 神經(jīng)網(wǎng)絡(luò)中的每個(gè)節(jié)點(diǎn)接受輸入值锚沸,并將輸入值傳遞給下一層,輸入節(jié)點(diǎn)會(huì)將輸入屬性值直接傳遞給下一層(隱層或輸出層)涕癣。在神經(jīng)網(wǎng)絡(luò)中哗蜈,隱層和輸出層節(jié)點(diǎn)的輸入和輸出之間具有函數(shù)關(guān)系,這個(gè)函數(shù)稱為激勵(lì)函數(shù)坠韩。常見的激勵(lì)函數(shù)有:線性激勵(lì)函數(shù)距潘、閾值或階躍激勵(lì)函數(shù)、S形激勵(lì)函數(shù)只搁、雙曲正切激勵(lì)函數(shù)和高斯激勵(lì)函數(shù)等音比。
-
我對(duì)激勵(lì)函數(shù)的理解:對(duì)于輸入的一個(gè)值,每一層的神經(jīng)元對(duì)這個(gè)值的敏感程度不同须蜗,會(huì)用不同的敏感程度硅确,把這個(gè)值放大或者縮小,傳入下一層神經(jīng)元明肮。
Tensorflow中激勵(lì)函數(shù)傳送門 - 激勵(lì)函數(shù)例子:添加一個(gè)層:
import tensorflow as tf
def add_layer(inputs,in_size,out_size,activation_function = None):
Weight = tf.Variable(tf.random_normal([in_size,out_size]))
biase = tf.Variable(tf.zeros([1,out_size]) + 0.1)
Wx_plus_b = tf.matmul(inputs,Weight) + biase
if activation_function is None:
outputs = Wx_plus_b
else:
outputs = activation_function(Wx_plus_b)
return outputs
- 簡(jiǎn)單計(jì)算一下y = x^2 + 0.5 的損失
"""
Please note, this code is only for python 3+. If you are using python 2+, please modify the code accordingly.
"""
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
def add_layer(inputs, in_size, out_size, activation_function=None):
Weights = tf.Variable(tf.random_normal([in_size, out_size]))
biases = tf.Variable(tf.zeros([1, out_size]) + 0.1)
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
# Make up some real data
x_data = np.linspace(-1, 1, 300)[:, np.newaxis]#構(gòu)造出300個(gè)x值
noise = np.random.normal(0, 0.05, x_data.shape)#隨機(jī)生成影響值
y_data = np.square(x_data) * x_data - 0.5 + noise#根據(jù)構(gòu)造的值生成函數(shù) y = x^2 - 0.5 + noise
##plt.scatter(x_data, y_data)
##plt.show()
# define placeholder for inputs to network
xs = tf.placeholder(tf.float32, [None, 1])#輸入的x
ys = tf.placeholder(tf.float32, [None, 1])#輸入的y
# add hidden layer
l1 = add_layer(xs, 1, 10, activation_function=tf.nn.relu)#這個(gè)是輸入的xs的神經(jīng)元 傳送給10個(gè)神經(jīng)元 用tf.nn.relu的激勵(lì)函數(shù)
# add output layer
prediction = add_layer(l1, 10, 1, activation_function=None)#輸出預(yù)測(cè)結(jié)果
# the error between prediciton and real data
loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys-prediction), reduction_indices=[1]))#計(jì)算損失
train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss)#訓(xùn)練 減少損失
# important step
init = tf.initialize_all_variables()
sess= tf.Session()
sess.run(init)
# plot the real data
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.scatter(x_data, y_data)
plt.ion()
plt.show()
for i in range(1000):
# training
sess.run(train_step, feed_dict={xs: x_data, ys: y_data})
if i % 50 == 0:
# to visualize the result and improvement
try:
ax.lines.remove(lines[0])
except Exception:
pass
prediction_value = sess.run(prediction, feed_dict={xs: x_data})
# plot the prediction
lines = ax.plot(x_data, prediction_value, 'r-', lw=5)
plt.pause(1)
七 數(shù)據(jù)可視化
- 需要用到matplotlib包
pip install matplotlib
fig = plt.figure() #創(chuàng)建一個(gè)圖
ax = fig.add_subplot(1,1,1)#規(guī)定圖的編號(hào)
ax.scatter(x_data, y_data)#寫入真實(shí)的數(shù)據(jù)
plt.ion()#畫圖表之后不要暫停
plt.show()#顯示圖標(biāo)
ax.lines.remove(lines[0])#去除第0條線
prediction_value = sess.run(prediction, feed_dict={xs: x_data})
lines = ax.plot(x_data, prediction_value, 'r-', lw=5)#生成紅色寬度為5的線
plt.pause(1) #暫停1秒