Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto dispatch create methods in python #5118

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 29 additions & 29 deletions doc/ipython-notebooks/classification/Classification.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@
"c = 0.5\n",
"epsilon = 1e-3\n",
"\n",
"svm_linear = sg.create_machine(\"LibLinear\", C1=c, C2=c, \n",
"svm_linear = sg.create(\"LibLinear\", C1=c, C2=c, \n",
" labels=shogun_labels_linear, \n",
" epsilon=epsilon,\n",
" liblinear_solver_type=\"L2R_L2LOSS_SVC\")\n",
Expand All @@ -224,7 +224,7 @@
"plt.title(\"Linear SVM - Linear Features\")\n",
"plot_model(plt,svm_linear,feats_linear,labels_linear)\n",
"\n",
"svm_non_linear = sg.create_machine(\"LibLinear\", C1=c, C2=c, \n",
"svm_non_linear = sg.create(\"LibLinear\", C1=c, C2=c, \n",
" labels=shogun_labels_non_linear,\n",
" epsilon=epsilon,\n",
" liblinear_solver_type=\"L2R_L2LOSS_SVC\")\n",
Expand Down Expand Up @@ -266,14 +266,14 @@
"gaussian_c = 0.7\n",
"\n",
"gaussian_kernel_linear = sg.create_kernel(\"GaussianKernel\", width=20)\n",
"gaussian_svm_linear = sg.create_machine('LibSVM', C1=gaussian_c, C2=gaussian_c, \n",
"gaussian_svm_linear = sg.create('LibSVM', C1=gaussian_c, C2=gaussian_c, \n",
" kernel=gaussian_kernel_linear, labels=shogun_labels_linear)\n",
"gaussian_svm_linear.train(shogun_feats_linear)\n",
"classifiers_linear.append(gaussian_svm_linear)\n",
"fadings.append(True)\n",
"\n",
"gaussian_kernel_non_linear = sg.create_kernel(\"GaussianKernel\", width=10)\n",
"gaussian_svm_non_linear=sg.create_machine('LibSVM', C1=gaussian_c, C2=gaussian_c, \n",
"gaussian_svm_non_linear=sg.create('LibSVM', C1=gaussian_c, C2=gaussian_c, \n",
" kernel=gaussian_kernel_non_linear, labels=shogun_labels_non_linear)\n",
"gaussian_svm_non_linear.train(shogun_feats_non_linear)\n",
"classifiers_non_linear.append(gaussian_svm_non_linear)\n",
Expand Down Expand Up @@ -309,7 +309,7 @@
"\n",
"sigmoid_kernel_linear = sg.create_kernel(\"SigmoidKernel\", cache_size=200, gamma=1, coef0=0.5)\n",
"sigmoid_kernel_linear.init(shogun_feats_linear, shogun_feats_linear)\n",
"sigmoid_svm_linear = sg.create_machine('LibSVM', C1=sigmoid_c, C2=sigmoid_c, \n",
"sigmoid_svm_linear = sg.create('LibSVM', C1=sigmoid_c, C2=sigmoid_c, \n",
" kernel=sigmoid_kernel_linear, labels=shogun_labels_linear)\n",
"sigmoid_svm_linear.train()\n",
"classifiers_linear.append(sigmoid_svm_linear)\n",
Expand All @@ -323,7 +323,7 @@
"\n",
"sigmoid_kernel_non_linear = sg.create_kernel(\"SigmoidKernel\", cache_size=400, gamma=2.5, coef0=2)\n",
"sigmoid_kernel_non_linear.init(shogun_feats_non_linear, shogun_feats_non_linear)\n",
"sigmoid_svm_non_linear = sg.create_machine('LibSVM', C1=sigmoid_c, C2=sigmoid_c, \n",
"sigmoid_svm_non_linear = sg.create('LibSVM', C1=sigmoid_c, C2=sigmoid_c, \n",
" kernel=sigmoid_kernel_non_linear, labels=shogun_labels_non_linear)\n",
"sigmoid_svm_non_linear.train()\n",
"classifiers_non_linear.append(sigmoid_svm_non_linear)\n",
Expand Down Expand Up @@ -353,7 +353,7 @@
"\n",
"poly_kernel_linear = sg.create_kernel('PolyKernel', degree=degree, c=1.0)\n",
"poly_kernel_linear.init(shogun_feats_linear, shogun_feats_linear)\n",
"poly_svm_linear = sg.create_machine('LibSVM', C1=poly_c, C2=poly_c, \n",
"poly_svm_linear = sg.create('LibSVM', C1=poly_c, C2=poly_c, \n",
" kernel=poly_kernel_linear, labels=shogun_labels_linear)\n",
"poly_svm_linear.train()\n",
"classifiers_linear.append(poly_svm_linear)\n",
Expand All @@ -367,7 +367,7 @@
"\n",
"poly_kernel_non_linear = sg.create_kernel('PolyKernel', degree=degree, c=1.0)\n",
"poly_kernel_non_linear.init(shogun_feats_non_linear, shogun_feats_non_linear)\n",
"poly_svm_non_linear = sg.create_machine('LibSVM', C1=poly_c, C2=poly_c, \n",
"poly_svm_non_linear = sg.create('LibSVM', C1=poly_c, C2=poly_c, \n",
" kernel=poly_kernel_non_linear, labels=shogun_labels_non_linear)\n",
"poly_svm_non_linear.train()\n",
"classifiers_non_linear.append(poly_svm_non_linear)\n",
Expand Down Expand Up @@ -404,7 +404,7 @@
"shogun_multiclass_labels_linear = sg.MulticlassLabels(multiclass_labels_linear)\n",
"shogun_multiclass_labels_non_linear = sg.MulticlassLabels(multiclass_labels_non_linear)\n",
"\n",
"naive_bayes_linear = sg.create_machine(\"GaussianNaiveBayes\")\n",
"naive_bayes_linear = sg.create(\"GaussianNaiveBayes\")\n",
"naive_bayes_linear.put('features', shogun_feats_linear)\n",
"naive_bayes_linear.put('labels', shogun_multiclass_labels_linear)\n",
"naive_bayes_linear.train()\n",
Expand All @@ -417,7 +417,7 @@
"plt.title(\"Naive Bayes - Linear Features\")\n",
"plot_model(plt,naive_bayes_linear,feats_linear,labels_linear,fading=False)\n",
"\n",
"naive_bayes_non_linear = sg.create_machine(\"GaussianNaiveBayes\")\n",
"naive_bayes_non_linear = sg.create(\"GaussianNaiveBayes\")\n",
"naive_bayes_non_linear.put('features', shogun_feats_non_linear)\n",
"naive_bayes_non_linear.put('labels', shogun_multiclass_labels_non_linear)\n",
"naive_bayes_non_linear.train()\n",
Expand All @@ -443,9 +443,9 @@
"source": [
"number_of_neighbors = 10\n",
"\n",
"distances_linear = sg.create_distance('EuclideanDistance')\n",
"distances_linear = sg.create('EuclideanDistance')\n",
"distances_linear.init(shogun_feats_linear, shogun_feats_linear)\n",
"knn_linear = sg.create_machine(\"KNN\", k=number_of_neighbors, distance=distances_linear, \n",
"knn_linear = sg.create(\"KNN\", k=number_of_neighbors, distance=distances_linear, \n",
" labels=shogun_labels_linear)\n",
"knn_linear.train()\n",
"classifiers_linear.append(knn_linear)\n",
Expand All @@ -457,9 +457,9 @@
"plt.title(\"Nearest Neighbors - Linear Features\")\n",
"plot_model(plt,knn_linear,feats_linear,labels_linear,fading=False)\n",
"\n",
"distances_non_linear = sg.create_distance('EuclideanDistance')\n",
"distances_non_linear = sg.create('EuclideanDistance')\n",
"distances_non_linear.init(shogun_feats_non_linear, shogun_feats_non_linear)\n",
"knn_non_linear = sg.create_machine(\"KNN\", k=number_of_neighbors, distance=distances_non_linear, \n",
"knn_non_linear = sg.create(\"KNN\", k=number_of_neighbors, distance=distances_non_linear, \n",
" labels=shogun_labels_non_linear)\n",
"knn_non_linear.train()\n",
"classifiers_non_linear.append(knn_non_linear)\n",
Expand All @@ -484,7 +484,7 @@
"source": [
"gamma = 0.1\n",
"\n",
"lda_linear = sg.create_machine('LDA', gamma=gamma, labels=shogun_labels_linear)\n",
"lda_linear = sg.create('LDA', gamma=gamma, labels=shogun_labels_linear)\n",
"lda_linear.train(shogun_feats_linear)\n",
"classifiers_linear.append(lda_linear)\n",
"classifiers_names.append(\"LDA\")\n",
Expand All @@ -495,7 +495,7 @@
"plt.title(\"LDA - Linear Features\")\n",
"plot_model(plt,lda_linear,feats_linear,labels_linear)\n",
"\n",
"lda_non_linear = sg.create_machine('LDA', gamma=gamma, labels=shogun_labels_non_linear)\n",
"lda_non_linear = sg.create('LDA', gamma=gamma, labels=shogun_labels_non_linear)\n",
"lda_non_linear.train(shogun_feats_non_linear)\n",
"classifiers_non_linear.append(lda_non_linear)\n",
"\n",
Expand All @@ -517,7 +517,7 @@
"metadata": {},
"outputs": [],
"source": [
"qda_linear = sg.create_machine(\"QDA\", labels=shogun_multiclass_labels_linear)\n",
"qda_linear = sg.create(\"QDA\", labels=shogun_multiclass_labels_linear)\n",
"qda_linear.train(shogun_feats_linear)\n",
"classifiers_linear.append(qda_linear)\n",
"classifiers_names.append(\"QDA\")\n",
Expand All @@ -528,7 +528,7 @@
"plt.title(\"QDA - Linear Features\")\n",
"plot_model(plt,qda_linear,feats_linear,labels_linear,fading=False)\n",
"\n",
"qda_non_linear = sg.create_machine(\"QDA\", labels=shogun_multiclass_labels_non_linear)\n",
"qda_non_linear = sg.create(\"QDA\", labels=shogun_multiclass_labels_non_linear)\n",
"qda_non_linear.train(shogun_feats_non_linear)\n",
"classifiers_non_linear.append(qda_non_linear)\n",
"\n",
Expand Down Expand Up @@ -567,17 +567,17 @@
"# create Gaussian kernel with width = 5.0\n",
"kernel = sg.create_kernel(\"GaussianKernel\", width=5.0)\n",
"# create zero mean function\n",
"zero_mean = sg.create_gp_mean(\"ZeroMean\")\n",
"zero_mean = sg.create(\"ZeroMean\")\n",
"# create logit likelihood model\n",
"likelihood = sg.create_gp_likelihood(\"LogitLikelihood\")\n",
"likelihood = sg.create(\"LogitLikelihood\")\n",
"# specify EP approximation inference method\n",
"inference_model_linear = sg.create_gp_inference(\"EPInferenceMethod\",kernel=kernel, \n",
"inference_model_linear = sg.create(\"EPInferenceMethod\",kernel=kernel, \n",
" features=shogun_feats_linear, \n",
" mean_function=zero_mean, \n",
" labels=shogun_labels_linear, \n",
" likelihood_model=likelihood)\n",
"# create and train GP classifier, which uses Laplace approximation\n",
"gaussian_logit_linear = sg.create_gaussian_process(\"GaussianProcessClassification\", inference_method=inference_model_linear)\n",
"gaussian_logit_linear = sg.create(\"GaussianProcessClassification\", inference_method=inference_model_linear)\n",
"gaussian_logit_linear.train()\n",
"classifiers_linear.append(gaussian_logit_linear)\n",
"classifiers_names.append(\"Gaussian Process Logit\")\n",
Expand All @@ -588,12 +588,12 @@
"plt.title(\"Gaussian Process - Logit - Linear Features\")\n",
"plot_model(plt,gaussian_logit_linear,feats_linear,labels_linear)\n",
"\n",
"inference_model_non_linear = sg.create_gp_inference(\"EPInferenceMethod\", kernel=kernel, \n",
"inference_model_non_linear = sg.create(\"EPInferenceMethod\", kernel=kernel, \n",
" features=shogun_feats_non_linear, \n",
" mean_function=zero_mean, \n",
" labels=shogun_labels_non_linear, \n",
" likelihood_model=likelihood)\n",
"gaussian_logit_non_linear = sg.create_gaussian_process(\"GaussianProcessClassification\", \n",
"gaussian_logit_non_linear = sg.create(\"GaussianProcessClassification\", \n",
" inference_method=inference_model_non_linear)\n",
"gaussian_logit_non_linear.train()\n",
"classifiers_non_linear.append(gaussian_logit_non_linear)\n",
Expand Down Expand Up @@ -623,14 +623,14 @@
"metadata": {},
"outputs": [],
"source": [
"likelihood = sg.create_gp_likelihood(\"ProbitLikelihood\")\n",
"likelihood = sg.create(\"ProbitLikelihood\")\n",
"\n",
"inference_model_linear = sg.create_gp_inference(\"EPInferenceMethod\", kernel=kernel, \n",
"inference_model_linear = sg.create(\"EPInferenceMethod\", kernel=kernel, \n",
" features=shogun_feats_linear, \n",
" mean_function=zero_mean, \n",
" labels=shogun_labels_linear, \n",
" likelihood_model=likelihood)\n",
"gaussian_probit_linear = sg.create_gaussian_process(\"GaussianProcessClassification\", \n",
"gaussian_probit_linear = sg.create(\"GaussianProcessClassification\", \n",
" inference_method=inference_model_linear)\n",
"gaussian_probit_linear.train()\n",
"classifiers_linear.append(gaussian_probit_linear)\n",
Expand All @@ -642,12 +642,12 @@
"plt.title(\"Gaussian Process - Probit - Linear Features\")\n",
"plot_model(plt,gaussian_probit_linear,feats_linear,labels_linear)\n",
"\n",
"inference_model_non_linear = sg.create_gp_inference(\"EPInferenceMethod\", kernel=kernel, \n",
"inference_model_non_linear = sg.create(\"EPInferenceMethod\", kernel=kernel, \n",
" features=shogun_feats_non_linear, \n",
" mean_function=zero_mean, \n",
" labels=shogun_labels_non_linear, \n",
" likelihood_model=likelihood)\n",
"gaussian_probit_non_linear = sg.create_gaussian_process(\"GaussianProcessClassification\", \n",
"gaussian_probit_non_linear = sg.create(\"GaussianProcessClassification\", \n",
" inference_method=inference_model_non_linear)\n",
"gaussian_probit_non_linear.train()\n",
"classifiers_non_linear.append(gaussian_probit_non_linear)\n",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@
"source": [
"C = 0.1\n",
"epsilon = 0.01\n",
"svm = sg.create_machine(\"SVMOcas\", C1=C, C2=C, labels=labels, epsilon=epsilon)"
"svm = sg.create(\"SVMOcas\", C1=C, C2=C, labels=labels, epsilon=epsilon)"
]
},
{
Expand Down
26 changes: 13 additions & 13 deletions doc/ipython-notebooks/classification/MKL.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@
"\n",
"covs=np.array([[1.0,0.0],[0.0,1.0]])\n",
"\n",
"# gmm=sg.create_distribution(\"GMM\")\n",
"# gmm=sg.create(\"GMM\")\n",
"# gmm.set_pseudo_count(num_components)\n",
"gmm=sg.GMM(num_components)\n",
"[gmm.set_nth_mean(means[i], i) for i in range(num_components)]\n",
Expand Down Expand Up @@ -253,7 +253,7 @@
"kernel.add(\"kernel_array\", kernel1)\n",
"kernel.init(feats_train, feats_train)\n",
"\n",
"mkl = sg.create_machine(\"MKLClassification\", mkl_norm=1, C1=1, C2=1, kernel=kernel, labels=labels)\n",
"mkl = sg.create(\"MKLClassification\", mkl_norm=1, C1=1, C2=1, kernel=kernel, labels=labels)\n",
"\n",
"#train to get weights\n",
"mkl.train() \n",
Expand Down Expand Up @@ -387,22 +387,22 @@
"mkl.put(\"kernel\", kernelt)\n",
"out = mkl.apply()\n",
"\n",
"evaluator = sg.create_evaluation(\"ErrorRateMeasure\")\n",
"evaluator = sg.create(\"ErrorRateMeasure\")\n",
"print(\"Test error is %2.2f%% :MKL\" % (100*evaluator.evaluate(out,sg.BinaryLabels(testlab))))\n",
"\n",
"\n",
"comb_ker0t.init(feats_train, sg.create_features(testdata)) \n",
"mkl.put(\"kernel\", comb_ker0t)\n",
"out = mkl.apply()\n",
"\n",
"evaluator = sg.create_evaluation(\"ErrorRateMeasure\")\n",
"evaluator = sg.create(\"ErrorRateMeasure\")\n",
"print(\"Test error is %2.2f%% :Subkernel1\"% (100*evaluator.evaluate(out,sg.BinaryLabels(testlab))))\n",
"\n",
"comb_ker1t.init(feats_train, sg.create_features(testdata))\n",
"mkl.put(\"kernel\", comb_ker1t)\n",
"out = mkl.apply()\n",
"\n",
"evaluator = sg.create_evaluation(\"ErrorRateMeasure\")\n",
"evaluator = sg.create(\"ErrorRateMeasure\")\n",
"print(\"Test error is %2.2f%% :subkernel2\" % (100*evaluator.evaluate(out,sg.BinaryLabels(testlab))))\n"
]
},
Expand Down Expand Up @@ -490,7 +490,7 @@
" kernel.add(\"kernel_array\", kernel3)\n",
" \n",
" kernel.init(feats_tr, feats_tr)\n",
" mkl = sg.create_machine(\"MKLClassification\", mkl_norm=1, C1=1, C2=2, kernel=kernel, labels=lab)\n",
" mkl = sg.create(\"MKLClassification\", mkl_norm=1, C1=1, C2=2, kernel=kernel, labels=lab)\n",
" \n",
" mkl.train()\n",
" \n",
Expand Down Expand Up @@ -703,7 +703,7 @@
"\n",
"kernel.init(feats_train, feats_train)\n",
"\n",
"mkl = sg.create_machine(\"MKLMulticlass\", C=1.2, kernel=kernel, \n",
"mkl = sg.create(\"MKLMulticlass\", C=1.2, kernel=kernel, \n",
" labels=labels, mkl_eps=0.001, mkl_norm=1)\n",
"\n",
"# set epsilon of SVM\n",
Expand All @@ -715,7 +715,7 @@
"kernel.init(feats_train, feats_test) \n",
"\n",
"out = mkl.apply()\n",
"evaluator = sg.create_evaluation(\"MulticlassAccuracy\")\n",
"evaluator = sg.create(\"MulticlassAccuracy\")\n",
"accuracy = evaluator.evaluate(out, labels_rem)\n",
"print(\"Accuracy = %2.2f%%\" % (100*accuracy))\n",
"\n",
Expand Down Expand Up @@ -748,10 +748,10 @@
"\n",
"pk = sg.create_kernel('PolyKernel', degree=10, c=2) \n",
"\n",
"svm = sg.create_machine(\"GMNPSVM\", C=C, kernel=pk, labels=labels)\n",
"svm = sg.create(\"GMNPSVM\", C=C, kernel=pk, labels=labels)\n",
"_=svm.train(feats)\n",
"out=svm.apply(feats_rem)\n",
"evaluator = sg.create_evaluation(\"MulticlassAccuracy\")\n",
"evaluator = sg.create(\"MulticlassAccuracy\")\n",
"accuracy = evaluator.evaluate(out, labels_rem)\n",
"\n",
"print(\"Accuracy = %2.2f%%\" % (100*accuracy))\n",
Expand All @@ -776,10 +776,10 @@
"\n",
"gk=sg.create_kernel(\"GaussianKernel\", width=width)\n",
"\n",
"svm=sg.create_machine(\"GMNPSVM\", C=C, kernel=gk, labels=labels)\n",
"svm=sg.create(\"GMNPSVM\", C=C, kernel=gk, labels=labels)\n",
"_=svm.train(feats)\n",
"out=svm.apply(feats_rem)\n",
"evaluator = sg.create_evaluation(\"MulticlassAccuracy\")\n",
"evaluator = sg.create(\"MulticlassAccuracy\")\n",
"accuracy = evaluator.evaluate(out, labels_rem)\n",
"\n",
"print(\"Accuracy = %2.2f%%\" % (100*accuracy))\n",
Expand Down Expand Up @@ -869,7 +869,7 @@
"\n",
"kernel.init(feats_train, feats_train)\n",
"\n",
"mkl = sg.create_machine(\"MKLOneClass\", kernel=kernel, labels=labels, interleaved_optimization=False,\n",
"mkl = sg.create(\"MKLOneClass\", kernel=kernel, labels=labels, interleaved_optimization=False,\n",
" mkl_norm=1)\n",
"\n",
"mkl.put(\"epsilon\", 1e-2)\n",
Expand Down
Loading