I have been trying to understand operator precedence but got stuck in this question - operator-precedence

I have been trying to understand operator precedence but got stuck in this question.Here is the code:
int i=1;
int a= ++i + ++i + ++i * i++ + i++;
When I calculate using pen and paper, I am getting 26 as the value of a (2+3+4*4+5 => 2+3+16+5 = 26).But when I compile it I am getting 31 as the output.
Could somebody please help me with this.

Related

Tackling imbalanced class members in Caffe: weight contribution of each instance to loss value

I have a highly imbalanced data, I know that some users suggesting using InfoGainLoss loss function, however, I am facing few errors when I tried to add this function to Caffe layers.
I have the following questions, I really appreciate if someone guides me:
How can I add this layer to Caffe? Does anyone know any sources/ codes of this layer?
I want to apply it for image segmentation and the proportion of some classes varies. How can I create the H matrix (a stack of weights) for my images? And how infoGainLoss layer can read a specific weight matrix (H) related to that specific image?
After adding the cpp and cu version of InforGainLoss layer to caffe, should I remake Caffe?
I am sorry for few question, but all are my concern and related to each other. I will be thankful to get some help and support.
Thanks
1.If you copy from current infogain_loss_layer.cpp you can easily adapt. For forward pass change line 59-66 like:
// assuming num = batch size, dim = label size, image_dim = image height * width
Dtype loss = 0;
for (int i = 0; i < num; ++i) {
for(int k = 0; k < image_dim; k++) {
int label = static_cast<int>(bottom_label[i*image_dim+k]);
for (int j = 0; j < dim; ++j) {
Dtype prob = std::max(bottom_data[i *image_dim *dim+ k * dim + j], Dtype(kLOG_THRESHOLD));
loss -= infogain_mat[label * dim + j] * log(prob);
}
}
}
Similarly for backward pass you could change line 95-101 like:
for (int i = 0; i < num; ++i) {
for(int k = 0; k < image_dim; k++) {
const int label = static_cast<int>(bottom_label[i*image_dim+k]);
for (int j = 0; j < dim; ++j) {
Dtype prob = std::max(bottom_data[i *image_dim *dim+ k * dim + j], Dtype(kLOG_THRESHOLD));
bottom_diff[i *image_dim *dim+ k * dim + j] = scale * infogain_mat[label * dim + j] / prob;
}
}
}
This is kind of naive. I don't seem to find any option for optimization. You will also need to change some setup code in reshape.
2.In this PR suggestion is that for diagonal entries in H put min_count/|i| where |i| is the number of samples has label i. Everything else as 0. Also see this . As for loading the weight matrix H is fixed for all input. You can load it as lmdb file or in other ways.
3.Yes you will need to rebuild.
Update:
As Shai pointed out the infogain pull for this has already been approved this week. So current version of caffe supports pixelwise infogain loss.

Add iterations to loop AS3

Hi something simple is annoying me and I'd like some help!
function highlight(textField:TextField):void
{
var l:int = textField.text.length
for(var i:int = 0; i < l; i++)
if (!highlightChar(textField, i))
l++;
}
This loops through a character string to add a box behind the character for a highlighter affect. Some of the characters fail (the bounding box is null, assume these are returns etc) and in the example I'm looking at it returns false 5 times, and the boxes are 5 characters short. I'm attempting to add another iteration when it fails to keep going for another 5 characters, but this loop never stops.
Is there another way of doing this?
Hacky solution - collecting the fail count and then doing another loop after the first one finished has fixed it
if(fails > 0)
for(var f:int = i; f < fails + i; f++){
var box:Shape = highlightChar(textField, f);
if(box) boxes.addChild(box)
}

CUDA run lead to display drive stop

