import lib needed

In [1]:
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
import re
from glob import glob
 

begin, load data

In [2]:
def load_data(train_path='train/',test_path='test/'):
train_list=glob(r'train/*.png')
pattern = re.compile(r'num(\d).png')
train_id = np.array([float(pattern.search(img_name).groups()[0]) for img_name in train_list])
train_data=np.concatenate([np.array(Image.open(img_name)).reshape(1,784) for img_name in train_list],axis=0).astype(np.float)
test_list=glob(r'test/*.png')
test_id=np.array([float(pattern.search(img_name).groups()[0]) for img_name in test_list])
test_data=np.concatenate([np.array(Image.open(img_name)).reshape(1,784) for img_name in test_list],axis=0).astype(np.float)
return train_id,train_data,test_id,test_data
 

load data, print the shape of data

In [3]:
train_id,train_data,test_id,test_data=load_data()
train_id.shape,train_data.shape,test_id.shape,test_data.shape
Out[3]:
((60000,), (60000, 784), (10000,), (10000, 784))
 

convert the shape of id/label

e.g. data_id "3" can be converted to [0,0,0,1,0,0,0,0,0,0]

In [5]:
train_val=np.zeros((train_id.shape[0],10))
for i in range(train_id.shape[0]):
train_val[i,train_id[i].astype('int')]=1
 

split data into minibatches

In [6]:
mini_batch_num=100
mini_batch_size=600
 

define function need, such as softmax, propagation,back_propagation

In [7]:
def softmax(x):
x=x-np.max(x) #using softmax(x)=softmax(x+c)
exp_x=np.exp(x)
softmax_x=exp_x/sum(np.exp(x))
return softmax_x
 if you want to know more about softmax, https://segmentfault.com/a/1190000010039529?utm_source=tag-newest  is recommended to you

use cross entrophy to compute loss, this is part of propagation

In [8]:
def propa(train_x,train_y,W,b): #propagation
yt=softmax(np.dot(train_x,W)+b)
loss=-np.sum(train_y.T.dot(np.log(yt))) #cross entrophy
dy=(yt-train_y).T
return dy,loss
 if you wan to know more about softmax's cross entrophy, https://blog.csdn.net/lilong117194/article/details/81542667  is recommended to you

update W

In [9]:
def back_propa(train_data,train_id,W,b,alpha,data_size):
for i in range(data_size):
dy,loss=propa(train_data[i,:],train_id[i,:],W,b)
dy=dy.reshape(1,10)
p=train_data[i,:]
p=p.reshape(784,1)
dW=alpha*np.dot(p,dy)
W-=dW
return W,loss
 

initialize W and b

In [14]:
W=np.zeros((784,10))
b=1
 

loop and update, also print accurancy of our traindataset

In [16]:
for i in range(mini_batch_num):
for iteration in range(20):
lb=(mini_batch_size*i)
ub=(mini_batch_size*(i+1))
mini_batch_data=train_data[lb:ub,:]
mini_batch_id=train_val[lb:ub,:]
W,loss=back_propa(mini_batch_data,mini_batch_id,W,b,0.01,600)
count=0
for j in range(600):
if np.argmax(softmax(train_data[j,:].dot(W)))==train_id[j].astype('int'):
count+=1
acc=count/600
if i%10==0:
print('batch={},acc={}'.format(i+1,acc))
 
e:\Anaconda3\lib\site-packages\ipykernel_launcher.py:3: RuntimeWarning: divide by zero encountered in log
This is separate from the ipykernel package so we can avoid doing imports until
 
batch=1,acc=1.0
batch=11,acc=0.8833333333333333
batch=21,acc=0.865
batch=31,acc=0.8983333333333333
batch=41,acc=0.8766666666666667
batch=51,acc=0.8883333333333333
batch=61,acc=0.8733333333333333
batch=71,acc=0.845
batch=81,acc=0.89
batch=91,acc=0.8766666666666667
 

predict in the test dataset

In [17]:
for j in range(test_id.shape[0]):
if np.argmax(softmax(test_data[j,:].dot(W)))==test_id[j].astype('int'):
count+=1
acc=count/test_id.shape[0]
print(acc)
 
0.9103

