Created
October 8, 2011 19:19
-
-
Save chiral/1272737 to your computer and use it in GitHub Desktop.
nnet applied to noisy circle image
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# resulting image is | |
# http://dl.dropbox.com/u/15259519/images/hinomaru_NN.png | |
sr <- function() source("hinomaru_NN.R") | |
W <<- 300 | |
H <<- 200 | |
R <<- 60 | |
N <<- 6000 | |
E <<- 20 | |
rgbstr <- function(c) { | |
sprintf("#%02x%02x%02x",c[1],c[2],c[3]) | |
} | |
beni.iro <<- rgbstr(c(215, 19, 69)) | |
center <- c(W/2,H/2) | |
in.circle <- function(x,y) colSums((rbind(x,y)-center)^2)<=R^2 | |
prepare <- function() { | |
x <- runif(N,max=W) | |
y <- runif(N,max=H) | |
z <- as.integer(in.circle(x,y)) | |
x1 <- (x+rnorm(N,0,E)) %% W; | |
y1 <- (y+rnorm(N,0,E)) %% H; | |
z1 <- as.integer(in.circle(x1,y1)) | |
df <- data.frame(x=x1,y=y1,observe=z,answer=z1) | |
return(df) | |
} | |
myplot2d <- function(df,main1='') { | |
plot(df[df$observe==0,][1:2], | |
xlim=c(0,W),ylim=c(0,H),xlab='',ylab='',axes=F) | |
par(new=T) | |
plot(df[df$observe==1,][1:2], | |
main=main1, | |
xlim=c(0,W),ylim=c(0,H),col=beni.iro) | |
} | |
myplot3d <- function(df) { | |
library(scatterplot3d) | |
scatterplot3d(df$x,df$y,df$observe, angle=70, | |
pch=20,highlight.3d=T,col.axis="blue",col.grid="lightblue") | |
} | |
cross <- function(x,y) { | |
cbind(rep(x),c(t(matrix(rep(y,length(x)),length(y))))) | |
} | |
learn.nnet <- function(df,size1=12,maxi1=1000) { | |
library(nnet) | |
xyo <- data.frame(df[1:2],z=factor(df$observe==1)) | |
nn <- nnet(z~.,size=size1,maxi=maxi1,data=xyo) | |
res <- predict(nn,df[1:2],type="class") | |
print(table(df$answer,res)) | |
grid <- cross((0:(W/5))*5,(0:(H/5))*5) | |
xy <- data.frame(x=grid[,1],y=grid[,2]) | |
res <- predict(nn,xy,type="class") | |
xy$observe <- as.integer(res==T) | |
print(summary(xy)) | |
return(xy) | |
} | |
main <- function() { | |
layout(t(matrix(1:6,2,3))) | |
df <- prepare() | |
myplot2d(df,sprintf("学習用データ(ノイズの標準偏差=%d)",E)) | |
myplot3d(df) | |
df1 <- learn.nnet(df,size1=5) | |
myplot2d(df1,"3層ニューラルネット(中間ノード数=5)") | |
myplot3d(df1) | |
df1 <- learn.nnet(df,size1=7) | |
myplot2d(df1,"3層ニューラルネット(中間ノード数=7)") | |
myplot3d(df1) | |
layout(1) | |
} | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment