Razor component instances created in loop - parameters problem - razor

I would like generate instances of component like these:
for (int i = 0; i < 10; i++)
{
<MyComponent Index="#i">
#i
</MyComponent>
}
Problem is that every component instance has in final view the same value of param Index, and this value is 9. It si probably because this is right variables value when render time. Is it possible to pass right value of "i" variable to components? Thanks very much.

Answer is very simple :-D
for (int i = 0; i < 10; i++)
{
int #iHlp = #i;
<MyComponent Index="#iHlp">
#iHlp
</MyComponent>
}

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.

Ask how to give jProgressBar for my iteration

I want to ask how to give jProgressBar in my code. i used netbeans interface. progressBar will end after it reach maximum_iteration.
jProgressBar already in other class. how can i fix it? Thank you. Please help.
train() is placed in other class.
public void train(){
for(int iteration=0; iteration<this.max_iteration; iteration++){
//int percent = (iteration/this.max_iteration) * 100;
//cs.jProgressBar1.setValue(percent);
for(int index=0; index<this.hms; index++){
Node[] newChord = {};
double random = Math.random();
/*process HS*/
if(this.original_chord != null){
// ensure the fist note of the new chord is not altered
newChord[0].notes = new int[first_chord_notes.length];
for(int j=0; j<first_chord_notes.length; j++){
newChord[0].notes[j] = first_chord_notes[j];
}
}
// evaluate new chord and substitute the worst one in hs if new chord is better
double newFitness = this.nodeListFitness(this.original_chord, newChord);
int worstIndex = this.worst_index();
double worstFitness = this.fitness[worstIndex];
if(newFitness > worstFitness){
this.hm[worstIndex] = newChord;
this.fitness[worstIndex] = newFitness;
}
}
System.out.println("Harmony Memory " + (iteration+1));
this.debug();
System.out.println("");
//JProgressBar jProgressBar1 = new JProgressBar();
//cs.jProgressBar1.setValue(iteration);
//cs.jProgressBar1.setEnabled(true);
}
}
I'm not sure at 100% but a Swing Component can be updated only inside the EventDispatcherThread, which is the Thread that run the GUI. For this reason, it will probably be better that you call the method train() inside you GUI, so it run into the EDT. Also, I can't understand why do you call the constructor of the JProgessBar, if you say that it is already in other class, probably the other class (I image it is the GUI) has already created a new instance of the progress bar.

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)
}

Prevent Google App Script Memoization

I have a function that I want to use to demonstrate flipping a coin 100 times:
function simulateFlips(n, pHeads){
var head_count = 0;
var H;
for(var i=0; i < n; i++){
H = 0;
if (Math.random() < pHeads){
H = 1;
}
head_count += H;
}
return head_count;
}
However, it looks like the standard Google App Behavior is to "memoize" custom functions that are called with exactly the same inputs, which is not what you want for this kind of demo:
I know I could do something hacky (like modify pHeads by a very small amount), but I was hoping there was some cleaner way to get the desired behavior.
Here's the explanation of this behavior:
Script to summarise data not updating
Read the comments to see suggestions on how to workaround it.
Pass a second param in that you ignore but which is different for each line.

As3 - Get Movieclips That The Name Start With A Specific String

I'm making a tile based game where ta_*(number)* and ca_*(number)* acts like bins. You drag things towards it and drop. But the level may put several these tiles.
I am not going to make something like:
if (my_mc.hitTestObject(ta_0) || my_mc.hitTestObject(ta_1) || my_mc.hitTestObject(ta_2).........)
Because some may not exist and throw an error at me, and I don't want to make like hundreds of them.
Is there a way to find movieclips on stage that start with the name "ta_" and "ca_"?
So that I can get: ta_1, ta_2.....?
No, you can't. Unless you loop on getChildAt() and check all children's names.
But, why don't you add your bins to an array when creating them?
(I assume you create them dynamically)
var myBinArray:Array = new Array(10);
for (var i:int = 0; i < myBinArray.length; i++)
{
var myBin = new Bin();
myBinArray[i] = myBin;
}
Then you simply loop on your array:
for (var i:int = 0; i < myBinArray.length; i++)
{
if (mybinArray[i] != null)
if (my_mc.hitTestObject(mybinArray[i])
{
// statements
// and here I assume you want to break for loop
}
}