コード例 #1
0
ファイル: testGradDesc.py プロジェクト: ctestart/ML-HW1
	def test_grad_SEE(self):
		data_file = 'curvefitting.txt'
		for order in xrange(10):
			[X, Y] = hw1.getData(data_file)
			phi = hw1.designMatrix(X, order)
			weights = hw1.regressionFit(X, Y, phi)
			delta = 1
			analytic = hw1.computeSEEGrad(X,Y, weights, order).flatten()
			approx = gd.gradient_approx_SEE(X, Y, weights, order, delta).flatten()
			for i in xrange(order):	
				self.assertAlmostEqual(analytic[i], approx[i])

			delta2 = .1
			analytic2 = hw1.computeSEEGrad(X,Y, weights, order).flatten()
			approx2 = gd.gradient_approx_SEE(X, Y, weights, order, delta2).flatten()
			for i in xrange(order):	
				self.assertAlmostEqual(analytic2[i], approx2[i])

			delta3 = .01
			analytic3 = hw1.computeSEEGrad(X,Y, weights, order).flatten()
			approx3 = gd.gradient_approx_SEE(X, Y, weights, order, delta3).flatten()
			for i in xrange(order):	
				self.assertAlmostEqual(analytic3[i], approx3[i])

			delta4 = .001
			analytic4 = hw1.computeSEEGrad(X,Y, weights, order).flatten()
			approx4 = gd.gradient_approx_SEE(X, Y, weights, order, delta4).flatten()
			for i in xrange(order):	
				self.assertAlmostEqual(analytic4[i], approx4[i])
コード例 #2
0
ファイル: testHomework1.py プロジェクト: ctestart/ML-HW1
    def test_compare_derivative(self):
        """ Verify the gradient using the numerical derivative code """
        """ Part of question 2.2 """
        order = 4
        [X,Y] = hw1.getData('curvefitting.txt')
        Phi_matrix=hw1.designMatrix(X,order)
        #print Phi_matrix.shape
        weight_vector=hw1.regressionFit(X,Y,Phi_matrix)
        SSEG=hw1.computeSEEGrad(X,Y,weight_vector,order)
        approx = gd.gradient_approx_SEE(X, Y, weight_vector, order, 1e-10)
        true_deriv = hw1.computeSEEGrad(X,Y, weight_vector, order)

        print "COMPARE approx", approx
        print "TRUE ", true_deriv