Simple feedforward neural network does not behave as intended - algorithm

I'm having a hard time figuring out why my feed forward artificial neural networks almost always fail to learn my simple "OR" perceptron:
0 or 0 : 0
1 or 0 : 1
0 or 1 : 1
1 or 1 : 1
I tried various network configurations : 1 or 2 hidden layers, 2 to 8 neurons on hidden layers, a bias (value = 1, connected to each hidden neuron and output neuron)
For this example, the network looks like:
[1] [3]
[2] [4] [6]
[5]
input layer : [1] and [2]
hidden layer : [3] and [4] and [6]
output layer : [6]
Notations:
Neuron i : [i]
Weight between [i] and [j] : w(i,j)
[i] value : v(i)
[i] error: e(i)
sig(t) : 1/(1 + e^-t)
Weights initialization
Each weight is set to a random value between -0.5 and 0.5
Input
v(1) and v(2) are set with random values : 0 or 1
Propagate value (from left to right) :
v(3) = sig(v(1) * w(1,3) + v(2) * w(2,3))
v(4) = sig(v(1) * w(1,4) + v(2) * w(2,4))
v(5) = sig(v(1) * w(1,5) + v(2) * w(2,5))
v(6) = sig(v(3) * w(3,6) + v(4) * w(4,6) + v(5) * w(5,6))
expected output : 1 if (v(1) or v(2)) is true, 0 otherwise
e(6) = (expected - v(6)) * v(6) * (1 - v(6))
Propagate error (from right to left)
e(3) = e(6) * w(3,6) * v(3) * (1 - v(3))
e(4) = e(6) * w(4,6) * v(4) * (1 - v(4))
e(5) = e(6) * w(5,6) * v(5) * (1 - v(5))
Update weights (learning rate = 1)
w(1,3) = w(1,3) + v(1) * e(3)
w(1,4) = w(1,4) + v(1) * e(4)
w(1,5) = w(1,5) + v(1) * e(5)
w(2,3) = w(2,3) + v(2) * e(3)
w(2,4) = w(2,4) + v(2) * e(4)
w(2,5) = w(2,5) + v(2) * e(5)
w(3,6) = w(3,6) + v(3) * e(6)
w(4,6) = w(4,6) + v(4) * e(6)
w(5,6) = w(5,6) + v(5) * e(6)
Do it 300 times and print each result:
if v(6) > 0.5, it’s a True
if v(6) < 0.5, it’s a False
compare with expected output
After a few epoch, the network almost always return v(6) > 0.9, even for v(1) = v(2) = 0
Sometimes (roughly 1 out of 20 times), it works, the network learned correctly
What am I doing wrong ?
EDIT:
I found it !
The culprits was :
e(3) = e(6) * w(3,6) * v(3) * (1 - v(3))
e(4) = e(6) * w(4,6) * v(4) * (1 - v(4))
e(5) = e(6) * w(5,6) * v(5) * (1 - v(5))
Which should have been :
e(3) = e(6) * w(3,6)
e(4) = e(6) * w(4,6)
e(5) = e(6) * w(5,6)

Related

PCL transformation error about Quaternion to matrix4f

