MATLAB神经网络(1)之R练习

将在MATLAB神经网络中学到的知识用R进行适当地重构,再写一遍,一方面可以加深理解和记忆,另一方面练习R,比较R和MATLAB的不同。
如要在R中使用之前的数据,应首先在MATLAB中用writetable函数将原本的由mat文件读入的数据写到csv文件中,以备R读入。

writetable(T,filename) writes to a file with the name and extension specified by filename.

writetable determines the file format based on the specified extension. The extension must be one of the following:

  1. .txt, .dat, or .csv for delimited text files
  2. .xls, .xlsm, or .xlsx for Excel® spreadsheet files
  3. .xlsb for Excel spreadsheet files supported on systems with Excel for Windows® See doc writetable.
writetable(table(c1),"data1.csv");
writetable(table(c2),"data2.csv");
writetable(table(c3),"data3.csv");
writetable(table(c4),"data4.csv");

这里我们使用R中十分经典的鸢尾花数据集iris(在dplyr包中)。

library(dplyr)

## Warning: package 'dplyr' was built under R version 3.5.3

##
## Attaching package: 'dplyr'

## The following objects are masked from 'package:stats':
##
## filter, lag

## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union

dim(iris)

## [1] 150 5

str(iris)

## 'data.frame': 150 obs. of 5 variables:
## Sepal.Length: num 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
## Sepal.Width : num 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
## Petal.Length: num 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
## Petal.Width : num 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
## Species : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...

可以看到该数据集共有150组数据,4个自变量,1个因变量(factor),鸢尾花有3类。

sort: sort a vector or factor (partially) into ascending or descending order.

order: returns a permutation which rearranges its first argument into ascending or descending order, breaking ties by further arguments.

k<-rnorm()
n<-order(k)
input<-iris[,1:]
output1<-iris[,]
output1<-as.integer(output1)
output<-matrix(,,)
output<-as.data.frame(output)
#把输出从1维变成3维
for(i in
1:)
{
if(output1[i]==)
output[i,]=
else
if(output1[i]==)
output[i,]=
else
output[i,]=
}
input_train=input[n[1:],]
output_train=output[n[1:],]
input_test=input[n[121:],]
input_test=input[n[121:],]
me<-apply(input_train,,mean)
va<-apply(input_train,,var)
me

## Sepal.Length Sepal.Width Petal.Length Petal.Width
## 5.875000 3.030000 3.849167 1.239167

va

## Sepal.Length Sepal.Width Petal.Length Petal.Width
## 0.6707983 0.1876639 3.0223522 0.5622346

#输入数据归一化
inputn<-scale(input_train)

下面进行神经网络初始化。

结构4-5-3

innum<-
midnum<-
outnum<-
#权值初始化
w1<-matrix(rnorm(innum*midnum),midnum,innum)
b1<-rnorm(midnum)
w2<-matrix(rnorm(outnum*midnum),midnum,outnum)
b2<-rnorm(outnum)
#学习率
xite<-0.1
loopNumber<-
I<-rep(,midnum)
Iout<-rep(,midnum)
FI<-rep(,midnum)
dw1<-matrix(,innum,midnum)
db1<-rep(,midnum)

神经网络训练

E<-rep(,loopNumber)
for(ii in
1:loopNumber)
{
for(i in
1:)
{
x=inputn[i,]
for(j in
1:midnum)
{
I[j]<-sum(inputn[i,]*w1[j,])+b1[j]
Iout[j]<-1/(1+exp(-I[j]))
}
yn<-t(w2)%*%Iout+b2

e<-output_train[i,]-yn
E[ii]<-E[ii]+sum(abs(e))

dw2<-t(e)%*%Iout
db2<-e

for(j in
1:midnum)
{
S<-1/(1+exp(-I[j]));
FI[j]<-S*(1-S);
}

for(k in
1:innum)
{
for(j in
1:midnum)
{
dw1[k,j]<-FI[j]*x[k]*sum(e*w2[j,])
db1[j]<-FI[j]*sum(e*w2[j,])
}
}

w1<-w1+xite*t(dw1)
b1<-b1+xite*t(db1)
w2<-w2+xite*t(dw2)
b2<-b2+xite*t(db2)
}
}

分类预测

inputn_test<-input_test
for(i in
1:)
{
inputn_test[i,]<-(input_test[i,]-me)/va^0.5
}
fore=matrix(,,);
for(i in
1:)
{
for(j in
1:midnum)
{
I[j]=sum(inputn_test[i,]*w1[j,])+b1[j]
I<-unlist(I)
Iout[j]=1/(1+exp(-I[j]))
}
fore[,i]=t(w2)%*%Iout+b2
}

结果分析

output_fore=rep(,)
for(i in
1:)
{
output_fore[i]<-which.max(fore[,i])
}
error=output_fore-output1[n[121:]]
t<-table(output_fore,output1[n[121:]])
t

##
## output_fore 1 2 3
## 1 13 0 0
## 2 0 9 0
## 3 0 0 8

