def wtest_ort_gradient_optimizers_binary(self, use_weight=False): from onnxcustom.utils.orttraining_helper import add_loss_output from onnxcustom.training.optimizers import OrtGradientOptimizer X = numpy.arange(60).astype(numpy.float32).reshape((-1, 3)) y = numpy.arange(X.shape[0]).astype(numpy.float32).reshape( (-1, 1)) > 10 X = X.astype(numpy.float32) y = y.astype(numpy.int64) y[0, 0] = 0 y[-1, 0] = 1 w = (numpy.random.rand(X.shape[0]) + 1).astype(numpy.float32) X_train, _, y_train, __, w_train, ___ = train_test_split(X, y, w) reg = SGDClassifier(loss='log') reg.fit(X_train, y_train.ravel()) onx = to_onnx(reg, X_train, target_opset=opset, black_op={'LinearClassifier'}, options={'zipmap': False}) onx_loss = add_loss_output( onx, 'log', output_index=1, weight_name='weight' if use_weight else None) inits = ['intercept', 'coef'] inputs = onx_loss.graph.input self.assertEqual(len(inputs), 3 if use_weight else 2) dt = inputs[1].type.tensor_type.elem_type self.assertEqual(TensorProto.INT64, dt) # pylint: disable=E1101 train_session = OrtGradientOptimizer(onx_loss, inits, learning_rate=1e9) self.assertRaise(lambda: train_session.get_state(), AttributeError) if use_weight: train_session.fit(X_train, y_train.reshape((-1, 1)), w_train.reshape((-1, 1)), use_numpy=False) else: train_session.fit(X_train, y_train.reshape((-1, 1)), use_numpy=False) state_tensors = train_session.get_state() self.assertEqual(len(state_tensors), 2) r = repr(train_session) self.assertIn("OrtGradientOptimizer(model_onnx=", r) self.assertIn("learning_rate='invscaling'", r) losses = train_session.train_losses_ self.assertGreater(len(losses), 1) if any(map(numpy.isnan, losses)): raise AssertionError(losses)
def wtest_ort_gradient_optimizers_reg(self, use_weight=False): from onnxcustom.utils.orttraining_helper import add_loss_output from onnxcustom.training.optimizers import OrtGradientOptimizer X = numpy.arange(60).astype(numpy.float32).reshape((-1, 3)) y = numpy.arange(X.shape[0]).astype(numpy.float32).reshape((-1, 1)) y[0, 0] += 1 y[-1, 0] += 1 w = (numpy.random.rand(X.shape[0]) + 1).astype(numpy.float32) X_train, _, y_train, __, w_train, ___ = train_test_split(X, y, w) reg = SGDRegressor() reg.fit(X_train, y_train.ravel()) onx = to_onnx(reg, X_train, target_opset=opset, black_op={'LinearRegressor'}) onx_loss = add_loss_output( onx, 'squared_error', weight_name='weight' if use_weight else None) inits = ['intercept', 'coef'] inputs = onx_loss.graph.input self.assertEqual(len(inputs), 3 if use_weight else 2) train_session = OrtGradientOptimizer(onx_loss, inits, learning_rate=1e9) self.assertRaise(lambda: train_session.get_state(), AttributeError) if use_weight: self.assertRaise( lambda: train_session.fit(X_train, y_train.reshape((-1, 1)), w_train.reshape((-1, 1)), use_numpy=False), ConvergenceError) else: self.assertRaise( lambda: train_session.fit( X_train, y_train.reshape((-1, 1)), use_numpy=False), ConvergenceError) state_tensors = train_session.get_state() self.assertEqual(len(state_tensors), 2) r = repr(train_session) self.assertIn("OrtGradientOptimizer(model_onnx=", r) self.assertIn("learning_rate='invscaling'", r) losses = train_session.train_losses_ self.assertGreater(len(losses), 1) if any(map(numpy.isnan, losses)): raise AssertionError(losses)
def wtest_ort_gradient_optimizers_binary(self, use_weight=False): from onnxcustom.utils.orttraining_helper import add_loss_output from onnxcustom.training.optimizers import OrtGradientOptimizer X, y = make_classification( # pylint: disable=W0632 100, n_features=10, random_state=0) X = X.astype(numpy.float32) y = y.astype(numpy.int64) w = (numpy.random.rand(X.shape[0]) + 1).astype(numpy.float32) X_train, _, y_train, __, w_train, ___ = train_test_split(X, y, w) reg = SGDClassifier(loss='log') reg.fit(X_train, y_train) onx = to_onnx(reg, X_train, target_opset=opset, black_op={'LinearClassifier'}, options={'zipmap': False}) onx2 = load_onnx(BytesIO(onx.SerializeToString())) set_model_props(onx, {'info': 'unit test'}) onx_loss = add_loss_output( onx, 'log', output_index=1, weight_name='weight' if use_weight else None) inits = ['intercept', 'coef'] inputs = onx_loss.graph.input self.assertEqual(len(inputs), 3 if use_weight else 2) dt = inputs[1].type.tensor_type.elem_type self.assertEqual(TensorProto.INT64, dt) # pylint: disable=E1101 train_session = OrtGradientOptimizer(onx_loss, inits, learning_rate=1e-3) self.assertRaise(lambda: train_session.get_state(), AttributeError) if use_weight: train_session.fit(X_train, y_train.reshape((-1, 1)), w_train.reshape((-1, 1)), use_numpy=False) else: train_session.fit(X_train, y_train.reshape((-1, 1)), use_numpy=False) state_tensors = train_session.get_state() self.assertEqual(len(state_tensors), 2) r = repr(train_session) self.assertIn("OrtGradientOptimizer(model_onnx=", r) self.assertIn("learning_rate='invscaling'", r) losses = train_session.train_losses_ self.assertGreater(len(losses), 1) self.assertFalse(any(map(numpy.isnan, losses))) # get_trained_weight trained_onnx = train_session.get_trained_onnx(model=onx2) sess = InferenceSession(onx2.SerializeToString(), providers=['CPUExecutionProvider']) got1 = sess.run(None, {'X': X_train}) sess = InferenceSession(trained_onnx.SerializeToString(), providers=['CPUExecutionProvider']) got2 = sess.run(None, {'X': X_train}) self.assertEqual(len(got1), len(got2)) self.assertEqual(got1[0].shape, got2[0].shape) # state state = train_session.get_state() self.assertIsInstance(state, dict) train_session.set_state(state)