def _sanity_check_model_conversion(self, svm_model, nn_model, x_val): #use flatten layer to vectorize multi-dim array flatten = Flatten() flatten.to_numpy() y_pred_svm = svm_model.decision_function(flatten.forward(x_val)) y_pred_nn = nn_model.forward(x_val) rtol = 1e-7 if y_pred_nn.shape[1] == 2: numpy.testing.assert_allclose( y_pred_svm, -y_pred_nn[:, 0], rtol, err_msg= 'Predictions of Trained SVM model and converted NN model are NOT equal! (2-Class-Case)' ) numpy.testing.assert_allclose( y_pred_svm, y_pred_nn[:, 1], rtol, err_msg= 'Predictions of Trained SVM model and converted NN model are NOT equal! (2-Class-Case)' ) else: numpy.testing.assert_allclose( y_pred_svm, y_pred_nn, rtol, err_msg= 'Predictions of Trained SVM model and converted NN model are NOT equal!' )
def train_model(self, x_train, y_train, x_val, y_val, *args, **kwargs): # train model using sklearn print('training {} model'.format(self.__class__.__name__)) #use flatten layer to vectorize multi-dim array flatten = Flatten() flatten.to_numpy() x_train = flatten.forward(x_train) self.model.fit(x_train, y_train) self.model = self._convert_to_nn(self.model, y_train, x_val)