RでDeep Learning(10)

Convolutin層、Pooling層の実装

Convolution <- R6Class("Convolution",
	
	#stride=1
	#pad=0
	#filterサイズ=5×5
	#filter数=30
	
	public = list(
		W = NULL,
		b = NULL,
		x = NULL,
		col = NULL,

		dW = NULL,
		db = NULL,
		
		initialize = function(W,b){
			self$W <- W
			self$b <- b
		},

		forward = function(x){
			H <- dim(x)[1]	
			W <- dim(x)[2]
			C <- dim(x)[3]
			N <- dim(x)[4] #batch数100

			out_h <- (H - 5) + 1
			out_w <- (W - 5) + 1
			
			col <- im2col(x,5,5,stride=1,pad=0)
			out <- col %*% self$W + matrix(rep(self$b,out_h*out_w*N),nrow=out_h*out_w*N,byrow=T)
			out <- array(out,c(out_h,out_w,N,30)) %>% aperm(.,c(1,2,4,3))
			self$x <- x
			self$col <- col
			return(out) 
		},
		
		backward = function(dout){
			
			dout <- matrix(aperm(dout,c(1,2,4,3)),ncol=30)
			
			self$db <- apply(dout,2,sum)			
			self$dW <- (self$col %>% t()) %*% dout
			
			#dxのコード
		}
	)
)
Pooling <- R6Class("Pooling",
	
	#stride=2
	#pad=0
	#filterサイズ=2×2
	
	public = list(
		x = NULL,
		arg_max = NULL,

		forward = function(x){

			N <- dim(x)[4] #batch数100

			col <- im2col_pooling(x,2,2)
			out <- matrix(apply(col,1,max),nrow=N,byrow=T)
			 
			self$x <- x
			self$arg_max <- apply(col,1,which.max)

			return(out) 
		},
		
		backward = function(dout){
			
			dout <- as.vector(t(dout))
			dmax <- matrix(0,length(self$arg_max),4)
			for(i in 1:length(self$arg_max)){
				dmax[i,self$arg_max[i]] <- dout[i]
			}
			H <- dim(self$x)[1]	
			W <- dim(self$x)[2]
			C <- dim(self$x)[3]
			N <- dim(self$x)[4] #batch数100
			dx <- col2im_pooling(dmax,H,W,C,N,2,2)

			return(dx)
		}
	)
)