def test_objective_sym_against_naive(): sigma = 1. D = 2 N = 10 Z = np.random.randn(N, D) K = gaussian_kernel(Z, sigma=sigma) num_trials = 10 for _ in range(num_trials): alpha = np.random.randn(N) J_naive_a = 0 for d in range(D): for i in range(N): for j in range(N): J_naive_a += alpha[i] * K[i, j] * \ (-1 + 2. / sigma * ((Z[i][d] - Z[j][d]) ** 2)) J_naive_a *= (2. / (N * sigma)) J_naive_b = 0 for d in range(D): for i in range(N): temp = 0 for j in range(N): temp += alpha[j] * (Z[j, d] - Z[i, d]) * K[i, j] J_naive_b += (temp ** 2) J_naive_b *= (2. / (N * (sigma ** 2))) J_naive = J_naive_a + J_naive_b # compare to unregularised objective lmbda = 0. J = develop_gaussian.objective_sym(Z, sigma, lmbda, alpha, K) assert_close(J_naive, J)
def test_rff_feature_map(): x = 3. u = 2. omega = 2. phi = rff_feature_map_single(x, omega, u) phi_manual = np.cos(omega * x + u) * np.sqrt(2.) assert_close(phi, phi_manual)
def test_rff_feature_map_derivative_d_2n(): X = np.array([[1.], [3.]]) u = np.array([2.]) omega = np.array([[2.]]) d = 0 phi_derivative = rff_feature_map_grad_d(X, omega, u, d) phi_derivative_manual = -np.sin(X * omega + u) * omega[:, d] * np.sqrt(2.) assert_close(phi_derivative, phi_derivative_manual)
def test_rff_feature_map_derivative2_d(): X = np.array([[1.]]) u = np.array([2.]) omega = np.array([[2.]]) d = 0 phi_derivative2 = rff_feature_map_grad2_d(X, omega, u, d) phi_derivative2_manual = -rff_feature_map(X, omega, u) * (omega[:, d] ** 2) assert_close(phi_derivative2, phi_derivative2_manual)
def test_rff_feature_map_comp_theano_result_equals_manual(): if not theano_available: raise SkipTest("Theano not available.") D = 2 x = np.random.randn(D) m = 10 sigma = 1. omega, u = rff_sample_basis(D, m, sigma) phi_manual = rff_feature_map_single(x, omega, u) for i in range(m): # phi_manual is a monte carlo average, so have to normalise by np.sqrt(m) here phi = rff_feature_map_comp_theano(x, omega[:, i], u[i]) / np.sqrt(m) assert_close(phi, phi_manual[i])
def test_rff_feature_map_grad_theano_result_equals_manual(): if not theano_available: raise SkipTest("Theano not available.") D = 2 x = np.random.randn(D) X = x[np.newaxis, :] m = 10 sigma = 1. omega, u = rff_sample_basis(D, m, sigma) grad_manual = rff_feature_map_grad(X, omega, u)[:, 0, :] for i in range(m): # phi_manual is a monte carlo average, so have to normalise by np.sqrt(m) here grad = rff_feature_map_comp_grad_theano(x, omega[:, i], u[i]) / np.sqrt(m) assert_close(grad, grad_manual[:, i])
def test_objective_sym_given_b_C(): N = 100 D = 3 m = 10 omega = np.random.randn(D, m) u = np.random.uniform(0, 2 * np.pi, m) X = np.random.randn(N, D) lmbda = 1. C = compute_C_memory(X, omega, u) b = compute_b_memory(X, omega, u) theta = np.random.randn(m) J = objective(X, theta, lmbda, omega, u, b, C) J_manual = 0.5 * np.dot(theta.T, np.dot(C + np.eye(m) * lmbda, theta)) - np.dot(theta, b) assert_close(J, J_manual)
def test_objective_matches_sym(): sigma = 1. lmbda = 1. Z = np.random.randn(100, 2) kernel = lambda X, Y: gaussian_kernel(X, Y, sigma=sigma) alpha = np.random.randn(len(Z)) temp = incomplete_cholesky(Z, kernel, eta=0.1) I, R, nu = (temp["I"], temp["R"], temp["nu"]) R_test = incomplete_cholesky_new_points(Z, Z, kernel, I, R, nu) b = gaussian_low_rank.compute_b(Z, Z, R.T, R_test.T, sigma) J_sym = develop_gaussian_low_rank.objective_sym(Z, sigma, lmbda, alpha, R.T, b) J = gaussian_low_rank.objective(Z, Z, sigma, lmbda, alpha, R.T, R_test.T, b) assert_close(J, J_sym)
def test_objective_low_rank_matches_sym(): sigma = 1. lmbda = 1. Z = np.random.randn(100, 2) kernel = lambda X, Y: gaussian_kernel(X, Y, sigma=sigma) alpha = np.random.randn(len(Z)) temp = incomplete_cholesky(Z, kernel, eta=0.1) I, R, nu = (temp["I"], temp["R"], temp["nu"]) R_test = incomplete_cholesky_new_points(Z, Z, kernel, I, R, nu) b = _compute_b_low_rank(Z, Z, R.T, R_test.T, sigma) J_sym = _objective_sym_low_rank(Z, sigma, lmbda, alpha, R.T, b) J = _objective_low_rank(Z, Z, sigma, lmbda, alpha, R.T, R_test.T, b) assert_close(J, J_sym)
def test_serialisation_round_trip_is_idempotent(tmp_path): video_id = "P01_101" frame_number = 10 detections = FrameDetections( video_id=video_id, frame_number=frame_number, objects=[ObjectDetection(bbox=BBox(0.1, 0.2, 0.3, 0.4), score=0.1)], hands=[ HandDetection( bbox=BBox(0.2, 0.3, 0.4, 0.5), score=0.2, state=HandState.PORTABLE_OBJECT, side=HandSide.RIGHT, object_offset=FloatVector(x=0.1, y=0.1), ) ], ) filepath = tmp_path / (video_id + ".pkl") save_detections([detections], filepath) loaded_detections = load_detections(filepath)[0] assert detections.video_id == loaded_detections.video_id assert detections.frame_number == loaded_detections.frame_number assert_close(detections.objects[0].score, loaded_detections.objects[0].score) assert_bbox_close(detections.objects[0].bbox, loaded_detections.objects[0].bbox) assert_close(detections.hands[0].score, loaded_detections.hands[0].score) assert detections.hands[0].side == loaded_detections.hands[0].side assert detections.hands[0].state == loaded_detections.hands[0].state assert_float_vector_close(detections.hands[0].object_offset, loaded_detections.hands[0].object_offset) assert_bbox_close(detections.hands[0].bbox, loaded_detections.hands[0].bbox)
def test_objective_matches_full(): sigma = 1. lmbda = 1. X = np.random.randn(100, 2) Y = np.random.randn(10, 2) low_rank_dim = int(len(X) * 0.9) kernel = lambda X, Y: gaussian_kernel(X, Y, sigma=sigma) alpha = np.random.randn(len(X)) K_XY = kernel(X, Y) C = gaussian.compute_C(X, Y, K_XY, sigma) b = gaussian.compute_b(X, Y, K_XY, sigma) J_full = gaussian.objective(X, Y, sigma, lmbda, alpha, K_XY=K_XY, b=b, C=C) temp = incomplete_cholesky(X, kernel, eta=low_rank_dim) I, R, nu = (temp["I"], temp["R"], temp["nu"]) R_test = incomplete_cholesky_new_points(X, Y, kernel, I, R, nu) b = gaussian_low_rank.compute_b(X, Y, R.T, R_test.T, sigma) J = gaussian_low_rank.objective(X, Y, sigma, lmbda, alpha, R.T, R_test.T, b) assert_close(J, J_full, decimal=1)
def test_objective_low_rank_matches_full(): sigma = 1. lmbda = 1. X = np.random.randn(100, 2) Y = np.random.randn(10, 2) low_rank_dim = int(len(X) * 0.9) kernel = lambda X, Y: gaussian_kernel(X, Y, sigma=sigma) alpha = np.random.randn(len(X)) K_XY = kernel(X, Y) C = _compute_C(X, Y, K_XY, sigma) b = _compute_b(X, Y, K_XY, sigma) J_full = _objective(X, Y, sigma, lmbda, alpha, K_XY=K_XY, b=b, C=C) temp = incomplete_cholesky(X, kernel, eta=low_rank_dim) I, R, nu = (temp["I"], temp["R"], temp["nu"]) R_test = incomplete_cholesky_new_points(X, Y, kernel, I, R, nu) b = _compute_b_low_rank(X, Y, R.T, R_test.T, sigma) J = _objective_low_rank(X, Y, sigma, lmbda, alpha, R.T, R_test.T, b) assert_close(J, J_full, decimal=1)
def test_objective_against_naive(): sigma = 1. D = 2 NX = 10 NY = 20 X = np.random.randn(NX, D) Y = np.random.randn(NY, D) K_XY = gaussian_kernel(X, Y, sigma=sigma) num_trials = 10 for _ in range(num_trials): alpha = np.random.randn(NX) J_naive_a = 0 for d in range(D): for i in range(NX): for j in range(NY): J_naive_a += alpha[i] * K_XY[i, j] * \ (-1 + 2. / sigma * ((X[i][d] - Y[j][d]) ** 2)) J_naive_a *= (2. / (NX * sigma)) J_naive_b = 0 for d in range(D): for i in range(NY): temp = 0 for j in range(NX): temp += alpha[j] * (X[j, d] - Y[i, d]) * K_XY[j, i] J_naive_b += (temp**2) J_naive_b *= (2. / (NX * (sigma**2))) J_naive = J_naive_a + J_naive_b # compare to unregularised objective lmbda = 0. J = gaussian.objective(X, Y, sigma, lmbda, alpha, K_XY=K_XY) assert_close(J_naive, J)
def test_objective_against_naive(): sigma = 1. D = 2 NX = 10 NY = 20 X = np.random.randn(NX, D) Y = np.random.randn(NY, D) K_XY = gaussian_kernel(X, Y, sigma=sigma) num_trials = 10 for _ in range(num_trials): alpha = np.random.randn(NX) J_naive_a = 0 for d in range(D): for i in range(NX): for j in range(NY): J_naive_a += alpha[i] * K_XY[i, j] * \ (-1 + 2. / sigma * ((X[i][d] - Y[j][d]) ** 2)) J_naive_a *= (2. / (NX * sigma)) J_naive_b = 0 for d in range(D): for i in range(NY): temp = 0 for j in range(NX): temp += alpha[j] * (X[j, d] - Y[i, d]) * K_XY[j, i] J_naive_b += (temp ** 2) J_naive_b *= (2. / (NX * (sigma ** 2))) J_naive = J_naive_a + J_naive_b # compare to unregularised objective lmbda = 0. J = gaussian.objective(X, Y, sigma, lmbda, alpha, K_XY=K_XY) assert_close(J_naive, J)
def assert_float_vector_close(expected_float_vector: FloatVector, actual_float_vector: FloatVector): assert_close(expected_float_vector.x, actual_float_vector.x) assert_close(expected_float_vector.y, actual_float_vector.y)
def assert_bbox_close(expected_bbox: BBox, actual_bbox: BBox): assert_close(expected_bbox.left, actual_bbox.left) assert_close(expected_bbox.top, actual_bbox.top) assert_close(expected_bbox.right, actual_bbox.right) assert_close(expected_bbox.bottom, actual_bbox.bottom)
def test_conditional_gaussian_dependency_matrix(self): length = 100 n_samples = 1000 X = array([sample_markov_chain(length) for _ in range(n_samples)]) # Next two should be equal s0 = AnomalyDetector( P_ConditionalGaussianDependencyMatrix( range(length), length)).fit(X).anomaly_score(X) ad1 = AnomalyDetector( P_ConditionalGaussianCombiner([ P_ConditionalGaussian([i + 1], [i]) for i in range(length - 1) ] + [P_ConditionalGaussian([0], [])]), cr_plus).fit(X) s1 = ad1.anomaly_score(X) assert_allclose(s0, s1, rtol=0.0001) # OK # Most likely, these two are not equal but highly correlated ad2 = AnomalyDetector( [P_ConditionalGaussian([i], []) for i in range(length)], cr_plus).fit(X) s2 = ad2.anomaly_score(X) ad3 = AnomalyDetector( P_ConditionalGaussianCombiner( [P_ConditionalGaussian([i], []) for i in range(length)]), cr_plus).fit(X) s3 = ad3.anomaly_score(X) assert_equal(pearsonr(s2, s3) > 0.985, True) # Test classification Y = array([sample_markov_chain(length, 0.2) for _ in range(n_samples)]) Z = array([sample_markov_chain(length, 0.3) for _ in range(n_samples)]) data = r_[X, Y, Z] labels = r_[['X'] * len(X), ['Y'] * len(Y), ['Z'] * len(Z)] data_index = shuffle(range(len(data))) training_set = data_index[:n_samples * 2] test_set = data_index[n_samples * 2:] models = { 'independent gaussian': AnomalyDetector([P_Gaussian([i]) for i in range(length)], cr_plus), 'independent conditional gaussian': AnomalyDetector( [P_ConditionalGaussian([i], []) for i in range(length)], cr_plus), 'independent conditional gaussian with combiner': AnomalyDetector( P_ConditionalGaussianCombiner( [P_ConditionalGaussian([i], []) for i in range(length)])), 'single conditional gaussian with combiner': AnomalyDetector( P_ConditionalGaussianCombiner([ P_ConditionalGaussian([i], [i - 1]) for i in range(1, length) ] + [P_ConditionalGaussian([0], [])])), 'dependency matrix': AnomalyDetector( P_ConditionalGaussianDependencyMatrix(range(length), length)) } all_acc = {} for key in models: ad = models[key].fit(data[training_set], labels[training_set]) adclf = SklearnClassifier.clf(ad) labels_predicted = adclf.predict(data[test_set]) accuracy = sum(labels[test_set] == labels_predicted) / float( len(test_set)) all_acc[key] = accuracy print key, "accuracy = ", accuracy assert_close(all_acc['independent gaussian'], all_acc['independent conditional gaussian'], decimal=2) assert_close(all_acc['independent gaussian'], all_acc['independent conditional gaussian with combiner'], decimal=2) assert_close(all_acc['single conditional gaussian with combiner'], all_acc['dependency matrix'], decimal=2)
def test_isotropic_zero_mean_equals_log_gaussian_pdf(): D = 2 x = np.random.randn(D) g = IsotropicZeroMeanGaussian(sigma=np.sqrt(2)) log_pdf = log_gaussian_pdf(x, mu=np.zeros(D), Sigma=np.eye(D) * 2, is_cholesky=False, compute_grad=False) assert_close(log_pdf, g.log_pdf(x))
def test_conditional_gaussian_dependency_matrix(self): length = 100 n_samples = 1000 X = array([sample_markov_chain(length) for _ in range(n_samples)]) # Next two should be equal s0 = AnomalyDetector( P_ConditionalGaussianDependencyMatrix(list(range(length)),length) ).fit(X).anomaly_score(X) ad1=AnomalyDetector( P_ConditionalGaussianCombiner([P_ConditionalGaussian([i + 1], [i]) for i in range(length - 1)]+[P_ConditionalGaussian([0], [])]), cr_plus ).fit(X) s1 = ad1.anomaly_score(X) assert_allclose(s0, s1, rtol=0.0001) # OK # Most likely, these two are not equal but highly correlated ad2=AnomalyDetector( [P_ConditionalGaussian([i], []) for i in range(length)], cr_plus ).fit(X) s2 = ad2.anomaly_score(X) ad3=AnomalyDetector( P_ConditionalGaussianCombiner([P_ConditionalGaussian([i], []) for i in range(length)]), cr_plus ).fit(X) s3 = ad3.anomaly_score(X) assert_equal(pearsonr(s2,s3)> 0.985, True) # Test classification Y = array([sample_markov_chain(length,0.2) for _ in range(n_samples)]) Z = array([sample_markov_chain(length,0.3) for _ in range(n_samples)]) data = r_[X,Y,Z] labels = r_[['X']*len(X), ['Y']*len(Y), ['Z']*len(Z)] data_index = shuffle(list(range(len(data)))) training_set = data_index[:n_samples*2] test_set = data_index[n_samples*2:] models = { 'independent gaussian': AnomalyDetector([P_Gaussian([i]) for i in range(length)],cr_plus), 'independent conditional gaussian': AnomalyDetector([P_ConditionalGaussian([i], []) for i in range(length)],cr_plus), 'independent conditional gaussian with combiner': AnomalyDetector(P_ConditionalGaussianCombiner([P_ConditionalGaussian([i], []) for i in range(length)])), 'single conditional gaussian with combiner': AnomalyDetector(P_ConditionalGaussianCombiner([P_ConditionalGaussian([i], [i-1]) for i in range(1, length)]+ [P_ConditionalGaussian([0], [])])), 'dependency matrix': AnomalyDetector(P_ConditionalGaussianDependencyMatrix(list(range(length)),length)) } all_acc = {} for key in models: ad=models[key].fit(data[training_set], labels[training_set]) adclf = SklearnClassifier.clf(ad) labels_predicted = adclf.predict(data[test_set]) accuracy = sum(labels[test_set]==labels_predicted)/float(len(test_set)) all_acc[key] = accuracy print(key, "accuracy = ", accuracy) assert_close(all_acc['independent gaussian'],all_acc['independent conditional gaussian'],decimal=2) assert_close(all_acc['independent gaussian'], all_acc['independent conditional gaussian with combiner'],decimal=2) assert_close(all_acc['single conditional gaussian with combiner'], all_acc['dependency matrix'],decimal=2)