Example of MultilayerPerceptron class - drake

Is there an example of how to use the MLP class? https://drake.mit.edu/doxygen_cxx/classdrake_1_1systems_1_1_multilayer_perceptron.html
Also, is there any source for how the backpropagation algorithm is implemented? I couldn't find it searching the source code - it just brought up some C++ headers.

Related

What's the equivalent to get_sentence_vector for Gensim's FastText?

The official Python implementation offers the useful get_sentence_vector() which sums, normalizes (L2) and averages the words in the given sentence.
In other words, I'd like to embed a new sentence, given a trained model of Gensim's FastText.
Is there such method?
As explained in this answer, Gensim doesn't provide get_sentence_vector method.
You can use official fastText library or instead rewrite this method on your own (here you can find the original C++ implementation).

Machine Learning - Map same line of training data to multiple targets

Let me start by saying that I am total noob when it comes to Machine Learning, so please excuse me if this is a stupid question.
I was making a MultinomialNB model following this tutorial.
And I was wondering if there was a way to map a single line to multiple targets classes.
For example, I have the sentence "Jesus cures cancer" and want it to map to both 'sci.med' and 'soc.religion.christian'.
Is there a way to do that in SciKit? Can I just put the same sentence twice with the different targets, or will that distort the final model?
What you are wanting to do is called multilabel classification; some scikit-learn classifiers support it, here's the documentation with more details.
If you put the same sentence twice into a model that is trying to predict one outcome, it will just confuse the model as one training example said it was one class and another said it was a different class and it is only learning to predict one class.
Yes, scikit's multinomialNB can be implemented as multilabel.
from sklearn.multiclass import OneVsRestClassifier
from sklearn.naive_bayes import MultinomialNB
classifier=OneVsRestClassifier(MultinomialNB)
You can now use "classifier" to fit your training data.
You can learn more about OneVsRestClassifier here

Applying custom costfunction in TensorFlow's SKFlow model training

I'm trying to make a regression model with TensorFlow while using the sklearn implementation so it plays nicely with all the other models I've made. However I cannot seem to find a way to train the model with a custom score function (cost function or objective function).
Is this simply impossible with skflow?
Thanks loads!
Many of the examples uses learn.models.logistic_regression, which is basically a built-in high-level model that returns predictions and losses. For example, models.logistic_regression uses ops.losses_ops.softmax_classifier, which means you can look into how ops.losses_ops.softmax_classifier is implemented and implement your own loss function using perhaps TensorFlow low-level APIs.

How to use the OneVsRestClassifier in Scikit-learn to analyse the performance of predicting each individual class with multi-class classification?

In the OneVsRestClassifier documentation on the scikit-learn website it states the following:
"Since each class is represented by one and one classifier only, it is possible to gain knowledge about the class by inspecting its corresponding classifier."
But it gives no explanation of how to do this and I can't see how any of the methods in the documentation on this page could achieve that. I want to be able to print out the accuracy of the model for each individual class, so that I can see the performance it has at predicting each class.
The code I have so far is below, but I really don't know where to go from here, as it seems that there is nothing in the documentation, which explains how to do this. Any help is much appreciated.
def predict_one_vs_rest(self):
clf = OneVsRestClassifier(LinearSVC(random_state=0))
clf.fit(self.X, self.y)
result = clf.classes_
estimators = clf.estimators_
print(result)
print("")
print(estimators)
You don't need to wrap LinearSVC in OneVsRestClassifier. As the documentation clearly says, LinearSVC already supports multi-class classification.
For inspecting accuracy of the classes, you could use the confusion matrix or the classification report, for example.

Build a custom svm kernel matrix with opencv

I have to train a Support Vector Machine model and I'd like to use a custom kernel matrix, instead of the preset ones (like RBF, Poly, ecc.).
How can I do that (if is it possible) with opencv's machine learning library?
Thank you!
AFAICT, custom kernels for SVM aren't supported directly in OpenCV. It looks like LIBSVM, which is the underlying library that OpenCV uses for this, doesn't provide a particularly easy means of defining custom kernels. So, many of the wrappers that use LIBSVM don't provide this either. There seem to be a few, e.g. scikit for python: scikit example of SVM with custom kernel
You could also take a look at a completely different library, like SVMlight. It supports custom kernels directly. Also take a look at this SO question. The answers there include a handful of SVM libraries, along with brief reviews.
If you have compelling reasons to stay within OpenCV, you might be able to accomplish it by using kernel type CvSVM::LINEAR and applying your custom kernel to the data before training the SVM. I'm a little fuzzy on whether this direction would be fruitful, so I hope someone with more experience with SVM can chime in and comment. If it is possible to use a "precomputed kernel" by choosing "linear" as your kernel, then take a look at this answer for more ideas on how to proceed.
You might also consider including LIBSVM and calling it directly, without using OpenCV. See FAQ #418 for LIBSVM, which briefly touches on how to do custom kernels:
Q: I would like to use my own kernel. Any example? In svm.cpp, there are two subroutines for kernel evaluations: k_function() and kernel_function(). Which one should I modify ?
An example is "LIBSVM for string data" in LIBSVM Tools.
The reason why we have two functions is as follows. For the RBF kernel exp(-g |xi - xj|^2), if we calculate xi - xj first and then the norm square, there are 3n operations. Thus we consider exp(-g (|xi|^2 - 2dot(xi,xj) +|xj|^2)) and by calculating all |xi|^2 in the beginning, the number of operations is reduced to 2n. This is for the training. For prediction we cannot do this so a regular subroutine using that 3n operations is needed. The easiest way to have your own kernel is to put the same code in these two subroutines by replacing any kernel.
That last option sounds like a bit of a pain, though. I'd recommend scikit or SVMlight. Best of luck to you!
If you're not married to OpenCV for the SVM stuff, have a look at the shogun toolbox ... lots of SVM voodoo in there.

Resources