diff --git a/doc/ipython-notebooks/multiclass/Tree/DecisionTrees.ipynb b/doc/ipython-notebooks/multiclass/Tree/DecisionTrees.ipynb index e229a277421..2c0d16ae009 100644 --- a/doc/ipython-notebooks/multiclass/Tree/DecisionTrees.ipynb +++ b/doc/ipython-notebooks/multiclass/Tree/DecisionTrees.ipynb @@ -197,10 +197,10 @@ "outputs": [], "source": [ "# create ID3ClassifierTree object\n", - "id3 = sg.create_machine(\"ID3ClassifierTree\", labels=labels)\n", + "id3 = sg.create_machine(\"ID3ClassifierTree\")\n", "\n", "# learn the tree from training features\n", - "is_successful = id3.train(train_feats)" + "is_successful = id3.train(train_feats, labels)" ] }, { @@ -412,10 +412,10 @@ " train_lab = sg.create_labels(labels)\n", "\n", " # create ID3ClassifierTree object\n", - " id3 = sg.create_machine(\"ID3ClassifierTree\", labels=train_lab)\n", + " id3 = sg.create_machine(\"ID3ClassifierTree\")\n", "\n", " # learn the tree from training features\n", - " id3.train(train_feats)\n", + " id3.train(train_feats, train_lab)\n", "\n", " # apply to test dataset\n", " output = id3.apply(test_feats)\n", @@ -610,9 +610,9 @@ "# steps in C4.5 Tree training bundled together in a python method\n", "def train_tree(feats,types,labels):\n", " # C4.5 Tree object\n", - " tree = sg.create_machine(\"C45ClassifierTree\", labels=labels, m_nominal=types)\n", + " tree = sg.create_machine(\"C45ClassifierTree\", m_nominal=types)\n", " # supply training matrix and train\n", - " tree.train(feats)\n", + " tree.train(feats, labels)\n", " \n", " return tree\n", "\n", @@ -1406,10 +1406,9 @@ " # create CHAID tree object\n", " c = sg.create_machine(\"CHAIDTree\", dependent_vartype=dependent_var_type,\n", " feature_types=feature_types,\n", - " num_breakpoints=num_bins,\n", - " labels = labels)\n", + " num_breakpoints=num_bins)\n", " # train using training features\n", - " c.train(feats)\n", + " c.train(feats, labels)\n", " \n", " return c\n", "\n", diff --git a/doc/ipython-notebooks/neuralnets/autoencoders.ipynb b/doc/ipython-notebooks/neuralnets/autoencoders.ipynb index 32327fee204..ff6f4808fc1 100644 --- a/doc/ipython-notebooks/neuralnets/autoencoders.ipynb +++ b/doc/ipython-notebooks/neuralnets/autoencoders.ipynb @@ -276,8 +276,7 @@ "\n", "nn.put('max_num_epochs', 50)\n", "\n", - "nn.put('labels', Ytrain)\n", - "_ = nn.train(Xtrain)" + "_ = nn.train(Xtrain, Ytrain)" ] }, { @@ -404,10 +403,9 @@ "# train the network\n", "conv_nn.put('epsilon', 0.0)\n", "conv_nn.put('max_num_epochs', 50)\n", - "conv_nn.put('labels', Ytrain)\n", "\n", "# start training. this might take some time\n", - "_ = conv_nn.train(Xtrain)" + "_ = conv_nn.train(Xtrain, Ytrain)" ] }, { @@ -462,7 +460,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.3" + "version": "3.6.9" } }, "nbformat": 4, diff --git a/doc/ipython-notebooks/neuralnets/neuralnets_digits.ipynb b/doc/ipython-notebooks/neuralnets/neuralnets_digits.ipynb index 4dca02f606a..0e15ba56f1c 100644 --- a/doc/ipython-notebooks/neuralnets/neuralnets_digits.ipynb +++ b/doc/ipython-notebooks/neuralnets/neuralnets_digits.ipynb @@ -236,8 +236,7 @@ "# uncomment this line to allow the training progress to be printed on the console\n", "#from shogun import MSG_INFO; net_no_reg.io.put('loglevel', MSG_INFO)\n", "\n", - "net_no_reg.put('labels', Ytrain)\n", - "net_no_reg.train(Xtrain) # this might take a while, depending on your machine\n", + "net_no_reg.train(Xtrain, Ytrain) # this might take a while, depending on your machine\n", "\n", "# compute accuracy on the validation set\n", "print(\"Without regularization, accuracy on the validation set =\", compute_accuracy(net_no_reg, Xval, Yval), \"%\")" @@ -265,8 +264,7 @@ "net_l2.put('max_num_epochs', 600)\n", "net_l2.put('seed', 10)\n", "\n", - "net_l2.put('labels', Ytrain)\n", - "net_l2.train(Xtrain) # this might take a while, depending on your machine\n", + "net_l2.train(Xtrain, Ytrain) # this might take a while, depending on your machine\n", "\n", "# compute accuracy on the validation set\n", "print(\"With L2 regularization, accuracy on the validation set =\", compute_accuracy(net_l2, Xval, Yval), \"%\")" @@ -294,8 +292,7 @@ "net_l1.put('max_num_epochs', 600)\n", "net_l1.put('seed', 10)\n", "\n", - "net_l1.put('labels', Ytrain)\n", - "net_l1.train(Xtrain) # this might take a while, depending on your machine\n", + "net_l1.train(Xtrain, Ytrain) # this might take a while, depending on your machine\n", "\n", "# compute accuracy on the validation set\n", "print(\"With L1 regularization, accuracy on the validation set =\", compute_accuracy(net_l1, Xval, Yval), \"%\")" @@ -336,8 +333,7 @@ "net_dropout.put('gd_learning_rate', 0.5)\n", "net_dropout.put('gd_mini_batch_size', 100)\n", "\n", - "net_dropout.put('labels', Ytrain)\n", - "net_dropout.train(Xtrain) # this might take a while, depending on your machine\n", + "net_dropout.train(Xtrain, Ytrain) # this might take a while, depending on your machine\n", "\n", "# compute accuracy on the validation set\n", "print(\"With dropout, accuracy on the validation set =\", compute_accuracy(net_dropout, Xval, Yval), \"%\")" @@ -431,8 +427,7 @@ "net_conv.put(\"seed\", 10)\n", "\n", "# start training\n", - "net_conv.put('labels', Ytrain)\n", - "net_conv.train(Xtrain)\n", + "net_conv.train(Xtrain, Ytrain)\n", "\n", "# compute accuracy on the validation set\n", "print(\"With a convolutional network, accuracy on the validation set =\", compute_accuracy(net_conv, Xval, Yval), \"%\")" @@ -511,7 +506,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.3" + "version": "3.6.9" } }, "nbformat": 4, diff --git a/doc/ipython-notebooks/neuralnets/rbms_dbns.ipynb b/doc/ipython-notebooks/neuralnets/rbms_dbns.ipynb index 6adb5c1b07a..36c28d0c5c5 100644 --- a/doc/ipython-notebooks/neuralnets/rbms_dbns.ipynb +++ b/doc/ipython-notebooks/neuralnets/rbms_dbns.ipynb @@ -370,8 +370,7 @@ "nn.put(\"l2_coefficient\", 0.0001)\n", "\n", "# start training\n", - "nn.put('labels', sg.create_labels(Ytrain))\n", - "nn.train(sg.create_features(Xtrain))" + "nn.train(sg.create_features(Xtrain), sg.create_labels(Ytrain))" ] }, { @@ -426,7 +425,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.3" + "version": "3.6.9" } }, "nbformat": 4, diff --git a/examples/meta/src/binary/domainadaptationsvm.sg.in b/examples/meta/src/binary/domainadaptationsvm.sg.in index 3dd0984b04a..6ed4b58a31f 100644 --- a/examples/meta/src/binary/domainadaptationsvm.sg.in +++ b/examples/meta/src/binary/domainadaptationsvm.sg.in @@ -14,8 +14,8 @@ svm_kernel.init(feats_train, feats_train) #![create_kernel] #![create_svm_and_train] -Machine svm = create_machine("SVMLight", kernel=svm_kernel, labels=labels_train, C1=1.0, C2=1.0) -svm.train() +Machine svm = create_machine("SVMLight", kernel=svm_kernel, C1=1.0, C2=1.0) +svm.train(feats_train, labels_train) #![create_svm_and_train] #![create_kernel] @@ -24,11 +24,11 @@ svm_kernel2.init(feats_train, feats_train) #![create_kernel] #![obtain_dasvm_from_the_previous_svm] -Machine dasvm = create_machine("DomainAdaptationSVM", C1=1.0, C2=1.0, kernel=svm_kernel2, labels=labels_train, presvm=as_svm(svm), B=1.0) +Machine dasvm = create_machine("DomainAdaptationSVM", C1=1.0, C2=1.0, kernel=svm_kernel2, presvm=as_svm(svm), B=1.0) #![obtain_dasvm_from_the_previous_svm] #![train_and_apply] -dasvm.train() +dasvm.train(feats_train, labels_train) Labels labels_predict = dasvm.apply(feats_test) RealVector labels_vector = labels_predict.get_real_vector("labels") RealVector weights = svm.get_real_vector("m_alpha") diff --git a/examples/meta/src/evaluation/cross_validation.sg.in b/examples/meta/src/evaluation/cross_validation.sg.in index 3dd9ead79b2..448f62f782b 100644 --- a/examples/meta/src/evaluation/cross_validation.sg.in +++ b/examples/meta/src/evaluation/cross_validation.sg.in @@ -51,7 +51,7 @@ Labels reg_labels_test = create_labels(reg_lab_test) #![create_machine_REGRESSION] real tau = 0.001 -Machine lrr = create_machine("LinearRidgeRegression", tau=tau, labels=reg_labels_train) +Machine lrr = create_machine("LinearRidgeRegression", tau=tau) #![create_instance_REGRESSION] #![create_cross_validation_REGRESSION] diff --git a/examples/meta/src/multiclass/chaid_tree.sg.in b/examples/meta/src/multiclass/chaid_tree.sg.in index cf43fc41b45..eebbfbd806a 100644 --- a/examples/meta/src/multiclass/chaid_tree.sg.in +++ b/examples/meta/src/multiclass/chaid_tree.sg.in @@ -18,11 +18,10 @@ ft[1] = 2 #![create_instance] CHAIDTree classifier(0, ft, 10) -classifier.set_labels(labels_train) #![create_instance] #![train_and_apply] -classifier.train(features_train) +classifier.train(features_train, labels_train) MulticlassLabels labels_predict = classifier.apply_multiclass(features_test) #![train_and_apply] diff --git a/examples/meta/src/multiclass/relaxed_tree.sg.in b/examples/meta/src/multiclass/relaxed_tree.sg.in index 0aaf98d5b7f..29a5e0d1683 100644 --- a/examples/meta/src/multiclass/relaxed_tree.sg.in +++ b/examples/meta/src/multiclass/relaxed_tree.sg.in @@ -17,13 +17,12 @@ Kernel k = create_kernel("GaussianKernel") #![create_instance] RelaxedTree machine() -machine.set_labels(labels_train) machine.set_machine_for_confusion_matrix(mll) machine.set_kernel(k) #![create_instance] #![train_and_apply] -machine.train(features_train) +machine.train(features_train, labels_train) MulticlassLabels labels_predict = machine.apply_multiclass(features_test) #![train_and_apply] diff --git a/examples/meta/src/neural_nets/convolutional_net_classification.sg.in b/examples/meta/src/neural_nets/convolutional_net_classification.sg.in index 18b8158e5cd..0ab8bbb6355 100644 --- a/examples/meta/src/neural_nets/convolutional_net_classification.sg.in +++ b/examples/meta/src/neural_nets/convolutional_net_classification.sg.in @@ -11,7 +11,7 @@ Labels labels_test = create_labels(f_labels_test) #![create_features] #![create_instance] -Machine network = create_machine("NeuralNetwork", labels=labels_train, auto_quick_initialize=True, max_num_epochs=4, epsilon=0.0, optimization_method="NNOM_GRADIENT_DESCENT", gd_learning_rate=0.01, gd_mini_batch_size=3, max_norm=1.0, dropout_input=0.5) +Machine network = create_machine("NeuralNetwork", auto_quick_initialize=True, max_num_epochs=4, epsilon=0.0, optimization_method="NNOM_GRADIENT_DESCENT", gd_learning_rate=0.01, gd_mini_batch_size=3, max_norm=1.0, dropout_input=0.5) #![create_instance] #![add_layers] @@ -27,7 +27,7 @@ network.put("seed", 10) #![add_layers] #![train_and_apply] -network.train(features_train) +network.train(features_train, labels_train) Labels labels_predict = network.apply(features_test) #![train_and_apply] diff --git a/examples/meta/src/neural_nets/feedforward_net_classification.sg.in b/examples/meta/src/neural_nets/feedforward_net_classification.sg.in index 8d396b8b2f1..beedfbad974 100644 --- a/examples/meta/src/neural_nets/feedforward_net_classification.sg.in +++ b/examples/meta/src/neural_nets/feedforward_net_classification.sg.in @@ -12,7 +12,7 @@ Labels labels_test = create_labels(f_labels_test) #![create_instance] int num_feats = features_train.get_int("num_features") -Machine network = create_machine("NeuralNetwork", labels=labels_train, auto_quick_initialize=True, l2_coefficient=0.01, dropout_hidden=0.5, max_num_epochs=50, gd_mini_batch_size=num_feats, gd_learning_rate=0.1, gd_momentum=0.9) +Machine network = create_machine("NeuralNetwork", auto_quick_initialize=True, l2_coefficient=0.01, dropout_hidden=0.5, max_num_epochs=50, gd_mini_batch_size=num_feats, gd_learning_rate=0.1, gd_momentum=0.9) #![create_instance] #![add_layers] @@ -26,7 +26,7 @@ network.put("seed", 1) #![add_layers] #![train_and_apply] -network.train(features_train) +network.train(features_train, labels_train) Labels labels_predict = network.apply(features_test) #![train_and_apply] diff --git a/examples/meta/src/neural_nets/feedforward_net_regression.sg.in b/examples/meta/src/neural_nets/feedforward_net_regression.sg.in index 53154541fd6..213138de542 100644 --- a/examples/meta/src/neural_nets/feedforward_net_regression.sg.in +++ b/examples/meta/src/neural_nets/feedforward_net_regression.sg.in @@ -13,7 +13,7 @@ Labels labels_test = create_labels(f_labels_test) #![create_instance] int num_feats = features_train.get_int("num_features") -Machine network = create_machine("NeuralNetwork", labels=labels_train, auto_quick_initialize=True, l2_coefficient=0.1, epsilon=0.0, max_num_epochs=40, gd_learning_rate=0.1, gd_momentum=0.9) +Machine network = create_machine("NeuralNetwork", auto_quick_initialize=True, l2_coefficient=0.1, epsilon=0.0, max_num_epochs=40, gd_learning_rate=0.1, gd_momentum=0.9) #![create_instance] #![add_layers] @@ -27,7 +27,7 @@ network.put("seed", 1) #![add_layers] #![train_and_apply] -network.train(features_train) +network.train(features_train, labels_train) Labels labels_predict = network.apply(features_test) #![train_and_apply] diff --git a/examples/meta/src/regression/chaid_tree.sg.in b/examples/meta/src/regression/chaid_tree.sg.in index 90c173ecc90..5fd6c37e816 100644 --- a/examples/meta/src/regression/chaid_tree.sg.in +++ b/examples/meta/src/regression/chaid_tree.sg.in @@ -14,11 +14,11 @@ ft[0] = 2 #![set_feature_types] #![create_machine] -Machine chaidtree = create_machine("CHAIDTree", labels=labels_train, dependent_vartype=2, feature_types=ft, num_breakpoints=50) +Machine chaidtree = create_machine("CHAIDTree", dependent_vartype=2, feature_types=ft, num_breakpoints=50) #![create_machine] #![train_and_apply] -chaidtree.train(feats_train) +chaidtree.train(feats_train, labels_train) Labels labels_predict = chaidtree.apply(feats_test) #![train_and_apply] diff --git a/examples/undocumented/python/kernel_histogram_word_string.py b/examples/undocumented/python/kernel_histogram_word_string.py index bb66029f25d..902b9fc212f 100644 --- a/examples/undocumented/python/kernel_histogram_word_string.py +++ b/examples/undocumented/python/kernel_histogram_word_string.py @@ -17,8 +17,8 @@ def kernel_histogram_word_string (fm_train_dna=traindat,fm_test_dna=testdat,labe feats_test=sg.create_string_features(charfeat, order-1, order, 0, False) labels=sg.create_labels(label_train_dna) - pie=sg.create_machine("PluginEstimate", pos_pseudo=ppseudo_count, neg_pseudo=npseudo_count, labels=labels) - pie.train(feats_train) + pie=sg.create_machine("PluginEstimate", pos_pseudo=ppseudo_count, neg_pseudo=npseudo_count) + pie.train(feats_train, labels) kernel=sg.create_kernel("HistogramWordStringKernel", estimate=pie) kernel.init(feats_train, feats_train) diff --git a/examples/undocumented/python/kernel_salzberg_word_string.py b/examples/undocumented/python/kernel_salzberg_word_string.py index ff5c16037b0..60c8b5e23ba 100644 --- a/examples/undocumented/python/kernel_salzberg_word_string.py +++ b/examples/undocumented/python/kernel_salzberg_word_string.py @@ -17,8 +17,8 @@ def kernel_salzberg_word_string (fm_train_dna=traindat,fm_test_dna=testdat,label feats_test=sg.create_string_features(charfeat, order-1, order, gap, reverse) labels=sg.create_labels(label_train_dna) - pie=sg.create_machine("PluginEstimate", labels=labels) - pie.train(feats_train) + pie=sg.create_machine("PluginEstimate") + pie.train(feats_train, labels) kernel=sg.create_kernel("SalzbergWordStringKernel", plugin_estimate=pie, labels=labels) kernel.init(feats_train, feats_train) diff --git a/examples/undocumented/python/multiclass_c45classifiertree.py b/examples/undocumented/python/multiclass_c45classifiertree.py index d1f2d860a99..07c89904ec1 100644 --- a/examples/undocumented/python/multiclass_c45classifiertree.py +++ b/examples/undocumented/python/multiclass_c45classifiertree.py @@ -34,9 +34,8 @@ def multiclass_c45classifiertree(train=traindat,test=testdat,labels=label_traind feats_train.add_subset(trsubset) c=C45ClassifierTree() - c.set_labels(train_labels) c.set_feature_types(ft) - c.train(feats_train) + c.train(feats_train, train_labels) train_labels.remove_subset() feats_train.remove_subset() diff --git a/examples/undocumented/python/multiclass_id3classifiertree.py b/examples/undocumented/python/multiclass_id3classifiertree.py index 6b1effe229b..b0ce96ca763 100644 --- a/examples/undocumented/python/multiclass_id3classifiertree.py +++ b/examples/undocumented/python/multiclass_id3classifiertree.py @@ -30,8 +30,7 @@ def multiclass_id3classifiertree(train=train_data,labels=train_labels,test=test_ # ID3 Tree formation id3=ID3ClassifierTree() - id3.set_labels(feats_labels) - id3.train(feats_train) + id3.train(feats_train, feats_labels) # Classify test data output=id3.apply_multiclass(feats_test).get_labels() diff --git a/examples/undocumented/python/stochasticgbmachine.py b/examples/undocumented/python/stochasticgbmachine.py index 04b2609b1fd..993d4274e12 100644 --- a/examples/undocumented/python/stochasticgbmachine.py +++ b/examples/undocumented/python/stochasticgbmachine.py @@ -28,8 +28,7 @@ def stochasticgbmachine(train=traindat,train_labels=label_traindat,ft=feat_types # train feats.add_subset(np.int32(p[0:int(num)])) labels.add_subset(np.int32(p[0:int(num)])) - s.set_labels(labels) - s.train(feats) + s.train(feats, labels) feats.remove_subset() labels.remove_subset() diff --git a/examples/undocumented/python/structure_discrete_hmsvm_bmrm.py b/examples/undocumented/python/structure_discrete_hmsvm_bmrm.py index 7907fb9f3bf..28a644d1bf5 100644 --- a/examples/undocumented/python/structure_discrete_hmsvm_bmrm.py +++ b/examples/undocumented/python/structure_discrete_hmsvm_bmrm.py @@ -29,8 +29,8 @@ def structure_discrete_hmsvm_bmrm (m_data_dict=data_dict): model = sg.create_structured_model("HMSVMModel", features=features, labels=labels, state_model_type="SMT_TWO_STATE", num_obs=num_obs) - sosvm = sg.create_machine("DualLibQPBMSOSVM", model=model, labels=labels, m_lambda=5000.0) - sosvm.train() + sosvm = sg.create_machine("DualLibQPBMSOSVM", model=model, m_lambda=5000.0) + sosvm.train(features, labels) #print sosvm.get_w() predicted = sosvm.apply(features) diff --git a/examples/undocumented/python/structure_factor_graph_model.py b/examples/undocumented/python/structure_factor_graph_model.py index e666ffdb85a..56c32f415f5 100644 --- a/examples/undocumented/python/structure_factor_graph_model.py +++ b/examples/undocumented/python/structure_factor_graph_model.py @@ -112,9 +112,9 @@ def structure_factor_graph_model(tr_samples = samples, tr_labels = labels, w = w model.add("factor_types", ftype[2]) # --- training with BMRM --- - bmrm = sg.create_machine("DualLibQPBMSOSVM", model=model, labels=tr_labels, m_lambda=0.01) + bmrm = sg.create_machine("DualLibQPBMSOSVM", model=model, m_lambda=0.01) #bmrm.set_verbose(True) - bmrm.train() + bmrm.train(tr_samples, tr_labels) #print 'learned weights:' #print bmrm.get_w() #print 'ground truth weights:' @@ -142,9 +142,9 @@ def structure_factor_graph_model(tr_samples = samples, tr_labels = labels, w = w #print hbm.get_train_errors() # --- training with SGD --- - sgd = sg.create_machine("StochasticSOSVM", model=model, labels=tr_labels, m_lambda=0.01) + sgd = sg.create_machine("StochasticSOSVM", model=model, m_lambda=0.01) #sgd.set_verbose(True) - sgd.train() + sgd.train(tr_samples, tr_labels) # evaluation #print('SGD: Average training error is %.4f' % SOSVMHelper.average_loss(sgd.get_w(), model)) @@ -154,9 +154,9 @@ def structure_factor_graph_model(tr_samples = samples, tr_labels = labels, w = w #print hp.get_train_errors() # --- training with FW --- - fw = sg.create_machine("FWSOSVM", model=model, labels=tr_labels, m_lambda=0.01, + fw = sg.create_machine("FWSOSVM", model=model, m_lambda=0.01, gap_threshold=0.01) - fw.train() + fw.train(tr_samples, tr_labels) # evaluation #print('FW: Average training error is %.4f' % SOSVMHelper.average_loss(fw.get_w(), model)) diff --git a/examples/undocumented/python/structure_graphcuts.py b/examples/undocumented/python/structure_graphcuts.py index 2da38de1a1b..fd6adec03e9 100644 --- a/examples/undocumented/python/structure_graphcuts.py +++ b/examples/undocumented/python/structure_graphcuts.py @@ -180,12 +180,12 @@ def graphcuts_sosvm(num_train_samples = 10, len_label = 5, len_feat = 20, num_te # the 3rd parameter is do_weighted_averaging, by turning this on, # a possibly faster convergence rate may be achieved. # the 4th parameter controls outputs of verbose training information - sgd = sg.create_machine("StochasticSOSVM", model=model, labels=labels_fg, do_weighted_averaging=True, + sgd = sg.create_machine("StochasticSOSVM", model=model, do_weighted_averaging=True, num_iter=150, m_lambda=0.0001) # train t0 = time.time() - sgd.train() + sgd.train(feats_fg, labels_fg) t1 = time.time() w_sgd = sgd.get("w") #print "SGD took", t1 - t0, "seconds." diff --git a/examples/undocumented/python/structure_hierarchical_multilabel_classification.py b/examples/undocumented/python/structure_hierarchical_multilabel_classification.py index a675a6b6ded..3cb94dc377b 100644 --- a/examples/undocumented/python/structure_hierarchical_multilabel_classification.py +++ b/examples/undocumented/python/structure_hierarchical_multilabel_classification.py @@ -110,7 +110,7 @@ def structure_hierarchical_multilabel_classification(train_file_name, features=train_features, labels=train_labels, taxonomy=train_taxonomy) - sgd = sg.create_machine("StochasticSOSVM", model=model, labels=train_labels) + sgd = sg.create_machine("StochasticSOSVM", model=model) # t1 = time.time() # sgd.train() # print('>>> Took %f time for training' % (time.time() - t1)) diff --git a/src/gpl b/src/gpl index 0b5edd5979d..e2c1db008aa 160000 --- a/src/gpl +++ b/src/gpl @@ -1 +1 @@ -Subproject commit 0b5edd5979d24b79e067756f2eb940ef5e403161 +Subproject commit e2c1db008aa05266f97e7f5f4e1ceb38003b6d13 diff --git a/src/shogun/classifier/PluginEstimate.cpp b/src/shogun/classifier/PluginEstimate.cpp index 0ad4266fbde..22112217dfd 100644 --- a/src/shogun/classifier/PluginEstimate.cpp +++ b/src/shogun/classifier/PluginEstimate.cpp @@ -39,10 +39,8 @@ PluginEstimate::~PluginEstimate() { } -bool PluginEstimate::train_machine(std::shared_ptr data) +bool PluginEstimate::train_machine(const std::shared_ptr& data, const std::shared_ptr& labs) { - ASSERT(m_labels) - ASSERT(m_labels->get_label_type() == LT_BINARY) if (data) { if (data->get_feature_class() != C_STRING || @@ -55,21 +53,19 @@ bool PluginEstimate::train_machine(std::shared_ptr data) } ASSERT(features) - - pos_model=std::make_shared(features); neg_model=std::make_shared(features); int32_t* pos_indizes=SG_MALLOC(int32_t, std::static_pointer_cast>(features)->get_num_vectors()); int32_t* neg_indizes=SG_MALLOC(int32_t, std::static_pointer_cast>(features)->get_num_vectors()); - ASSERT(m_labels->get_num_labels() == features->get_num_vectors()) + ASSERT(labs->get_num_labels() == features->get_num_vectors()) int32_t pos_idx = 0; int32_t neg_idx = 0; - auto binary_labels = std::static_pointer_cast(m_labels); - for (int32_t i=0; iget_num_labels(); i++) + auto binary_labels = std::static_pointer_cast(labs); + for (int32_t i=0; iget_num_labels(); i++) { if (binary_labels->get_label(i) > 0) pos_indizes[pos_idx++]=i; diff --git a/src/shogun/classifier/PluginEstimate.h b/src/shogun/classifier/PluginEstimate.h index ee40dbc3240..e9dfbc6224a 100644 --- a/src/shogun/classifier/PluginEstimate.h +++ b/src/shogun/classifier/PluginEstimate.h @@ -49,7 +49,7 @@ class PluginEstimate: public Machine * @param data (test)data to be classified * @return classified labels */ - std::shared_ptr apply_binary(std::shared_ptr data=NULL) override; + std::shared_ptr apply_binary(std::shared_ptr data) override; /** set features * @@ -206,7 +206,7 @@ class PluginEstimate: public Machine * * @return whether training was successful */ - bool train_machine(std::shared_ptr data=NULL) override; + bool train_machine(const std::shared_ptr& data, const std::shared_ptr& labs) override; protected: /** pseudo count for positive class */ diff --git a/src/shogun/classifier/mkl/MKLMulticlass.cpp b/src/shogun/classifier/mkl/MKLMulticlass.cpp index 7e5ac207c7c..c47a0e8f93e 100644 --- a/src/shogun/classifier/mkl/MKLMulticlass.cpp +++ b/src/shogun/classifier/mkl/MKLMulticlass.cpp @@ -89,8 +89,6 @@ void MKLMulticlass::initsvm( const std::shared_ptr& labs) error("MKLMulticlass::initsvm(): the number of labels is " "nonpositive, do not know how to handle this!\n"); } - - svm->set_labels(labs); } void MKLMulticlass::initlpsolver() diff --git a/src/shogun/classifier/svm/LibLinear.cpp b/src/shogun/classifier/svm/LibLinear.cpp index f94cfc5ac90..4ba93d24b98 100644 --- a/src/shogun/classifier/svm/LibLinear.cpp +++ b/src/shogun/classifier/svm/LibLinear.cpp @@ -70,6 +70,7 @@ LibLinear::~LibLinear() bool LibLinear::train(const std::shared_ptr& data, const std::shared_ptr& labs) { + m_num_labels = labs->get_num_labels(); return train_machine(data->as(), labs); } @@ -1363,19 +1364,9 @@ void LibLinear::solve_l2r_lr_dual( void LibLinear::set_linear_term(const SGVector linear_term) { - if (!m_labels) - error("Please assign labels first!"); - - int32_t num_labels = m_labels->get_num_labels(); - - if (num_labels != linear_term.vlen) - { - error( - "Number of labels ({}) does not match number" + require(m_num_labels == linear_term.vlen, "Number of labels ({}) does not match number" " of entries ({}) in linear term \n", - num_labels, linear_term.vlen); - } - + m_num_labels, linear_term.vlen); m_linear_term = linear_term; } diff --git a/src/shogun/classifier/svm/LibLinear.h b/src/shogun/classifier/svm/LibLinear.h index fd4fd89e8bf..ecee5992a8a 100644 --- a/src/shogun/classifier/svm/LibLinear.h +++ b/src/shogun/classifier/svm/LibLinear.h @@ -263,6 +263,8 @@ namespace shogun /** solver type */ LIBLINEAR_SOLVER_TYPE liblinear_solver_type; + + int32_t m_num_labels; }; } /* namespace shogun */ diff --git a/src/shogun/classifier/svm/WDSVMOcas.cpp b/src/shogun/classifier/svm/WDSVMOcas.cpp index c7dc47b652c..aa2ea22d8aa 100644 --- a/src/shogun/classifier/svm/WDSVMOcas.cpp +++ b/src/shogun/classifier/svm/WDSVMOcas.cpp @@ -76,8 +76,7 @@ WDSVMOcas::WDSVMOcas(E_SVM_TYPE type) } WDSVMOcas::WDSVMOcas( - float64_t C, int32_t d, int32_t from_d, std::shared_ptr> traindat, - std::shared_ptr trainlab) + float64_t C, int32_t d, int32_t from_d, std::shared_ptr> traindat) : Machine(), use_bias(false), bufsize(3000), C1(C), C2(C), epsilon(1e-3), degree(d), from_degree(from_d) { @@ -85,7 +84,6 @@ WDSVMOcas::WDSVMOcas( old_w=NULL; method=SVM_OCAS; features=std::move(traindat); - set_labels(std::move(trainlab)); wd_weights=NULL; w_offsets=NULL; normalization_const=1.0; @@ -158,29 +156,24 @@ int32_t WDSVMOcas::set_wd_weights() return w_dim_single_c; } -bool WDSVMOcas::train_machine(std::shared_ptr data) +bool WDSVMOcas::train_machine(const std::shared_ptr& data, const std::shared_ptr& labs) { io::info("C={}, epsilon={}, bufsize={}", get_C1(), get_epsilon(), bufsize); - ASSERT(m_labels) - ASSERT(m_labels->get_label_type() == LT_BINARY) - if (data) - { - if (data->get_feature_class() != C_STRING || + if (data->get_feature_class() != C_STRING || data->get_feature_type() != F_BYTE) - { - error("Features not of class string type byte"); - } - set_features(std::static_pointer_cast>(data)); + { + error("Features not of class string type byte"); } ASSERT(get_features()) - auto alphabet=get_features()->get_alphabet(); + features = data->as>(); + auto alphabet=features->get_alphabet(); ASSERT(alphabet && alphabet->get_alphabet()==RAWDNA) alphabet_size=alphabet->get_num_symbols(); string_length=features->get_num_vectors(); - SGVector labvec=(std::static_pointer_cast(m_labels))->get_labels(); + SGVector labvec=(std::static_pointer_cast(labs))->get_labels(); lab=labvec.vector; w_dim_single_char=set_wd_weights(); @@ -188,7 +181,7 @@ bool WDSVMOcas::train_machine(std::shared_ptr data) SG_DEBUG("w_dim_single_char={}", w_dim_single_char) w_dim=string_length*w_dim_single_char; SG_DEBUG("cutting plane has {} dims", w_dim) - num_vec=get_features()->get_max_vector_length(); + num_vec=features->get_max_vector_length(); set_normalization_const(); io::info("num_vec: {} num_lab: {}", num_vec, labvec.vlen); diff --git a/src/shogun/classifier/svm/WDSVMOcas.h b/src/shogun/classifier/svm/WDSVMOcas.h index 8220b279116..dd798f613cd 100644 --- a/src/shogun/classifier/svm/WDSVMOcas.h +++ b/src/shogun/classifier/svm/WDSVMOcas.h @@ -46,7 +46,7 @@ class WDSVMOcas : public Machine */ WDSVMOcas( float64_t C, int32_t d, int32_t from_d, - std::shared_ptr> traindat, std::shared_ptr trainlab); + std::shared_ptr> traindat); ~WDSVMOcas() override; /** get classifier type @@ -311,7 +311,7 @@ class WDSVMOcas : public Machine * * @return whether training was successful */ - bool train_machine(std::shared_ptr data=NULL) override; + bool train_machine(const std::shared_ptr& data, const std::shared_ptr& labs) override; protected: /** features */ diff --git a/src/shogun/evaluation/CrossValidation.cpp b/src/shogun/evaluation/CrossValidation.cpp index 839d859ef85..af5e83deee7 100644 --- a/src/shogun/evaluation/CrossValidation.cpp +++ b/src/shogun/evaluation/CrossValidation.cpp @@ -116,15 +116,7 @@ float64_t CrossValidation::evaluate_one_run(int64_t index) const auto evaluation_criterion = make_clone(m_evaluation_criterion); - try - { - machine->train(features_train, labels_train); - } - catch(const std::exception& e){ - machine->set_labels(labels_train); - machine->train(features_train); - } - + machine->train(features_train, labels_train); auto result_labels = machine->apply(features_test); diff --git a/src/shogun/machine/BaggingMachine.cpp b/src/shogun/machine/BaggingMachine.cpp index cbfd444f313..a41289edfa9 100644 --- a/src/shogun/machine/BaggingMachine.cpp +++ b/src/shogun/machine/BaggingMachine.cpp @@ -182,7 +182,7 @@ bool BaggingMachine::train_machine(const std::shared_ptr& data, const set_machine_parameters(c, idx); c->train(features, labels); - #pragma omp critical +#pragma omp critical { features->remove_subset(); labels->remove_subset(); diff --git a/src/shogun/machine/Composite.h b/src/shogun/machine/Composite.h index 4b7471589ea..000b1b619c9 100644 --- a/src/shogun/machine/Composite.h +++ b/src/shogun/machine/Composite.h @@ -56,7 +56,7 @@ namespace shogun m_stages = std::forward(stages); } - std::shared_ptr train( + bool train( const std::shared_ptr& data, const std::shared_ptr& labs) { @@ -78,7 +78,7 @@ namespace shogun }, v.second); } m_ensemble_machine->train(current_data, labs); - return m_ensemble_machine; + return true; } std::shared_ptr apply_multiclass(std::shared_ptr data) override diff --git a/src/shogun/machine/EnsembleMachine.h b/src/shogun/machine/EnsembleMachine.h index b21e6227828..bf0880f46dc 100644 --- a/src/shogun/machine/EnsembleMachine.h +++ b/src/shogun/machine/EnsembleMachine.h @@ -61,14 +61,14 @@ namespace shogun m_machines.push_back(machine); } - bool train_machine(std::shared_ptr data) override{ - require(m_labels, "Labels not set"); - train(data, m_labels); - return true; + bool train_machine( + const std::shared_ptr& data, const std::shared_ptr& labs) override { + return train(data, labs); } - void train( + + bool train( const std::shared_ptr& data, - const std::shared_ptr& labs) + const std::shared_ptr& labs) override { const int32_t& num_threads = env()->get_num_threads(); if (num_threads > 1) @@ -86,8 +86,7 @@ namespace shogun [&](int32_t start, int32_t end) { for (auto i = start; i < end; i++) { - m_machines[i]->set_labels(labs); - m_machines[i]->train(data); + m_machines[i]->train(data, labs); } }, t, t + machine_per_thread); @@ -98,18 +97,17 @@ namespace shogun } for (int i = machine_per_thread * num_threads; i < num_machine; i++) { - m_machines[i]->set_labels(labs); - m_machines[i]->train(data); + m_machines[i]->train(data, labs); } } else { for (auto&& machine : m_machines) { - machine->set_labels(labs); - machine->train(data); + machine->train(data, labs); } } + return true; } const char* get_name() const override diff --git a/src/shogun/machine/GLM.cpp b/src/shogun/machine/GLM.cpp index 9f0d5a09799..534649a3b99 100644 --- a/src/shogun/machine/GLM.cpp +++ b/src/shogun/machine/GLM.cpp @@ -71,15 +71,14 @@ GLM::GLM( std::shared_ptr GLM::apply_regression(std::shared_ptr data) { + std::shared_ptr features; if (data) { if (!data->has_property(FP_DOT)) error("Specified features are not of type CDotFeatures"); - set_features(std::static_pointer_cast(data)); + features = std::static_pointer_cast(data); } - require(features, "Features are not provided"); - auto num = features->get_num_vectors(); ASSERT(num > 0) ASSERT(m_w.vlen == features->get_dim_feature_space()) @@ -92,19 +91,10 @@ GLM::apply_regression(std::shared_ptr data) return std::make_shared(result); } -void GLM::init_model(const std::shared_ptr data) +void GLM::init_model(const std::shared_ptr& data) { - ASSERT(m_labels) - if (data) - { - if (!data->has_property(FP_DOT)) - error("Specified features are not of type CDotFeatures"); - set_features(std::static_pointer_cast(data)); - } - ASSERT(features) - NormalDistribution normal_dist; - const auto& n_features = features->get_dim_feature_space(); + const auto& n_features = data->get_dim_feature_space(); if (m_w.vlen == 0) { @@ -123,12 +113,13 @@ void GLM::init_model(const std::shared_ptr data) } } -void GLM::iteration() +void GLM::iteration(const std::shared_ptr& features, + const std::shared_ptr& labs) { SGVector w_old = m_w.clone(); - auto X = get_features()->get_computed_dot_feature_matrix(); - auto y = regression_labels(get_labels())->get_labels(); + auto X = features->get_computed_dot_feature_matrix(); + auto y = regression_labels(labs)->get_labels(); auto gradient_w = m_cost_function->get_gradient_weights( X, y, m_w, bias, m_lambda, m_alpha, m_compute_bias, m_eta, diff --git a/src/shogun/machine/GLM.h b/src/shogun/machine/GLM.h index 6b616cd11f5..948521795c2 100644 --- a/src/shogun/machine/GLM.h +++ b/src/shogun/machine/GLM.h @@ -90,9 +90,10 @@ namespace shogun } protected: - void init_model(const std::shared_ptr data) override; + void init_model(const std::shared_ptr& data) override; - void iteration() override; + void iteration(const std::shared_ptr& features, + const std::shared_ptr& labs) override; private: /** Distribution type */ diff --git a/src/shogun/machine/GaussianProcess.h b/src/shogun/machine/GaussianProcess.h index da175de103e..032cb859e3f 100644 --- a/src/shogun/machine/GaussianProcess.h +++ b/src/shogun/machine/GaussianProcess.h @@ -105,7 +105,7 @@ namespace shogun */ void set_labels(std::shared_ptr lab) override { - Machine::set_labels(lab); + NonParametricMachine::set_labels(lab); m_method->set_labels(lab); } diff --git a/src/shogun/machine/Machine.cpp b/src/shogun/machine/Machine.cpp index cc07f2e294f..a9af08d68ca 100644 --- a/src/shogun/machine/Machine.cpp +++ b/src/shogun/machine/Machine.cpp @@ -13,11 +13,10 @@ using namespace shogun; Machine::Machine() - : StoppableSGObject(), m_max_train_time(0), m_labels(NULL), + : StoppableSGObject(), m_max_train_time(0), m_solver_type(ST_AUTO) { SG_ADD(&m_max_train_time, "max_train_time", "Maximum training time."); - SG_ADD(&m_labels, "labels", "Labels to be used."); SG_ADD_OPTIONS( (machine_int_t*)&m_solver_type, "solver_type", "Type of solver.", ParameterProperties::NONE, @@ -33,26 +32,11 @@ Machine::~Machine() bool Machine::train(std::shared_ptr data) { - if (train_require_labels()) - { - if (m_labels == NULL) - error("{}@{}: No labels given", get_name(), fmt::ptr(this)); - - m_labels->ensure_valid(get_name()); - } - auto sub = connect_to_signal_handler(); bool result = false; if (support_feature_dispatching()) { - require(data != NULL, "Features not provided!"); - require( - data->get_num_vectors() == m_labels->get_num_labels(), - "Number of training vectors ({}) does not match number of " - "labels ({})", - data->get_num_vectors(), m_labels->get_num_labels()); - if (support_dense_dispatching() && data->get_feature_class() == C_DENSE) result = train_dense(data); else if ( @@ -74,10 +58,14 @@ bool Machine::train(std::shared_ptr data) bool Machine::train( const std::shared_ptr& data, const std::shared_ptr& labs) { - require(data->get_num_vectors() == labs->get_num_labels(), + if(data) + { + require(data->get_num_vectors() == labs->get_num_labels(), "Number of training vectors ({}) does not match number of " "labels ({})", data->get_num_vectors(), labs->get_num_labels()); + } + auto sub = connect_to_signal_handler(); bool result = false; @@ -101,21 +89,6 @@ bool Machine::train( return result; } -void Machine::set_labels(std::shared_ptr lab) -{ - if (lab != NULL) - { - if (!is_label_valid(lab)) - error("Invalid label for {}", get_name()); - - m_labels = lab; - } -} - -std::shared_ptr Machine::get_labels() -{ - return m_labels; -} void Machine::set_max_train_time(float64_t t) { diff --git a/src/shogun/machine/Machine.h b/src/shogun/machine/Machine.h index 682baaeabd7..3b4f615c2f0 100644 --- a/src/shogun/machine/Machine.h +++ b/src/shogun/machine/Machine.h @@ -184,18 +184,6 @@ class Machine : public StoppableSGObject /** apply machine to data in means of latent problem */ virtual std::shared_ptr apply_latent(std::shared_ptr data=NULL); - /** set labels - * - * @param lab labels - */ - virtual void set_labels(std::shared_ptr lab); - - /** get labels - * - * @return labels - */ - virtual std::shared_ptr get_labels(); - /** set maximum training time * * @param t maximimum training time @@ -346,7 +334,7 @@ class Machine : public StoppableSGObject float64_t m_max_train_time; /** labels */ - std::shared_ptr m_labels; + //std::shared_ptr m_labels; /** solver type */ ESolverType m_solver_type; diff --git a/src/shogun/machine/MulticlassMachine.cpp b/src/shogun/machine/MulticlassMachine.cpp index a54088af470..0e4bbcf3692 100644 --- a/src/shogun/machine/MulticlassMachine.cpp +++ b/src/shogun/machine/MulticlassMachine.cpp @@ -215,8 +215,6 @@ bool MulticlassMachine::train_machine(const std::shared_ptr& data, con m_machines.clear(); auto train_labels = std::make_shared(get_num_rhs_vectors()); - m_machine->set_labels(train_labels); - m_multiclass_strategy->train_start( multiclass_labels(labs), train_labels); while (m_multiclass_strategy->train_has_more()) @@ -228,7 +226,7 @@ bool MulticlassMachine::train_machine(const std::shared_ptr& data, con add_machine_subset(subset); } - m_machine->train(); + m_machine->train(data, train_labels); m_machines.push_back(get_machine_from_trained(m_machine)); if (subset.vlen) diff --git a/src/shogun/machine/NonParametricMachine.h b/src/shogun/machine/NonParametricMachine.h index 09351d1f9d0..2d78be4da2d 100644 --- a/src/shogun/machine/NonParametricMachine.h +++ b/src/shogun/machine/NonParametricMachine.h @@ -19,9 +19,8 @@ namespace shogun { // TODO : when all refactor is done, m_labels should be removed from // Machine Class - // SG_ADD( - // &m_labels, "labels", "labels used in train machine - // algorithm", ParameterProperties::READONLY); + SG_ADD( + &m_labels, "labels", "labels used in train machine algorithm"); SG_ADD( &m_features, "features_train", "Training features of nonparametric model", @@ -37,19 +36,35 @@ namespace shogun const std::shared_ptr& lab) override { m_labels = lab; + require( + data->get_num_vectors() == m_labels->get_num_labels(), + "Number of training vectors ({}) does not match number of " + "labels ({})", + data->get_num_vectors(), m_labels->get_num_labels()); return Machine::train(data); } + const char* get_name() const override { return "NonParametricMachine"; } + + virtual void set_labels(std::shared_ptr lab) + { + m_labels = lab; + } + /** get labels + * + * @return labels + */ + virtual std::shared_ptr get_labels() + { + return m_labels; + } protected: std::shared_ptr m_features; - - // TODO - // when all refactor is done, we should use this m_labels - // std::shared_ptr m_labels; + std::shared_ptr m_labels; }; } // namespace shogun #endif \ No newline at end of file diff --git a/src/shogun/machine/Pipeline.cpp b/src/shogun/machine/Pipeline.cpp index f43737b5b1c..8ae0d31c5a8 100644 --- a/src/shogun/machine/Pipeline.cpp +++ b/src/shogun/machine/Pipeline.cpp @@ -122,39 +122,12 @@ namespace shogun bool Pipeline::train_machine(std::shared_ptr data) { - if (train_require_labels()) - { - require(m_labels, "No labels given."); - } - auto current_data = data; - for (auto&& stage : m_stages) - { - if (holds_alternative>(stage.second)) - { - auto transformer = shogun::get>(stage.second); - transformer->train_require_labels() - ? transformer->fit(current_data, m_labels) - : transformer->fit(current_data); - - current_data = transformer->transform(current_data); - } - else - { - auto machine = shogun::get>(stage.second); - try - { - if (machine->train_require_labels()) - machine->set_labels(m_labels); - machine->train(current_data); - } - catch(const std::exception& e) - { - machine->train(current_data, m_labels); - } - - } - } - return true; + return train_machine_impl(data); + } + bool Pipeline::train_machine(const std::shared_ptr& data, + const std::shared_ptr& labs) + { + return train_machine_impl(data, labs); } std::shared_ptr Pipeline::apply(std::shared_ptr data) diff --git a/src/shogun/machine/Pipeline.h b/src/shogun/machine/Pipeline.h index 69944bb1504..ca814e6db37 100644 --- a/src/shogun/machine/Pipeline.h +++ b/src/shogun/machine/Pipeline.h @@ -123,8 +123,32 @@ namespace shogun EProblemType get_machine_problem_type() const override; protected: - bool train_machine(std::shared_ptr data = NULL) override; - + template + bool train_machine_impl(std::shared_ptr data, Args&& ... args) + { + require(data, "Data should not be NULL"); + auto current_data = data; + for (auto&& stage : m_stages) + { + if (holds_alternative>(stage.second)) + { + auto transformer = shogun::get>(stage.second); + transformer->train_require_labels() + ? transformer->fit(current_data, args...) + : transformer->fit(current_data); + current_data = transformer->transform(current_data); + } + else + { + auto machine = shogun::get>(stage.second); + machine->train(current_data, args...); + } + } + return true; + } + bool train_machine(std::shared_ptr data) override; + bool train_machine(const std::shared_ptr& data, + const std::shared_ptr& labs) override; std::vector, std::shared_ptr>>> m_stages; bool train_require_labels() const override; diff --git a/src/shogun/machine/StochasticGBMachine.cpp b/src/shogun/machine/StochasticGBMachine.cpp index a8be1f9e0a6..7cbb8991418 100644 --- a/src/shogun/machine/StochasticGBMachine.cpp +++ b/src/shogun/machine/StochasticGBMachine.cpp @@ -161,12 +161,11 @@ std::shared_ptr StochasticGBMachine::apply_regression(std::sha return std::make_shared(retlabs); } -bool StochasticGBMachine::train_machine(std::shared_ptr data) +bool StochasticGBMachine::train_machine(const std::shared_ptr& data, const std::shared_ptr& labs) { require(data,"training data not supplied!"); require(m_machine,"machine not set!"); require(m_loss,"loss function not specified"); - require(m_labels, "labels not specified"); auto feats=data->as>(); @@ -181,7 +180,7 @@ bool StochasticGBMachine::train_machine(std::shared_ptr data) for (auto i : SG_PROGRESS(range(m_num_iter))) { - const auto result = get_subset(feats, interf); + const auto result = get_subset(feats, interf, labs); const auto& feats_iter = std::get<0>(result); const auto& interf_iter = std::get<1>(result); const auto& labels_iter = std::get<2>(result); @@ -258,10 +257,10 @@ std::shared_ptr StochasticGBMachine::compute_pseudo_residuals( std::tuple>, std::shared_ptr, std::shared_ptr> StochasticGBMachine::get_subset( - std::shared_ptr> f, std::shared_ptr interf) + std::shared_ptr> f, std::shared_ptr interf, std::shared_ptr labs) { if (m_subset_frac == 1.0) - return std::make_tuple(f, interf, m_labels); + return std::make_tuple(f, interf, labs); int32_t subset_size=m_subset_frac*(f->get_num_vectors()); SGVector idx(f->get_num_vectors()); @@ -273,7 +272,7 @@ StochasticGBMachine::get_subset( return std::make_tuple( view(f, subset), view(interf, subset), - view(m_labels, subset)); + view(labs, subset)); } void StochasticGBMachine::initialize_learners() diff --git a/src/shogun/machine/StochasticGBMachine.h b/src/shogun/machine/StochasticGBMachine.h index 75086b08526..c8887972495 100644 --- a/src/shogun/machine/StochasticGBMachine.h +++ b/src/shogun/machine/StochasticGBMachine.h @@ -148,7 +148,7 @@ class StochasticGBMachine : public RandomMixin * @param data training data * @return true */ - bool train_machine(std::shared_ptr data=NULL) override; + bool train_machine(const std::shared_ptr& data, const std::shared_ptr& labs) override; /** compute gamma values * @@ -185,7 +185,8 @@ class StochasticGBMachine : public RandomMixin */ std::tuple>, std::shared_ptr, std::shared_ptr> - get_subset(std::shared_ptr> f, std::shared_ptr interf); + get_subset(std::shared_ptr> f, std::shared_ptr interf, + std::shared_ptr labs); /** reset arrays of weak learners and gamma values */ void initialize_learners(); diff --git a/src/shogun/machine/StructuredOutputMachine.cpp b/src/shogun/machine/StructuredOutputMachine.cpp index b3d42f8396a..f4200a9c0f7 100644 --- a/src/shogun/machine/StructuredOutputMachine.cpp +++ b/src/shogun/machine/StructuredOutputMachine.cpp @@ -27,7 +27,6 @@ StructuredOutputMachine::StructuredOutputMachine( const std::shared_ptr& labs) : Machine(), m_model(std::move(model)), m_surrogate_loss(NULL) { - set_labels(labs); register_parameters(); } @@ -56,13 +55,6 @@ void StructuredOutputMachine::register_parameters() m_helper = NULL; } -void StructuredOutputMachine::set_labels(std::shared_ptr lab) -{ - Machine::set_labels(lab); - require(m_model != NULL, "please call set_model() before set_labels()"); - m_model->set_labels(lab->as()); -} - void StructuredOutputMachine::set_features(std::shared_ptr f) { m_model->set_features(std::move(f)); diff --git a/src/shogun/machine/StructuredOutputMachine.h b/src/shogun/machine/StructuredOutputMachine.h index d0ce6d4f716..3ac0e1a73ad 100644 --- a/src/shogun/machine/StructuredOutputMachine.h +++ b/src/shogun/machine/StructuredOutputMachine.h @@ -77,12 +77,6 @@ class StructuredOutputMachine : public Machine return "StructuredOutputMachine"; } - /** set labels - * - * @param lab labels - */ - void set_labels(std::shared_ptr lab) override; - /** set features * * @param f features diff --git a/src/shogun/multiclass/GaussianNaiveBayes.cpp b/src/shogun/multiclass/GaussianNaiveBayes.cpp index 94ba8303c42..8090de32793 100644 --- a/src/shogun/multiclass/GaussianNaiveBayes.cpp +++ b/src/shogun/multiclass/GaussianNaiveBayes.cpp @@ -25,15 +25,12 @@ GaussianNaiveBayes::GaussianNaiveBayes() : NativeMulticlassMachine(), m_features init(); }; -GaussianNaiveBayes::GaussianNaiveBayes(const std::shared_ptr& train_examples, - const std::shared_ptr& train_labels) : NativeMulticlassMachine(), m_features(NULL), +GaussianNaiveBayes::GaussianNaiveBayes(const std::shared_ptr& train_examples) + : NativeMulticlassMachine(), m_features(NULL), m_min_label(0), m_num_classes(0), m_dim(0), m_means(), m_variances(), m_label_prob(), m_rates() { init(); - ASSERT(train_examples->get_num_vectors() == train_labels->get_num_labels()) - set_labels(train_labels); - if (!train_examples->has_property(FP_DOT)) error("Specified features are not of type CDotFeatures"); diff --git a/src/shogun/multiclass/GaussianNaiveBayes.h b/src/shogun/multiclass/GaussianNaiveBayes.h index fb3280942e2..5f4438b87f1 100644 --- a/src/shogun/multiclass/GaussianNaiveBayes.h +++ b/src/shogun/multiclass/GaussianNaiveBayes.h @@ -46,7 +46,7 @@ class GaussianNaiveBayes : public NativeMulticlassMachine * @param train_examples train examples * @param train_labels labels corresponding to train_examples */ - GaussianNaiveBayes(const std::shared_ptr& train_examples, const std::shared_ptr& train_labels); + GaussianNaiveBayes(const std::shared_ptr& train_examples); /** destructor * diff --git a/src/shogun/multiclass/tree/C45ClassifierTree.cpp b/src/shogun/multiclass/tree/C45ClassifierTree.cpp index 146ea836b7a..ce285a7cef3 100644 --- a/src/shogun/multiclass/tree/C45ClassifierTree.cpp +++ b/src/shogun/multiclass/tree/C45ClassifierTree.cpp @@ -105,7 +105,8 @@ void C45ClassifierTree::clear_feature_types() m_types_set=false; } -bool C45ClassifierTree::train_machine(std::shared_ptr data) +bool C45ClassifierTree::train_machine(const std::shared_ptr& data, + const std::shared_ptr& labs) { require(data,"Data required for training"); require(data->get_feature_class()==C_DENSE,"Dense data required for training"); @@ -140,7 +141,7 @@ bool C45ClassifierTree::train_machine(std::shared_ptr data) SGVector feature_ids(num_features); feature_ids.range_fill(); - set_root(C45train(data, m_weights, multiclass_labels(m_labels), feature_ids, 0)); + set_root(C45train(data, m_weights, multiclass_labels(labs), feature_ids, 0)); if (m_root) { compute_feature_importance(num_features, m_root); diff --git a/src/shogun/multiclass/tree/C45ClassifierTree.h b/src/shogun/multiclass/tree/C45ClassifierTree.h index 22353ff5ddd..c3e32d0341d 100644 --- a/src/shogun/multiclass/tree/C45ClassifierTree.h +++ b/src/shogun/multiclass/tree/C45ClassifierTree.h @@ -157,7 +157,7 @@ class C45ClassifierTree : public FeatureImportanceTree /** train machine - build C4.5 Tree from training data * @param data training data */ - bool train_machine(std::shared_ptr data=NULL) override; + bool train_machine(const std::shared_ptr& data, const std::shared_ptr& labs) override; private: diff --git a/src/shogun/multiclass/tree/CHAIDTree.cpp b/src/shogun/multiclass/tree/CHAIDTree.cpp index b3973647b6c..7e041b4a09f 100644 --- a/src/shogun/multiclass/tree/CHAIDTree.cpp +++ b/src/shogun/multiclass/tree/CHAIDTree.cpp @@ -152,7 +152,7 @@ void CHAIDTree::set_dependent_vartype(int32_t var) m_dependent_vartype=var; } -bool CHAIDTree::train_machine(std::shared_ptr data) +bool CHAIDTree::train_machine(const std::shared_ptr& data, const std::shared_ptr& labs) { require(data, "Data required for training"); @@ -188,7 +188,7 @@ bool CHAIDTree::train_machine(std::shared_ptr data) } } - set_root(CHAIDtrain(data,m_weights,m_labels,0)); + set_root(CHAIDtrain(data,m_weights,labs,0)); // restore feature types if (updated) diff --git a/src/shogun/multiclass/tree/CHAIDTree.h b/src/shogun/multiclass/tree/CHAIDTree.h index 2492cc981bd..d53352ae426 100644 --- a/src/shogun/multiclass/tree/CHAIDTree.h +++ b/src/shogun/multiclass/tree/CHAIDTree.h @@ -231,7 +231,7 @@ class CHAIDTree : public TreeMachine * @param data training data * @return true */ - bool train_machine(std::shared_ptr data=NULL) override; + bool train_machine(const std::shared_ptr& data, const std::shared_ptr& labs) override; private: /** CHAIDtrain - recursive CHAID training method diff --git a/src/shogun/multiclass/tree/ID3ClassifierTree.cpp b/src/shogun/multiclass/tree/ID3ClassifierTree.cpp index 715cf51e4e3..d5866bdf545 100644 --- a/src/shogun/multiclass/tree/ID3ClassifierTree.cpp +++ b/src/shogun/multiclass/tree/ID3ClassifierTree.cpp @@ -68,7 +68,7 @@ bool ID3ClassifierTree::prune_tree(std::shared_ptr> val return true; } -bool ID3ClassifierTree::train_machine(std::shared_ptr data) +bool ID3ClassifierTree::train_machine(const std::shared_ptr& data, const std::shared_ptr& labs) { require(data,"Data required for training"); require(data->get_feature_class()==C_DENSE, "Dense data required for training"); @@ -77,7 +77,7 @@ bool ID3ClassifierTree::train_machine(std::shared_ptr data) SGVector feature_ids = SGVector(num_features); feature_ids.range_fill(); - set_root(id3train(data, multiclass_labels(m_labels), feature_ids, 0)); + set_root(id3train(data, multiclass_labels(labs), feature_ids, 0)); if (m_root) { diff --git a/src/shogun/multiclass/tree/ID3ClassifierTree.h b/src/shogun/multiclass/tree/ID3ClassifierTree.h index f601ba239dc..595301ebdde 100644 --- a/src/shogun/multiclass/tree/ID3ClassifierTree.h +++ b/src/shogun/multiclass/tree/ID3ClassifierTree.h @@ -120,7 +120,7 @@ class ID3ClassifierTree : public FeatureImportanceTree /** train machine - build ID3 Tree from training data * @param data training data */ - bool train_machine(std::shared_ptr data=NULL) override; + bool train_machine(const std::shared_ptr& data, const std::shared_ptr& labs) override; private: diff --git a/src/shogun/multiclass/tree/RelaxedTree.cpp b/src/shogun/multiclass/tree/RelaxedTree.cpp index 70e3a537aa6..425e5796940 100644 --- a/src/shogun/multiclass/tree/RelaxedTree.cpp +++ b/src/shogun/multiclass/tree/RelaxedTree.cpp @@ -21,7 +21,7 @@ using namespace shogun; RelaxedTree::RelaxedTree() :m_max_num_iter(3), m_A(0.5), m_B(5), m_svm_C(1), m_svm_epsilon(0.001), - m_kernel(NULL), m_feats(NULL), m_machine_for_confusion_matrix(NULL), m_num_classes(0) + m_kernel(NULL), m_machine_for_confusion_matrix(NULL), m_num_classes(0) { SG_ADD(&m_max_num_iter, "m_max_num_iter", "max number of iterations in alternating optimization"); SG_ADD(&m_svm_C, "m_svm_C", "C for svm", ParameterProperties::HYPER); @@ -36,22 +36,17 @@ RelaxedTree::~RelaxedTree() std::shared_ptr RelaxedTree::apply_multiclass(std::shared_ptr data) { - if (data != NULL) - { - auto feats = data->as>(); - set_features(feats); - } - + auto feats = data->as>(); // init kernels for all sub-machines for (auto m: m_machines) { auto machine = m->as(); auto kernel = machine->get_kernel(); auto lhs = kernel->get_lhs(); - kernel->init(lhs, m_feats); + kernel->init(lhs, feats); } - auto lab = std::make_shared(m_feats->get_num_vectors()); + auto lab = std::make_shared(feats->get_num_vectors()); for (int32_t i=0; i < lab->get_num_labels(); ++i) { @@ -115,31 +110,23 @@ float64_t RelaxedTree::apply_one(int32_t idx) return klass; } -bool RelaxedTree::train_machine(std::shared_ptr data) +bool RelaxedTree::train_machine(const std::shared_ptr& data, const std::shared_ptr& labs) { - if (m_machine_for_confusion_matrix == NULL) - error("Call set_machine_for_confusion_matrix before training"); - if (m_kernel == NULL) - error("assign a valid kernel before training"); - - if (data) - { - set_features(data->template as>()); - } - - auto lab = multiclass_labels(m_labels); - + require(m_machine_for_confusion_matrix, + "Call set_machine_for_confusion_matrix before training"); + auto lab = multiclass_labels(labs); + m_num_classes = lab->get_num_classes(); RelaxedTreeUtil util; SGMatrix conf_mat = util.estimate_confusion_matrix( m_machine_for_confusion_matrix->as(), - m_feats, lab, m_num_classes); + data, lab, m_num_classes); // train root SGVector classes(m_num_classes); classes.range_fill(); - m_root = train_node(conf_mat, classes); + m_root = train_node(conf_mat, classes, data, labs); std::queue> node_q; node_q.push(m_root->as()); @@ -163,7 +150,7 @@ bool RelaxedTree::train_machine(std::shared_ptr data) if (left_classes.vlen >= 2) { - auto left_node = train_node(conf_mat, left_classes); + auto left_node = train_node(conf_mat, left_classes, data, labs); node->left(left_node); node_q.push(left_node); } @@ -182,7 +169,7 @@ bool RelaxedTree::train_machine(std::shared_ptr data) if (right_classes.vlen >= 2) { - auto right_node = train_node(conf_mat, right_classes); + auto right_node = train_node(conf_mat, right_classes, data, labs); node->right(right_node); node_q.push(right_node); } @@ -193,7 +180,8 @@ bool RelaxedTree::train_machine(std::shared_ptr data) return true; } -std::shared_ptr RelaxedTree::train_node(const SGMatrix &conf_mat, SGVector classes) +std::shared_ptr RelaxedTree::train_node(const SGMatrix &conf_mat, + SGVector classes, const std::shared_ptr& data, const std::shared_ptr& labs) { SGVector best_mu; std::shared_ptr best_svm = NULL; @@ -204,7 +192,7 @@ std::shared_ptr RelaxedTree::train_node(const SGMatrix(); - SGVector mu = train_node_with_initialization(*it, classes, svm); + SGVector mu = train_node_with_initialization(*it, classes, svm, data, labs); float64_t score = compute_score(mu, svm); if (score < best_score) @@ -255,7 +243,8 @@ float64_t RelaxedTree::compute_score(SGVector mu, const std::shared_ptr return score; } -SGVector RelaxedTree::train_node_with_initialization(const RelaxedTree::entry_t &mu_entry, SGVector classes, const std::shared_ptr&svm) +SGVector RelaxedTree::train_node_with_initialization(const RelaxedTree::entry_t &mu_entry, SGVector classes, + const std::shared_ptr&svm, const std::shared_ptr& data, const std::shared_ptr& labels) { SGVector mu(classes.vlen), prev_mu(classes.vlen); mu.zero(); @@ -266,7 +255,7 @@ SGVector RelaxedTree::train_node_with_initialization(const RelaxedTree: svm->set_C(m_svm_C, m_svm_C); svm->set_epsilon(m_svm_epsilon); - auto labs = multiclass_labels(m_labels); + auto labs = multiclass_labels(labels); for (int32_t iiter=0; iiter < m_max_num_iter; ++iiter) { long_mu.zero(); @@ -278,8 +267,8 @@ SGVector RelaxedTree::train_node_with_initialization(const RelaxedTree: long_mu[classes[i]] = -1; } - SGVector subset(m_feats->get_num_vectors()); - SGVector binlab(m_feats->get_num_vectors()); + SGVector subset(data->get_num_vectors()); + SGVector binlab(data->get_num_vectors()); int32_t k=0; for (int32_t i=0; i < binlab.vlen; ++i) @@ -293,7 +282,7 @@ SGVector RelaxedTree::train_node_with_initialization(const RelaxedTree: subset.vlen = k; auto binary_labels = std::make_shared(binlab); - auto feats_train = view(m_feats, subset); + auto feats_train = view(data, subset); auto labels_train = view(binary_labels, subset); auto kernel = make_clone(m_kernel, ParameterProperties::ALL^ParameterProperties::MODEL); @@ -305,7 +294,7 @@ SGVector RelaxedTree::train_node_with_initialization(const RelaxedTree: std::copy(&mu[0], &mu[mu.vlen], &prev_mu[0]); - mu = color_label_space(svm, classes); + mu = color_label_space(svm, classes, data, labs); bool bbreak = true; for (int32_t i=0; i < mu.vlen; ++i) @@ -369,12 +358,12 @@ std::vector RelaxedTree::init_node(const SGMatrix(entries.begin(), entries.begin() + n_samples); } -SGVector RelaxedTree::color_label_space(std::shared_ptrsvm, SGVector classes) +SGVector RelaxedTree::color_label_space(std::shared_ptrsvm, SGVector classes, const std::shared_ptr& data, const std::shared_ptr& labs) { SGVector mu(classes.vlen); - auto labels = multiclass_labels(m_labels); + auto labels = multiclass_labels(labs); - SGVector resp = eval_binary_model_K(std::move(svm)); + SGVector resp = eval_binary_model_K(std::move(svm), data); ASSERT(resp.vlen == labels->get_num_labels()) SGVector xi_pos_class(classes.vlen), xi_neg_class(classes.vlen); @@ -871,9 +860,9 @@ void RelaxedTree::enforce_balance_constraints_lower(SGVector &mu, SGVec } } -SGVector RelaxedTree::eval_binary_model_K(const std::shared_ptr&svm) +SGVector RelaxedTree::eval_binary_model_K(const std::shared_ptr&svm, const std::shared_ptr& data) { - auto lab = svm->apply_regression(m_feats); + auto lab = svm->apply_regression(data); SGVector resp(lab->get_num_labels()); for (int32_t i=0; i < resp.vlen; ++i) resp[i] = lab->get_label(i) - m_A/m_svm_C; diff --git a/src/shogun/multiclass/tree/RelaxedTree.h b/src/shogun/multiclass/tree/RelaxedTree.h index 1e82fa6f920..12e26b8ae20 100644 --- a/src/shogun/multiclass/tree/RelaxedTree.h +++ b/src/shogun/multiclass/tree/RelaxedTree.h @@ -45,14 +45,6 @@ class RelaxedTree: public TreeMachine /** apply machine to data in means of multiclass classification problem */ std::shared_ptr apply_multiclass(std::shared_ptr data=NULL) override; - /** set features - * @param feats features - */ - void set_features(std::shared_ptr >feats) - { - m_feats = std::move(feats); - } - /** set kernel * @param kernel the kernel to be used */ @@ -61,19 +53,6 @@ class RelaxedTree: public TreeMachine m_kernel = std::move(kernel); } - /** set labels - * - * @param lab labels - */ - void set_labels(std::shared_ptr lab) override - { - auto mlab = multiclass_labels(lab); - require(lab, "requires MulticlassLabes"); - - Machine::set_labels(mlab); - m_num_classes = mlab->get_num_classes(); - } - /** set machine for confusion matrix * @param machine the multiclass machine for initializing the confusion matrix */ @@ -162,20 +141,6 @@ class RelaxedTree: public TreeMachine return m_max_num_iter; } - /** train machine - * - * @param data training data (parameter can be avoided if distance or - * kernel-based classifiers are used and distance/kernels are - * initialized with train data). - * If flag is set, model features will be stored after training. - * - * @return whether training was successful - */ - bool train(std::shared_ptr data=NULL) override - { - return Machine::train(data); - } - /** entry type */ typedef std::pair, float64_t> entry_t; protected: @@ -193,21 +158,24 @@ class RelaxedTree: public TreeMachine * * @return whether training was successful */ - bool train_machine(std::shared_ptr data) override; + bool train_machine(const std::shared_ptr& data, const std::shared_ptr& labs) override; /** train node */ - std::shared_ptr train_node(const SGMatrix &conf_mat, SGVector classes); + std::shared_ptr train_node(const SGMatrix &conf_mat, SGVector classes, + const std::shared_ptr&, const std::shared_ptr&); /** init node */ std::vector init_node(const SGMatrix &global_conf_mat, SGVector classes); /** train node with initialization */ - SGVector train_node_with_initialization(const RelaxedTree::entry_t &mu_entry, SGVector classes, const std::shared_ptr&svm); + SGVector train_node_with_initialization(const RelaxedTree::entry_t &mu_entry, SGVector classes, + const std::shared_ptr&svm, const std::shared_ptr&, const std::shared_ptr&); /** compute score */ float64_t compute_score(SGVector mu, const std::shared_ptr&svm); /** color label space */ - SGVector color_label_space(std::shared_ptrsvm, SGVector classes); + SGVector color_label_space(std::shared_ptrsvm, SGVector classes, + const std::shared_ptr&, const std::shared_ptr&); /** evaluate binary model K */ - SGVector eval_binary_model_K(const std::shared_ptr&svm); + SGVector eval_binary_model_K(const std::shared_ptr&svm, const std::shared_ptr& data); /** enforce balance constraints upper */ void enforce_balance_constraints_upper(SGVector &mu, SGVector &delta_neg, SGVector &delta_pos, int32_t B_prime, SGVector& xi_neg_class); diff --git a/src/shogun/neuralnets/NeuralNetwork.cpp b/src/shogun/neuralnets/NeuralNetwork.cpp index 80438378cf5..5a9784f7dbd 100644 --- a/src/shogun/neuralnets/NeuralNetwork.cpp +++ b/src/shogun/neuralnets/NeuralNetwork.cpp @@ -231,8 +231,14 @@ std::shared_ptr> NeuralNetwork::transform( return std::make_shared>(output_activations); } -bool NeuralNetwork::train_machine(std::shared_ptr data) +bool NeuralNetwork::train_machine(const std::shared_ptr& data, const std::shared_ptr& labs) { + if (labs->get_label_type() == LT_BINARY) + m_problem_type = PT_BINARY; + else if (labs->get_label_type() == LT_REGRESSION) + m_problem_type = PT_REGRESSION; + else + m_problem_type = PT_MULTICLASS; if (m_auto_quick_initialize) { quick_connect(); @@ -243,7 +249,7 @@ bool NeuralNetwork::train_machine(std::shared_ptr data) "Maximum number of epochs ({}) must be >= 0", m_max_num_epochs); SGMatrix inputs = features_to_matrix(data); - SGMatrix targets = labels_to_matrix(m_labels); + SGMatrix targets = labels_to_matrix(labs); for (int32_t i=0; i NeuralNetwork::labels_to_matrix(const std::shared_ptrget_label_type() == LT_BINARY) - return PT_BINARY; - else if (m_labels->get_label_type() == LT_REGRESSION) - return PT_REGRESSION; - else return PT_MULTICLASS; + return m_problem_type; } bool NeuralNetwork::is_label_valid(std::shared_ptr lab) const @@ -703,21 +700,6 @@ bool NeuralNetwork::is_label_valid(std::shared_ptr lab) const lab->get_label_type() == LT_REGRESSION); } -void NeuralNetwork::set_labels(std::shared_ptr lab) -{ - if (lab->get_label_type() == LT_BINARY) - { - require(get_num_outputs() <= 2, "Cannot use {} in a neural network " - "with more that 2 output neurons", lab->get_name()); - } - else if (lab->get_label_type() == LT_REGRESSION) - { - require(get_num_outputs() == 1, "Cannot use {} in a neural network " - "with more that 1 output neuron", lab->get_name()); - } - - Machine::set_labels(lab); -} SGVector* NeuralNetwork::get_layer_parameters(int32_t i) const { diff --git a/src/shogun/neuralnets/NeuralNetwork.h b/src/shogun/neuralnets/NeuralNetwork.h index 1a929337b5e..ab275f8c19d 100644 --- a/src/shogun/neuralnets/NeuralNetwork.h +++ b/src/shogun/neuralnets/NeuralNetwork.h @@ -180,12 +180,6 @@ friend class DeepBeliefNetwork; virtual std::shared_ptr> transform( std::shared_ptr> data); - /** set labels - * - * @param lab labels - */ - void set_labels(std::shared_ptr lab) override; - /** get classifier type * * @return classifier type CT_NEURALNETWORK @@ -469,7 +463,7 @@ friend class DeepBeliefNetwork; protected: /** trains the network */ - bool train_machine(std::shared_ptr data=NULL) override; + bool train_machine(const std::shared_ptr& data, const std::shared_ptr& labs) override; /** trains the network using gradient descent*/ virtual bool train_gradient_descent(SGMatrix inputs, @@ -737,6 +731,8 @@ friend class DeepBeliefNetwork; */ const SGMatrix* m_lbfgs_temp_inputs; const SGMatrix* m_lbfgs_temp_targets; + + EProblemType m_problem_type; }; } diff --git a/src/shogun/structure/FWSOSVM.cpp b/src/shogun/structure/FWSOSVM.cpp index 475d31fbfbf..a3b7d09b4c7 100644 --- a/src/shogun/structure/FWSOSVM.cpp +++ b/src/shogun/structure/FWSOSVM.cpp @@ -61,7 +61,7 @@ EMachineType FWSOSVM::get_classifier_type() return CT_FWSOSVM; } -bool FWSOSVM::train_machine(std::shared_ptr data) +bool FWSOSVM::train_machine(const std::shared_ptr& data, const std::shared_ptr& labs) { SG_TRACE("Entering CFWSOSVM::train_machine."); if (data) @@ -76,7 +76,7 @@ bool FWSOSVM::train_machine(std::shared_ptr data) // Dimensionality of the joint feature space int32_t M = m_model->get_dim(); // Number of training examples - int32_t N = m_labels->as()->get_num_labels(); + int32_t N = labs->as()->get_num_labels(); SG_DEBUG("M={}, N ={}.", M, N); diff --git a/src/shogun/structure/FWSOSVM.h b/src/shogun/structure/FWSOSVM.h index 0995aab3178..70a6d42946e 100644 --- a/src/shogun/structure/FWSOSVM.h +++ b/src/shogun/structure/FWSOSVM.h @@ -89,7 +89,7 @@ class FWSOSVM : public LinearStructuredOutputMachine * @param data training data * @return whether the training was successful */ - bool train_machine(std::shared_ptr data = NULL) override; + bool train_machine(const std::shared_ptr& data, const std::shared_ptr& labs) override; private: /** register and initialize parameters */ diff --git a/src/shogun/structure/FactorGraphDataGenerator.cpp b/src/shogun/structure/FactorGraphDataGenerator.cpp index c3ff583bb2e..f4001ef300a 100644 --- a/src/shogun/structure/FactorGraphDataGenerator.cpp +++ b/src/shogun/structure/FactorGraphDataGenerator.cpp @@ -531,7 +531,7 @@ float64_t FactorGraphDataGenerator::test_sosvm(EMAPInferType infer_type) // 2.2 Train SGD - sgd->train(); + sgd->train(fg_feats_train, fg_labels_train); // 3.1 Evaluation auto labels_sgd = sgd->apply()->as(); diff --git a/src/shogun/structure/StochasticSOSVM.cpp b/src/shogun/structure/StochasticSOSVM.cpp index b953734bb7a..81f903f4c4b 100644 --- a/src/shogun/structure/StochasticSOSVM.cpp +++ b/src/shogun/structure/StochasticSOSVM.cpp @@ -61,7 +61,8 @@ EMachineType StochasticSOSVM::get_classifier_type() return CT_STOCHASTICSOSVM; } -bool StochasticSOSVM::train_machine(std::shared_ptr data) +bool StochasticSOSVM::train_machine(const std::shared_ptr& data, + const std::shared_ptr& labs) { SG_TRACE("Entering CStochasticSOSVM::train_machine."); if (data) @@ -76,7 +77,7 @@ bool StochasticSOSVM::train_machine(std::shared_ptr data) // Dimensionality of the joint feature space int32_t M = m_model->get_dim(); // Number of training examples - int32_t N = m_labels->as()->get_num_labels(); + int32_t N = labs->as()->get_num_labels(); require(M > 0, "StochasticSOSVM underlying model has not been initialized properly." "Expected number of dimensions to be greater than 0."); diff --git a/src/shogun/structure/StochasticSOSVM.h b/src/shogun/structure/StochasticSOSVM.h index ca83b039a68..0e5e4aac2e9 100644 --- a/src/shogun/structure/StochasticSOSVM.h +++ b/src/shogun/structure/StochasticSOSVM.h @@ -88,7 +88,8 @@ class StochasticSOSVM : public RandomMixin * @param data training data * @return whether the training was successful */ - bool train_machine(std::shared_ptr data = NULL) override; + bool train_machine(const std::shared_ptr& data, + const std::shared_ptr& labs) override; private: /** register and initialize parameters */ diff --git a/src/shogun/transfer/multitask/LibLinearMTL.cpp b/src/shogun/transfer/multitask/LibLinearMTL.cpp index c9ff4ea2d12..47f415bc838 100644 --- a/src/shogun/transfer/multitask/LibLinearMTL.cpp +++ b/src/shogun/transfer/multitask/LibLinearMTL.cpp @@ -65,7 +65,7 @@ bool LibLinearMTL::train_machine(const std::shared_ptr& features, c " of entries ({}) in linear term ", num_labels, m_linear_term.vlen); labs->ensure_valid(); - int32_t num_train_labels=m_labels->get_num_labels(); + int32_t num_train_labels=labs->get_num_labels(); int32_t num_feat=features->get_dim_feature_space(); int32_t num_vec=features->get_num_vectors(); @@ -448,7 +448,7 @@ return obj } // loss - auto bl = binary_labels(m_labels); + auto bl = binary_labels(labs); for(int32_t i=0; i(mockData->get_labels_test()); auto composite = std::make_shared(); - auto pred = composite->over(std::make_shared()) - ->over(std::make_shared()) - ->then(std::make_shared()) - ->train(train_feats, train_labels) - ->apply_multiclass(test_feats); + composite->over(std::make_shared()) + ->over(std::make_shared()) + ->then(std::make_shared()) + ->train(train_feats, train_labels); + + auto pred = composite->apply_multiclass(test_feats); MulticlassAccuracy evaluate; float64_t result = evaluate.evaluate(pred, ground_truth); @@ -77,13 +78,15 @@ TEST(combinate_composite_and_pipeline, train) auto ground_truth = std::static_pointer_cast(mockData->get_labels_test()); auto pipeline = std::make_shared(); - auto pred = pipeline ->over(std::make_shared()) - ->composite() - ->over(std::make_shared()) - ->over(std::make_shared()) - ->then(std::make_shared()) - ->train(train_feats, train_labels) - ->apply_multiclass(test_feats); + auto machine = pipeline->over(std::make_shared()) + ->composite() + ->over(std::make_shared()) + ->over(std::make_shared()) + ->then(std::make_shared()); + + machine->train(train_feats, train_labels); + + auto pred = machine->apply_multiclass(test_feats); MulticlassAccuracy evaluate; float64_t result = evaluate.evaluate(pred, ground_truth); diff --git a/tests/unit/machine/Pipeline_unittest.cc b/tests/unit/machine/Pipeline_unittest.cc index 23002ceda46..c8db5c75328 100644 --- a/tests/unit/machine/Pipeline_unittest.cc +++ b/tests/unit/machine/Pipeline_unittest.cc @@ -50,7 +50,8 @@ TEST_F(PipelineTest, fit_predict) ->then(machine); // no labels given - EXPECT_THROW(pipeline->train(features), ShogunException); + //EXPECT_THROW(pipeline->train(features), ShogunException); + //pipeline->train(features, labels); InSequence s; @@ -60,10 +61,9 @@ TEST_F(PipelineTest, fit_predict) EXPECT_CALL(*transformer2, fit(_)).Times(0); EXPECT_CALL(*transformer2, fit(_, _)); EXPECT_CALL(*transformer2, transform(_, _)); - EXPECT_CALL(*machine, train_machine(_)); + EXPECT_CALL(*machine, train_machine(_,_)); - pipeline->set_labels(labels); - pipeline->train(features); + pipeline->train(features, labels); Mock::VerifyAndClearExpectations(transformer1.get()); Mock::VerifyAndClearExpectations(transformer2.get()); diff --git a/tests/unit/machine/StochasticGBMachine_unittest.cc b/tests/unit/machine/StochasticGBMachine_unittest.cc index c241cf34d7f..563682c9633 100644 --- a/tests/unit/machine/StochasticGBMachine_unittest.cc +++ b/tests/unit/machine/StochasticGBMachine_unittest.cc @@ -129,8 +129,7 @@ TEST_F(StochasticGBMachineTest, sinusoid_curve_fitting) auto sq=std::make_shared(); auto sgbm = std::make_shared(tree, sq, 100, 0.1, 1.0); sgbm->put("seed", seed); - sgbm->set_labels(train_labels); - sgbm->train(train_feats); + sgbm->train(train_feats, train_labels); auto ret_labels = sgbm->apply_regression(test_feats); SGVector ret=ret_labels->get_labels(); @@ -160,8 +159,7 @@ TEST_F(StochasticGBMachineTest, sinusoid_curve_fitting_subset_fraction) auto sgbm = std::make_shared(tree, sq, 100, 0.1, fraction); sgbm->put("seed", seed); - sgbm->set_labels(train_labels); - sgbm->train(train_feats); + sgbm->train(train_feats, train_labels); auto ret_labels = sgbm->apply_regression(test_feats); SGVector ret = ret_labels->get_labels(); diff --git a/tests/unit/machine/glm_unittest.cc b/tests/unit/machine/glm_unittest.cc index 7be84b7cab1..317bd1b165c 100644 --- a/tests/unit/machine/glm_unittest.cc +++ b/tests/unit/machine/glm_unittest.cc @@ -67,9 +67,7 @@ TEST(GLM, GLM_basic_test) glm->set_bias(0.44101309); glm->set_w(SGVector({0.1000393, 0.2446845, 0.5602233})); - glm->set_labels(labels_train); - - glm->train(features_train); + glm->train(features_train, labels_train); auto labels_predict = glm->apply_regression(features_test); diff --git a/tests/unit/multiclass/tree/C45ClassifierTree_unittest.cc b/tests/unit/multiclass/tree/C45ClassifierTree_unittest.cc index 9f4bb4eba99..08aecff27d7 100644 --- a/tests/unit/multiclass/tree/C45ClassifierTree_unittest.cc +++ b/tests/unit/multiclass/tree/C45ClassifierTree_unittest.cc @@ -160,9 +160,8 @@ TEST(C45ClassifierTree, classify_equivalence_check_to_id3) auto labels=std::make_shared(lab); auto c45=std::make_shared(); - c45->set_labels(labels); c45->set_feature_types(ft); - c45->train(feats); + c45->train(feats, labels); SGMatrix test(4,5); test(0,0)=overcast; @@ -310,9 +309,8 @@ TEST(C45ClassifierTree, classify_continuous_plus_categorical_data) auto labels=std::make_shared(lab); auto c45=std::make_shared(); - c45->set_labels(labels); c45->set_feature_types(ft); - c45->train(feats); + c45->train(feats, labels); SGMatrix test(4,5); test(0,0)=overcast; @@ -386,9 +384,8 @@ TEST(C45ClassifierTree, missing_attribute) auto labels=std::make_shared(lab); auto c45=std::make_shared(); - c45->set_labels(labels); c45->set_feature_types(ft); - c45->train(feats); + c45->train(feats, labels); SGMatrix test(1,2); test(0,0)=32; @@ -500,9 +497,8 @@ TEST(C45ClassifierTree, tree_prune_categorical_attributes) auto c45tree=std::make_shared(); - c45tree->set_labels(train_lab); c45tree->set_feature_types(feature_types); - c45tree->train(train_features); + c45tree->train(train_features, train_lab); c45tree->prune_tree(train_features,validation_lab); auto result=c45tree->apply(train_features)->as(); @@ -589,9 +585,8 @@ TEST(C45ClassifierTree, tree_prune_continuous_attributes) auto c45tree=std::make_shared(); - c45tree->set_labels(train_lab); c45tree->set_feature_types(feature_types); - c45tree->train(train_features); + c45tree->train(train_features, train_lab); c45tree->prune_tree(validation_features,validation_lab); auto result=c45tree->apply(train_features)->as(); diff --git a/tests/unit/multiclass/tree/CARTree_unittest.cc b/tests/unit/multiclass/tree/CARTree_unittest.cc index 390e33995d9..d766fb279e7 100644 --- a/tests/unit/multiclass/tree/CARTree_unittest.cc +++ b/tests/unit/multiclass/tree/CARTree_unittest.cc @@ -338,7 +338,6 @@ TEST(CARTree, classify_non_nominal) auto labels=std::make_shared(lab); auto c=std::make_shared(); - c->set_labels(labels); c->set_feature_types(ft); c->train(feats, labels); @@ -441,7 +440,6 @@ TEST(CARTree, handle_missing_nominal) auto labels=std::make_shared(lab); auto c=std::make_shared(); - c->set_labels(labels); c->set_feature_types(ft); c->train(feats, labels); diff --git a/tests/unit/multiclass/tree/CHAIDTree_unittest.cc b/tests/unit/multiclass/tree/CHAIDTree_unittest.cc index 7f6f39c83f7..4a9d4679649 100644 --- a/tests/unit/multiclass/tree/CHAIDTree_unittest.cc +++ b/tests/unit/multiclass/tree/CHAIDTree_unittest.cc @@ -157,11 +157,10 @@ TEST(CHAIDTree, test_tree_structure) auto labels=std::make_shared(lab); auto c=std::make_shared(0); - c->set_labels(labels); c->set_feature_types(ft); c->set_alpha_merge(Math::MIN_REAL_NUMBER); c->set_alpha_split(Math::MAX_REAL_NUMBER); - c->train(feats); + c->train(feats, labels); auto node=c->get_root(); EXPECT_EQ(2,node->data.attribute_id); @@ -190,7 +189,7 @@ TEST(CHAIDTree, test_tree_structure) ft[2]=1; ft[3]=1; c->set_feature_types(ft); - c->train(feats); + c->train(feats, labels); @@ -251,11 +250,10 @@ TEST(CHAIDTree, test_classify_multiclass) auto labels=std::make_shared(lab); auto c=std::make_shared(0); - c->set_labels(labels); c->set_feature_types(ft); c->set_alpha_merge(Math::MIN_REAL_NUMBER); c->set_alpha_split(Math::MAX_REAL_NUMBER); - c->train(feats); + c->train(feats, labels); SGMatrix test(4,5); test(0,0)=overcast; @@ -298,7 +296,7 @@ TEST(CHAIDTree, test_classify_multiclass) ft[2]=1; ft[3]=1; c->set_feature_types(ft); - c->train(feats); + c->train(feats, labels); result=c->apply_multiclass(test_feats); diff --git a/tests/unit/multiclass/tree/ID3ClassifierTree_unittest.cc b/tests/unit/multiclass/tree/ID3ClassifierTree_unittest.cc index faa45c52a75..68fd3199068 100644 --- a/tests/unit/multiclass/tree/ID3ClassifierTree_unittest.cc +++ b/tests/unit/multiclass/tree/ID3ClassifierTree_unittest.cc @@ -154,8 +154,7 @@ TEST(ID3ClassifierTree, classify_simple) auto labels=std::make_shared(lab); auto id3=std::make_shared(); - id3->set_labels(labels); - id3->train(feats); + id3->train(feats, labels); SGMatrix test(4,5); test(0,0)=overcast; @@ -282,8 +281,7 @@ TEST(ID3ClassifierTree, tree_prune) auto id3tree=std::make_shared(); - id3tree->set_labels(train_lab); - id3tree->train(train_features); + id3tree->train(train_features, train_lab); id3tree->prune_tree(train_features,validation_lab); auto result=id3tree->apply(train_features)->as(); diff --git a/tests/unit/neuralnets/NeuralNetwork_unittest.cc b/tests/unit/neuralnets/NeuralNetwork_unittest.cc index 1048d3c9628..d292a602f06 100644 --- a/tests/unit/neuralnets/NeuralNetwork_unittest.cc +++ b/tests/unit/neuralnets/NeuralNetwork_unittest.cc @@ -284,8 +284,7 @@ TEST(NeuralNetwork, binary_classification) network->set_epsilon(1e-8); - network->set_labels(labels); - network->train(features); + network->train(features, labels); auto predictions = network->apply_binary(features); @@ -339,8 +338,7 @@ TEST(NeuralNetwork, multiclass_classification) network->set_epsilon(1e-8); - network->set_labels(labels); - network->train(features); + network->train(features, labels); auto predictions = network->apply_multiclass(features); @@ -386,8 +384,7 @@ TEST(NeuralNetwork, regression) network->set_epsilon(1e-6); - network->set_labels(labels); - network->train(features); + network->train(features, labels); auto predictions = network->apply_regression(features); @@ -439,8 +436,7 @@ TEST(NeuralNetwork, gradient_descent) network->set_epsilon(0.0); network->set_max_num_epochs(1000); - network->set_labels(labels); - network->train(features); + network->train(features, labels); auto predictions = network->apply_binary(features); diff --git a/tests/unit/structure/DualLibQPBMSOSVM_unittest.cc b/tests/unit/structure/DualLibQPBMSOSVM_unittest.cc index 1f7708da0f9..7b543d6c4b6 100644 --- a/tests/unit/structure/DualLibQPBMSOSVM_unittest.cc +++ b/tests/unit/structure/DualLibQPBMSOSVM_unittest.cc @@ -102,7 +102,7 @@ TEST_P(DualLibQPBMSOSVMTestLoopSolvers,train_small_problem_and_predict) // sosvm->set_verbose(true); sosvm->set_BufSize(8); - sosvm->train(); + sosvm->train(features, labels); BmrmStatistics res = sosvm->get_result(); //SG_PRINT("result = { Fp={}, Fd={}, nIter={}, nCP={}, nzA={}, exitflag={} }\n", diff --git a/tests/unit/structure/SOSVM_unittest.cc b/tests/unit/structure/SOSVM_unittest.cc index 21bb06de02a..9cd13ed65a6 100644 --- a/tests/unit/structure/SOSVM_unittest.cc +++ b/tests/unit/structure/SOSVM_unittest.cc @@ -78,7 +78,7 @@ TEST(SOSVM, sgd_check_w_helper) auto sgd = std::make_shared(model, labels, false, false); sgd->set_num_iter(1); sgd->set_lambda(1.0); - sgd->train(); + sgd->train(instances, labels); w = sgd->get_w(); for (int32_t i = 0; i < w.vlen; i++) @@ -161,7 +161,7 @@ TEST(SOSVM, fw_check_w_helper) fw->set_num_iter(1); fw->set_lambda(1.0); fw->set_gap_threshold(0.0); - fw->train(); + fw->train(instances, labels); w = fw->get_w(); for (int32_t i = 0; i < w.vlen; i++)