I am writing a neural network based on this guide (https://learn.microsoft.com/en-us/archive/msdn-magazine/2012/october/test-run-neural-network-back-propagation-for-programmers), and no matter what the inputs, desired outputs, or layer sizes are, it always outputs one:
using System;
namespace Neural_Network_Test
{
internal class Program
{
private int numInput;
private int numHidden;
private int numOutput;
private float[,] inputNodes;
private float[,] weightsIH;
private float[,] weightsHO;
private float[,] outputsIH;
private float[,] outputsHO;
private float[,] sumIH;
private float[,] sumHO;
private float[,] biasIH;
private float[,] biasHO;
private float[,] gradientsO;
private float[,] gradientsH;
private float[,] prevWeightsDeltaIH;
private float[,] prevBiasesDeltaIH;
private float[,] prevWeightsDeltaHO;
private float[,] prevBiasesDeltaHO;
private float[,] rawIH;
private float[,] desiredOutputs;
private float eta = 0.9f;
private float momentum = 0.4f;
private int iteration = 0;
private static void Main(string[] args)
{
Program program = new Program();
program.numInput = 3;
program.numHidden = 4;
program.numOutput = 2;
program.desiredOutputs = new float[program.numOutput, 1];
for (int i = 0; i < program.numOutput; i++)
{
Console.Write("Output " + (i + 1).ToString() + ": ");
program.desiredOutputs[i, 0] = Convert.ToInt32(Console.ReadLine());
}
Console.Write("Enter Iterations: ");
int numIterations = 0;
numIterations = Convert.ToInt32(Console.ReadLine());
program.SetMisc();
program.SetNodes();
program.SetWeights();
for (int i = 0; i < numIterations; i++)
{
program.ComputeOutputs();
program.BackPropagation();
program.AddDeltas();
Console.WriteLine("Outputs: ");
for (int j = 0; j < program.numOutput; j++)
{
Console.WriteLine("Output " + (j + 1).ToString() + ": " + program.outputsHO[j, 0].ToString());
}
float cost = 0f;
for (int w = 0; w < program.numOutput; w++)
{
cost += 0.5f * (float)Math.Pow(program.desiredOutputs[w, 0] - program.outputsHO[w, 0], 2);
}
Console.WriteLine("Cost: " + cost.ToString());
program.iteration++;
}
}
private void SetNodes()
{
Random rnd = new Random();
for (int i = 0; i < numInput; i++)
{
inputNodes[i, 0] = (float)rnd.NextDouble();
}
}
private void SetWeights()
{
Random rnd = new Random();
weightsIH = new float[numInput, numHidden];
for(int i = 0; i < numInput; i++)
{
for(int j = 0; j < numHidden; j++)
{
if (rnd.NextDouble() > 0.5)
{
weightsIH[i, j] = (float)rnd.NextDouble();
}
else
{
weightsIH[i, j] = (float)rnd.NextDouble() * -1;
}
weightsIH[i, j] = 1f;
}
}
weightsHO = new float[numHidden, numOutput];
for (int i = 0; i < numHidden; i++)
{
for (int j = 0; j < numOutput; j++)
{
if (rnd.NextDouble() > 0.5)
{
weightsHO[i, j] = (float)rnd.NextDouble();
}
else
{
weightsHO[i, j] = (float)rnd.NextDouble() * -1;
}
weightsHO[i, j] = 1f;
}
}
for(int i = 0; i < numHidden; i++)
{
biasIH[i, 0] = rnd.Next(20) - 10;
}
for (int i = 0; i < numOutput; i++)
{
biasHO[i, 0] = rnd.Next(20) - 10;
}
for(int i = 0; i < numHidden; i++)
{
biasIH[i, 0] = 0;
}
for(int i = 0; i < numOutput; i++)
{
biasHO[i, 0] = 0;
}
weightsIH = new float[numInput, numHidden];
weightsHO = new float[numHidden, numOutput];
float value = 0.1f;
for (int i = 0; i < numInput; i++)
{
for (int j = 0; j < numHidden; j++)
{
weightsIH[i, j] = (float)Math.Round(value, 2);
value += 0.1f;
}
}
for (int i = 0; i < numHidden; i++)
{
for (int j = 0; j < numOutput; j++)
{
weightsHO[i, j] = value;
value += 0.1f;
}
}
}
private void SetMisc()
{
sumIH = new float[numHidden, 1];
biasIH = new float[numHidden, 1];
sumHO = new float[numOutput, 1];
biasHO = new float[numOutput, 1];
gradientsO = new float[numOutput, 1];
gradientsH = new float[numHidden, 1];
prevWeightsDeltaIH = new float[numInput, numHidden];
prevWeightsDeltaHO = new float[numHidden, numOutput];
prevBiasesDeltaIH = new float[numHidden, 1];
prevBiasesDeltaHO = new float[numOutput, 1];
outputsIH = new float[numHidden, 1];
outputsHO = new float[numOutput, 1];
rawIH = new float[numHidden, 1];
inputNodes = new float[numHidden, 1];
}
private void ComputeOutputs()
{
//Hidden
for (int i = 0; i < numHidden; i++)
{
for (int j = 0; j < numInput; j++)
{
sumIH[i, 0] += inputNodes[j, 0] * weightsIH[j, i]; //
}
sumIH[i, 0] += biasIH[i, 0];
rawIH[i, 0] = sumIH[i, 0];
outputsIH[i, 0] = SigmoidFunction(sumIH[i, 0]);
}
//Output
for (int i = 0; i < numOutput; i++)
{
for (int j = 0; j < numHidden; j++)
{
sumHO[i, 0] += outputsIH[j, 0] * weightsHO[j, i];
}
sumHO[i, 0] += biasHO[i, 0];
outputsHO[i, 0] = HyperTanFunction(sumHO[i, 0]);
}
}
private void BackPropagation()
{
//Outputs
for (int i = 0; i < numOutput; i++)
{
gradientsO[i, 0] = (desiredOutputs[i, 0] - outputsHO[i, 0]) * (1 - outputsHO[i, 0]) * (1 + outputsHO[i, 0]);
}
//Hidden
for (int i = 0; i < numHidden; i++)
{
float outputComp = 0;
for (int j = 0; j < numOutput; j++)
{
outputComp += gradientsO[j, 0] * weightsHO[i, j];
}
gradientsH[i, 0] = (outputsIH[i, 0]) * (1 - outputsIH[i, 0]) * outputComp;
}
//Delta IH
for (int i = 0; i < numInput; i++)
{
for (int j = 0; j < numHidden; j++)
{
prevWeightsDeltaIH[i, j] = eta * gradientsH[j, 0] * inputNodes[i, 0];
}
}
//Delta HO
for (int i = 0; i < numHidden; i++)
{
for (int j = 0; j < numOutput; j++)
{
}
}
//Bias IH
for (int i = 0; i < numHidden; i++)
{
biasIH[i, 0] = eta * gradientsH[i, 0] * 1;
}
//Bias HO
for (int i = 0; i < numOutput; i++)
{
biasHO[i, 0] = eta * gradientsO[i, 0] * 1;
}
}
private void AddDeltas()
{
if (iteration > 0)
{
for (int i = 0; i < numInput; i++)
{
for (int j = 0; j < numHidden; j++)
{
weightsIH[i, j] += prevWeightsDeltaIH[i, j] + (prevWeightsDeltaIH[i, j] * momentum);
}
}
for (int i = 0; i < numHidden; i++)
{
for (int j = 0; j < numOutput; j++)
{
weightsHO[i, j] += prevWeightsDeltaHO[i, j] + (prevWeightsDeltaHO[i, j] * momentum);
}
}
for (int i = 0; i < numHidden; i++)
{
biasIH[i, 0] += prevBiasesDeltaIH[i, 0] + (prevBiasesDeltaIH[i, 0] * momentum);
}
for (int i = 0; i < numOutput; i++)
{
biasHO[i, 0] += prevBiasesDeltaHO[i, 0] + (prevBiasesDeltaHO[i, 0] * momentum);
}
}
else
{
for (int i = 0; i < numInput; i++)
{
for (int j = 0; j < numHidden; j++)
{
weightsIH[i, j] += prevWeightsDeltaIH[i, j];
}
}
for (int i = 0; i < numHidden; i++)
{
for (int j = 0; j < numOutput; j++)
{
weightsHO[i, j] += prevWeightsDeltaHO[i, j];
}
}
for (int i = 0; i < numHidden; i++)
{
biasIH[i, 0] += prevBiasesDeltaIH[i, 0];
}
for (int i = 0; i < numOutput; i++)
{
biasHO[i, 0] += prevBiasesDeltaHO[i, 0];
}
}
}
private static float SigmoidFunction(float x)
{
if (x < -45.0f) return 0.0f;
else if (x > 45.0) return 1.0f;
else return (float)(1.0f / (1.0f + Math.Exp(-x)));
}
private static float HyperTanFunction(float x)
{
if (x < -10.0) return -1.0f;
else if (x > 10.0) return 1.0f;
else return (float)(Math.Tanh(x));
}
}
}
I have tried changing the values of the input layer, the desired values, layer sizes, checked the backpropagation code, and many other things, but it always outputs one.
I am trying to perform Cross Correlation on these 2 images in C#:
Image, Template
Matlab says the result is supposed to look like: Matlab result
But this is my result: My result
Here is my Cross Correlation function:
public static Signal2D CrossCorrelation2D(Signal2D signal, Signal2D pulse) {
return InverseFFT2D(FFT2D(signal) * FFT2D(pulse).GetConjugate());
}
Here is my FFT2D:
public static Signal2D FFT2D(Signal2D signal) {
Signal2D result = new Signal2D(signal.Height, signal.Width);
for (int i = 0; i < result.Height; i++)
result[i] = new ComplexNumber[signal[i].Length];
//rows
for (int n = 0; n < signal.Height; n++) {
result[n] = FFT(signal[n]);
}
//columns
for (int i = 0; i < signal[0].Length; i++) {
ComplexNumber[] col = new ComplexNumber[signal.Height];
for (int j = 0; j < col.Length; j++) {
col[j] = result[j][i];
}
col = FFT(col);
for (int j = 0; j < col.Length; j++) {
result[j][i] = col[j];
}
}
return result;
}
Here is my FFT:
public static Signal FFT(Signal signal) {
int N = signal.Length;
if (N == 1)
return signal;
if ((N & (N - 1)) != 0)
throw new ArgumentOutOfRangeException("signal length must be a power of 2");
Signal evenArr = new Signal(N / 2);
Signal oddArr = new Signal(N / 2);
for (int i = 0; i < N / 2; i++) {
evenArr[i] = signal[2 * i];
}
evenArr = FFT(evenArr);
for (int i = 0; i < N / 2; i++) {
oddArr[i] = signal[2 * i + 1];
}
oddArr = FFT(oddArr);
Signal result = new Signal(N);
for (int k = 0; k < N / 2; k++) {
double w = -2.0 * k * Math.PI / N;
ComplexNumber wk = new ComplexNumber(Math.Cos(w), Math.Sin(w));
ComplexNumber even = evenArr[k];
ComplexNumber odd = oddArr[k];
result[k] = even + (wk * odd);
result[k + N / 2] = even - (wk * odd);
}
return result;
}
Here is my Signal multiplication (using pointwise multiplication):
public static Signal2D operator* (Signal2D a, Signal2D b) {
if (a.Height != b.Height || a.Width != b.Width)
throw new ArgumentException("Sizes must be equal");
Signal2D result = new Signal2D(a.Height, a.Width);
for (int y = 0; y < a.Height; y++) {
for (int x = 0; x < a.Width; x++) {
result[y][x] = a[y][x] * b[y][x];
}
}
return result;
}
Any help is appreciated, thanks.
Edit: I left the matlab image at the original size of 1023 by 1023 and overlayed my result. It looks like I may already be at the result, I am just not sure how Matlab pads the image. Overlayed results (The red is the white part from my result, the grey is the black part from my result. Black/white is from Matlab)
i am working oncomplex valued neural networks, rvnn ok but when convert it to cvnn error is not decreases, and i cant figure out where is mistake, my code is here
public bool Train(List<Complex> input, List<Complex> output)
{
if ((input.Count != this.Layers[0].Neurons.Count) || (output.Count != this.Layers[this.Layers.Count - 1].Neurons.Count)) return false;
Run(input);
error = 0;
for(int i = 0; i < this.Layers[this.Layers.Count - 1].Neurons.Count; i++)
{
Neuron neuron = this.Layers[this.Layers.Count - 1].Neurons[i];
neuron.Delta = dSigmoid(neuron.Value) * (output[i] - neuron.Value);
error += (output[i] - neuron.Value).Magnitude;
for(int j = this.Layers.Count - 2; j > 2; j--)
{
for(int k = 0; k < this.Layers[j].Neurons.Count; k++)
{
Neuron n = this.Layers[j].Neurons[k];
n.Delta = dSigmoid(n.Value)*
this.Layers[j + 1].Neurons[i].Dendrites[k].Weight *
this.Layers[j + 1].Neurons[i].Delta;
}
}
}
for(int i = this.Layers.Count - 1; i > 1; i--)
{
for(int j=0; j < this.Layers[i].Neurons.Count; j++)
{
Neuron n = this.Layers[i].Neurons[j];
n.Bias = n.Bias + (this.LearningRate * n.Delta);
for (int k = 0; k < n.Dendrites.Count; k++)
n.Dendrites[k].Weight = n.Dendrites[k].Weight + (this.LearningRate * this.Layers[i - 1].Neurons[k].Value * n.Delta);
}
}
return true;
}
I am implementing counting sort But some thing is wrong with my code
I am new in Programming Please help me to find an error.
I am implenting it step by step .
namespace ConsoleApplication1
{
class Program
{
public static int[] a = { 0,0,0,5,4,8,9,9,7,3, 3, 2, 1 };
public static void Sorting()
{
int j = 0, i = 0, smallestvalue = 0, largestvalue = 0, n = a.Length, lengthof_B = 0, temp = 0, anothersmallestvalue;
smallestvalue = largestvalue = a[0];
for (i = 0; i < n; i++)
{
if (smallestvalue > a[i])
{
smallestvalue = a[i];
}
else if (largestvalue < a[i])
{
largestvalue = a[i];
}
}
int x = anothersmallestvalue = smallestvalue;
lengthof_B = largestvalue - smallestvalue + 1;
int[] b = new int[lengthof_B];
for (i = 0; i < lengthof_B && smallestvalue <= largestvalue; i++)
{
for (j = 0; j < n; j++)
{
if (smallestvalue == a[j])
{
b[i] = b[i] + 1;
}
}
b[i] = temp + b[i];
temp = b[i];
smallestvalue++;
}
int[] c = new int[a.Length];
// I think error here
for (i = n - 1; i >= 0; i--)
{
anothersmallestvalue = x;
for (j = 0; j <= lengthof_B ; j++)
{
if (a[i] == anothersmallestvalue)
{
temp = b[j];
c[temp - 1] = anothersmallestvalue;
b[j] = b[j];
}
anothersmallestvalue++;
}
}
for (i = 0; i < c.Length; i++)
{
Console.WriteLine("c[i] : " + c[i]);
}
}
}
class Demo
{
static void Main(string[] args)
{
Program.Sorting();
Console.ReadLine();
}
}
}
Desired Output is
000123457899
But output of my program is
000120457809
This Is Your Code Here I found a mistake.
And your Code is too complex Please Go through your code Once more.
for (i = n - 1; i >= 0; i--)
{
anothersmallestvalue = x;
for (j = 0; j <= lengthof_B ; j++)
{
if (a[i] == anothersmallestvalue)
{
temp = b[j];
c[temp - 1] = anothersmallestvalue;
b[j] = b[j] -1 ;// Possible Mistake I think here
}
anothersmallestvalue++;
}
}
the very simple and stylish way is described and shown here.
en.wikipedia.org/wiki/Counting_sort#The_algorithm
Normal sorting your two loops should look like this
for (i = 0; i < lengthof_B - 1; i++)
{
for (j = i + 1; j < lengthof_B; j++)
{
}
}
I have a problem with NSGA. When I have 100 pop, 2 objectives, sigma_share=0.01, epsilon=0.22 and run N times with NSGA then the assigned_fitness values are not negative.
When I have above 200 pop, 2 objectives, sigma_share=0.01, epsilon=0.22 and run with N=5 or N=6 times, then the assigned_fitness and shared_fitness are negative values.
How do I fix this?
public void Compute_Assigned_fitness()
{
//Np is 200, epsilon=0.22
double Fmin = this.Np + epsilon;
double fmin_temp=Fmin;
for (int i = 0; i < Front_id.Count; i++)
{
for (int j = 0; j < Front_id[i].Front_item.Count; j++)
{
Front_id[i].Front_item[j].Assigned_fitness = Fmin - epsilon;
double niche = 0;
for (int k = 0; k < Front_id[i].Front_item.Count; k++)
{
double d = Front_id[i].Front_item[j].distances_to(Front_id[i].Front_item[k]);
if (d <= sigma_share)
{
double shd = 1 - (d / sigma_share) * (d / sigma_share);
niche += shd;
}
Front_id[i].Front_item[j].Niche = niche;
}
Front_id[i].Front_item[j].Shared_fitness = Front_id[i].Front_item[j].Assigned_fitness / niche;
niche = 0;
if (fmin_temp > Front_id[i].Front_item[j].Shared_fitness)
fmin_temp = Front_id[i].Front_item[j].Shared_fitness;
}
Fmin = fmin_temp;
}
for (int i = 0; i < Front_id.Count; i++)
{
for (int j = 0; j < Front_id[i].Front_item.Count; j++)
{
P.Add(Front_id[i].Front_item[j]);
}
}
}