I use these camera extrinsics parameters to transform .ply file through PCL, but the result is not correct. I think it is because of the formula is not correct from Quaternion to matrix4f.
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
master
Eigen::Matrix4f transform_1 = Eigen::Matrix4f::Identity();
qw = 0.980613;
qx = -0.0777902;
qy = -0.176786;
qz = -0.0330758,
tx = -0.798112;
ty = -0.774293;
tz = 3.76053;
transform_1 (0, 0) = 1 - 2 * pow(qy, 2) - 2 * pow(qz, 2);
transform_1 (0, 1) = 2 * qx*qy - 2 * qz*qw;
transform_1 (0, 2) = 2 * qx*qz + 2 * qy*qw;
transform_1 (0, 3) = tx;
transform_1 (1, 0) = 2 * qx*qy + 2 * qz*qw;
transform_1 (1, 1) = 1 - 2 * pow(qx,2) - 2 * pow(qz,2);
transform_1 (1, 2) = 2 * qy*qz - 2 * qx*qw;
transform_1 (1, 3) = ty;
transform_1 (2, 0) = 2 * qx*qz - 2 * qy*qw;
transform_1 (2, 1) = 2 * qy*qz + 2 * qx*qw;
transform_1 (2, 2) = 1 - 2 * pow(qx,2) - 2 * pow(qy,2);
transform_1 (2, 3) = tz;
transform_1(3, 0) = 0;
transform_1(3, 1) = 0;
transform_1(3, 2) = 0;
transform_1(3, 3) = 1;
sub01
Eigen::Matrix4f transform_2 = Eigen::Matrix4f::Identity();
qw = 0.861117;
qx = -0.0716478;
qy = 0.427619;
qz = 0.265493,
tx = -2.94326;
ty = -1.91445;
tz = 6.074;
transform_2(0, 0) = 1 - 2 * pow(qy, 2) - 2 * pow(qz, 2);
transform_2(0, 1) = 2 * qx*qy - 2 * qz*qw;
transform_2(0, 2) = 2 * qx*qz + 2 * qy*qw;
transform_2(0, 3) = tx;
transform_2(1, 0) = 2 * qx*qy + 2 * qz*qw;
transform_2(1, 1) = 1 - 2 * pow(qx, 2) - 2 * pow(qz, 2);
transform_2(1, 2) = 2 * qy*qz - 2 * qx*qw;
transform_2(1, 3) = ty;
transform_2(2, 0) = 2 * qx*qz - 2 * qy*qw;
transform_2(2, 1) = 2 * qy*qz + 2 * qx*qw;
transform_2(2, 2) = 1 - 2 * pow(qx, 2) - 2 * pow(qy, 2);
transform_2(2, 3) = tz;
transform_2(3, 0) = 0;
transform_2(3, 1) = 0;
transform_2(3, 2) = 0;
transform_2(3, 3) = 1;
I use cloudcompare also got the same result

How to make out Reverse triangle and pyramid in Ruby using for loop?

I want to print triangle & pyramid "*" using for loop.
can somebody help me on this?
Output like :
*****
****
***
**
*
and
*
**
***
****
*****
1: Print triangle using While loop
n = 5
while n >= 1
puts "* " * n
n = n - 1
end
* * * * *
* * * *
* * *
* *
*
n = 1
while n <= 5
puts ("* " * n).rjust(10)
n += 1
end
*
* *
* * *
* * * *
* * * * *
2: Print pyramid using loop
n = 4 # Set number of rows
i = 1
1.upto(n) do
print ' ' * n
print '*' * (2 * i - 1)
print "\n"
n -= 1
i += 1
end
*
***
*****
*******
5.downto(1).each{|n| puts ("*" * n).ljust(5)}
1.upto(5).each{|n| puts ("*" * n).rjust(5)}

Find ratio/algorithm to translate different coordinate systems?

So I have two different coordinate systems, one is the incoming data and the other is how i plot points in the app. Is there a formula that, given some aligned up points, will give me an algorithm to calculate future points? Eg:
X = 6.5 & Y = 2 on one system is == X = 3101 & Y = 3441 in the other system.
Another set is 6.45/2.37 = 3211/3414. From these two matchups, how can I calculate a formula to "translate" one system into the other?
You need to solve two equations with two unknowns for X, and then the same for Y.
Let's go through this. First we do X:
3101 = 6.5 * a + b
3211 = 6.45 * a + b
First we isolate b:
3101 - 6.5 * a = b
3211 - 6.45 * a = b
Which gives:
3101 - 6.5 * a = 3211 - 6.45 * a
Now we can solve for a:
3101 - 6.5 * a - 3211 + 6.45 * a = 0
-110 -0.05 * a = 0
-110 = 0.05 * a
-2200 = a
Given this a we can solve for b using either of the original two equations:
3101 - 6.5 * a = b
3101 - 6.5 * -2200 = b
3101 + 14300 = b
17401 = b
b = 17401
So for X we have this formula:
X2 = -2200 * X1 + 17401
Let's try X1 = 6.5 and X1 = 6.45:
X2 = -2200 * 6.5 + 17401
X2 = -14300 + 17401
X2 = 3101
X2 = -2200 * 6.45 + 17401
X2 = -14190 + 17401
X2 = 3211
Doing the exact same math for Y gives me that the relationship follows this formula:
Y2 = Y1 * A + B
3441 = 2 * A + B 3414 = 2.37 * A + B
3441 - 2 * A = B 3414 - 2.37 * A = B
3441 - 2 * A = 3414 - 2.37 * A
3441 - 2 * A - 3414 + 2.37 * A = 0
27 + 0.37 * A = 0
0.37 * A = -27
0.37 * A = -27
A = -72.973
3441 = 2 * A + B
3441 = 2 * -72.973 + B
3441 - 2 * -72.973 = B
3441 + 145.946 = B
3586.946 = B
B = 3586.946
Which gives:
Y2 = Y1 * -72.973 + 3586.946
Let's test:
Y2 = 2 * -72.973 + 3586.946
Y2 = -145.946 + 3586.946
Y2 = 3441
Y2 = 2.37 * -72.973 + 3586.946
Y2 = -172.946 + 3586.946
Y2 = 3414
Your translation formulas are this:
X2 = X1 * -2200 + 17401
Y2 = Y1 * 72.973 + 3586.946
what you want is called linear interpolation, rule of three, etc