One layer SoftMax Classifier, "Handwriting recognition"的更多相关文章

  1. Online handwriting recognition using multi convolution neural networks

    w可以考虑从计算机的“机械性.重复性”特征去设计“低效的”算法. https://www.codeproject.com/articles/523074/webcontrols/ Online han ...

  2. 机器学习: Softmax Classifier (三个隐含层)

    程序实现 softmax classifier, 含有三个隐含层的情况.activation function 是 ReLU : f(x)=max(0,x) f1=w1x+b1 h1=max(0,f1 ...

  3. 机器学习:Softmax Classifier (两个隐含层)

    程序实现 softmax classifier, 含有两个隐含层的情况.activation function 是 ReLU : f(x)=max(0,x) f1=w1x+b1 h1=max(0,f1 ...

  4. [DeeplearningAI笔记]序列模型2.6Word2Vec/Skip-grams/hierarchical softmax classifier 分级softmax 分类器

    5.2自然语言处理 觉得有用的话,欢迎一起讨论相互学习~Follow Me 2.6 Word2Vec Word2Vec相对于原先介绍的词嵌入的方法来说更加的简单快速. Mikolov T, Chen ...

  5. 机器学习 Softmax classifier (一个隐含层)

    程序实现 softmax classifier, 含有一个隐含层的情况.activation function 是 ReLU : f(x)=max(0,x) f1=w1x+b1 h1=max(0,f1 ...

  6. 机器学习 Softmax classifier (无隐含层)

    程序实现 Softmax classifer, 没有隐含层, f=wx+b y=efi∑jefj %% Softmax classifier function Out=Softmax_Classifi ...

  7. [转]csharp:Microsoft.Ink 手写识别(HandWriting Recognition)

    原贴:http://www.cnblogs.com/geovindu/p/3702427.html 下載: //Microsoft Windows XP Tablet PC Edition 2005 ...

  8. csharp:Microsoft.Ink 手写识别(HandWriting Recognition)

    /* 下載: //Microsoft Windows XP Tablet PC Edition 2005 Recognizer Pack http://www.microsoft.com/zh-cn/ ...

  9. Kernel Functions for Machine Learning Applications

    In recent years, Kernel methods have received major attention, particularly due to the increased pop ...

随机推荐

  1. 理解Js的parseInt(转)

    parseInt() 方法首先查看位置 0 处的字符,判断它是否是个有效数字:如果不是,该方法将返回 NaN,不再继续执行其他操作.但如果该字符是有效数字,该方法将查看位置 1 处的字符,进行同样的测 ...

  2. 深入浅出TypeScript(3)- 函数重载和泛型

    面向对象特性中,最根本的就是面向对象的三大基本特征:封装.继承.多态.同时,TypeScript中也存在多态的使用,比如函数重载,今天我们先看一下函数重载以及泛型的概念. 什么是函数重载 简单来说,函 ...

  3. odoo12从零开始:二、个性化定制odoo12 之 创建数据库页面

    剧情回顾 上一文章,我们已经成功运行了odoo12,并访问localhost:8069看到如下界面: 我们还没有创建数据库,但是我们发现,数据库管理页面的logo是odoo,数据库页面全是英文的,对于 ...

  4. POJ 2491 Scavenger Hunt map

    Scavenger Hunt Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2848   Accepted: 1553 De ...

  5. HDU 4565 So Easy! 广义斐波拉数 数论 (a+sqrt(b))^n%mod 模板

    So Easy! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  6. HTML连载35-背景图片的练习、精灵图

    一.背景图片练习 解释:这个例子需要注意的是,我们背景图片嵌套到另一个图片之中.我们设计的注意点在于,怎么定位到我们想定位到的地方. 总结:背景图片就是一块一块的,我们想把块的位置定位好(一般就是宽和 ...

  7. Spring 两大核心 IOC 和 AOP

    如果你的简历上写着Spring (请详述一下spring的两大核心)这个问题一定会被问到. 一.什么叫IOC 1. IOC 全称(Inversion of Control)-- 控制反转. IOC 只 ...

  8. 最小生成树问题---Prim算法学习

    一个具有n个节点的连通图的生成树是原图的最小连通子集,它包含了n个节点和n-1条边.若砍去任一条边,则生成树变为非连通图:若增加一条边,则在图中形成一条回路.本文所写的是一个带权的无向连通图中寻求各边 ...

  9. 解决php中文乱码的两种方法

    第一种是添加html标签变为如下格式: <html> <head> <meta http-equiv="Content-Type" content=& ...

  10. Vulkan(1)用apispec生成Vulkan库

    Vulkan(1)用apispec生成Vulkan库 我的Vulkan.net库已在(https://github.com/bitzhuwei/Vulkan.net)开源,欢迎交流. apispec. ...