Hey guys,
I just wanted to quickly share how I was optimizing hyperparameters in XGBoost using bayes_opt.
It does a k-fold cross validation while optimizing for stable parameters.
Keep in mind that bayes_opt maximizes the objective function, so change all the required hardcoded values along those lines to fit your problem. It's pretty compact, so I thought I just leave it here for your convenience as a gist.
Cheers,
Thomas
Oct 15, 2016
Mar 18, 2016
XGBoost Validation and Early Stopping in R
Hey people,
While using XGBoost in Rfor some Kaggle competitions I always come to a stage where I want to do early stopping of the training based on a held-out validation set.
There are very little code snippets out there to actually do it in R, so I wanted to share my quite generic code here on the blog.
Let's say you have a training set in some csv and you want to split that into a 9:1 training:validation set. Here's the naive (not stratified way) of doing it:
Now before feeding it back into XGBoost, we need to create a xgb.DMatrix and remove the targets to not spoil the classifier. This can be done via this code:
So now we can go and setup a watchlist and actually start the training. Here's some simple sample code to get you started:
Here we setup a watchlist with the validation set in the first dimension of the list and the trainingset in the latter. The reason that you need to put the validation set first is that the early stopping only works on one metric - where we should obviously choose the validation set.
The rest is straightforward setup of the xgb tree itself. Keep in mind that when you use early stopping, you also need to supply whether or not to maximize the chosen objective function- otherwise you might find yourself stopping very fast!
Here's the full snippet as a gist:
https://gist.github.com/thomasjungblut/e60217c5b7609e4dfef3
Thanks for reading,
Thomas
While using XGBoost in Rfor some Kaggle competitions I always come to a stage where I want to do early stopping of the training based on a held-out validation set.
There are very little code snippets out there to actually do it in R, so I wanted to share my quite generic code here on the blog.
Let's say you have a training set in some csv and you want to split that into a 9:1 training:validation set. Here's the naive (not stratified way) of doing it:
train <- read.csv("train.csv")
bound <- floor(nrow(train) * 0.9)
train <- train[sample(nrow(train)), ]
df.train <- train[1:bound, ]
df.validation <- train[(bound+1):nrow(train), ]
Now before feeding it back into XGBoost, we need to create a xgb.DMatrix and remove the targets to not spoil the classifier. This can be done via this code:
train.y <- df.train$TARGET
validation.y <- df.validation$TARGET
dtrain <- xgb.DMatrix(data=df.train, label=train.y)
dvalidation <- xgb.DMatrix(data=df.validation, label=validation.y)
So now we can go and setup a watchlist and actually start the training. Here's some simple sample code to get you started:
watchlist <- list(validation=dvalidation, train=dtrain)
param <- list(
objective = "binary:logistic",
eta = 0.3,
max_depth = 8
)
clf <- xgb.train(
params = param,
data = dtrain,
nrounds = 500,
watchlist = watchlist,
maximize = FALSE,
early.stop.round = 20
)
Here we setup a watchlist with the validation set in the first dimension of the list and the trainingset in the latter. The reason that you need to put the validation set first is that the early stopping only works on one metric - where we should obviously choose the validation set.
The rest is straightforward setup of the xgb tree itself. Keep in mind that when you use early stopping, you also need to supply whether or not to maximize the chosen objective function- otherwise you might find yourself stopping very fast!
Here's the full snippet as a gist:
https://gist.github.com/thomasjungblut/e60217c5b7609e4dfef3
Thanks for reading,
Thomas
Mar 5, 2015
MinHashing for Beginners
Due to popular demand of my MinHashing class in my common library, I decided to do a quick and dirty walkthrough of how to vectorize, MinHash and lookup your data!
What is MinHashing good for?
MinHashing in the first place is used to speed up nearest neighbour lookups. Think of having this huge terabyte sized dataset of movie reviews and you want to find similar reviews (either for deduplication or for recommendation).
Now MinHashing will give you a very small and dense representation of your data by hashing values of a vectorized representation of your data. You can read more about the theory and some details behind it on Wikipedia.
Example Data
To get some base-line data, let's grab a dataset from Kaggle. This example uses the 50k movie reviews from IMDB, namely the "unlabeledTrainData.tsv". Sign-in and get the data from there- alternatively any data will do if it has the same columnar TSV style formatting, here is an example:
We need to do some massaging here, since there are some HTML parts in it, but for this example we can simply get away with lower casing and removing everything that isn't alphanumeric.
Tokenization and Vectorization
The main part here is to normalize and tokenize the data into bigrams that we can use our vectorizer to build TF-IDF / or bag-of-word-frequency vectors out of it. Using my common library and Java 8 streams this looks like this:
MinHashing
Now that we have the vectors, we can MinHash them.
In this case, we go for 20 min hashes and a rather strong murmur 128 bit hash. This is usually the default I try out for various text tasks.
Finding Similar Documents
To find similar documents, we have two ways: brute force or using an inverted index. Let's start using brute force by finding the top five similar elements to the third (index = 2) movie review in our list.
This will output a very interesting pattern:
It found two near duplicates to the source document we provided :-)
Please find the full code here:
https://gist.github.com/thomasjungblut/e4759797f5a52d78e06d
I will cover an edit later on with the inverted index version and how the performance actually improved.
Thanks for reading,
Thomas
What is MinHashing good for?
MinHashing in the first place is used to speed up nearest neighbour lookups. Think of having this huge terabyte sized dataset of movie reviews and you want to find similar reviews (either for deduplication or for recommendation).
Now MinHashing will give you a very small and dense representation of your data by hashing values of a vectorized representation of your data. You can read more about the theory and some details behind it on Wikipedia.
Example Data
To get some base-line data, let's grab a dataset from Kaggle. This example uses the 50k movie reviews from IMDB, namely the "unlabeledTrainData.tsv". Sign-in and get the data from there- alternatively any data will do if it has the same columnar TSV style formatting, here is an example:
id review "9999_0" "Watching Time Chasers, it obvious that it was made by a bunch of friends. Maybe they were sitting around one day in film school and said, \"Hey, let's pool our money together and make a really bad movie!\" Or something like that. What ever they said, they still ended up making a really bad movie--dull story, bad script, lame acting, poor cinematography, bottom of the barrel stock music, etc. All corners were cut, except the one that would have prevented this film's release. Life's like that." "45057_0" "I saw this film about 20 years ago and remember it as being particularly nasty. I believe it is based on a true incident: a young man breaks into a nurses' home and rapes, tortures and kills various women.<br /><br />It is in black and white but saves the colour for one shocking shot.<br /><br />At the end the film seems to be trying to make some political statement but it just comes across as confused and obscene.<br /><br />Avoid."
We need to do some massaging here, since there are some HTML parts in it, but for this example we can simply get away with lower casing and removing everything that isn't alphanumeric.
Tokenization and Vectorization
The main part here is to normalize and tokenize the data into bigrams that we can use our vectorizer to build TF-IDF / or bag-of-word-frequency vectors out of it. Using my common library and Java 8 streams this looks like this:
Stream<String> lines = Files.lines(
Paths.get("C:/Users/thomas.jungblut/Downloads/unlabeledTrainData.tsv"))
.skip(1); // skip the header
// normalization and bigram tokenization
List<String[]> documents = lines
.map((line) -> TokenizerUtils.normalizeString(line))
.map((normalized) -> TokenizerUtils.whiteSpaceTokenizeNGrams(normalized, 2))
.collect(Collectors.toList());
// every token that occurs more often than 50% in the corpus and less than twice will be discarded
String[] dict = VectorizerUtils.buildDictionary(documents.stream(), 0.5f, 2);
List<DoubleVector> vectors = VectorizerUtils.wordFrequencyVectorize(documents.stream().parallel(), dict).collect(Collectors.toList());
MinHashing
Now that we have the vectors, we can MinHash them.
MinHash hasher = MinHash.create(20, HashType.MURMUR128);
List<int[]> minHashes = vectors.stream().map((v) -> hasher.minHashVector(v)).collect(Collectors.toList());
In this case, we go for 20 min hashes and a rather strong murmur 128 bit hash. This is usually the default I try out for various text tasks.
Finding Similar Documents
To find similar documents, we have two ways: brute force or using an inverted index. Let's start using brute force by finding the top five similar elements to the third (index = 2) movie review in our list.
final int sourceDocIndex = 2;
// document index
LimitedPriorityQueue<Integer> queue = new LimitedPriorityQueue<>(5);
int[] first = minHashes.get(sourceDocIndex);
Stopwatch watch = Stopwatch.createStarted();
for (int i = 0; i < minHashes.size(); i++) {
if (i != sourceDocIndex) {
int[] reference = minHashes.get(i);
double similarity = hasher.measureSimilarity(first, reference);
if (similarity > 0.1) {
queue.add(i, similarity);
}
}
}
System.out.println("Done finding similar docs in "
+ watch.elapsed(TimeUnit.MILLISECONDS) + "ms!");
System.out.println("document:");
System.out.println(lines.get(sourceDocIndex));
while (!queue.isEmpty()) {
double similarity = queue.getMaximumPriority();
int index = queue.poll();
System.out.println(similarity + " -> " + lines.get(index));
}
This will output a very interesting pattern:
Done finding similar docs in 146ms!
document:
"15561_0" "Minor Spoilers<br /><br />In New York, Joan Barnard (Elvire Audrey) is informed that her husband, the archeologist Arthur Barnard (John Saxon), was mysteriously murdered in Italy while searching an Etruscan tomb. Joan decides to travel to Italy, in the company of her colleague, who offers his support. Once in Italy, she starts having visions relative to an ancient people and maggots, many maggots. After shootings and weird events, Joan realizes that her father is an international drug dealer, there are drugs hidden in the tomb and her colleague is a detective of the narcotic department. The story ends back in New York, when Joan and her colleague decide to get married with each other, in a very romantic end. Yesterday I had the displeasure of wasting my time watching this crap. The story is so absurd, mixing thriller, crime, supernatural and horror (and even a romantic end) in a non-sense way. The acting is the worst possible, highlighting the horrible performance of the beautiful Elvire Audrey. John Saxon just gives his name to the credits and works less than five minutes, when his character is killed. The special effects are limited to maggots everywhere. The direction is ridiculous. I lost a couple of hours of my life watching 'Assassinio al Cimitero Etrusco'. If you have the desire or curiosity of seeing this trash, choose another movie, go to a pizzeria, watch TV, go sleep, navigate in Internet, go to the gym, but do not waste your time like I did. My vote is two.<br /><br />Title (Brazil): 'O Mistério Etrusco' ('The Etruscan Mystery')"
0.9047619047619048 -> "15556_0" "Minor Spoilers<br /><br />In New York, Joan Barnard (Elvire Audrey) is informed that her husband, the archaeologist Arthur Barnard (John Saxon), was mysteriously murdered in Italy while searching an Etruscan tomb. Joan decides to travel to Italy, in the company of her colleague, who offers his support. Once in Italy, she starts having visions relative to an ancient people and maggots, many maggots. After shootings and weird events, Joan realizes that her father is an international drug dealer, there are drugs hidden in the tomb and her colleague is a detective of the narcotic department. The story ends back in New York, when Joan and her colleague decide to get married with each other, in a very romantic end. Yesterday I had the displeasure of wasting my time watching this crap. The story is so absurd, mixing thriller, crime, supernatural and horror (and even a romantic end) in a non-sense way. The acting is the worst possible, highlighting the horrible and screaming performance of the beautiful Elvire Audrey. John Saxon just gives his name to the credits and works less than five minutes, when his character is killed. The special effects are limited to maggots everywhere. The direction is ridiculous. I lost a couple of hours of my life watching 'Assassinio al Cimitero Etrusco'. My suggestion is that if you have the desire or curiosity of seeing this trash, choose another movie, go to a pizzeria, watch TV, go sleep, navigate in Internet, go to the gym, but do not waste your time like I did. My vote is two.<br /><br />Title (Brazil): 'O Mistério Etrusco' ('The Etruscan Mystery')"
0.9047619047619048 -> "15555_0" "Minor Spoilers<br /><br />In New York, Joan Barnard (Elvire Audrey) is informed that her husband, the archeologist Arthur Barnard (John Saxon), was mysteriously murdered in Italy while searching an Etruscan tomb. Joan decides to travel to Italy, in the company of her colleague, who offers his support. Once in Italy, she starts having visions relative to an ancient people and maggots, many maggots. After shootings and weird events, Joan realizes that her father is an international drug dealer, there are drugs hidden in the tomb and her colleague is a detective of the narcotic department. The story ends back in New York, when Joan and her colleague decide to get married with each other, in a very romantic end. Yesterday I had the displeasure of wasting my time watching this crap. The story is so absurd, mixing thriller, crime, supernatural and horror (and even a romantic end) in a non-sense way. The acting is the worst possible, highlighting the horrible and screaming performance of the beautiful Elvire Audrey. John Saxon just gives his name to the credits and works less than five minutes, when his character is killed. The special effects are limited to maggots everywhere. The direction is ridiculous. I lost a couple of hours of my life watching 'Assassinio al Cimitero Etrusco'. If you have the desire or curiosity of seeing this trash, choose another movie, go to a pizzeria, watch TV, go sleep, navigate in Internet, go to the gym, but do not waste your time like I did. AVOID IT! My vote is two.<br /><br />Title (Brazil): 'O Mistério Etrusco' ('The Etruscan Mystery')"
It found two near duplicates to the source document we provided :-)
Please find the full code here:
https://gist.github.com/thomasjungblut/e4759797f5a52d78e06d
I will cover an edit later on with the inverted index version and how the performance actually improved.
Thanks for reading,
Thomas
Feb 8, 2015
Fixing Alienware's MX14r2 Audio on Ubuntu 14.04 LTS
Today I was using my Alienware MX14r2 again since a long time and after the upgrade to Ubuntu 14.04 my speakers work, but my headphone jack didn't respond anymore.
So if you see the following symptoms, I think I got a fix for you.
Make it fullscreen, so you can see every input / output codec:
So if you see the following symptoms, I think I got a fix for you.
- Speakers work and headphones don't (I experienced it the other way around already last year)
- When the headphones are plugged in, you hear sound from the speaker
After going through numerous threads and multiple config hacks and reinstalls of ALSA, I came to the surprisingly simply fix of changing some things on the alsamixer.
So go to a bash (CTRL+ALT+T) and type "alsamixer":
Make it fullscreen, so you can see every input / output codec:
The culprit really was that both HP/Speaker were set to "MM" while they should be enabled to "00":
So navigate with your arrow-keys to these and press "m" until it shows up as "00".
Surprisingly, enabling only one will only enable the speakers, the others only the headphones. Switching both to "00" will make them work like you're expecting.
Thanks for reading and I hope it helped,
Thomas
May 31, 2014
Stochastic Logistic Regression in F
Hello!
I'm back with some exiting new hacking. At the beginning of the week, Jon Harrop came to the Microsoft London office and gave a two day training on F#. The training was nice, I enjoyed it very much and we all laughed a lot :-)
In the end, I want to share a small project that I came up with during the training. It is about Stochastic Logistic Regression, or Logistic Regression "learning" the weights using Stochastic Gradient Descent.
This is just a small write-up of what I've learned, explaining some of the language features.
Let's jump into F#.
As you can see, F# is pretty much like the (more or less) mathematical notation I used in the previous section. Notable in the first place is that F# is indentation sensitive, just like Python.
In the first line we define the function "Train", which defines some arguments and no return type. F# is quite smart about type inference, so it can detect what the types of the parameters are, so with the return type. In this method, we return the learned weights "start"- thus the compiler knows that this method returns a DenseVector. Like in most of the functional languages, there is always a return- in case there is not, there is a "unit" return (literally defined by () ), which always indicates an absense of a value.
In the raw algorithm, where I used a for loop to loop over all iteration, we sample a new feature and pass it to another function which I defined in the module "OnlineLogisticRegression" containing the update and calculation logic. At the end, we simply print (using the standard printing in C#) the vector and return it to the outside. You can seamlessly use F# and C# with each other in a program as they compile to the same IL.
Let's step into the gradient descent function for a while:
Yet another example for the type inference, but in this case I want to put your attention to the operator overloading. Yes in F# you can overload operators: As you can see in the parameter update, "currentParameters" and "biasedFeature" is a DenseVector, while loss and learningRate are floats (floats in F# are 64bit doubles, float32 is the "normal" float). The compiler has a small problem, because you can't leave the brackets out, as it can't determine the precedence correctly?
In any case, the logic around that is pretty simple, very similar to a definition in maths.
As you can see, I have used the pipeline operator ( |> ) to chain operations together. First we create a new list containing the new and sampled test features, then we make it a sequence (which is a lazily evaluated structure that is similar to IEnumerable in C#).
Into that sequence, we map a three-tuple (the feature, the prediction and the learned weights) and then use another map stage to assess whether a classification was correct or not (threshold of the sigmoid here is 0.5). This in fact, is basically what's currying about: we chain multiple operators together forming a new function.
In the end, we sum the number of correct predictions by piping the sequences through the defined pipeline.
All of this is lazily evaluated, the whole computation was just executed within the last line.
I'm back with some exiting new hacking. At the beginning of the week, Jon Harrop came to the Microsoft London office and gave a two day training on F#. The training was nice, I enjoyed it very much and we all laughed a lot :-)
In the end, I want to share a small project that I came up with during the training. It is about Stochastic Logistic Regression, or Logistic Regression "learning" the weights using Stochastic Gradient Descent.
This is just a small write-up of what I've learned, explaining some of the language features.
Learning Rule
In (more or less) mathematical terms, we will execute the following algorithm:Let n = number of iterations Let a = learning rate Let w = weights that need to be learned (including a bias term) For i to N: Let f = get a random feature Let c = the class of feature (either 0 or 1) Let prediction = Sigmoid ( f dot w ) Let loss = prediction - c w := w - a * f * lossPretty easy algorithm, can be rewritten to use a convergence criterion very easily since there is only a single global minimum. If you participated in the ML course of Andrew Ng you'll recognize some similarity, because the full-batch algorithm they used basically uses the whole feature matrix and then updates the gradient by the average over all losses.
Let's jump into F#.
Training in F#
Since we are basically executing a math equation over and over again, it is handy to have a math library at hand. I've chosen Math.Net Numerics, because it has some nice F# bindings. Let's start by looking at the training loop of the whole program:
let Train(numIterations, dim, learningRate, rnd) =
// new vector including the bias
let mutable start = DenseVector.CreateRandom(dim + 1, Normal())
for i = 0 to numIterations do
let feature = SampleNewFeature(dim, rnd)
start <- OnlineLogisticRegression.GradientDescentStep(learningRate, start, FeatureVector(feature), FeatureClass(feature))
Console.WriteLine("Learned Weights: {0}", System.String.Join(", ", start.ToArray()))
start
As you can see, F# is pretty much like the (more or less) mathematical notation I used in the previous section. Notable in the first place is that F# is indentation sensitive, just like Python.
In the first line we define the function "Train", which defines some arguments and no return type. F# is quite smart about type inference, so it can detect what the types of the parameters are, so with the return type. In this method, we return the learned weights "start"- thus the compiler knows that this method returns a DenseVector. Like in most of the functional languages, there is always a return- in case there is not, there is a "unit" return (literally defined by () ), which always indicates an absense of a value.
In the raw algorithm, where I used a for loop to loop over all iteration, we sample a new feature and pass it to another function which I defined in the module "OnlineLogisticRegression" containing the update and calculation logic. At the end, we simply print (using the standard printing in C#) the vector and return it to the outside. You can seamlessly use F# and C# with each other in a program as they compile to the same IL.
Let's step into the gradient descent function for a while:
/// Does a stochastic gradient descent step. Returns a new vector with the updated weights.
let GradientDescentStep(learningRate, currentParameters, currentFeatureVector, outcome) =
let biasedFeature = CreateBiasVector currentFeatureVector
let prediction = Predict(biasedFeature, currentParameters)
let loss = prediction - outcome
// do a gradient descent step into the gradient direction
DenseVector.OfVector(currentParameters - (biasedFeature * (loss * learningRate)))
Yet another example for the type inference, but in this case I want to put your attention to the operator overloading. Yes in F# you can overload operators: As you can see in the parameter update, "currentParameters" and "biasedFeature" is a DenseVector, while loss and learningRate are floats (floats in F# are 64bit doubles, float32 is the "normal" float). The compiler has a small problem, because you can't leave the brackets out, as it can't determine the precedence correctly?
In any case, the logic around that is pretty simple, very similar to a definition in maths.
Testing the classifier in F#
In case the classifier is trained, we want to assess its learned weights. Usually, we use a hold-out test set to measure some metrics like accuracy or precision/recall, in this case I settled with sampling some new features from the random distribution we created the features with in the training stage. So how does that look like in F#?
let testSetSource = List.init testSetSize (fun (x) -> SampleNewFeature(dim, rnd))
let testSet =
Seq.ofList testSetSource
|> Seq.map (fun(feat) -> feat, OnlineLogisticRegression.Predict(FeatureVector(feat), weights))
|> Seq.map (fun(feat, prediction) -> feat, prediction, abs(FeatureClass(feat) - prediction) < 0.5)
let countCorrectPredictions = Seq.sumBy (fun(feat, prediction, correct) -> if correct then 1 else 0)
let numCorrect = countCorrectPredictions testSet
As you can see, I have used the pipeline operator ( |> ) to chain operations together. First we create a new list containing the new and sampled test features, then we make it a sequence (which is a lazily evaluated structure that is similar to IEnumerable in C#).
Into that sequence, we map a three-tuple (the feature, the prediction and the learned weights) and then use another map stage to assess whether a classification was correct or not (threshold of the sigmoid here is 0.5). This in fact, is basically what's currying about: we chain multiple operators together forming a new function.
In the end, we sum the number of correct predictions by piping the sequences through the defined pipeline.
All of this is lazily evaluated, the whole computation was just executed within the last line.
Code
The code can be found on GitHub, Apache 2.0 licensed:
Result
Executing the code in the GitHub repository yields to the following plot.
Thanks for reading!
Next time (maybe), I'll write an online logistic regression service that can be trained on Microsoft Azure using a real-time data stream.
Apr 7, 2014
Greatest common divisor using F#
Hey guys,
I played with F# on the weekend. Since I'm working at Microsoft now and mainly working with C#, I thought F# would be a nice counterpart to my Scala work in the past.
A few personal updates
The start at Microsoft was quite a lot of work until now and I find less time and motivation in coding much in the evening. You can mainly see this in the commit activity of my common library on Github which looks like this:
Flatline since christmas... Not very proud of it, due to my relocation to London I was two months without my development desktop PC, in addition, context-switching between C# and Java isn't easy. Personally I find it very hard to go back to Java, as I'm missing a ton of functionality from C#! But I promise that I will find my way back, especially when it comes to awesome machine learning algorithms.
Back to the topic
So like most of the time, I was lurking around at Stackoverflow and found a poor guy using Java8 lambdas to make the greatest common divisor working. Besides that his algorithm is quite strange and by the time you read this the question is likely to be deleted, I find a quite interesting idea to practice my (still poor) weekend's F# skills.
Just a few words about the greatest common divisor definition, given two integers, we want the largest number that divides both without a remainder.
I chose the simplest set theory approach, which is just calculating two sets of divisors and intersecting them. Then I choose the maximum from the intersection.
Here is what I came up in F# (even using the pipelining operators!):
Rather easy binding of a function, not much to say here.
Small obligatory testcase:
How is the language so far?
I found F# until now quite succinct, especially compared to C#. .NET integrates very nicely, too.
However, sometimes the type system makes me want to punch a wall.
But the most disappointing experience so far: the .fs file that contains your main method needs to be the last one in the project's solution. Yes you heard correctly, it must be on the lowest possible list entry in the project solution list.
How dumb is that?! Obviously, this was hiding behind a MSFT typical error message:
Complexity
Let's talk about the complexity a bit. So as far as I read here, a Set in F# is unordered, most likely to be a HashSet. Thus constructing and filtering both sets is O(A) respectively O(B), the intersection of two unordered sets is linear in time as well. So what we will end up is something like this:
But hey! It is just a naive method to show off my new aquired F# knowledge ;-)
Thanks for reading, see you next time with a more interesting Big Data topic I hope,
Thomas
I played with F# on the weekend. Since I'm working at Microsoft now and mainly working with C#, I thought F# would be a nice counterpart to my Scala work in the past.
A few personal updates
The start at Microsoft was quite a lot of work until now and I find less time and motivation in coding much in the evening. You can mainly see this in the commit activity of my common library on Github which looks like this:
Flatline since christmas... Not very proud of it, due to my relocation to London I was two months without my development desktop PC, in addition, context-switching between C# and Java isn't easy. Personally I find it very hard to go back to Java, as I'm missing a ton of functionality from C#! But I promise that I will find my way back, especially when it comes to awesome machine learning algorithms.
Back to the topic
So like most of the time, I was lurking around at Stackoverflow and found a poor guy using Java8 lambdas to make the greatest common divisor working. Besides that his algorithm is quite strange and by the time you read this the question is likely to be deleted, I find a quite interesting idea to practice my (still poor) weekend's F# skills.
Just a few words about the greatest common divisor definition, given two integers, we want the largest number that divides both without a remainder.
I chose the simplest set theory approach, which is just calculating two sets of divisors and intersecting them. Then I choose the maximum from the intersection.
Here is what I came up in F# (even using the pipelining operators!):
let gcd = fun (a, b) ->
let set1 = Set.ofList [ 1 .. a ]
|> Set.filter (fun x -> a % x = 0)
let inter = Set.ofList [ 1 .. b ]
|> Set.filter (fun x -> b % x = 0)
|> Set.intersect set1
inter.MaximumElement
Rather easy binding of a function, not much to say here.
Small obligatory testcase:
let a = 12
let b = 8
Console.WriteLine("GCD: {0}", gcd(a, b))
// yields to result
// GCD: 4
How is the language so far?
I found F# until now quite succinct, especially compared to C#. .NET integrates very nicely, too.
However, sometimes the type system makes me want to punch a wall.
But the most disappointing experience so far: the .fs file that contains your main method needs to be the last one in the project's solution. Yes you heard correctly, it must be on the lowest possible list entry in the project solution list.
How dumb is that?! Obviously, this was hiding behind a MSFT typical error message:
error FS0039: The namespace or module '_my_namespace' is not definedThat cost me a few hours to figure out. -.-
Complexity
Let's talk about the complexity a bit. So as far as I read here, a Set in F# is unordered, most likely to be a HashSet. Thus constructing and filtering both sets is O(A) respectively O(B), the intersection of two unordered sets is linear in time as well. So what we will end up is something like this:
2 * (O(A) + O(B)) + O(A+B) = O(n)Still linear time, but very bad constants. Also the space complexity is rather bad, it is linear as well, as we are creating two quite large sets.
But hey! It is just a naive method to show off my new aquired F# knowledge ;-)
Thanks for reading, see you next time with a more interesting Big Data topic I hope,
Thomas
Jun 8, 2013
Wiring services together using the actor model
Hey folks,
I had a lot of stuff to do in my last semester, thus I couldn't write up the post about news clustering completely yet. The stress will keep constant until my study is finally over, I guess until arround the beginning of october. Then I will have more time to explain this topic more thoroughly, of course with lots of pictures ;)
But today I have a bit of time and wanted to share a smart idea of joining two cool things together: Service Oriented Architectures (SOA) and the Actor Model. We will go through a small definition of both, why we should join these technologies and at the end have a small look on how to implement such an architecture with plain Java.
For many of you, this might look like a data access object (DAO) that is going to ask an underlying database implementation about the concrete values. This is not what the goal of the service itself should be: a service defines just the functionality and not how it transports the information (whether there is a RPC call or an Oracle database in the back shouldn't be part of your interface/service design).
Thus the user should never care about the underlying complexity or the implementation of the system. That is a similar statement like in Object Oriented Programming that naturally yields to polymorphism (multiple implementations of an interface).
But how do Services wire up together?
Imagine a computing cluster where a scheduler is a service that looks at the resources in the cluster and makes decisions on where to place your application best. How does it communicate the result of its computation to the next service- say the service that handles allocation of those resources?
There are basically few ways to handle this:
Consider the following most simplistic scenario when dealing with services:
In this case the services form a simple chaining, so calls from Service A can only reach B, and B can only reach C. Clearly, there is no need for anything spectacular- if I were in that situation I would put those services in a list and call them after each other:
This is what is called the Pipeline pattern, because you feed the result of the stage before to the next one and enhance/filter/modify the results. This is a supervised architecture, because you are controlling how the data flows by hardcoding the control flow in the above loop.
But what happens when we want to process requests through the services concurrently?
Now that is where the problems usually begins. The above architecture will work in a concurrent environment without any problems, as long as the code that is calling the services in sequence is thread-safe and all the services are designed thread-safe. That means, if your service has state (for example our scheduler that has information about the current cluster resources), it needs to lock all access to it while modifying it. This is not a big deal for someone who worked with threads already and is familiar with standard libraries like in Java (see for example a ReadWriteLock).
However, think of what complexity you are imposing to your software in that moment:
What do you want to do when Service B locks it's state for a long time (e.g. our scheduler just received a big update from a rack that just got back online)? Clearly other services will have to wait and throughput and responsiveness starts to suffer severely. You can spin this even a tick further: What if you're in a distributed environment and Service B just doesn't exist anymore (server goes down, netlink breaks)? Services A_[1-n] will have to wait until B comes back online and can't do anything else than wait. Always note that those are the easiest service architectures! In reality your call graph looks much more connected throughout all services.
All that is an issue if you're relying on synchronous communication between services. What we need is to define a decoupling between the services- not of their functionality, but this time of the communication between them.
While you can use a framework like Akka for the actor model, it is very easy to implement in Java using the standard API:
As you can see, it is super easy to setup a producer/consumer inbox within a thread and use the service as a callback listener. All concurrency and signalling is the problem of the underlying implementation of the inbox, here a LinkedBlockingQueue is used.
Now your Service can easily implement the callback, with the guarantee that every message that arrives will be processed sequentially (because your run method takes only one message at a time from the queue). So you will never have to worry about explicit locking in your code, you just have to react to events that happen.
A simplistic and fictitious variant of a scheduler that reacts can look like this:
You can see, the logic is very clean, no locking is needed and you can react on specific events- or ignore them if you don't care about them. In a real-life scenario I would add an ActorManager that helps messaging by a defined name or class, or you can design actors as singletons and directly access their messaging methods.
Let's get back to our problems we had with the synchronous and supervised calls and see if we solved them:
But what are the clear disadvantages of this actor model?
Of course there is no silver bullet in such technology. The actor model also has drawbacks, here are a few that I have observed when working with event driven actor architectures:
Do you want to create a service for functionality? Or do you want to create services that interact with each other? Both are (by definition) service oriented architectures and both can be used in conjunction with each other - choose wisely which one you need to use.
The second bullet point is something that will drive developers nuts in their daily lifes. When writing an actor model, be sure that your actors are named accordingly to their usecase. Nobody wants to send a message not knowing whose inbox to reach. So make it clear to which destination you're sending a message to.
Something that I have employed to neglect this was to use classnames as the address and make all services singletons. This helps to write code like this:
People working with that will immediately know, that they can click on the class entry in their IDE and get to the implementation fast and will always know where the message will end up.
Still the amount of scrolling to be done is too damn high! I hope that the IDEs will soon catch up on those paradigms (especially when lambdas and function pointers come with Java8) and make it easy to navigate to callback/listener methods.
So thank you very much for reading, you've definitely won a cookie for reading the whole article.
I had a lot of stuff to do in my last semester, thus I couldn't write up the post about news clustering completely yet. The stress will keep constant until my study is finally over, I guess until arround the beginning of october. Then I will have more time to explain this topic more thoroughly, of course with lots of pictures ;)
But today I have a bit of time and wanted to share a smart idea of joining two cool things together: Service Oriented Architectures (SOA) and the Actor Model. We will go through a small definition of both, why we should join these technologies and at the end have a small look on how to implement such an architecture with plain Java.
Why Sevice Oriented Architectures?
Service Oriented Architecture is a design pattern based on chunking a large software system into smaller and discrete modules called services. The goal is to design services solely based on their functionality and thus decouple them from other services. The result should be an ensemble of services that are only defined by their simplistic interfaces that offer functionality to the outside world.
A pretty intuitive example is a checkout process in ecommerce systems: it is a large and complicated process that can be chunked into simpler parts. At the beginning of your internet shopping trip you are likely to visit a few products. Retrieving products and their information is also a good candicate for a service, because it has a defined behaviour and its functionality can be reused very well for other purposes. The corresponding interface could be something like this:
// could directly retrieve objects from a database
public Product getProduct(long productId);
// could be a proxy to another service
public Opinions getUserOpinions(long productId);
// could be a filesystem call
public Images getProductImages(long productId);
For many of you, this might look like a data access object (DAO) that is going to ask an underlying database implementation about the concrete values. This is not what the goal of the service itself should be: a service defines just the functionality and not how it transports the information (whether there is a RPC call or an Oracle database in the back shouldn't be part of your interface/service design).
Thus the user should never care about the underlying complexity or the implementation of the system. That is a similar statement like in Object Oriented Programming that naturally yields to polymorphism (multiple implementations of an interface).
But how do Services wire up together?
Imagine a computing cluster where a scheduler is a service that looks at the resources in the cluster and makes decisions on where to place your application best. How does it communicate the result of its computation to the next service- say the service that handles allocation of those resources?
There are basically few ways to handle this:
- Just call the next service via its interface methods (synchronous call)
- Send a message to the other service and continue (asynchronous call)
- A superviser/manager that calls services after each other (supervised call)
Consider the following most simplistic scenario when dealing with services:
| Super simple service chain |
List<Service> services = ... some list ...;
Result lastResult = null;
for(Service service : services){
lastResult = service.call(lastResult);
}
// go further down the road with the final service result
This is what is called the Pipeline pattern, because you feed the result of the stage before to the next one and enhance/filter/modify the results. This is a supervised architecture, because you are controlling how the data flows by hardcoding the control flow in the above loop.
But what happens when we want to process requests through the services concurrently?
Now that is where the problems usually begins. The above architecture will work in a concurrent environment without any problems, as long as the code that is calling the services in sequence is thread-safe and all the services are designed thread-safe. That means, if your service has state (for example our scheduler that has information about the current cluster resources), it needs to lock all access to it while modifying it. This is not a big deal for someone who worked with threads already and is familiar with standard libraries like in Java (see for example a ReadWriteLock).
However, think of what complexity you are imposing to your software in that moment:
- Every service needs to handle the locks for itself and must be thread-safe
- Even with standard library support you clutter your code with try/finally unlock statements
- The performance is likely to suffer in high concurrency/throughput environments
Overall, this is a complexity nightmare (have you ever traced down a race condition?) and exactly what we wanted to avoid when choosing a SOA.
It just begins to get worse:
| Slightly more complex service chain |
All that is an issue if you're relying on synchronous communication between services. What we need is to define a decoupling between the services- not of their functionality, but this time of the communication between them.
The Actor Model
The most intuitive way to make asynchronous communication to happen is to send a message!
If I want Bob to work on issue X in our bug tracker, I write him an email that he should have a look at issue X soon. Now Bob can decide on his own when he looks into his mailbox (for example when he is finished with the current task) and also when he wants to start working on issue X. Transferred to computer science: you don't disturb the service in doing its job as you would with locking or interrupts.
The intuition is the same behind the actor model, here Bob would be the actor and emails would be some kind of messages that land in an actors' inbox. Normally we want to have many more actors that can interact with each other and provide functionality. That's where we come back to services: so actors and services both provide functionality / behaviour and messaging between actors helps us to solve the synchronous comunication problems.
While you can use a framework like Akka for the actor model, it is very easy to implement in Java using the standard API:
public class SimpleActor<MSG_TYPE> implements Runnable {
public static interface Service<MSG_TYPE> {
void onMessage(MSG_TYPE message);
}
private final LinkedBlockingQueue<MSG_TYPE> inbox = new LinkedBlockingQueue<>();
private Service<MSG_TYPE> messageListener;
public SimpleActor(Service<MSG_TYPE> listener) {
this.messageListener = listener;
}
public void message(MSG_TYPE message) {
inbox.add(message);
}
@Override
public void run() {
while (!Thread.currentThread().isInterrupted()) {
// blocks until we have a new message
MSG_TYPE take = inbox.take();
messageListener.onMessage(take);
// interrupted exception omitted
}
}
}
As you can see, it is super easy to setup a producer/consumer inbox within a thread and use the service as a callback listener. All concurrency and signalling is the problem of the underlying implementation of the inbox, here a LinkedBlockingQueue is used.
Now your Service can easily implement the callback, with the guarantee that every message that arrives will be processed sequentially (because your run method takes only one message at a time from the queue). So you will never have to worry about explicit locking in your code, you just have to react to events that happen.
A simplistic and fictitious variant of a scheduler that reacts can look like this:
Service<SchedulingEvent> scheduler = new Service<SchedulingEvent>() {
@Override
public void onMessage(SchedulingEvent message) {
if(message.isSchedulingMessage()){
if(cluster.getFreeMemory() > message.memoryNeeded()){
// tell the allocation actor to run that
message(Allocator.class, new Allocation(message));
}
} else if(message.isUpdateMessage()){
cluster.update(message.getFreeMemory());
}
}
};
You can see, the logic is very clean, no locking is needed and you can react on specific events- or ignore them if you don't care about them. In a real-life scenario I would add an ActorManager that helps messaging by a defined name or class, or you can design actors as singletons and directly access their messaging methods.
Let's get back to our problems we had with the synchronous and supervised calls and see if we solved them:
- Locking complexity
- Best case: no locking anymore in the service code itself
- Code clutter
- Everything looks very clean and tied to the servers functionality
- Performance
- Every service can work at its own speed/pace, no polling is involved
- What if the inbox fills up faster than the messages can be consumed?
- Is it really faster?
- Availability
- When a service goes down, it is up to the messaging implementation to buffer those messages in a secondary storage so they can be retrieved after a crash. But certainly this is now easier to implement and maintain.
But what are the clear disadvantages of this actor model?
Of course there is no silver bullet in such technology. The actor model also has drawbacks, here are a few that I have observed when working with event driven actor architectures:
- You have no explicit returns, e.g. if an exception happens you will be notified long time afterwards via message that comes back
- Debugging is the hell if you don't optimize readability for it
Do you want to create a service for functionality? Or do you want to create services that interact with each other? Both are (by definition) service oriented architectures and both can be used in conjunction with each other - choose wisely which one you need to use.
The second bullet point is something that will drive developers nuts in their daily lifes. When writing an actor model, be sure that your actors are named accordingly to their usecase. Nobody wants to send a message not knowing whose inbox to reach. So make it clear to which destination you're sending a message to.
Something that I have employed to neglect this was to use classnames as the address and make all services singletons. This helps to write code like this:
// class name based routing
message(Allocator.class, new Allocation(message));
// singleton based routing
message(Allocator.getInstance(), new Allocation(message));
// singleton based direct messaging, NOTE getInstance() is a
// convention, not a defined interface!
Allocator.getInstance().message(new Allocation(message));
People working with that will immediately know, that they can click on the class entry in their IDE and get to the implementation fast and will always know where the message will end up.
Still the amount of scrolling to be done is too damn high! I hope that the IDEs will soon catch up on those paradigms (especially when lambdas and function pointers come with Java8) and make it easy to navigate to callback/listener methods.
So thank you very much for reading, you've definitely won a cookie for reading the whole article.
Jan 27, 2013
Named Entity Recognition in News Articles
Hello,
been a while since the last post. Was into a lot of work- couldn't really get up on the weekend to write about named entity recognition in news articles. But today we can finally talk about it.
This post is about a few things:
But how do we need this for our news aggregation engine? A very simple assumption: News are about people, what they did and where they did it. A simple example would be:
How do we model this as a machine learning problem?
Basically, it is nothing else than a (multiclass) classification problem. Namely classifying if a token belongs to the class PERSON, LOCATION- or O which is none of the ones before.
The main difference to other NLP tasks is that we need the context of a token, because the meaning of the current token is depending on the previous or the following token/class. So let's have a look at another example:
So what kind of classifier do we use? I used a self written version of the Maximum Entropy Markov Model supplied in week four of the NLP class exercise optimizable with normal Gradient Descent or Conjugate Gradient (or even with the given quasi newton minimizer supplied in the class). Also I written some utilities to extract sparse feature vectors as conveniently as in NLP class.
You can browse some code in my common repository's NER package and see how it looks like to use with the data supplied from NLP class in my testcase.
What features to extract for our learner?
Features are pretty important, they must cover structural as well as dictionary features. Here is my feature set for dictionary features:
For example the feature prevPosTag could have value "prevPosTag=NN". This is a rare occation as we have a unigram encoded as a feature vector, so it totally makes sense to encode them with a sparse vector.
Now that we have our text encoded as a list of vectors (a sparse vector for each unigram we observe) we can optimize the vectors and their outcome by minimizing a conditional likelyhood costfunction to be used in the Markov Model. This will learn conditional probabilities between features and the outcomes, describing when we are observing a feature- how likely is that the PERSON class occurs, for math lovers this can be described as P( class | features ). I optimized my model for 1k iterations using conjugate gradient and obtained a very low training error of arround 5%. To obtain the class for a feature vector we are doing a viterbi decoding on the learned weights. The trick here is that you need to encode the feature vector for all the possible classes, only that way the viterbi can decode the probabilities correctly.
So yeah, that is basically what it's all that named entity recognition is about. The next blog post will most probably be about how to cluster the news together using the information we gathered through this post.
Bye!
been a while since the last post. Was into a lot of work- couldn't really get up on the weekend to write about named entity recognition in news articles. But today we can finally talk about it.
This post is about a few things:
- What is named entity recognition?
- How do we model it as a machine learning problem?
- What features to extract for our learner?
So let's dive in, if you have taken the Natural Language Processing (NLP) class on Coursera, you will be familiar with the topic already and should start with the features we use in the third paragraph.
What is named entity recognition (NER)?
The most easiest explanation is to find word level concepts like a location or person in an unstructured text file. Let's say we have the following snippet of text, shamelessly stolen from Wikipedia:
The idea is to tag parts of this sentence with tuples of concepts and their value, such that we get this:What is named entity recognition (NER)?
The most easiest explanation is to find word level concepts like a location or person in an unstructured text file. Let's say we have the following snippet of text, shamelessly stolen from Wikipedia:
Jim bought 300 shares of Acme Corp. in 2006.
<PERSON, "Jim"> bought 300 shares of <CORP,"Acme Corp"> in 2006.So we detected Jim as a person and the Acme Corp as a corporation in this sentence.
But how do we need this for our news aggregation engine? A very simple assumption: News are about people, what they did and where they did it. A simple example would be:
"David Cameron talks in Davos about the EU"The topic is clearly about the person David Cameron- the action of talking and a location namely Davos in Switzerland. This is needed for our news aggregation engine to cluster topics together, even if their content is slightly different. We will talk about this in one of the following blog posts.
How do we model this as a machine learning problem?
Basically, it is nothing else than a (multiclass) classification problem. Namely classifying if a token belongs to the class PERSON, LOCATION- or O which is none of the ones before.
The main difference to other NLP tasks is that we need the context of a token, because the meaning of the current token is depending on the previous or the following token/class. So let's have a look at another example:
It did not, and most of Mr. Cameron’s European partners do not wish to revisit that fundamental question.How do we recognize Cameron in this case here? There are two cases that are representative for english. First the "Mr." is a strong indicator that there is following a name, second the "'s" is a strong indicator that the previous token was a name. Also prepositions like "of", "to", "in" or "for" are likely indicators for names or locations. The trick that I learned from the NLP class on coursera was the encoding of the text as a sequence of unigrams. The previous text would look like this:
most OSo what we do is predicting the label of the current unigram, by looking at the previous and following unigram and maybe also at the previous label. The reason we need to look at the previous label is that names could be composed of name and surname like David Cameron. So if the last class was a person, in many cases the current class might also be a person.
of O
Mr. O
Cameron PERSON
's O
So what kind of classifier do we use? I used a self written version of the Maximum Entropy Markov Model supplied in week four of the NLP class exercise optimizable with normal Gradient Descent or Conjugate Gradient (or even with the given quasi newton minimizer supplied in the class). Also I written some utilities to extract sparse feature vectors as conveniently as in NLP class.
You can browse some code in my common repository's NER package and see how it looks like to use with the data supplied from NLP class in my testcase.
What features to extract for our learner?
Features are pretty important, they must cover structural as well as dictionary features. Here is my feature set for dictionary features:
- current word
- last word
- next word
And for structural features:
- previous class label
- current word upper case start character
- last word upper case start character
- current word length
- current word containing only alphabetic characters (1=true or 0=false)
- next word containing only alphabetic characters (1=true or 0=false)
- was the last word a preposition
- was the last word a special character like dot, comma or question mark
- last word ending with "ing"
- previous/current/next words POS tag
For example the feature prevPosTag could have value "prevPosTag=NN". This is a rare occation as we have a unigram encoded as a feature vector, so it totally makes sense to encode them with a sparse vector.
Now that we have our text encoded as a list of vectors (a sparse vector for each unigram we observe) we can optimize the vectors and their outcome by minimizing a conditional likelyhood costfunction to be used in the Markov Model. This will learn conditional probabilities between features and the outcomes, describing when we are observing a feature- how likely is that the PERSON class occurs, for math lovers this can be described as P( class | features ). I optimized my model for 1k iterations using conjugate gradient and obtained a very low training error of arround 5%. To obtain the class for a feature vector we are doing a viterbi decoding on the learned weights. The trick here is that you need to encode the feature vector for all the possible classes, only that way the viterbi can decode the probabilities correctly.
So yeah, that is basically what it's all that named entity recognition is about. The next blog post will most probably be about how to cluster the news together using the information we gathered through this post.
Bye!
Jan 1, 2013
Extracting articles from crawled HTML sites
So welcome to our second part of Building a news aggregation engine!
If you don't know how crawlers work, have a look into the last part of the series: Build a basic crawler.
This time we will talk about how to get the actual raw content of a site. Something that we humans see on a newspage isn't visible for an algorithm, because it just has to look at the code- not the actual rendered page. Therefore I will introduce you with a technique called Boilerplate removal.
What is this "Boilerplate"?
Boilerplate is everything, but the content of the page you are seeking for. Some people call it: "design" or "nagivation" and in their first attempts they try to parse websites with XPath expressions to get their content. But that is not necessary and an unsual pain if the design really shifts.
So the lesson learned is (like in the last blog post as well): IGNORE. Yes you have to ignore the parts you don't want to have. That doesn't mean that we don't take the parts to make decisions- in fact we need those to decide whether a block of html code belongs to the IGNORE- or the content-part.
Therefore I want to introduce to you the boilerpipe framework. Boilerpipe is written in Java by Christian Kohlschütter and it is licensed with Apache 2.0.
It uses some really simple machine learning algorithm (a small decision tree) to classify whether a given block of html is content or not. You can read the details in his research paper "Boilerplate Detection using Shallow Text Features" which is a really good read.
In short, it analyses the amount of tokens and links in the previous, current and next block of HTML. It is the same idea we will use later in sequence-learning when we deal with Named Entity Recognition in order to get people and events from the article.
In Java, you can use it like this:
It is sooo simple and you have the content of a webpage in a string. Of course, this only applies for news articles, since they are shaped really consistent over many news sites therefore the name ArticleExtractor.
But in real world, if we crawl the web, most of the sites we examine with our crawlers won't be news- so the content in the resulting text string might not be a news article. Maybe it is just the imprint or privacy statement that looks like an article, but isn't.
Classifying News sites
Since I faced this issue while crawling, I had to train some machine learning algorithm on the output of boilerpipe to detect news accurately.
Here is what I did:
But many people don't tell what kind of features they used, so here are mine:
I trained a neural net (7-35-1) with sigmoid activations (and 1.0 as regularization) for 10k epochs with Conjugate Gradient. Here are the average results from a 10 fold crossvalidation:
If you want to have some data of my crawl, I have ~1500 classified news articles and 16.5k unclassified- so if you need a bit of german news data for whatever research: let me know via email!
Congratz! We can now crawl the world wide web and classify news sites very accurately. Our next step will be to develop a named entity recognition engine that allows us to extract the keywords we need from our text in order to group them efficiently.
If you don't know how crawlers work, have a look into the last part of the series: Build a basic crawler.
This time we will talk about how to get the actual raw content of a site. Something that we humans see on a newspage isn't visible for an algorithm, because it just has to look at the code- not the actual rendered page. Therefore I will introduce you with a technique called Boilerplate removal.
What is this "Boilerplate"?
Boilerplate is everything, but the content of the page you are seeking for. Some people call it: "design" or "nagivation" and in their first attempts they try to parse websites with XPath expressions to get their content. But that is not necessary and an unsual pain if the design really shifts.
So the lesson learned is (like in the last blog post as well): IGNORE. Yes you have to ignore the parts you don't want to have. That doesn't mean that we don't take the parts to make decisions- in fact we need those to decide whether a block of html code belongs to the IGNORE- or the content-part.
Therefore I want to introduce to you the boilerpipe framework. Boilerpipe is written in Java by Christian Kohlschütter and it is licensed with Apache 2.0.
It uses some really simple machine learning algorithm (a small decision tree) to classify whether a given block of html is content or not. You can read the details in his research paper "Boilerplate Detection using Shallow Text Features" which is a really good read.
In short, it analyses the amount of tokens and links in the previous, current and next block of HTML. It is the same idea we will use later in sequence-learning when we deal with Named Entity Recognition in order to get people and events from the article.
In Java, you can use it like this:
final BoilerpipeExtractor extractor = ArticleExtractor.getInstance(); String text = extractor.getText(html);
It is sooo simple and you have the content of a webpage in a string. Of course, this only applies for news articles, since they are shaped really consistent over many news sites therefore the name ArticleExtractor.
But in real world, if we crawl the web, most of the sites we examine with our crawlers won't be news- so the content in the resulting text string might not be a news article. Maybe it is just the imprint or privacy statement that looks like an article, but isn't.
Classifying News sites
Since I faced this issue while crawling, I had to train some machine learning algorithm on the output of boilerpipe to detect news accurately.
Here is what I did:
- Let the crawler run on the top 40 news sites in germany (hand seeded list) for 100k sites
- Write a small python application to loop over all files and ask me if it was news or not
- After 1k hand-classified items (was just 1h of work!), train a classifier.
But many people don't tell what kind of features they used, so here are mine:
- Length of the extracted text
- URL ends with '/'
- Length of the extracted title
- Number of '\n' in the extracted text
- Text mention of "impressum","haftung","agb","datenschutz","nutzungsbedingungen" (imprint, authors etc.)
- Title mention of "impressum","haftung","agb","datenschutz","nutzungsbedingungen" (imprint, authors etc.)
- Number of upper case letters in the text
I trained a neural net (7-35-1) with sigmoid activations (and 1.0 as regularization) for 10k epochs with Conjugate Gradient. Here are the average results from a 10 fold crossvalidation:
Accuracy: 0.9259259259259259That is pretty good for such simple methods! And I didn't even used HTML features like boilerpipe does ;-)
Precision: 0.9173553719008265
Recall: 0.9823008849557522
F1 Score: 0.9487179487179487
If you want to have some data of my crawl, I have ~1500 classified news articles and 16.5k unclassified- so if you need a bit of german news data for whatever research: let me know via email!
Congratz! We can now crawl the world wide web and classify news sites very accurately. Our next step will be to develop a named entity recognition engine that allows us to extract the keywords we need from our text in order to group them efficiently.
Build a basic crawler
So welcome to our first part of Building a news aggregation engine!
This time we talk about how we build a really simple crawler, that crawls us some sites.
The (more or less) formal definition of a crawler is stated in Wikipedia:
A Web crawler is a computer program that browses the World Wide Web in a methodical, automated manner or in an orderly fashion.
The basic workflow looks like that:
It looks really simple, but tell you what? It is more difficult than it looks.
Once you started with it, you wish you never started with that- the web is ugly. I'm working in the backend team at work and I'm surrounded by a lot of garbage from various data sources, but the web is a whole new level. Just a small excerpt of what you need to deal with:
So as you can see, I'm guarding against anything here. Of course they are completely useless if someone does not supply a suffix of the filetype. In the latter case, you will need to get on the stream and look for a html or body tag to verify it is really a website (which is the worst case, because you're wasting bandwidth and time the crawler could use to do something else).
Something that poked me for quite a while were encoding issues. As a german, umlauts like öäüß are completely garbled if you read them with the wrong encoding. So most of the time, germany news look really bad and you can directly throw them into the next trash bin.
I ran across a project of the Mozilla foundation called universalchardet (abbrev. for universal charset detector) and its Java descendent called juniversalchardet. It detects encodings with really high accuracy and helps you to get the content of your crawl correct like you would browse the site.
In Java you have to obtain the site via streams, so let me show you a small example of juniversalchardet and how to read a stream into a string of HTML with NIO.
That is actually everything to know about getting HTML from a raw URL.
But, how do you extract the outlinks from a HTML page?
Many of you will now go ahead and say: let's compile some RegEx. You will FAIL.
As a computer scientist it is enough if you tell that HTML is a context free grammar (chomsky type 2) and RegEx needs a regular language (type 3) to operate properly. Type 2 languages are way more complex and can't be parsed with a regular expression. So please have a look at the funny rage answer at stackoverflow or read up the other very informative answers at the bottom to know why you shouldn't do this. Don't get me wrong: You will find URLs that you can parse with RegEx, but I don't think it is worth the stress. I always use the htmlparser on sourceforge, it is clean, well tested and pretty fast.
To end this post, I tell you how to extract some outlinks from a html page as string:
It is simple as that. How expanding of URLs can be done is another part- but I leave that up to you ;-) Java's URI may help you with that.
So thanks for attending, my next post is about how to extract actual text content (news) from pure HTML code.
- Seed some URLs and queue them up
- Keep a set about what URLs were visited
- While our queue is not empty (or we reached some maximum amounts of sites)
- Get the first URL from the queue, put it into the visited set
- Query the URL, obtain some HTML
- Extract new URLs from the HTML and queue them up if they are not in the visited set yet
A small Java program could look like this:
String[] seedUrl = new String[]{"http://www.wikipedia.com/"};
final Deque<String> linksToCrawl = new ArrayDeque<>();
final HashSet<String> visited = new HashSet<>();
linksToCrawl.addAll(Arrays.asList(seedUrl));
visited.addAll(Arrays.asList(seedUrl));
while (fetches < 100 && !linksToCrawl.isEmpty()) {
String urlToCrawl = linksToCrawl.poll();
// open a connection and parse HTML
...
// loop over all links we found on that page
for (String outlink : extractedResult.outlinks) {
if (visited.add(outlink))
linksToCrawl.add(outlink);
}
fetches++;
}
Once you started with it, you wish you never started with that- the web is ugly. I'm working in the backend team at work and I'm surrounded by a lot of garbage from various data sources, but the web is a whole new level. Just a small excerpt of what you need to deal with:
- Encoding issues (we will fix them later on in this post)
- Link expansions (relative vs. absolute URLs vs. JavaScript URLs like void(0); )
- Not parsable stuff like videos or images
So for example, how do you deal with data you can't handle (which don't contain HTML)? You IGNORE it. For this kind of purpose I've clamped together a bunch of suffixes that can happen within links and they guard against running into not parsable binary data:
Pattern IGNORE_SUFFIX_PATTERN = Pattern
.compile(".*(\\.(css|js|bmp|gif|jpe?g|png|tiff?|mid|mp2|mp3|mp4|wav|avi|mov|mpeg|ram|m4v|pdf|iso|rm|smil|wmv|swf|wma|zip|rar|gz))$");
So as you can see, I'm guarding against anything here. Of course they are completely useless if someone does not supply a suffix of the filetype. In the latter case, you will need to get on the stream and look for a html or body tag to verify it is really a website (which is the worst case, because you're wasting bandwidth and time the crawler could use to do something else).
Something that poked me for quite a while were encoding issues. As a german, umlauts like öäüß are completely garbled if you read them with the wrong encoding. So most of the time, germany news look really bad and you can directly throw them into the next trash bin.
I ran across a project of the Mozilla foundation called universalchardet (abbrev. for universal charset detector) and its Java descendent called juniversalchardet. It detects encodings with really high accuracy and helps you to get the content of your crawl correct like you would browse the site.
In Java you have to obtain the site via streams, so let me show you a small example of juniversalchardet and how to read a stream into a string of HTML with NIO.
String someURLAsString = "http://www.facebook.com";
URL url = new URL(someURLAsString);
InputStream stream = url.openStream();
String html = consumeStream(stream);
// the helper methods
public static String consumeStream(InputStream stream) throws IOException {
try {
// setup the universal detector for charsets
UniversalDetector detector = new UniversalDetector(null);
ReadableByteChannel bc = Channels.newChannel(stream);
// allocate a byte buffer of BUFFER_SIZE size
// 1mb is enough for every usual webpage
ByteBuffer buffer = ByteBuffer.allocate(BUFFER_SIZE);
int read = 0;
while ((read = bc.read(buffer)) != -1) {
// let the detector work on the downloaded chunk
detector.handleData(buffer.array(), buffer.position() - read, read);
// check if we found a larger site, then resize the buffer
buffer = resizeBuffer(buffer);
}
// finish the sequence
detector.dataEnd();
// copy the result back to a byte array
String encoding = detector.getDetectedCharset();
// obtain the encoding, if null fall back to UTF-8
return new String(buffer.array(), 0, buffer.position(),
encoding == null ? "UTF-8" : encoding);
} finally {
if (stream != null) {
stream.close();
}
}
}
// basic resize operation when 90% of the buffer is occupied
// simply double the correct size and copy the buffer
private static ByteBuffer resizeBuffer(ByteBuffer buffer) {
ByteBuffer result = buffer;
// double the size if we have only 10% capacity left
if (buffer.remaining() < (int) (buffer.capacity() * 0.1f)) {
result = ByteBuffer.allocate(buffer.capacity() * 2);
buffer.flip();
result.put(buffer);
}
return result;
}
That is actually everything to know about getting HTML from a raw URL.
But, how do you extract the outlinks from a HTML page?
Many of you will now go ahead and say: let's compile some RegEx. You will FAIL.
As a computer scientist it is enough if you tell that HTML is a context free grammar (chomsky type 2) and RegEx needs a regular language (type 3) to operate properly. Type 2 languages are way more complex and can't be parsed with a regular expression. So please have a look at the funny rage answer at stackoverflow or read up the other very informative answers at the bottom to know why you shouldn't do this. Don't get me wrong: You will find URLs that you can parse with RegEx, but I don't think it is worth the stress. I always use the htmlparser on sourceforge, it is clean, well tested and pretty fast.
To end this post, I tell you how to extract some outlinks from a html page as string:
static final NodeFilter LINK_FILTER = new NodeClassFilter(
LinkTag.class);
Parser parser = new Parser(html);
NodeList matches = parser.extractAllNodesThatMatch(LINK_FILTER);
SimpleNodeIterator it = matches.elements();
while (it.hasMoreNodes()) {
LinkTag node = (LinkTag) it.nextNode();
String link = node.getLink().trim();
// now expand for relative urls and store somewhere
}
It is simple as that. How expanding of URLs can be done is another part- but I leave that up to you ;-) Java's URI may help you with that.
So thanks for attending, my next post is about how to extract actual text content (news) from pure HTML code.
Building a news aggregation engine
Hey all,
first off- happy new year!
It has been a few months now since I posted my last blog post. It wasn't just the stress (open source projects, work, study) that prevented me from writing, but more that I've found no really good topic to blog about.
Personally, what I always found to be interesting are these news aggregation sites. You know them: Google News, Yahoo News etc. They are crawling the web, getting fresh articles about what happens in the world, group them together and present them. Also, they require a lot of machine learning and natural language processing- topics that most of my blogposts are already about.
Since my last posts are more about distributing such algorithms, I want to focus alot more on the application of several algorithms and intentions in order to build up such a news aggregation engine. I know this is a topic where you can write several books about, but I think we can build a working application within three to five blog posts. My personal goal would be to derive a small Java application, that you just start with a seed of a few URLs and it is grouping the incoming pages in realtime- so you can watch the result in an embedded jetty web application by refreshing the site constantly. I currently have some simple parts of it clamped together, but they don't act as a single application so I will rewrite this for the public ;-)
Here is a rough topic grind what we need to cover in order to plug the system together:
first off- happy new year!
It has been a few months now since I posted my last blog post. It wasn't just the stress (open source projects, work, study) that prevented me from writing, but more that I've found no really good topic to blog about.
Personally, what I always found to be interesting are these news aggregation sites. You know them: Google News, Yahoo News etc. They are crawling the web, getting fresh articles about what happens in the world, group them together and present them. Also, they require a lot of machine learning and natural language processing- topics that most of my blogposts are already about.
Since my last posts are more about distributing such algorithms, I want to focus alot more on the application of several algorithms and intentions in order to build up such a news aggregation engine. I know this is a topic where you can write several books about, but I think we can build a working application within three to five blog posts. My personal goal would be to derive a small Java application, that you just start with a seed of a few URLs and it is grouping the incoming pages in realtime- so you can watch the result in an embedded jetty web application by refreshing the site constantly. I currently have some simple parts of it clamped together, but they don't act as a single application so I will rewrite this for the public ;-)
Here is a rough topic grind what we need to cover in order to plug the system together:
- Crawling and extraction of news articles
- Detect text encodings while crawling (otherwise you'll get low quality results)
- Article Classification (you have to detect that a site contains an article)
- Boilerplate Removal (you don't care about the design, so remove it!)
- Page Deduplication (Archieves or mirrors host the same article, we want to sort out those fast)
- Topic modelling of news articles
- Named Entity Recognition (News are about people and events, so we have to detect them)
- Grouping News
- Hierarchical clustering with realtime extension
We will use frameworks for some parts of the task, some coursework from online courses, but the most things are written by myself.
So stay tuned, I will start with basic crawling in the next post and how to detect encodings efficiently.
Oct 6, 2012
Java XOR Swap Performance
Hey,
just quick post, because I was working to polish my min heap and checked about different swap techniques.
From my experience I know there are two different versions to swap two integers in an array.
So basically I'm swapping n-times in an n-size array, where n ranges from ten to 100,000,000 which is really big.
The result isn't very exciting, I used my Nehalem i7 with 3,3GHZ and latest Java7u7:
just quick post, because I was working to polish my min heap and checked about different swap techniques.
From my experience I know there are two different versions to swap two integers in an array.
- XOR swap algorithm
- Swap using temporary variable
Since swapping is used pretty much everywhere, I decided to micro benchmark these against each other using Caliper.
I've seen many people using the XOR algorithm lately, I don't know if they know that this is inefficient. Many people seem to do not care about this, because bitshifting makes them kinda look cool probably?
I've seen many people using the XOR algorithm lately, I don't know if they know that this is inefficient. Many people seem to do not care about this, because bitshifting makes them kinda look cool probably?
However, here is my code so we can sort out the performance of both pretty easy:
public class SwapBenchmark extends SimpleBenchmark {
@Param({ "10", "100", "1000", "10000", "100000", "1000000", "10000000", "100000000" })
private int size;
@Param
SwapType type;
public enum SwapType {
XOR, TMP
};
int[] array;
@Override
protected void setUp() throws Exception {
array = new int[size];
Random r = new Random();
for (int i = 0; i < size; i++) {
array[i] = r.nextInt();
}
}
public void timeSwap(int reps) {
for (int rep = 0; rep < reps; rep++) {
int sum = 0;
for (int i = 0; i < size; i++) {
final int x = i;
final int y = size - i - 1;
if (type == SwapType.XOR) {
array[x] ^= array[y];
array[x] ^= (array[y] ^= array[x]);
sum += i;
} else {
int tmpIndex = array[x];
array[x] = array[y];
array[y] = tmpIndex;
sum += i;
}
}
System.out.println(sum);
}
}
public static void main(String[] args) {
Runner.main(SwapBenchmark.class, args);
}
}
The result isn't very exciting, I used my Nehalem i7 with 3,3GHZ and latest Java7u7:
size type us linear runtime 10 XOR 2,90 = 10 TMP 2,84 = 100 XOR 3,06 = 100 TMP 2,85 = 1000 XOR 3,92 = 1000 TMP 3,52 = 10000 XOR 20,21 = 10000 TMP 14,22 = 100000 XOR 183,33 = 100000 TMP 118,57 = 1000000 XOR 1822,98 = 1000000 TMP 1192,19 = 10000000 XOR 19401,65 == 10000000 TMP 13266,78 == 100000000 XOR 194173,73 ============================== 100000000 TMP 134364,67 ====================
The TMP swap is much more efficient in every case. Why is this?
Wikipedia states the following:
There is actually nothing more to add. So please don't do any pre-mature optimization!
Even if it looks cool to do a bit-shifting trick ;)
Wikipedia states the following:
On modern CPU architectures, the XOR technique is considerably slower than using a temporary variable to do swapping. One reason is that modern CPUs strive to execute instructions in parallel via instruction pipelines. In the XOR technique, the inputs to each operation depend on the results of the previous operation, so they must be executed in strictly sequential order. If efficiency is of tremendous concern, it is advised to test the speeds of both the XOR technique and temporary variable swapping on the target architecture.
There is actually nothing more to add. So please don't do any pre-mature optimization!
Even if it looks cool to do a bit-shifting trick ;)
Sep 1, 2012
Particle Swarm Optimization
Hey there,
I recently started to attend a artificial intelligence lecture (a real course, not online) and we have gotten a small intro to NetLogo yesterday. Although I'm not a real fan of it (mainly because I was tortured with LOGO as a kid, you don't know how painful it is if you already know C++), I liked the idea of plotting agents and scripting all kinds of logic in a compressed way.
So I used to came across an example in their modules library called "Particle Swarm Optimization" (PSO), which was really amazing. I can give you a small picture of it:
| Particle Swarm Optimization plot Source |
You can see a lot of colored particles (or agents) and a terrain that is greyscaled according to its height. Your job is to find a pretty good minimum (whitened areas) in this terrain via swarm intelligence.
A short summary is, that if you have a function and want to minimize the cost by a given parameter set "Theta", you can use the PSO algorithm to find a good minimum of your function. You can compare this for example with Gradient Descent or Conjugate Gradient, but there is a subtle difference between them, for PSO no differentiable function is needed.
Intuition
Of course I will provide you with the basic intuition of this algorithm. So I told that there is a certain number of agents in this algorithm. They are initialized uniformly distributed in our search space, ideally you want to fill the map to get a good spread of your agents so your chance is higher to find a global minimum.
For a fixed number of iterations, you are going to change the position of each particle randomized with a weight on three objectives which are parameters of this meta-optimizer.
- Alpha (in literature also called φp / Phi_personal) is the weight of an agent following its personal memory (based on prior found minimas)
- Beta (in literature also called φg / Phi_global) is the weight of an agent following the global memory of the swarm (based on the currently best minimum from the swarm)
- Phi (in literature also called ω / Omega) is the inertia/velocity of an agent.
From the parameters you can tell that each particle has its own memory (the best personal found solution), also there is a swarm memory, which basically is just a record of the currently best found solution from all particles.
Math
To understand the weighted formula of the movement of the particles, we have to have a look at the formula:
![]() |
| Formula to calculate the new position of a particle, based on the current position "v" |
Algorithm
The algorithm is quite easy, because there is some really good pseudo code on Wikipedia.
However here is my small summary of it:
- Initialize...
- the particle positions to some random positions in our search space
- the memorys of the agents and the swarm memory
- In each iteration until we haven't hit our maximum iterations do...
- loop over all particles
- calculate the new position of a particle
- check the cost of this position, if its smaller than the particles memory then update, do the same update with the global memory if its smaller.
- Profit!
This is a nice and easy algorithm. To further visualize it, I have found a very good youtube video about it, implemented with MatLab: http://www.youtube.com/watch?v=bbbUwyMr1W8
There you can see the movement of the particles very well for a number of functions.
In Java
Of course I have done this in Java with my math library. So let's dive into my normal testcase of the function
Which is a good testcase, because it is convex so it has a global minimum which can be good tested.
A nice looking plot of it:
![]() |
| Convex function f(x,y) = x^2 + y^2 |
Let's sneak into the Java code:
DoubleVector start = new DenseDoubleVector(new double[] { 2, 5 });
// our function is f(x,y) = x^2+y^2
CostFunction inlineFunction = new CostFunction() {
@Override
public Tuple<Double, DoubleVector> evaluateCost(DoubleVector input) {
double cost = Math.pow(input.get(0), 2) + Math.pow(input.get(1), 2);
return new Tuple<Double, DoubleVector>(cost, null);
}
};
DoubleVector theta = ParticleSwarmOptimization.minimizeFunction(
inlineFunction, start, 1000, 0.1, 0.2, 0.4, 100, false);
// retrieve the optimal parameters from theta
We run 1000 particles for 100 iterations, with a weight of 0.1 to the personal best solution, 0.2 to the swarm solution and 0.4 for its own exploration. Basically this was the usage of the algorithm, you can see that we don't need a gradient in this computation (it is nulled). Theta in this case should contain a value close to zero.Of course you can find the source code here:
https://github.com/thomasjungblut/thomasjungblut-common/blob/master/src/de/jungblut/math/minimize/ParticleSwarmOptimization.java
And the testcase above here:
https://github.com/thomasjungblut/thomasjungblut-common/blob/master/test/de/jungblut/math/minimize/ParticleSwarmOptimizationTest.java
And BTW, I just trained a XOR neural network with a single hidden layer with it. This is a very very cool algorithm ;)
A small parameterization tip is to set alpha to 2.8 so it does not fall into local minimas too fast (beta was 0.2 and phi 0.4).
You can have a look how it works in my testcase of the MultiLayerPerceptron:
https://github.com/thomasjungblut/thomasjungblut-common/blob/master/test/de/jungblut/classification/nn/MultiLayerPerceptronTest.java#L40
Aug 4, 2012
Creating a semantic graph
This is a small follow-up post to the similarity aggregation (short SEISA, set expansion by iterative similarity aggregation) post before, it is just a small idea about creating a graph from data collected by this algorithm. So don't expect the master plan how to create a semantic graph from your data, it is just some brainstorming post.
I was very much into semantic relations in the last few months and I think the best way to express them is a graph.
| Semantic graph arround Canon |
You may be reminded of the Knowledge Graph by Google, yes it actually was inspired by it. However it is much more computationally heavy to use it (and even build it) than you might think of it first.
In any case, an algorithm that builds this can be seeded from the SEISA algorithm. Think of a generic starting point that starts in wikipedia and reads the facts about Carl Friedrich Gauss, keywords like "mathematician" will yield to other mathematicians very fast.
Google is (MAYBE) mainly using the semantic annotations to create a relation between the nodes, which is mainly depth first search (because you get a lot of information through Gauß). Whereas the SEISA information could yield to more breadth results like other mathematicians in his century like Riemann or Poincaré.
Creating this semantic graph for (at least) products, brands and other product related features will be my personal ambition for the next few years. Of course Google aims at the most generic way, they have the computational resources to do it for all the web.
Another problem they have is actually how to cut off information to be valuable to the user. Not everybody searching for "Gauß" is interested in his whole life story or other mathematicians, but maybe more into the algorithm for solving equations.
Also understanding queries and parsing the graph accordingly is another interesting new research field.
For example in the above graph arround Canon, what if a user wants to know about other brands LIKE Canon (SQL like for example: SELECT brands LIKE "Canon"). In this case, an engine would traverse the graph breadth first search starting by Canon to find nodes with the attribute "Brand". It would find their brand PIXMA and EOS. Distance is also a very important topic in this relation, how far are the relevant nodes away from their starting point?
Frameworks building such a graph is currently a very important aspect. There are a few helpful ones:
- Graph databases, e.G. Titan https://github.com/thinkaurelius/titan
- Graph only focused processing engines, e.G. Giraph http://giraph.apache.org/
- Of course Hama, to use sophisticated machine learning or mathematical algorithms that can't be expressed as a graph http://hama.apache.org/
- Good old Hadoop MapReduce is of course a help for various data crunchings as well.
Beeing part of that, is one of the things I most enjoy.
We will see how far Google integrates the graph into their search, it will be a driving factor if they manage to query a large scale graph successfully and within milliseconds.
Set Expansion by Iterative Similarity Aggregation
Hey all,
been a very long time since my last post. I was very busy with Apache Hama, we have graduated from the Apache incubator and call us now a top level project and of course made another release 0.5.0. I think this was just the beginning of a rising open source project and I am really excited to be a part of it.
However, responsibility comes with spending a lot of time. Time is always sparse, especially if you are young and have a lot of stuff to do.
So blog posts like this will be very sparse in the future, but today it should not matter and I will tell you about a semi-supervised learning algorithm with the very phonetic name called SEISA.
SEISA itself is a acronym for "Set Expansion by Iterative Similarity Aggregation", not an invention by me rather than a wrap-up and implementation of the paper I found here by Yeye He and Dong Xin.
What is Set Expansion?
Set expansion is used to find similar words to an a priori given list of items in a given "artificial environment". For example you want to find additional digitial camera brands to a given seed set like {Canon, Nikon} and the algorithm should provide you with additional brands like Olympus, Kodak, Sony and/or Leica.
"Artificial environment" is actually a generic term for a context that you have to provide. In the referenced paper it is majorly the internet (crawled pages) and search query logs.
In a real world scenario, set expansion might be used to create a semantic database of related items, improve relevance of search querys, leverage the performance of other named entity recognition algorithms, or maybe also for SEO things (Dear SEOs, I won't help you on that, even if you pay me a lot of $$$).
Data Model
The main idea behind the algorithm is the data modelling. How do we represent this "artificial environment" (will call it context from now on) I talked about above?
The idea is like in any sophisticated algorithm: as a graph! Actually as a so called bipartite graph.
On the left side of this bipartite graph are the so called "candidate terms", the seedset we provide should be a subset of them. The nodes on the right are called the context nodes, they are representing the link between the candidate terms. In the case of the paper, they have crawled the internet for list items and created an edge to a candidate term if it exists in a given list. The neigbours of a context nodes, are the actual list items in this case. In the case of the web, the picture should also contain words that are not digital camera brands, also think about this simple rule: "more data wins". As in every normal graph, you can also weight the edges, for example if you trust the list data from wikipedia more than of craigslist, then you should assign a higher weight the edges between the context nodes and their candidate term nodes.
I have implemented it by a String array and a DoubleMatrix, where the string array is representing the term nodes and the matrix (NxM, where n is the number of entity tokens and m is the number of context nodes) is representing the weight of the edges. In my experience this matrix is always very sparse, even on very small datasets.
Iterative Similarity Aggregation (the algorithm)
Time to describe the algorithm, now we know how we represented the data.
It is rather simply to explain and I don't know why they complicated it so much in their paper.
There are three main methods, first the relevance score computation, the method that filters out newly top ranked items and at last the ranking function.
The relevance score computation is working like this:
been a very long time since my last post. I was very busy with Apache Hama, we have graduated from the Apache incubator and call us now a top level project and of course made another release 0.5.0. I think this was just the beginning of a rising open source project and I am really excited to be a part of it.
However, responsibility comes with spending a lot of time. Time is always sparse, especially if you are young and have a lot of stuff to do.
So blog posts like this will be very sparse in the future, but today it should not matter and I will tell you about a semi-supervised learning algorithm with the very phonetic name called SEISA.
SEISA itself is a acronym for "Set Expansion by Iterative Similarity Aggregation", not an invention by me rather than a wrap-up and implementation of the paper I found here by Yeye He and Dong Xin.
What is Set Expansion?
Set expansion is used to find similar words to an a priori given list of items in a given "artificial environment". For example you want to find additional digitial camera brands to a given seed set like {Canon, Nikon} and the algorithm should provide you with additional brands like Olympus, Kodak, Sony and/or Leica.
"Artificial environment" is actually a generic term for a context that you have to provide. In the referenced paper it is majorly the internet (crawled pages) and search query logs.
In a real world scenario, set expansion might be used to create a semantic database of related items, improve relevance of search querys, leverage the performance of other named entity recognition algorithms, or maybe also for SEO things (Dear SEOs, I won't help you on that, even if you pay me a lot of $$$).
Data Model
The main idea behind the algorithm is the data modelling. How do we represent this "artificial environment" (will call it context from now on) I talked about above?
The idea is like in any sophisticated algorithm: as a graph! Actually as a so called bipartite graph.
| Example of a bipartite graph from list data |
On the left side of this bipartite graph are the so called "candidate terms", the seedset we provide should be a subset of them. The nodes on the right are called the context nodes, they are representing the link between the candidate terms. In the case of the paper, they have crawled the internet for list items and created an edge to a candidate term if it exists in a given list. The neigbours of a context nodes, are the actual list items in this case. In the case of the web, the picture should also contain words that are not digital camera brands, also think about this simple rule: "more data wins". As in every normal graph, you can also weight the edges, for example if you trust the list data from wikipedia more than of craigslist, then you should assign a higher weight the edges between the context nodes and their candidate term nodes.
I have implemented it by a String array and a DoubleMatrix, where the string array is representing the term nodes and the matrix (NxM, where n is the number of entity tokens and m is the number of context nodes) is representing the weight of the edges. In my experience this matrix is always very sparse, even on very small datasets.
Iterative Similarity Aggregation (the algorithm)
Time to describe the algorithm, now we know how we represented the data.
It is rather simply to explain and I don't know why they complicated it so much in their paper.
There are three main methods, first the relevance score computation, the method that filters out newly top ranked items and at last the ranking function.
The relevance score computation is working like this:
- Loop over all the terms in your matrix while looping over all terms in your seedset
- Get the column vector of your seed element and the column vector of the candidate term
- Measure the similarity between both and sum this similarity over all seedset terms, in the paper it is normalized by the length of the seedset, so it is actually the average similarity
What you now get is a vector (length is the number of term nodes you have), and in each index is the average relevance score to the seedset tokens.
Of course you want to pick the highest relevance nodes of this vector, that's where the static thresholding comes in. You pass the algorithm a similarity threshold, this threshold affects how fast the algorithm converges to a certain solution and how much false positives it will have in the end. Of course a higher threshold will yield to less false positives and faster convergence, while the exact opposite will yield to the other extremum. The truth (as always) lies anywhere between, so it is up to you to choose this correctly.
Since the algorithm always converges quite fast, you can experiment with other thresholds and see how it affects your output. Sure we are going to filter by the threshold and get a number of most relevant new candidate nodes.
Now we can enter the main loop and compute the relevance score again for the newly found relevant candidates (this is now called the similarity score). Based on this we are going to rank these two scores with some simple vecor multiplications:
So where does this alpha come from? That is what the paper names as "weighting factor", as you can see it is used to weight the scores. It is mainly used for the convergence of the algorithm, they say, halfing the scores (alpha = 0.5) yields to the best result.
Once we have a ranked scores, we can now filter again by the similarity threshold and get our new candidate terms for the next iteration.
In each iteration we do:
- Compute similarity score by newly found candidate terms (in the beginning this is the seed set)
- Rank the calculated similarity score against the newly found candidate terms
- Apply the static threshold and get new candidate terms
Until it converges: by means the relevant tokens does not change their order anymore- they are equal. Note that you can also equalize on their ranks, I haven't tried it yet but it should converge also quite fast.
At the end you can now optain your expanded set by getting the candidate terms from the last iteration.
Results
Since I'm working in the e-commerce industry, I wanted to try it out to find similar camera brands like in the paper. It works, but it has very noisy result, partly because the algorithm is just iterative and not parallelized thus I can't work on huge datasets and partly because I had no real clean dataset.
But it really worked, I seeded "Canon" and "Olympus" and got all the other camera brands, sadly a few lenses as well.
On colors there are some similar behaviours, I seeded "blue", "red" and "yellow". Funnyly the most relevant token was "X" and "XS", by means that especially t-shirt products have the color in their name. It became very noise afterwards, found Ronaldo jerseys and other fancy clothes, but a bunch of new colors also.
The paper mentions a really nice trick for it: using the internet!
They have collected millions of list items from websites and let the algorithm run, in my opinion in every machine learning algorithm more data wins. The noise on small dataset will be vanished by the sheer size of the data. So I can very much think that they got really good result.
Parallelization strategy
The easiest way to parallelize this is using BSP and Apache Hama (in my opinion).
Let me tell you why:
- Algorithm is strongly iterative
- Needs communication between a master and the slaves
Here is my strategy to implement this in BSP:
- The bipartite graph is split among the tasks, partitioned by the term nodes
- A slave only partially computes the similarity between the seed tokens and the candidate terms
- A master will be used to put all the similarities back into a single vector, rank it and distribute the newly expanded set again as well as checking convergence.
This is pretty much work, so I guess the follow-up post will take a few weeks. But I promise, I will code this algorithm!
So thank you very much for reading, you can find my sequential code on github, as well as the testcase:
Subscribe to:
Posts (Atom)





