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. vue.js如何根据后台返回来的图片url进行图片下载

    最近在做一个前端vue.js对接的功能模块时,需要实现一个下载图片的功能,后台返回来的是一串图片url,试了很多种方法,发现点击下载时出来的效果都是跳着到一个新的图片网页,后来经过一番琢磨,终于解决了 ...

  2. CF1028D Order book 思维

    Order book time limit per test 2 seconds memory limit per test 256 megabytes input standard input ou ...

  3. 详解RMQ-ST算法 ST模板

    RMQ问题是求解区间最值的问题. 这里分析的是ST算法,它可以对所有要处理的数据做到O(nlogn)的预处理,对每个区间查询做到O(1)查询 ST算法本质是一个DP的过程 这里通过举一个求最大值实例来 ...

  4. codeforces 361 D. Levko and Array(dp+二分)

    题目链接:http://codeforces.com/contest/361/problem/D 题意:最多可以修改K次数字,每次修改一个数字变成任意值,C=max(a[i+1]-a[i]):求操作之 ...

  5. git 生成密钥

    1.本地安装好git: 2.桌面右键 Git Bash Here 打开git命令行: 3.ssh-keygen -t rsa -C "nideyouxiang@xxx.com"  ...

  6. zabbix -- 学习之一

    网上说这东西是运维必须学会的东西,于是乎捣鼓的第一步就开始了. 首先,在度娘上搜索了一下,找到了官网,按照官网的说法没操作成功.后来照这博主的帖子(https://www.cnblogs.com/xi ...

  7. Fire Balls 10——UI界面的制作

    版权申明: 本文原创首发于以下网站: 博客园『优梦创客』的空间:https://www.cnblogs.com/raymondking123 优梦创客的官方博客:https://91make.top ...

  8. Python作业本——前言

    大四毕业了,9月才开始研究生生涯,导师也没有严格要求我暑假留校做项目,也没提具体的学习要求.这两三个月比较闲,所以就打算学学Python.学习过程中肯定会有些心得体会,以及一些小练习.学习编程不同于传 ...

  9. Go语言标准库之net_http

    Go语言内置的net/http包十分的优秀,提供了HTTP客户端和服务端的实现. net/http介绍 Go语言内置的net/http包提供了HTTP客户端和服务端的实现. HTTP协议 超文本传输协 ...

  10. C#委托(delegate、Action、Func、predicate)和事件

    一.前言 刚开始工作的时候,觉得委托和事件有些神秘,而当你理解他们之后,也觉得好像没有想象中的那么难.在项目中运用委托和事件,你会发现他非常棒,这篇博文算是自己对委托和事件的一次梳理和总结. 二.委托 ...