예제 #1
0
	def forward(self, V):
		self.in_act = V 

		## We shall use the technique of im2Col as detailed in the notes for the course 
		## CS231n. However, there are still speed-ups to be had if we could link 
		## numpy with the BLAS Library 

		A = Vol(self.out_sx, self.out_sy, self.out_depth, 0.0)
		input_vol = V.w 
		input_vol = input_vol.reshape(V.depth, V.sx, V.sy)

		inputim2col = im2col(input_vol, [self.filter_sx, self.filter_sy], self.stride)

		##Create the filter matrix 
		filtercol = np.zeros((self.out_depth,self.filter_sx*self.filter_sy*self.in_depth))
		for i in xrange(self.out_depth):
			filtercol[i] = self.filters[i].w.flatten()
		##Perform the convolution step 

		convolve = np.dot(filtercol, inputim2col)
		##Adding biases 
		##Could be done in a neater fashion
		for i in xrange(self.out_depth):
			convolve[i] += self.biases.w[i]
		A.w = convolve.flatten()
		self.out_act = A 
		return self.out_act
	def forward(self, V):
		self.in_act = V

		V2 = Vol(1,1,self.out_depth, 0.0)

		##To prevent the exponentials from exploding, 
		##we follow the advice from the CS231n 
		##by subtracting the maximum weight 
		##thereby making the calculation stable 
		
		V2_weights = V.w

		max_weight = np.amax(V2_weights)

		exponentials = np.exp(V2_weights - max_weight)
		denom = np.sum(exponentials)

		exponentials /= denom 

		V2.w = exponentials
		##We will use the exponentials for backprop
		self.exponentials = exponentials 
		self.out_act = V2 
		return self.out_act