項目上做不走了,感覺遇到了難以跨越的大山,是以真正的AI落地才是難點啊!
1、定義layer
活學活用,舉一反三
import tensorflow as tf
import numpy as np
x_data = np.random.rand(100).astype(np.float32)
#x_data = np.random.rand(100).astype(np.float32)
#print(x_data)
y_data = x_data*0.1+0.3
def add_layer(inputs, inputs_size, output_size, activation = None):
w = tf.Variable(tf.random_uniform([inputs_size, output_size]))
b = tf.Variable(tf.zeros([1])+0.1)
if activation is None:
y = inputs*w + b
else:
y = activation((inputs*w)+b)
return w, b, y
w, b, y = add_layer(x_data, 1, 1)
#b = tf.Variable(tf.zeros([1]))
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)
loss = tf.reduce_mean(tf.square(y-y_data))
optimizer = tf.train.GradientDescentOptimizer(0.5)
train = optimizer.minimize(loss)
for step in range(100):
sess.run(train)
#print("loss is {0}".format(sess.run(loss)))
if(sess.run(loss) < 0.0001):
print("w is {0} b is {1}".format(sess.run(w), sess.run(b)))
w一般不需要是0,因為在生成初始參數時,随機變量(normal distribution)會比全部為0要好很多,是以我們這裡的
weights
為一個
in_size
行,
out_size
列的随機變量矩陣
b一般也不是0
在機器學習中,
biases
的推薦值不為0,是以我們這裡是在0向量的基礎上又加了
0.1
。
2、定義神經網絡
import tensorflow as tf
import numpy as np
x_data = np.linspace(-1,1,300, dtype=np.float32)[:, np.newaxis]
print(x_data.shape)
noise = np.random.normal(0, 0.005, x_data.shape).astype(np.float32)
y_data = np.square(x_data)-0.5+noise
print(y_data.shape)
def add_layer(inputs, inputs_size, output_size, activation = None):
w = tf.Variable(tf.random_uniform([inputs_size, output_size]))
b = tf.Variable(tf.zeros([1])+0.1)
y = tf.matmul(inputs,w )+b
if activation is None:
output = y
else:
output = activation(y)
return output
xs = tf.placeholder(tf.float32, [None, 1])
ys = tf.placeholder(tf.float32, [None, 1])
l1 = add_layer(xs, 1, 10, activation=tf.nn.relu)
print(l1)
predict = add_layer(l1, 10, 1, activation=None)
print(predict)
#b = tf.Variable(tf.zeros([1]))
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)
loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys - predict),reduction_indices=[1]))#這裡帶入的是ys
optimizer = tf.train.GradientDescentOptimizer(0.5)
train = optimizer.minimize(loss)
for step in range(100):
sess.run(train, feed_dict={xs:x_data, ys:y_data})
#print("loss is {0}".format(sess.run(loss)))
#if(sess.run(loss, feed_dict={xs:x_data, ys:y_data}) < 0.116):
#pass
#print("w is {0} b is {1}".format(sess.run(w), sess.run(b)))
圖形可視化結果:
for step in range(100):
sess.run(train, feed_dict={xs:x_data, ys:y_data})
if step%5==0:
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
print("step")
try:
ax.lines.remove(lines[0])
except:
pass
prediction = sess.run(predict, feed_dict={xs:x_data})
ax.scatter(x_data, y_data)
lines = ax.plot(x_data, prediction, 'r-', lw=5)
plt.pause(0.1)
plt.ion()
plt.show()
使用tensorboard
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
#形成一個大的圖層,将輸入包起來
with tf.name_scope("input"):
xs = tf.placeholder(np.float32, [None, 1], name="xs_in")
ys = tf.placeholder(np.float32, [None, 1], name="ys_out")
def add_layer(inputs, in_size, out_size, activation=None):
with tf.name_scope("layer"):
with tf.name_scope("weight"):
w = tf.Variable(tf.random_uniform([in_size, out_size]))
with tf.name_scope("bias"):
b = tf.Variable(tf.zeros([1])+0.1)
with tf.name_scope("y"):
y = tf.matmul(inputs, w) +b
if activation is None:
out_put = y
else:
out_put = activation(y)
return out_put
l1 = add_layer(xs, 1, 20, activation=tf.nn.relu)
predict = add_layer(l1, 20, 1, None)
with tf.name_scope("loss"):
loss = tf.reduce_mean(tf.square(ys-predict))
with tf.name_scope("train"):
optimizer = tf.train.GradientDescentOptimizer(0.1)
train = optimizer.minimize(loss)
sess = tf.Session()
writer = tf.summary.FileWriter("logs", sess.graph)
init = tf.global_variables_initializer()
sess.run(init)
x_data = np.linspace(-1,1,300).astype(np.float32)[:, np.newaxis]
print(x_data.shape)
noise = np.random.normal(0, 1, x_data.shape).astype(np.float32)
print(noise.shape)
y_data = np.square(x_data)-0.5+noise
print(y_data.shape)
for step in range(100):
sess.run(train, feed_dict={xs:x_data, ys:y_data})
if step%5==0:
print("loss is {0}".format(sess.run(loss, feed_dict={xs:x_data,ys:y_data})))
tensorboard顯示過程資料
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
#形成一個大的圖層,将輸入包起來
with tf.name_scope("input"):
xs = tf.placeholder(np.float32, [None, 1], name="xs_in")#這裡是numpy的float32的函數
ys = tf.placeholder(np.float32, [None, 1], name="ys_out")
def add_layer(inputs, in_size, out_size, n_layer, activation=None):
layer_name = "layer%s"%n_layer
with tf.name_scope("layer"):
with tf.name_scope("weight"):
w = tf.Variable(tf.random_uniform([in_size, out_size]))
tf.summary.histogram(layer_name+'weights', w)
with tf.name_scope("bias"):
b = tf.Variable(tf.zeros([1])+0.1)
tf.summary.histogram(layer_name+'bias', b)
with tf.name_scope("y"):
y = tf.matmul(inputs, w) +b
if activation is None:
out_put = y
else:
out_put = activation(y)
tf.summary.histogram(layer_name+"output", out_put)
return out_put
l1 = add_layer(xs, 1, 20, 0, activation=tf.nn.relu)
predict = add_layer(l1, 20, 1, 1, None)
with tf.name_scope("loss"):
loss = tf.reduce_mean(tf.square(ys-predict))
with tf.name_scope("train"):
optimizer = tf.train.GradientDescentOptimizer(0.1)
train = optimizer.minimize(loss)
tf.summary.scalar("loss", loss)
sess = tf.Session()
merged = tf.summary.merge_all()
writer = tf.summary.FileWriter("logs", sess.graph)
init = tf.global_variables_initializer()
sess.run(init)
x_data = np.linspace(-1,1,300).astype(np.float32)[:, np.newaxis]
print(x_data.shape)
noise = np.random.normal(0, 1, x_data.shape).astype(np.float32)
print(noise.shape)
y_data = np.square(x_data)-0.5+noise
print(y_data.shape)
for step in range(100):
sess.run(train, feed_dict={xs:x_data, ys:y_data})
if step%5==0:
print("loss is {0}".format(sess.run(loss, feed_dict={xs:x_data,ys:y_data})))
rs = sess.run(merged, feed_dict={xs:x_data, ys:y_data})
writer.add_summary(rs, step)
會出來如上的board。
3、手寫資料分類問題
import tensorflow as tf
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)
with tf.name_scope("input"):
xs = tf.placeholder(np.float32, [None, 28*28], name="in_xs")
ys = tf.placeholder(np.float32, [None, 10], name="in_ys")
def add_layer(inputs, in_size, out_size, n_layer, activation=None):
with tf.name_scope("layer"):
layer_name = "layer%s"%n_layer
with tf.name_scope("weights"):
w = tf.Variable(tf.random_uniform([in_size, out_size]))
tf.summary.histogram("layer"+layer_name+"weight", w)
with tf.name_scope("bias"):
b = tf.Variable(tf.zeros([1])+0.1)
tf.summary.histogram("layer"+layer_name+"bias", b)
with tf.name_scope("w_b"):
w_b = tf.matmul(inputs, w) +b
if activation is None:
out_put = w_b
else:
out_put = activation(w_b)
tf.summary.histogram("out_put", out_put)
return out_put
#l1 = add_layer(xs, 28*28, 20, 0, activation=None)
predict = add_layer(xs, 28*28, 10, 1, activation=tf.nn.softmax)
with tf.name_scope("loss"):
loss = tf.reduce_mean(-tf.reduce_sum(ys*tf.log(predict), reduction_indices=[1]))
tf.summary.scalar("loss", loss)
optimizer = tf.train.GradientDescentOptimizer(0.1)
with tf.name_scope("train"):
train = optimizer.minimize(loss)
sess = tf.Session()
init = tf.global_variables_initializer()
sess.run(init)
merge = tf.summary.merge_all()
writer = tf.summary.FileWriter("logs", sess.graph)
x_data, y_data = mnist.train.next_batch(100)
for step in range(100):
sess.run(train, feed_dict={xs:x_data, ys:y_data})
rs = sess.run(merge, feed_dict={xs:x_data, ys:y_data})
if step%5==0:
print("loss is {0}".format(sess.run(loss, feed_dict={xs:x_data, ys:y_data})))
#print(compute_accuracy(mnist.test.images, mnist.test.labels))
4、卷積神經網絡解決分類問題
- convolutional layer1 + max pooling;
- convolutional layer2 + max pooling;
- fully connected layer1 + dropout;
- fully connected layer2 to prediction.
import tensorflow as tf
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)
def weight_variable(shape):
inital = tf.Variable(tf.truncated_normal(shape, stddev=0.1))
return inital
def bias_variable(shape):
intial = tf.Variable(tf.zeros(shape)+0.1)
return intial
"""
定義卷積,tf.nn.conv2d函數是tensoflow裡面的二維的卷積函數,x是圖檔的所有參數,
W是此卷積層的權重,然後定義步長strides=[1,1,1,1]值,strides[0]和strides[3]的兩個1是預設值,
中間兩個1代表padding時在x方向運動一步,y方向運動一步,padding采用的方式是SAME。
"""
def conv2d(x, w):
return tf.nn.conv2d(x, w, strides=[1,1,1,1], padding='SAME')
"""
接着定義池化pooling,為了得到更多的圖檔資訊,padding時我們選的是一次一步,
也就是strides[1]=strides[2]=1,這樣得到的圖檔尺寸沒有變化,
而我們希望壓縮一下圖檔也就是參數能少一些進而減小系統的複雜度,
是以我們采用pooling來稀疏化參數,也就是卷積神經網絡中所謂的下采樣層。pooling 有兩種,
一種是最大值池化,一種是平均值池化,本例采用的是最大值池化tf.max_pool()。
池化的核函數大小為2x2,是以ksize=[1,2,2,1],步長為2,是以strides=[1,2,2,1]:
"""
def max_pooling(x):
return tf.nn.max_pool(x, ksize=[1,2,2,1], strides=[1,2,2,1], padding='SAME')
xs = tf.placeholder(tf.float32, [None, 784])/255
ys = tf.placeholder(tf.float32, [None, 10])
keep_prob = tf.placeholder(tf.float32)
x_image = tf.reshape(xs, [-1,28,28,1])
w_conv1 = weight_variable([5,5,1,32])
b_conv1 = bias_variable([32])
h_conv1 = tf.nn.relu(conv2d(x_image, w_conv1)+b_conv1)#輸入圖檔的厚度變厚
h_pool1 = max_pooling(h_conv1)
w_conv2 = weight_variable([5,5,32,64])
b_conv2 = bias_variable([64])
h_conv2 = tf.nn.relu(conv2d(h_pool1, w_conv2)+b_conv2)
h_pool2 = max_pooling(h_conv2)
h_pool2_flat = tf.reshape(h_pool2, [-1, 7*7*64])
w_fc1 = weight_variable([7*7*64, 1024])
b_fc1 = bias_variable([1024])
hfc1 = tf.nn.relu((tf.matmul(h_pool2_flat, w_fc1)+b_fc1))
h_fc1_drop = tf.nn.dropout(hfc1, keep_prob)
w_fc2 = weight_variable([1024, 10])
b_fc2 = bias_variable([10])
predict = tf.nn.softmax(tf.matmul(h_fc1_drop, w_fc2)+b_fc2)
cross_entrory = tf.reduce_mean(-tf.reduce_sum(ys*tf.log(predict), reduction_indices=[1]))
optimizer = tf.train.AdamOptimizer(1e-4)
train = optimizer.minimize(cross_entrory)
sess = tf.Session()
init = tf.global_variables_initializer()
sess.run(init)
for step in range(10):
x_data, y_data = mnist.train.next_batch(100)
sess.run(train, feed_dict={xs:x_data, ys:y_data, keep_prob:0.5})
if step%5==0:
print(sess.run(cross_entrory, feed_dict={xs:x_data, ys:y_data, keep_prob:0.5}))