tiny-dnn example breaks on training, when using 2 classes instead of 10 - tiny-dnn

I'm very new to tiny-dnn. This example works fine with my data:
net << convolutional_layer(32, 32, 5, 1, 6) << tanh() // 32x32in, conv5x5
<< average_pooling_layer(28, 28, 6, 2) << tanh() // 28x28in, pool2x2
<< fully_connected_layer(14 * 14 * 6, 120) << tanh()
<< fully_connected_layer(120, 10);
When I try to reduce the output to my actual number of classes 2 - the example breaks. It is the oly change between working and not working code - 10 changed by 2. Is 2 a special number for classes?

I've found th reason. I've enumerated the classes starting from 1 - 1,2. There is an assertion checking the number of classes. The number of classes should be greater then the last class id.
After renaming the class IDs to 0,1 instead of 1,2 everything works.

Related

How to print more than one value in the same line?

A simple example
Print("This is a number {0}", 1) // results in 4
How can I print 1 2 3 in the same line?
I have tried
Print(1, 2, 3) // Does not work
The forloop also does not work.
The while loop below prints the elements but each in a separate line since we do not have control over the line feed character \n:
fn Main() -> i32 {
var a: [i32;3] = (1, 2, 3); // Define array containing the numbers 1,2,3
var i:i32 =0;
while(i<3){
Print("{0}", a[i]);
i= i+1;
}
return 0;
}
which results in
1
2
3
here is the code
How can I get 1 2 3?
Short explanation: Providing more than 2 arguments to Print is currently not supported, so is not including a line break.
Details
This is definitely something that will change in the future, as the language is in its early design phases. You can find the source for Print as an instrinsic (non-carbon native) here: https://github.com/carbon-language/carbon-lang/blob/trunk/explorer/interpreter/type_checker.cpp#L2297
case IntrinsicExpression::Intrinsic::Print:
// TODO: Remove Print special casing once we have variadics or
// overloads. Here, that's the name Print instead of __intrinsic_print
// in errors.
if (args.size() < 1 || args.size() > 2) {
return CompilationError(e->source_loc())
<< "Print takes 1 or 2 arguments, received " << args.size();
}
CARBON_RETURN_IF_ERROR(ExpectExactType(
e->source_loc(), "Print argument 0", arena_->New<StringType>(),
&args[0]->static_type(), impl_scope));
if (args.size() >= 2) {
CARBON_RETURN_IF_ERROR(ExpectExactType(
e->source_loc(), "Print argument 1", arena_->New<IntType>(),
&args[1]->static_type(), impl_scope));
}
If you look at the code you will also see that the type of input variable is limited to just integer types. It would be easy enough create a PR to manually add support for 2-3 if you want :)

Generate 5 Unique Random Numbers in min and max range and sort in Ascending Order in Groovy

I am new to this group and this is my first post.
Can some one please help me to generate 5 Unique numbers in min and max range and then sort them in ascending order using groovy.
note: I am doing this in soapui.
import org.apache.commons.lang.RandomStringUtils
import java.util.Random
Random r = new Random();
def i
List random_num
for (i=1;i<=5;i++)
{
random_num(i) = r.nextInt(9+2)
if(i>0)
{
if(random_num[i]==random_num[i-1])
random_num[i] = r.nextInt(9+2)
}
}
random_num.sort() ;
log.info random_num
thank you
The part about rand between a min and max can be found
here.
For the other constraints:
Unique values: use a Set for your result
Ascending order: use a sorted Set
E.g.:
import java.util.concurrent.ThreadLocalRandom
def n = 5
def min = 5
def max = 42
def result = new TreeSet()
while (result.size()<n) {
result << ThreadLocalRandom.current().nextInt(min, max + 1)
}
println result
// → [7, 8, 29, 37, 42]
Note: make sure max - min > n
Your code is very Java-ish. Groovy was developed, partly to eliminate a lot of the Java ceremony.
This is what I use to generate a random number:
def randNum(minSize, maxSize) {
return Math.abs(new Random().nextInt(maxSize + 1 - minSize) + minSize)
}
You can call that five times, collect the results in a list, and sort the list. To call a routine, you can use:
5.times {randNum(minSize, maxSize)}

Problem in implementing Persistent Segment Tree

I am trying to implement Persistent Segment Tree. The queries are of 2 types: 1 and 2.
1 ind val : update the value at ind to val in the array
2 k l r : find the sum of elements from index l to r after the kth update operation.
I have implemented the update and query functions properly and they are working fine on an array. But the problem arises when I am forming different versions. Basically this is my part of code
while (q--) {
cin >> type;
if (type == 1) {
cin >> ind >> val;
node *t = new node;
*t = *ver[size - 1];
update(t, ind, val);
ver.pb(t);
size++;
}
}
cout << query(ver[0], 0, 1) << ' ' << query(ver[1], 0, 1) << query(ver[2], 0, 1);
Now the problem is it is also changing the parameters for the all the node is the array. That means after 3 updates all the versions are storing the latest tree. This is probably because I am not properly allocating the new pointer. The changes made to the new pointer are getting reflected in all the pointers in the array
For example if I give this input
5
1 2 3 4 5
2
1 1 10
1 0 5
where 5 is the number of elements in the array and following is the array. Then there is q, number of queries and then all the queries. After carrying out the update the value of query function called for (l, r) = (0, 1) for all the 3 versions are 15. But it should be 3, 11, 15. What am I doing wrong
So let's say we have some simple segment tree like this:
For Persistant segment tree, during update we generate new nodes for all changed nodes and replace pointers to new nodes where needed, so let's say we update node 4, then we get a persistent segment tree like this (new nodes marked with *):
And all you're doing is replacing the root and copying all data so you get something like this:

