项目上做不走了,感觉遇到了难以跨越的大山,所以真正的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}))