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. 大型互联网公司分布式ID方案总结

    ID是数据的唯一标识,传统的做法是利用UUID和数据库的自增ID,在互联网企业中,大部分公司使用的都是Mysql,并且因为需要事务支持,所以通常会使用Innodb存储引擎,UUID太长以及无序,所以并 ...

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

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

  3. 2019DX#8

    Solved Pro.ID Title Ratio(Accepted / Submitted)   1001 Acesrc and Cube Hypernet 7.32%(3/41)   1002 A ...

  4. CF985C Liebig's Barrels 贪心 第二十

    Liebig's Barrels time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  5. android CTS 命令

    > h //help Host:  help: show this message  help all: show the complete tradefed help  exit: grace ...

  6. 分析一次double强转float的翻车原因

    背景 人逢喜事精神爽,总算熬到下班撩~~ 正准备和同事打个招呼回家,被同事拖住问了.

  7. ets查询接口match、select说明

    ets:match/2用法:match(Tab, Pattern) -> [Match]返回和模式Pattern匹配的对象.一个匹配模式可能包含:绑定部分.'_'匹配任何Erlang项和匹配变量 ...

  8. python实现持久化存储,操作表格,时间戳

    import xlrd,xlwt,pickle,time,datetime book = xlrd.open_workbook("练习.xlsx") sheet1 = book.s ...

  9. Spring Cloud Alibaba | Sentinel:分布式系统的流量防卫兵进阶实战

    Spring Cloud Alibaba | Sentinel:分布式系统的流量防卫兵进阶实战 在阅读本文前,建议先阅读<Spring Cloud Alibaba | Sentinel:分布式系 ...

  10. 011 实例2-Python蟒蛇绘制

    目录 一."Python蟒蛇绘制"问题分析 1.1 Python蟒蛇绘制 二."Python蟒蛇绘制"实例编写 三.运行效果 3.1 程序关键 四." ...