Add regularization to the cost function
This commit is contained in:
		
							parent
							
								
									f2154a8cc1
								
							
						
					
					
						commit
						395c5676dc
					
				
					 1 changed files with 7 additions and 16 deletions
				
			
		| 
						 | 
					@ -80,23 +80,14 @@ assert(size(J) == [1 1]);
 | 
				
			||||||
%               and Theta2_grad from Part 2.
 | 
					%               and Theta2_grad from Part 2.
 | 
				
			||||||
%
 | 
					%
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					% Note: Theta1/2 are matrixes here, we want all their rows, but skip their
 | 
				
			||||||
 | 
					% first column (not regularizing the bias term).
 | 
				
			||||||
 | 
					regularization_term = lambda/(2*m) * ...
 | 
				
			||||||
 | 
					                      (sum(sum(Theta1(:,2:end).^2)) ...
 | 
				
			||||||
 | 
					                       + sum(sum(Theta2(:,2:end).^2)));
 | 
				
			||||||
 | 
					assert(size(regularization_term) == [1 1]);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					J += regularization_term;
 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
% -------------------------------------------------------------
 | 
					% -------------------------------------------------------------
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Reference in a new issue