一、多层感知机手写数字识别网络结构(mnist)
本源码的神经网络为单隐层神经网络,输出层的神经元数量为784,隐层为全连接层,隐元个数为1000.在隐层后接入一个dropput层,用来防止过拟合。输出层为神经元为10的softmax层。keras代码中手动转换了数据的存储形式,并且进行了归一化。而tensorflow中的数据已经进行了预处理,在tensorflow中数据直接使用。在keras的代码中batch_size是200,在tensorflow中的batch_size是100。
二 、keras多层感知机手写数字识别源码(mnist)
import numpy as np
import pandas as pd
from keras.utils import np_utils
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import Dropout
np.random.seed(10)
(X_train_image, Y_train_label),(X_test_image,Y_test_label) = mnist.load_data()
x_train = X_train_image.reshape(60000,784).astype('float32')
x_test = X_test_image.reshape(10000,784).astype('float32')
#normalization
x_train_n = x_train/255
x_test_n = x_test/255
#one_hot code
y_train_onehot = np_utils.to_categorical(Y_train_label)
y_test_onehot = np_utils.to_categorical(Y_test_label)
# build a model
model = Sequential()
model.add(Dense(units = 1000 ,input_dim=784,kernel_initializer = 'normal',activation = 'relu'))
model.add(Dropout(0.5))
model.add(Dense(units = 10,kernel_initializer = 'normal',activation = 'softmax'))
print(model.summary())
# train
model.compile(loss='categorical_crossentropy',optimizer='adam',metrics=['accuracy'])
train_history = model.fit(x=x_train_n,y = y_train_onehot ,validation_split = 0.2, epochs = 10,batch_size = 200,verbose=2)
#show_train_history(train_history,'acc','val_acc')
#test
scores = model.evaluate(x_test_n,y_test_onehot)
#print accuracy of test data
print(scores[1])
# analysize
prediction = model.predict_classes(x_test_n)
#print a confusion matrix
pd.crosstab(Y_test_label,prediction,rownames=['label'],colnames=['predicti'])
#df = pd.DataFrame({'label':Y_test_label,'predict':prediction})
#df[(df.label==5)&(df.predict==3)]
三 、tensorflow多层感知机手写数字识别源码(mnist)
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import tensorflow as tf
import tensorflow.examples.tutorials.mnist.input_data as input_data
import numpy as np
def Dense(input_dim,output_dim,inputs,activation = None):
W = tf.Variable(tf.random_normal([input_dim,output_dim]))
b = tf.Variable(tf.random_normal([1,output_dim]))
XWb = tf.matmul(inputs,W) + b
if activation is None:
outputs = XWb
else:
outputs = activation(XWb)
return outputs
mnist = input_data.read_data_sets(".",one_hot=True)
#build a input layer
x =tf.placeholder('float',[None,784])
#build a dense layer
h1 = Dense(input_dim=784,output_dim=1000,inputs=x,activation=tf.nn.relu)
#build a dropout layer
h2 = tf.nn.dropout(h1,keep_prob=0.5)
#build a output layer softmax is applied in loss_function
y_p = Dense(input_dim=1000,output_dim=10,inputs=h2,activation=None)
#train
y_t = tf.placeholder('float',[None,10])
#loss_function
loss_function = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=y_p,labels=y_t))
optimizer = tf.train.AdamOptimizer(learning_rate=0.001).minimize(loss_function)
label_p = tf.equal(tf.argmax(y_t,1),tf.argmax(y_p,1))
accu=tf.reduce_mean(tf.cast(label_p,'float'))
#
epochs=15
batch_size = 100
sum_batch = int(mnist.train.num_examples/batch_size)
loss_list=[]
epoch_list=[]
accu_list=[]
tfconfig = tf.ConfigProto(log_device_placement = True)
sess = tf.Session(config = tfconfig)
sess.run(tf.global_variables_initializer())
for ep in range(epochs):
for i in range(sum_batch):
batch_x,batch_y = mnist.train.next_batch(batch_size=100)
sess.run(optimizer,feed_dict={x:batch_x,y_t:batch_y})
v_loss,v_accu = sess.run([loss_function,accu],feed_dict={x:mnist.validation.images,y_t:mnist.validation.labels})
epoch_list.append(ep)
loss_list.append(v_loss)
accu_list.append(v_accu)
print ('train epoch:',ep+1,'v_loss:',v_loss,'v_accu:',v_accu)
#evaluate test data
t_accu = sess.run(accu,feed_dict={x:mnist.test.images,y_t:mnist.test.labels})
t_p = sess.run(tf.argmax(y_p,1),feed_dict={x:mnist.test.images})
sess.close()
四 、卷积神经网络手写数字识别网络说明(mnist)
![](https://i-blog.csdnimg.cn/blog_migrate/a87e5c32e711f867cf36b14a97d7dfd3.png)
五、keras卷积神经网络手写数字识别网络源码(mnist)
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from keras.utils import np_utils
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense,Dropout,Conv2D,MaxPooling2D,Flatten
def show_train_history(train_history,train,validation):
plt.plot(train_history.history[train])
plt.plot(train_history.history[validation])
plt.title('Train History')
plt.ylabel(train)
plt.xlabel('Epoch')
plt.legend(['train','validation'],loc='upper left')
plt.show()
np.random.seed(10)
(x_train_image, y_train_label),(x_test_image,y_test_label) = mnist.load_data()
x_train = x_train_image.reshape(60000,28,28,1).astype('float32')
x_test = x_test_image.reshape(10000,28,28,1).astype('float32')
#normalization
x_train_n = x_train/255
x_test_n = x_test/255
#one_hot code
y_train_onehot = np_utils.to_categorical(y_train_label)
y_test_onehot = np_utils.to_categorical(y_test_label)
# build a model
model = Sequential()
model.add(Conv2D(filters=16,kernel_size=(3,3),padding='same',input_shape=(28,28,1),activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Conv2D(filters=32,kernel_size=(3,3),padding='same',input_shape=(28,28,1),activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Flatten())
model.add(Dense(units = 32,kernel_initializer = 'normal',activation = 'relu'))
model.add(Dense(units = 10,kernel_initializer = 'normal',activation = 'softmax'))
print(model.summary())
# train
model.compile(loss='categorical_crossentropy',optimizer='adam',metrics=['accuracy'])
train_history = model.fit(x=x_train_n,y = y_train_onehot ,validation_split = 0.2, epochs = 10,batch_size = 256,verbose=2)
show_train_history(train_history,'acc','val_acc')
#test
scores = model.evaluate(x_test_n,y_test_onehot)
#print accuracy of test data
print(scores[1])
# analysize
prediction = model.predict_classes(x_test_n)
#print a confusion matrix
pd.crosstab(y_test_label,prediction,rownames=['label'],colnames=['predicti'])
#df = pd.DataFrame({'label':Y_test_label,'predict':prediction})
#df[(df.label==5)&(df.predict==3)]
六 、tensorflow卷积神经网络手写数字识别网络源码(mnist)
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import tensorflow as tf
import tensorflow.examples.tutorials.mnist.input_data as input_data
import numpy as np
def Dense(input_dim,output_dim,inputs,activation = None):
W = tf.Variable(tf.random_normal([input_dim,output_dim]))
b = tf.Variable(tf.random_normal([1,output_dim]))
XWb = tf.matmul(inputs,W) + b
if activation is None:
outputs = XWb
else:
outputs = activation(XWb)
return outputs
def Conv(image,w_height,w_width,w_channels,filters):
W = tf.Variable(tf.truncated_normal([w_height,w_width,w_channels,filters]))
b = tf.Variable(tf.truncated_normal([filters]))
c1 = tf.nn.conv2d(image,W,strides=[1,1,1,1],padding='SAME')+b
outputs = tf.nn.relu(c1)
return outputs
def Pool(image):
return tf.nn.max_pool(image,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME')
mnist = input_data.read_data_sets(".",one_hot=True)
#build a input layer
x_image =tf.placeholder('float',[None,784])
x = tf.reshape(x_image,[-1,28,28,1])
#build a conv layer
C1 = Conv(x,5,5,1,16)
#print(C1.shape)
C1_p = Pool(C1)
#print(C1_p.shape)
C2 = Conv(C1_p,5,5,16,32)
#print(C2.shape)
C2_p = Pool(C2)
#print(C2_p.shape)
Flat = tf.reshape(C2_p,[-1,1568])
#print(Flat.shape)
h1 = Dense(input_dim=1568,output_dim=32,inputs=Flat,activation=tf.nn.relu)
y_p = Dense(input_dim=32,output_dim=10,inputs=h1,activation=None)
#train
y_t = tf.placeholder('float',[None,10])
#loss_function
loss_function = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=y_p,labels=y_t))
optimizer = tf.train.AdamOptimizer(learning_rate=0.001).minimize(loss_function)
label_p = tf.equal(tf.argmax(y_t,1),tf.argmax(y_p,1))
accu=tf.reduce_mean(tf.cast(label_p,'float'))
#
epochs=15
batch_size = 100
sum_batch = int(mnist.train.num_examples/batch_size)
loss_list=[]
epoch_list=[]
accu_list=[]
tfconfig = tf.ConfigProto(log_device_placement = True)
sess = tf.Session(config = tfconfig)
sess.run(tf.global_variables_initializer())
for ep in range(epochs):
for i in range(sum_batch):
batch_x,batch_y = mnist.train.next_batch(batch_size=100)
sess.run(optimizer,feed_dict={x_image:batch_x,y_t:batch_y})
v_loss,v_accu = sess.run([loss_function,accu],feed_dict={x_image:mnist.validation.images,y_t:mnist.validation.labels})
epoch_list.append(ep)
loss_list.append(v_loss)
accu_list.append(v_accu)
print ('train epoch:',ep+1,'v_loss:',v_loss,'v_accu:',v_accu)
#evaluate test data
t_accu = sess.run(accu,feed_dict={x_image:mnist.test.images,y_t:mnist.test.labels})
t_p = sess.run(tf.argmax(y_p,1),feed_dict={x_image:mnist.test.images})
sess.close()
七、卷积神经网络图像识别说明(CIFAR-10)
八、卷积神经网络图像源码(CIFAR-10)
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from keras.utils import np_utils
from keras.datasets import cifar10
from keras.models import Sequential
from keras.layers import Dense,Conv2D,MaxPooling2D,Flatten
np.random.seed(10)
label_dict={0:'airplane',1:'automobile',2:'bird',3:'cat',4:'deer',5:'dog',6:'frog',7:'horse',8:'ship',9:'truck'}
def plot_images_labels_prediction(images,labels,prediction,idx,num=10):
fig = plt.gcf()
fig.set_size_inches(12,14)
if num>25 : num=25
for i in range(0,num):
ax = plt.subplot(5,5,1+i)
ax.imshow(images[idx],cmap='binary')
title = str(i)+','+label_dict[labels[i][0]]
if len(prediction) > 0:
title += '=>'+label_dict[prediction[i]]
ax.set_title(title,fontsize=10)
ax.set_xticks([]);ax.set_yticks([])
idx +=1
plt.show()
#read data
(x_train_image, y_train_label),(x_test_image,y_test_label) = cifar10.load_data()
#normalization
x_train_n = x_train_image/255
x_test_n = x_test_image/255
#reshape
y_train_label1 = y_train_label.reshape(-1)
y_test_label1 = y_test_label.reshape(-1)
#one_hot code
y_train_onehot = np_utils.to_categorical(y_train_label1)
y_test_onehot = np_utils.to_categorical(y_test_label1)
# build a model
model = Sequential()
model.add(Conv2D(filters=32,kernel_size=(3,3),padding='same',input_shape=(32,32,3),activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Conv2D(filters=64,kernel_size=(3,3),padding='same',activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Flatten())
model.add(Dense(1024,activation = 'relu'))
model.add(Dense(10,activation = 'softmax'))
print(model.summary())
model.save_weights("cifarCnnModel.h5")
# train
model.compile(loss='categorical_crossentropy',optimizer='adam',metrics=['accuracy'])
train_history = model.fit(x_train_n,y_train_onehot ,validation_split = 0.2, epochs = 10,batch_size = 128,verbose=2)
#test
scores = model.evaluate(x_test_n,y_test_onehot)
#print accuracy of test data
print(scores[1])
# analysize
prediction = model.predict_classes(x_test_n)
prediction_pro = model.predict(x_test_n)
#print a confusion matrix
pd.crosstab(y_test_label1,prediction,rownames=['label'],colnames=['predicti'])