splitting trapezoid in given proportion

I need to split trapezoid in 2 part of given size with line, parallel basement. I need to get new h1 of new trapezoid.
For example I have trapezoid of area S and I want to split it in 2 trapezoids of areas S1 and S2.
S1 = aS; S2 = (1-a)S;
S1 = (a+z)*(h1)/2;
S2 = (b+z)*(1-h1)/2;
S1/S2 = KS;
To get new h1 I compare a and b, if a != b, I solve square equation and if a == b I work like with square. But sometimes I get mistakes because of rounding (for example when I solve this analytically I get a = b and program thinks a > b). How can I handle this? Or maybe there is another better way to split trapezoid?
Here is simplifyed code:
if (base > base_prev) {
b_t = base; // base of trapezoid
h = H; //height of trapezoid
a_t = base_prev; //another base of trapezoid
KS = S1 / S2;
a_x = (a_t - b_t) * (1 + KS) / h;
b_x = 2 * KS * b_t + 2 * b_t;
c_x = -(a_t * h + b_t * h);
h_tmp = (-b_x + sqrt(b_x * b_x - 4 * a_x * c_x)) / (2 * a_x);
if (h_tmp > h || h_tmp < 0)
h_tmp = (-b_x - sqrt(b_x * b_x - 4 * a_x * c_x)) / (2 * a_x);
} else if (base < base_prev) {
b_t = base_prev;
a_t = base;
KS = S1 / S2;
a_x = (a_t - b_t) * (1 + KS) / h;
b_x = 2 * KS * b_t + 2 * b_t;
c_x = -(a_t * h + b_t * h);
h_tmp = (-b_x + sqrt(b_x * b_x - 4 * a_x * c_x)) / (2 * a_x);
if (h_tmp > h || h_tmp < 0)
h_tmp = (-b_x - sqrt(b_x * b_x - 4 * a_x * c_x)) / (2 * a_x);
}
else {
KS = S1 / S2;
h_tmp = h * KS;
}
If you're dealing with catastrophic cancellation, one approach, dating back to a classic article by Forsythe, is to use the alternative solution form x = 2c/(-b -+ sqrt(b^2 - 4ac)) for the quadratic equation ax^2 + bx + c = 0. One way to write the two roots, good for b < 0, is
x = (-b + sqrt(b^2 - 4ac))/(2a)
x = 2c/(-b + sqrt(b^2 - 4ac)),
and another, good for b >= 0, is
x = 2c/(-b - sqrt(b^2 - 4ac))
x = (-b - sqrt(b^2 - 4ac))/(2a).
Alternatively, you could use the bisection method to obtain a reasonably good guess and polish it with Newton's method.

Circle-Circle Collision Prediction

