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

Make Machine class stateless #5111

Open
wants to merge 9 commits into
base: develop
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion data
Submodule data updated 0 files
42 changes: 18 additions & 24 deletions doc/ipython-notebooks/classification/Classification.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -212,10 +212,9 @@
"epsilon = 1e-3\n",
"\n",
"svm_linear = sg.create_machine(\"LibLinear\", C1=c, C2=c, \n",
" labels=shogun_labels_linear, \n",
" epsilon=epsilon,\n",
" liblinear_solver_type=\"L2R_L2LOSS_SVC\")\n",
"svm_linear.train(shogun_feats_linear)\n",
"svm_linear.train(shogun_feats_linear, shogun_labels_linear)\n",
"classifiers_linear.append(svm_linear)\n",
"classifiers_names.append(\"SVM Linear\")\n",
"fadings.append(True)\n",
Expand All @@ -224,11 +223,10 @@
"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",
" labels=shogun_labels_non_linear,\n",
"svm_non_linear = sg.create_machine(\"LibLinear\", C1=c, C2=c,\n",
" epsilon=epsilon,\n",
" liblinear_solver_type=\"L2R_L2LOSS_SVC\")\n",
"svm_non_linear.train(shogun_feats_non_linear)\n",
"svm_non_linear.train(shogun_feats_non_linear, shogun_labels_non_linear)\n",
"classifiers_non_linear.append(svm_non_linear)\n",
"\n",
"plt.subplot(122)\n",
Expand Down Expand Up @@ -405,9 +403,7 @@
"shogun_multiclass_labels_non_linear = sg.MulticlassLabels(multiclass_labels_non_linear)\n",
"\n",
"naive_bayes_linear = sg.create_machine(\"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",
"naive_bayes_linear.train(shogun_feats_linear, shogun_multiclass_labels_linear)\n",
"classifiers_linear.append(naive_bayes_linear)\n",
"classifiers_names.append(\"Naive Bayes\")\n",
"fadings.append(False)\n",
Expand All @@ -418,9 +414,7 @@
"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.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",
"naive_bayes_non_linear.train(shogun_feats_non_linear, shogun_multiclass_labels_non_linear)\n",
"classifiers_non_linear.append(naive_bayes_non_linear)\n",
"\n",
"plt.subplot(122)\n",
Expand All @@ -447,7 +441,7 @@
"distances_linear.init(shogun_feats_linear, shogun_feats_linear)\n",
"knn_linear = sg.create_machine(\"KNN\", k=number_of_neighbors, distance=distances_linear, \n",
" labels=shogun_labels_linear)\n",
"knn_linear.train()\n",
"knn_linear.train(shogun_feats_linear)\n",
"classifiers_linear.append(knn_linear)\n",
"classifiers_names.append(\"Nearest Neighbors\")\n",
"fadings.append(False)\n",
Expand All @@ -461,7 +455,7 @@
"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",
" labels=shogun_labels_non_linear)\n",
"knn_non_linear.train()\n",
"knn_non_linear.train(shogun_feats_non_linear)\n",
"classifiers_non_linear.append(knn_non_linear)\n",
"\n",
"plt.subplot(122)\n",
Expand All @@ -484,8 +478,8 @@
"source": [
"gamma = 0.1\n",
"\n",
"lda_linear = sg.create_machine('LDA', gamma=gamma, labels=shogun_labels_linear)\n",
"lda_linear.train(shogun_feats_linear)\n",
"lda_linear = sg.create_machine('LDA', gamma=gamma)\n",
"lda_linear.train(shogun_feats_linear, shogun_labels_linear)\n",
"classifiers_linear.append(lda_linear)\n",
"classifiers_names.append(\"LDA\")\n",
"fadings.append(True)\n",
Expand All @@ -495,8 +489,8 @@
"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.train(shogun_feats_non_linear)\n",
"lda_non_linear = sg.create_machine('LDA', gamma=gamma)\n",
"lda_non_linear.train(shogun_feats_non_linear, shogun_labels_non_linear)\n",
"classifiers_non_linear.append(lda_non_linear)\n",
"\n",
"plt.subplot(122)\n",
Expand All @@ -517,8 +511,8 @@
"metadata": {},
"outputs": [],
"source": [
"qda_linear = sg.create_machine(\"QDA\", labels=shogun_multiclass_labels_linear)\n",
"qda_linear.train(shogun_feats_linear)\n",
"qda_linear = sg.create_machine(\"QDA\")\n",
"qda_linear.train(shogun_feats_linear, shogun_multiclass_labels_linear)\n",
"classifiers_linear.append(qda_linear)\n",
"classifiers_names.append(\"QDA\")\n",
"fadings.append(False)\n",
Expand All @@ -528,8 +522,8 @@
"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.train(shogun_feats_non_linear)\n",
"qda_non_linear = sg.create_machine(\"QDA\")\n",
"qda_non_linear.train(shogun_feats_non_linear, shogun_multiclass_labels_non_linear)\n",
"classifiers_non_linear.append(qda_non_linear)\n",
"\n",
"plt.subplot(122)\n",
Expand Down Expand Up @@ -682,8 +676,8 @@
"plot_binary_data(plt,feats_non_linear, labels_non_linear)\n",
"\n",
"for i in range(0,10):\n",
" plt.subplot(2,11,13+i)\n",
" plot_model(plt,classifiers_non_linear[i],feats_non_linear,labels_non_linear,fading=fadings[i])"
" plt.subplot(2,11,13+i)\n",
" plot_model(plt,classifiers_non_linear[i],feats_non_linear,labels_non_linear,fading=fadings[i])"
]
},
{
Expand All @@ -710,7 +704,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.1"
"version": "3.6.9"
}
},
"nbformat": 4,
Expand Down
12 changes: 6 additions & 6 deletions doc/ipython-notebooks/classification/HashedDocDotFeatures.ipynb
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_machine(\"SVMOcas\", C1=C, C2=C, epsilon=epsilon)"
]
},
{
Expand All @@ -207,7 +207,7 @@
"metadata": {},
"outputs": [],
"source": [
"_=svm.train(hashed_feats)"
"_=svm.train(hashed_feats, labels)"
]
},
{
Expand All @@ -224,7 +224,7 @@
"metadata": {},
"outputs": [],
"source": [
"predicted_labels = svm.apply()\n",
"predicted_labels = svm.apply(hashed_feats)\n",
"print(predicted_labels.get(\"labels\"))"
]
},
Expand Down Expand Up @@ -286,8 +286,8 @@
"metadata": {},
"outputs": [],
"source": [
"svm.train(hashed_feats_quad)\n",
"predicted_labels = svm.apply()\n",
"svm.train(hashed_feats_quad, labels)\n",
"predicted_labels = svm.apply(hashed_feats_quad)\n",
"print(predicted_labels.get(\"labels\"))"
]
},
Expand Down Expand Up @@ -454,4 +454,4 @@
},
"nbformat": 4,
"nbformat_minor": 1
}
}
24 changes: 12 additions & 12 deletions doc/ipython-notebooks/classification/MKL.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -253,10 +253,10 @@
"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_machine(\"MKLClassification\", mkl_norm=1, C1=1, C2=1, kernel=kernel)\n",
"\n",
"#train to get weights\n",
"mkl.train() \n",
"mkl.train(feats_train, labels) \n",
"\n",
"w=kernel.get_subkernel_weights()\n",
"print(w)"
Expand Down Expand Up @@ -490,9 +490,9 @@
" 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_machine(\"MKLClassification\", mkl_norm=1, C1=1, C2=2, kernel=kernel)\n",
" \n",
" mkl.train()\n",
" mkl.train(feats_tr, lab)\n",
" \n",
" w=kernel.get_subkernel_weights()\n",
" return w, mkl\n",
Expand Down Expand Up @@ -704,17 +704,17 @@
"kernel.init(feats_train, feats_train)\n",
"\n",
"mkl = sg.create_machine(\"MKLMulticlass\", C=1.2, kernel=kernel, \n",
" labels=labels, mkl_eps=0.001, mkl_norm=1)\n",
" mkl_eps=0.001, mkl_norm=1)\n",
"\n",
"# set epsilon of SVM\n",
"mkl.get(\"machine\").put(\"epsilon\", 1e-2)\n",
"\n",
"mkl.train()\n",
"mkl.train(feats_train, labels)\n",
"\n",
"#initialize with test features\n",
"kernel.init(feats_train, feats_test) \n",
"\n",
"out = mkl.apply()\n",
"out = mkl.apply(feats_test)\n",
"evaluator = sg.create_evaluation(\"MulticlassAccuracy\")\n",
"accuracy = evaluator.evaluate(out, labels_rem)\n",
"print(\"Accuracy = %2.2f%%\" % (100*accuracy))\n",
Expand Down Expand Up @@ -748,8 +748,8 @@
"\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.train(feats)\n",
"svm = sg.create_machine(\"GMNPSVM\", C=C, kernel=pk)\n",
"_=svm.train(feats, labels)\n",
"out=svm.apply(feats_rem)\n",
"evaluator = sg.create_evaluation(\"MulticlassAccuracy\")\n",
"accuracy = evaluator.evaluate(out, labels_rem)\n",
Expand All @@ -776,8 +776,8 @@
"\n",
"gk=sg.create_kernel(\"GaussianKernel\", width=width)\n",
"\n",
"svm=sg.create_machine(\"GMNPSVM\", C=C, kernel=gk, labels=labels)\n",
"_=svm.train(feats)\n",
"svm=sg.create_machine(\"GMNPSVM\", C=C, kernel=gk)\n",
"_=svm.train(feats, labels)\n",
"out=svm.apply(feats_rem)\n",
"evaluator = sg.create_evaluation(\"MulticlassAccuracy\")\n",
"accuracy = evaluator.evaluate(out, labels_rem)\n",
Expand Down Expand Up @@ -984,7 +984,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.1"
"version": "3.6.9"
}
},
"nbformat": 4,
Expand Down
11 changes: 5 additions & 6 deletions doc/ipython-notebooks/classification/SupportVectorMachines.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,7 @@
"svm=sg.create_machine('LibLinear', C1=C, C2=C, liblinear_solver_type='L2R_L2LOSS_SVC', epsilon=epsilon)\n",
"\n",
"#train\n",
"svm.put('labels', labels)\n",
"svm.train(feats_train)\n",
"svm.train(feats_train, labels)\n",
"w=svm.get('w')\n",
"b=svm.get('bias')"
]
Expand Down Expand Up @@ -933,8 +932,8 @@
"metadata": {},
"outputs": [],
"source": [
"svm=sg.create_machine(\"GMNPSVM\", C=1, kernel=gaussian_kernel, labels=labels)\n",
"_=svm.train(feats_train)\n",
"svm=sg.create_machine(\"GMNPSVM\", C=1, kernel=gaussian_kernel)\n",
"_=svm.train(feats_train, labels)\n",
"\n",
"size=100\n",
"x1=np.linspace(-6, 6, size)\n",
Expand All @@ -948,7 +947,7 @@
" plt.subplot(1,len(kernels),i+1)\n",
" plt.title(kernels[i].get_name())\n",
" svm.put(\"kernel\", kernels[i])\n",
" svm.train(feats_train)\n",
" svm.train(feats_train, labels)\n",
" grid_out=svm.apply(grid)\n",
" z=grid_out.get(\"labels\").reshape((size, size))\n",
" plt.pcolor(x, y, z)\n",
Expand Down Expand Up @@ -1001,7 +1000,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.1"
"version": "3.6.9"
}
},
"nbformat": 4,
Expand Down
10 changes: 5 additions & 5 deletions doc/ipython-notebooks/intro/Introduction.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -338,10 +338,10 @@
"#prameters to svm\n",
"C=0.9\n",
"\n",
"svm=sg.create_machine(\"LibLinear\", C1=C, C2=C, labels=labels, \n",
"svm=sg.create_machine(\"LibLinear\", C1=C, C2=C,\n",
" liblinear_solver_type=\"L2R_L2LOSS_SVC\")\n",
"#train\n",
"svm.train(feats_train)\n",
"svm.train(feats_train, labels)\n",
"\n",
"size=100"
]
Expand Down Expand Up @@ -495,11 +495,11 @@
"label_e=trainlab[num_train:]\n",
"labels_true=sg.create_labels(label_e)\n",
"\n",
"svm=sg.create_machine(\"LibLinear\", C1=C, C2=C, labels=labels, \n",
"svm=sg.create_machine(\"LibLinear\", C1=C, C2=C,\n",
" liblinear_solver_type=\"L2R_L2LOSS_SVC\")\n",
"\n",
"#train and evaluate\n",
"svm.train(feats_train)\n",
"svm.train(feats_train, labels)\n",
"output=svm.apply(feats_evaluate)\n",
"\n",
"#use AccuracyMeasure to get accuracy\n",
Expand Down Expand Up @@ -688,7 +688,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.3"
"version": "3.6.9"
}
},
"nbformat": 4,
Expand Down
13 changes: 6 additions & 7 deletions doc/ipython-notebooks/multiclass/KNN.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -286,19 +286,18 @@
" labels.add_subset(idx_train)\n",
"\n",
" dist = sg.create_distance('EuclideanDistance')\n",
" dist.init(feats, feats)\n",
" knn = sg.create_machine(\"KNN\", k=k, distance=dist, labels=labels)\n",
" knn = sg.create_machine(\"KNN\", k=k, distance=dist)\n",
" #knn.set_store_model_features(True)\n",
" #FIXME: causes SEGFAULT\n",
" if use_cover_tree:\n",
" continue\n",
" # knn.put('knn_solver', \"KNN_COVER_TREE\")\n",
" else:\n",
" knn.put('knn_solver', \"KNN_BRUTE\")\n",
" knn.train()\n",
" knn.train(feats, labels)\n",
"\n",
" evaluator = sg.create_evaluation(\"MulticlassAccuracy\")\n",
" pred = knn.apply()\n",
" pred = knn.apply(feats)\n",
" acc_train[i, j] = evaluator.evaluate(pred, labels)\n",
"\n",
" feats.remove_subset()\n",
Expand Down Expand Up @@ -409,8 +408,8 @@
"\n",
"gk=sg.create_kernel(\"GaussianKernel\", width=width)\n",
"\n",
"svm=sg.create_machine(\"GMNPSVM\", C=C, kernel=gk, labels=labels)\n",
"_=svm.train(feats)"
"svm=sg.create_machine(\"GMNPSVM\", C=C, kernel=gk)\n",
"_=svm.train(feats, labels)"
]
},
{
Expand Down Expand Up @@ -490,7 +489,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.3"
"version": "3.6.9"
}
},
"nbformat": 4,
Expand Down
Loading