I wrote a function. When I run it in the cpu I can get the right result. The part of cpu code is:
for(int x = startx; x < endx; x+=SampleStep)
for(int y = starty; y < endy; y+=SampleMin)
{
int idoff = Width;
Then I port it to the GPU, like this:
int x = threadIdx.x + blockIdx.x * blockDim.x + startx;
int y = threadIdx.y + blockIdx.y * blockDim.y + starty;
int idoff = blockDim.x * gridDim.x;
when I run the code, the black screen happened and then recovered after a little while. At the same time, the system showed the message like: Display drive stopped responding.
and the cuda event time output cost time is 0ms, the result is wrong.
for (int k = CircleBegin; k < CircleEnd; k++)
{
bool Isright = (k-ww>=0) && (k+ww<Width);
if (Isright)
{
float AverR = 0;
for (int i = -ww; i <= ww; i++)
{
for (int j = -wh; j <= wh; j++)
{
AverR += ImgR[(k+i)+(y+j)*idoff];
}
}
when I comment the AverR += ImgR[(k+i)+(y+j)*idoff]; The code can run without black screen. I want to know why. Is this related with my display device (my device is nvida gt 240) or is there some access violation happened? how can I solve this problem?
Your screen is turning black because you are hitting the windows TDR event. For further description of this and possible solutions, see my answer here.
Since you have nested for-loops, and you haven't told us the size of the data set, it's certainly possible that your code is taking too long to execute, if your for-loops are operating over a large enough range.
When you comment out that line of code, the compiler can completely optimize away the loops, and so that section of code would take essentially zero time to run. As a result, your kenel is no longer taking too long and so you don't hit the TDR event.
There's no reason based on any of the above to assume an access violation is occurring. In fact, I would say it is unlikely because an access violation will often cause an unspecified launch failure, which will terminate a running kernel.
So you'll need to investigate some of the ideas I mentioned in the answer I linked above.

thrust::unique_by_key eating up last element

Please consider the below simple code:
thrust::device_vector<int> positions(6);
thrust::sequence(positions.begin(), positions.end());
thrust::pair<thrust::device_vector<int>::iterator, thrust::device_vector<int>::iterator > end;
//copyListOfNgramCounteachdoc contains: 0,1,1,1,1,3
end.first = copyListOfNgramCounteachdoc.begin();
end.second = positions.begin();
for(int i =0 ; i < numDocs; i++){
end= thrust::unique_by_key(end.first, end.first + 3,end.second);
}
int length = end.first - copyListOfNgramCounteachdoc.begin() ;
cout<<"the value of end -s is: "<<length;
for(int i =0 ; i< length ; i++){
cout<<copyListOfNgramCounteachdoc[i];
}
I expected the output to be 0,1,1,3 of this code; however, the output is 0,1,1. Can anyone let me know what I am missing? Note: the contents of copyListOfNgramCounteachdoc is 0,1,1,1,1,3 . Also the type of copyListOfNgramCounteachdoc is thrust::device_vector<int>.
EDIT:
end.first = storeNcCounts.begin();
end.second = storeCompactedPositions.begin();
int indexToWriteForIndexesarr = 0;
for(int i =0 ; i < numDocs; i++){
iter = end.first;
end = thrust::unique_by_key_copy(copyListOfNgramCounteachdoc.begin() + (i*numUniqueNgrams), copyListOfNgramCounteachdoc.begin()+(i*numUniqueNgrams)+ numUniqueNgrams,positions.begin() + (i*numUniqueNgrams),end.first,end.second);
int numElementsCopied = (end.first - iter);
endIndex = beginIndex + numElementsCopied - 1;
storeBeginIndexEndIndexSCNCtoRead[indexToWriteForIndexesarr++] = beginIndex;
storeBeginIndexEndIndexSCNCtoRead[indexToWriteForIndexesarr++] = endIndex;
beginIndex = endIndex + 1;
}
I think what you want to use in this case is thrust::unique_by_key_copy, but read on.
The problem is that unique_by_key is not updating your input array unless it has to. In the case of the first call, it can return a sequence of unique keys by just dropping the duplicate 1 -- by moving the returned iterator forward, without actually compacting the input array.
You can see what is happening if you replace your loop with this one:
end.first = copyListOfNgramCounteachdoc.begin();
end.second = positions.begin();
thrust::device_vector<int>::iterator iter;
for(int i =0 ; i < numDocs; i++){
cout <<"before ";
for(iter = end.first; iter != end.first+3; iter++) cout<<*iter;
end = thrust::unique_by_key(end.first, end.first + 3,end.second);
cout <<" after ";
for(iter = copyListOfNgramCounteachdoc.begin(); iter != end.first; iter++) cout<<*iter;
cout << endl;
for(int i =0 ; i< 6; i++) cout<<copyListOfNgramCounteachdoc[i];
cout << endl;
}
For this code I get this output:
before 011 after 01
011223
before 122 after 0112
011223
You can see that the values in copyListofNgramCounteachdoc are not changing. This is valid behavior. If you had used unique_by_key_copy instead of unique_by_key then Thrust would have been forced to actually compact the values in order to guarantee uniqueness, but in this case since there are only two values in each sequence, there is no need. The docs say:
The return value is an iterator new_last such that no two consecutive elements in the range [first, new_last) are equal. The iterators in the range [new_last, last) are all still dereferenceable, but the elements that they point to are unspecified. unique is stable, meaning that the relative order of elements that are not removed is unchanged.
If you use unique_by_key_copy, then Thrust will be forced to copy the unique keys and values (with obvious cost implications), and you should see the behavior you were expecting.
BTW, if you can do this in a single call to unique_by_key rather than doing them in a loop, I suggest that you do so.

OCR: weighted Levenshtein distance

I'm trying to create an optical character recognition system with the dictionary.
In fact I don't have an implemented dictionary yet=)
I've heard that there are simple metrics based on Levenstein distance which take in account different distance between different symbols. E.g. 'N' and 'H' are very close to each other and d("THEATRE", "TNEATRE") should be less than d("THEATRE", "TOEATRE") which is impossible using basic Levenstein distance.
Could you help me locating such metric, please.
This might be what you are looking for: http://en.wikipedia.org/wiki/Damerau%E2%80%93Levenshtein_distance (and kindly some working code is included in the link)
Update:
http://nlp.stanford.edu/IR-book/html/htmledition/edit-distance-1.html
Here is an example (C#) where weight of "replace character" operation depends on distance between character codes:
static double WeightedLevenshtein(string b1, string b2) {
b1 = b1.ToUpper();
b2 = b2.ToUpper();
double[,] matrix = new double[b1.Length + 1, b2.Length + 1];
for (int i = 1; i <= b1.Length; i++) {
matrix[i, 0] = i;
}
for (int i = 1; i <= b2.Length; i++) {
matrix[0, i] = i;
}
for (int i = 1; i <= b1.Length; i++) {
for (int j = 1; j <= b2.Length; j++) {
double distance_replace = matrix[(i - 1), (j - 1)];
if (b1[i - 1] != b2[j - 1]) {
// Cost of replace
distance_replace += Math.Abs((float)(b1[i - 1]) - b2[j - 1]) / ('Z'-'A');
}
// Cost of remove = 1
double distance_remove = matrix[(i - 1), j] + 1;
// Cost of add = 1
double distance_add = matrix[i, (j - 1)] + 1;
matrix[i, j] = Math.Min(distance_replace,
Math.Min(distance_add, distance_remove));
}
}
return matrix[b1.Length, b2.Length] ;
}
You see how it works here: http://ideone.com/RblFK
A few years too late but the following python package (with which I am NOT affiliated) allows for arbitrary weighting of all the Levenshtein edit operations and ASCII character mappings etc.
https://github.com/infoscout/weighted-levenshtein
pip install weighted-levenshtein
Also this one (also not affiliated):
https://github.com/luozhouyang/python-string-similarity
I've recently created a python package that does exactly that https://github.com/zas97/ocr_weighted_levenshtein.
In my Weigthed-Levenshtein implementation the distance between "THEATRE" and "TNEATRE" is 1.3 while the distance between "THEATRE" and "TOEATRE" is 1.42.
Other exemples are the d("O", "0") is 0.06 and d("e","c") is 0.57.
This distances have been calculated by running multiple ocrs in a synthetic dataset and doing statistics on the most common ocr errors. I hope it helps someone =)