I'm aware of how to check if two circles are intersecting one another. However, sometimes the circles move too fast and end up avoiding collision on the next frame.
My current solution to the problem is to check circle-circle collision an arbitrary amount of times between the previous position and it's current position.
Is there a mathematical way to find the time it takes for the two circle to collide? If I was able to get that time value, I could move the circle to the position at that time and then collide them at that point.
Edit: Constant Velocity
I'm assuming the motion of the circles is linear. Let's say the position of circle A's centre is given by the vector equation Ca = Oa + t*Da where
Ca = (Cax, Cay) is the current position
Oa = (Oax, Oay) is the starting position
t is the elapsed time
Da = (Dax, Day) is the displacement per unit of time (velocity).
Likewise for circle B's centre: Cb = Ob + t*Db.
Then you want to find t such that ||Ca - Cb|| = (ra + rb) where ra and rb are the radii of circles A and B respectively.
Squaring both sides:
||Ca-Cb||^2 = (ra+rb)^2
and expanding:
(Oax + t*Dax - Obx - t*Dbx)^2 + (Oay + t*Day - Oby - t*Dby)^2 = (ra + rb)^2
From that you should get a quadratic polynomial that you can solve for t (if such a t exists).
Here is a way to solve for t the equation in Andrew Durward's excellent answer.
To just plug in values one can skip to the bottom.
(Oax + t*Dax - Obx - t*Dbx)^2 + (Oay + t*Day - Oby - t*Dby)^2 = (ra + rb)^2
(Oax * (Oax + t*Dax - Obx - t*Dbx) + t*Dax * (Oax + t*Dax - Obx - t*Dbx)
- Obx * (Oax + t*Dax - Obx - t*Dbx) - t*Dbx * (Oax + t*Dax - Obx - t*Dbx))
+
(Oay * (Oay + t*Day - Oby - t*Dby) + t*Day * (Oay + t*Day - Oby - t*Dby)
- Oby * (Oay + t*Day - Oby - t*Dby) - t*Dby * (Oay + t*Day - Oby - t*Dby))
=
(ra + rb)^2
Oax^2 + (Oax * t*Dax) - (Oax * Obx) - (Oax * t*Dbx)
+ (t*Dax * Oax) + (t*Dax)^2 - (t*Dax * Obx) - (t*Dax * t*Dbx)
- (Obx * Oax) - (Obx * t*Dax) + Obx^2 + (Obx * t*Dbx)
- (t*Dbx * Oax) - (t*Dbx * t*Dax) + (t*Dbx * Obx) + (t*Dbx)^2
+
Oay^2 + (Oay * t*Day) - (Oay * Oby) - (Oay * t*Dby)
+ (t*Day * Oay) + (t*Day)^2 - (t*Day * Oby) - (t*Day * t*Dby)
- (Oby * Oay) - (Oby * t*Day) + Oby^2 + (Oby * t*Dby)
- (t*Dby * Oay) - (t*Dby * t*Day) + (t*Dby * Oby) + (t*Dby)^2
=
(ra + rb)^2
t^2 * (Dax^2 + Dbx^2 - (Dax * Dbx) - (Dbx * Dax)
+ Day^2 + Dby^2 - (Day * Dby) - (Dby * Day))
+
t * ((Oax * Dax) - (Oax * Dbx) + (Dax * Oax) - (Dax * Obx)
- (Obx * Dax) + (Obx * Dbx) - (Dbx * Oax) + (Dbx * Obx)
+ (Oay * Day) - (Oay * Dby) + (Day * Oay) - (Day * Oby)
- (Oby * Day) + (Oby * Dby) - (Dby * Oay) + (Dby * Oby))
+
Oax^2 - (Oax * Obx) - (Obx * Oax) + Obx^2
+ Oay^2 - (Oay * Oby) - (Oby * Oay) + Oby^2 - (ra + rb)^2
=
0
Now it's a standard form quadratic equation:
ax2 + bx + c = 0
solved like this:
x = (−b ± sqrt(b^2 - 4ac)) / 2a // this x here is t
where--
a = Dax^2 + Dbx^2 + Day^2 + Dby^2 - (2 * Dax * Dbx) - (2 * Day * Dby)
b = (2 * Oax * Dax) - (2 * Oax * Dbx) - (2 * Obx * Dax) + (2 * Obx * Dbx)
+ (2 * Oay * Day) - (2 * Oay * Dby) - (2 * Oby * Day) + (2 * Oby * Dby)
c = Oax^2 + Obx^2 + Oay^2 + Oby^2
- (2 * Oax * Obx) - (2 * Oay * Oby) - (ra + rb)^2
t exists (collision will occur) if--
(a != 0) && (b^2 >= 4ac)
You can predict collision by using direction vector and speed, this gives you the next steps, and when they will make a collision (if there will be).
You just need to check line crossing algorithm to detect that...

Resources