#正确率
options(digits=)
rightridio<-(t[,]+t[,]+t[,])/
result<-paste("正确率是 ",round(rightridio*,digits=),"%")
result

## [1] "正确率是 100 %"

MATLAB神经网络(1)之R练习的更多相关文章

  1. 12.Matlab神经网络工具箱

    概述: 1 人工神经网络介绍 2 人工神经元 3 MATLAB神经网络工具箱 4 感知器神经网络 5 感知器神经网络 5.1 设计实例分析 clear all; close all; P=[ ; ]; ...

  2. MATLAB神经网络原理与实例精解视频教程

    教程内容:<MATLAB神经网络原理与实例精解>随书附带源程序.rar9.随机神经网络.rar8.反馈神经网络.rar7.自组织竞争神经网络.rar6.径向基函数网络.rar5.BP神经网 ...

  3. 《精通Matlab神经网络》例10-16的新写法

    <精通Matlab神经网络>书中示例10-16,在创建BP网络时,原来的写法是: net = newff(minmax(alphabet),[S1 S2],{'logsig' 'logsi ...

  4. Matlab神经网络

    1. <MATLAB神经网络原理与实例精解> 2. B站:https://search.bilibili.com/all?keyword=matlab&from_source=na ...

  5. 使用opencv-python实现MATLAB的fspecial('Gaussian', [r, c], sigma)

    reference_opencv实现高斯核 reference_MATLAB_fspecial函数说明 # MATLAB H = fspecial('Gaussian', [r, c], sigma) ...

  6. MATLAB神经网络(2)之R练习

    1. AMORE 1.1 newff newff(n.neurons, learning.rate.global, momentum.global, error.criterium, Stao, hi ...

  7. Matlab神经网络工具箱学习之一

    1.神经网络设计的流程 2.神经网络设计四个层次 3.神经网络模型 4.神经网络结构 5.创建神经网络对象 6.配置神经网络的输入输出 7.理解神经网络工具箱的数据结构 8.神经网络训练 1.神经网络 ...

  8. matlab神经网络工具箱创建神经网络

    为了看懂师兄的文章中使用的方法,研究了一下神经网络 昨天花了一天的时间查怎么写程序,但是费了半天劲,不能运行,百度知道里倒是有一个,可以运行的,先贴着做标本 % 生成训练样本集 clear all; ...

  9. Matlab神经网络验证码识别

    本文,将会简述如何利用Matlab的强大功能,调用神经网络处理验证码的识别问题.  预备知识,Matlab基础编程,神经网络基础.  可以先看下: Matlab基础视频教程 Matlab经典教程--从 ...

随机推荐

  1. 函数动态参数和Python中的三种空间

    动态参数 : *args 实参角度: 定义一个函数时, * 将所有的位置参数聚合到一个元祖中 顺序 : 位置参数 > * args > 默认参数 > **kwargs 接受所有参数 ...

  2. cs231n spring 2017 lecture2 Image Classification

    1. 相比于传统的人工提取特征(边.角等),深度学习是一种Data-Driven Approach.深度学习有统一的框架,喂不同的数据集,可以训练识别不同的物体.而人工提取特征的方式很脆弱,换一个物体 ...

  3. “冰桶挑战”之外:微软科技助力ALS患者

    编者按:"直到ALS出现治疗方法,科技就是我的解药."ALS患者,前美国橄榄球联盟(NFL)球员Steve Gleason如是说.最近,一支微软首届黑客马拉松(Hackathon) ...

  4. Java发送Post请求,参数JSON,接收JSON

    /** * 发送post请求 * @param url 路径 * @param jsonObject 参数(json类型) * @param encoding 编码格式 * @return * @th ...

  5. JS一维数组、多维数组和对象的混合使用

    转载地址:http://blog.csdn.net/wangyuchun_799/article/details/38460515 引言 这篇文章的主要目的是讲解JavaScript数组和对象的混合使 ...

  6. 题解 HDU 3698 Let the light guide us Dp + 线段树优化

    http://acm.hdu.edu.cn/showproblem.php?pid=3698 Let the light guide us Time Limit: 5000/2000 MS (Java ...

  7. idea常见需求

    1.给class加注释模板 /** *@ClassName ${NAME} *@Description TODO *@Author xxx *@Date ${DATE} ${TIME} *@Versi ...

  8. 在腾讯云centos7.2上安装配置Node.js记录

    应为爱好前端所以打算在腾讯云服务器上安装JavaScript引擎Node.js,下面是安装步骤: 安装准备: 下载node.js的.tar.xz安装包:https://nodejs.org/dist/ ...

  9. yii2.0 集合七牛SDK 上传图片到第三方

    首先,请用composer下载七牛phpSDK (具体参考官方文档) composer require qiniu/php-sdk 注册七牛账号 获取 AK SK(密匙) ,创建资源对象 获取doma ...

  10. MyBatis之ResultMap的association和collection标签(一)

    1.先说resultMap比较容易混淆的点, 2. Map结尾是映射,Type是类型  resultType 和restltMap restulyType: 1.对应的是java对象中的属性,大小写不 ...