C++ i am confuse on how to write formula for code c and I

Write a program that displays water bills. Your program should prompt the user to enter an integer account number, a character use code, and a real number representing the gallons of water used. The output from your program should include the account number, message indicating the type of usage, and the amount of money due from the user. Draw a flowchart for your program.
The water rates vary depending on the type of usage. A code of H means home use, a code of C means commercial use, and a code of I means industrial use. Any other code value should be treated as an error. Water rates are computed as follows:
Code H: $5.00 plus $0.005 per gallon
Code C: $1000.00 for the first 4 million gallons used plus
$0.025 for each additional gallon used
Code I: $1000.00 if usage does not exceed 4 million gallons
$2000.00 is usage is between 4 million and 10 million gallons
$3000.00 is 10 million gallons or more
cout << " Enter your account number" << endl;
cin >> account;
cout << " Enter your code (h, c or i): h means home, c means commercial use and i means industrial use" << endl;
cin >> code;
cout << " Enter the number of gallons of water used" << endl;
cin >> gallon;
return 0;
}
void company() {
switch (code) {
case 'H': case 'h':
total = (0.0005*gallon) + 5;
cout, , " your total gallons of water is" << endl;
break;
case 'C': case 'c':
case 'I': case 'i':
This is simple translation of the human readable text.
for code c, :
total = 1000; // 1000 dollars in any case
int remaining_gallons_to_pay = gallon - 4000000; // the first 4 million are already paid for
if(remaining_gallons_to_pay > 0) { // maybe they used less than 4 million, if so, their problem
total = total + (remaining_gallons_to_pay * 0.025);
}
break;
for code i:
if (gallon <= 4000000) { // 'does not exceed' means less or equal
total = 1000;
}
else if (gallon <= 10000000) { // cannot be 4 million or less, already checked, but "between 4 million and 10 million", is this inclusive 10 million? I assumed so for this statement...
total = 2000;
}
else { // so, it must be over 10 million
total = 3000;
}
break;
As you are using the switch ... case statement, you have another option to your disposal: default. All cases you have not dealt with - and ended with break - land here. So you are left with adding
default : cout << "Code error"
into your switch-block.

Converting 8 bit color into RGB value

I'm implementing global illumination in my game engine with "reflective shadow maps". RSM has i.a. color texture. To save memory. I'm packing 24 bit value into 8 bit value. Ok. I know how to pack it. But how do I unpack it? I had idea to create a 1D texture with 8 bit palette, with 255 different colors. My 8 bit color would be index of pixel in that texture.
I'm not sure how to generate this kind of texture.
Are there any mathematical ways to convert 8 bit value into rgb?
#edit
The color is in this format:
RRR GGG BB
#edit2:
And I'm packing my colour like this:
int packed = (red / 32 << 5) + (green / 32 << 2) + (blue / 64);
//the int is actually a byte, c# compiler is bitching if it's byte.
#edit3:
Alright, I found a way to do this I think. Tell me if it's wrong.
#edit4 It's wrong...
int r = (packed >> 5) * 32;
int g = ((packed >> 2) << 3) * 32;
int b = (packed << 6) * 64;
In javascript
Encode
encodedData = (Math.floor((red / 32)) << 5) + (Math.floor((green / 32)) << 2) + Math.floor((blue / 64));
Decode
red = (encodedData >> 5) * 32;
green = ((encodedData & 28) >> 2) * 32;
blue = (encodedData & 3) * 64;
While decoding we are using AND Gate/Operator to extract desired bits and discard leading bits. With green, we would then have to shift right to discard bits at right.
While encoding Math.floor is used to truncate decimal part, if rounded off it would create total value greater than 255 making it a 9 bit number.
UPDATE 1
It does not provide accurate results if we divide color by 32 or 64.
RRRGGGBB
R/G = 3bit, max value is 111 in binary which is 7 in decimal.
B = 2bit, max value is 11 in binary which is 3 in decimal.
We should divide R/G by value equal or greater than 255/7 and B by value equal or greater than 255/3.
We should also note that in place of Math.floor we should use Math.round because rounding off gives more accurate results.
To convert 8bit [0 - 255] value into 3bit [0, 7], the 0 is not a problem, but remember 255 should be converted to 7, so the formula should be Red3 = Red8 * 7 / 255.
To convert 24bit color into 8bit,
8bit Color = (Red * 7 / 255) << 5 + (Green * 7 / 255) << 2 + (Blue * 3 / 255)
To reverse,
Red = (Color >> 5) * 255 / 7
Green = ((Color >> 2) & 0x07) * 255 / 7
Blue = (Color & 0x03) * 255 / 3

Resources