# calculate FeedForward Network
# usage
# > source("NN.r")
# > init(nin, nhidden, nout) -> some global variables are defined here
# > fprop(x, sigmoid, identity) -> calculate feedforward propagation
# names below are reserved
# (---variables---)
# dim.in, dim.hidden, dim.out, D, M, K, w1, w2
# (---functions---)
# init, fprop, bprop, sigmoid, identity
D <- numeric(0)
M <- numeric(0)
K <- numeric(0)
dim.in <- numeric(0)
dim.hidden <- numeric(0)
dim.out <- numeric(0)
w1 <- matrix(0)
w2 <- matrix(0)
# initializer
init <- function(nin, nhidden, nout){
# dimensions of input, hidden and output layers
D <<- dim.in <<- nin
M <<- dim.hidden <<- nhidden
K <<- dim.out <<- nout
# initialize weights conforming to the PRML
w1 <<- matrix( runif( M*(D+1) ), M, D+1 ) # w1[,D+1] is bias
w2 <<- matrix( runif( K*(M+1) ), K, M+1 ) # w2[,M+1] is bias
}
# calculate forward propergation
# x : D-dim. input vector
# actf : an activation function of the hidden layers
# outf : output functions of the output layers
fprop <- function( x, actf, outf){
# calculate hidden layers
x <- as.matrix(x) # x to be a 'column' vector
x.ex <- rbind(x, 1) # bind (D+1)-th row to x
z <- actf(w1 %*% x.ex)
# calculate output layers
z <- as.matrix(z)
z.ex <- rbind(z, 1)
y <- outf(w2 %*% z.ex)
return( list( out=y, hidden=z ) )
}
# back propergation
bprop <- function(data.in, data.out, times, eta){
# calculate output layers -> renew w2
# calculate hidden layers -> renew w1
}
# functions
sigmoid <- function(x) 1/(1+exp(-x))
identity <- function(x) x
最終更新:2009年06月05日 03:10