Related
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.
If matrix A of size (3x3), then should i use the method of finding determinants, like grabbing the rows and column of first element and removing it from the array 2D array to get the remaining elements and then moving to the next element and repeating the same steps ?
[{1,2,3},
{4,5,6},
{7,8,9}]
I finally was able to do it, here's what I did :
enter image description here
class program
{
public static void Main()
{
int[,] arr = new int[3, 3];
Console.WriteLine("Enter elements of " + (arr.GetUpperBound(0) + 1) + "x" + (arr.GetUpperBound(1) + 1) + " matrix:");
for (int i = 0; i < (arr.GetUpperBound(0) + 1); i++)
{
for (int j = 0; j < (arr.GetUpperBound(1) + 1); j++)
{
arr[i, j] = Convert.ToInt32(Console.ReadLine());
}
}
Console.WriteLine("Matrix entered: ");
for (int i = 0; i < (arr.GetUpperBound(0) + 1); i++)
{
for (int j = 0; j < (arr.GetUpperBound(1) + 1); j++)
{
Console.Write("\t" + arr[i, j]);
}
Console.WriteLine();
}
Console.WriteLine("Possible sub-matrices: ");
for (int i = 0; i < 3; i++)
{
for (int j = 0; j< 3; j++)
{
TrimArray(i,j,arr);
}
}
}
public static int[,] TrimArray(int row, int column, int[,] original)
{
int[,] resultant = new int[original.GetLength(0) - 1, original.GetLength(1) - 1];
for (int i = 0, j = 0; i < original.GetLength(0); i++)
{
if (i == row)
continue;
for (int k = 0, u = 0; k < original.GetLength(1); k++)
{
if (k == column)
continue;
resultant[j, u] = original[i, k];
u++;
}
j++;
}
Console.WriteLine();
for (int i = 0; i < 2; i++)
{
for (int j = 0; j< 2; j++)
{
Console.Write("\t"+resultant[i,j]);
}
Console.WriteLine();
}
return resultant;
}
}
I did this for you yesterday, I created a method that will return a square matrix, given a parent matrix and the length.
static void Main(string[] args)
{
int[][] parentMatrix = new int[][]
{
new int [] { 1, 2, 3 },
new int [] { 4, 5, 6 },
new int [] { 7, 8, 9 }
};
var chunks = GetSubMatrices(parentMatrix, 2);
Console.WriteLine(chunks);
}
static List<int[][]> GetSubMatrices(int[][] parentMatrix, int m)
{
int n = parentMatrix.Length > m ? parentMatrix.Length : throw new InvalidOperationException("You can't use a matrix smaller than the chunk size");
var chunks = new List<int[][]>();
int movLimit = n - m + 1;
var allCount = Math.Pow(movLimit, 2);
for (int selRow = 0; selRow < movLimit; selRow ++)
{
for (int selCol = 0; selCol < movLimit; selCol ++)
{
// this is start position of the chunk
var chunk = new int[m][];
for (int row = 0; row < m; row++)
{
chunk[row] = new int[m];
for (int col = 0; col < m; col++)
{
chunk[row][col] = parentMatrix[selRow + row][selCol + col];
}
}
chunks.Add(chunk);
}
}
return chunks;
}
If you have any problems using it, you can simply comment below.
I needed to solve a problem like and came up with this answer. Hope it adds to your library of answers. If the submatrix specified is not greater than 1, do nothing.
public static void GetSubMatrixes(int[,] arr, int size)
{
int parentMatrixRowLength = arr.GetLength(0);
int parentMatrixColLength = arr.GetLength(1);
var overall = new List<object>();
if(size > 1)
{
for (int i = 0; i < parentMatrixRowLength; i++)
{
//get the columns
for (int j = 0; j < parentMatrixColLength; j++)
{
var subMatrix = new int[size, size];
/*if the new matrix starts from second to the last value in either the row(horizontal or column)
* do not proceed, go to the row or column in the parent matrix
* */
if (j < parentMatrixColLength - (size - 1) && i < parentMatrixRowLength - (size - 1))
{
//add
for (int m = 0; m < subMatrix.GetLength(0); m++)
{
for (int n = 0; n < subMatrix.GetLength(1); n++)
{
/*check the sum of current column value and the sum of the current row value
* of the parent column length and row length if it goes out of bounds
*/
var row = i + m; var col = j + n;
//actual check here
if (row < parentMatrixRowLength && col < parentMatrixColLength)
{
subMatrix[m, n] = arr[i + m, j + n];
}
}
}
overall.Add(subMatrix);
}
}
}
//display the sub matrixes here
for (int i = 0; i < overall.Count; i++)
{
var matrix = overall[i] as int[,];
for (int y = 0; y < matrix.GetLength(0); y++)
{
for (int x = 0; x < matrix.GetLength(1); x++)
{
Console.Write(string.Format("{0} ", matrix[y, x]));
}
Console.Write(Environment.NewLine + Environment.NewLine);
}
Console.WriteLine();
}
}
}
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++)
{
}
}
The following code sorts rows by the first element using bubble method.
I can't change it to counting sort.
public void SortStack(double[,] n)
{
for (int i = 0; i < n.GetLength(0) - 1; i++)
{
for (int j = i; j < n.GetLength(0); j++)
{
if (n[i, 0] > n[j, 0])
{
for (int k = 0; k < n.GetLength(1); k++)
{
var temp = n[i, k];
n[i, k] = n[j, k];
n[j, k] = temp;
}
}
}
}
}
Please help.
As you do bubble sort based on first element of each row. you should do counting sort like that too. so you just need to count first item of each row.
private static int[,] CountingSort2D(int[,] arr)
{
// find the max number by first item of each row
int max = arr[0, 0];
for (int i = 0; i < arr.GetLength(0); i++)
{
if (arr[i, 0] > max) max = arr[i, 0];
}
// we want to get count of first items of each row. thus we need 1d array.
int[] counts = new int[max + 1];
// do the counting. again on first index of each row
for (int i = 0; i < arr.GetLength(0); i++)
{
counts[arr[i, 0]]++;
}
for (int i = 1; i < counts.Length; i++)
{
counts[i] += counts[i - 1];
}
// result is sorted array
int[,] result = new int[arr.GetLength(0), arr.GetLength(1)];
for (int i = arr.GetLength(0) - 1; i >= 0; i--)
{
counts[arr[i, 0]]--;
// now we need to copy columns too. thus we need another loop
for (int j = 0; j < arr.GetLength(1); j++)
{
result[counts[arr[i, 0]], j] = arr[i, j];
}
}
return result;
}
Here is the test.
static void Main()
{
Random rand = new Random();
int[,] arr = new int[3,3];
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
arr[i, j] = rand.Next(0, 100);
}
}
int[,] newarr = CountingSort2D(arr);
for (int i = 0; i < arr.GetLength(0); i++)
{
Console.Write("{ ");
for (int j = 0; j < arr.GetLength(1); j++)
{
Console.Write(arr[i, j] + " ,");
}
Console.Write("} ,");
}
Console.WriteLine();
for (int i = 0; i < newarr.GetLength(0); i++)
{
Console.Write("{ ");
for (int j = 0; j < newarr.GetLength(1); j++)
{
Console.Write(newarr[i, j] + " ,");
}
Console.Write("} ,");
}
Console.WriteLine();
}
Example Output:
{ 49,66,8 },{ 33,39,64 },{ 65,52,76 } // Original array
{ 33,39,64 },{ 49,66,8 },{ 65,52,76 } // Sorted array
I have a 2D array of integers which stores only values [1,0] as shown below.
int[,] INPUT = new int[10, 10] {
{1,1,0,0,0,1,1,1,0,0},
{1,0,0,0,1,0,0,0,1,0},
{0,0,0,1,1,0,0,1,0,1},
{1,1,0,0,0,0,0,1,1,0},
{0,1,0,0,0,1,0,1,1,0},
{0,0,0,0,1,0,1,0,0,0},
{1,0,0,1,0,0,1,1,1,0},
{0,1,0,0,1,1,0,0,1,1},
{0,1,1,1,0,1,0,1,1,0},
{0,0,0,0,0,1,1,0,0,0},
};
I need to identify a Colony count for the input image, where a Colony is defined as a contiguous sequence of 1s (Like in this array, there are 3 colony of 1's). Connection between 1s can be either adjacent or on the diagonal.
I am required to use recursion for this.
class Program
{
public static int count;
public static int colony;
public static int[,] INPUT;
public static int rows = 10;
public static int cols = 10;
static void Main(string[] args)
{
int[,] INPUT = new int[10, 10] {
{1,1,0,0,0,1,1,1,0,0},
{1,0,0,0,1,0,0,0,1,0},
{0,0,0,1,1,0,0,1,0,1},
{1,1,0,0,0,0,0,1,1,0},
{0,1,0,0,0,1,0,1,1,0},
{0,0,0,0,1,0,1,0,0,0},
{1,0,0,1,0,0,1,1,1,0},
{0,1,0,0,1,1,0,0,1,1},
{0,1,1,1,0,1,0,1,1,0},
{0,0,0,0,0,1,1,0,0,0},
};
// Showing All Values From Upper 2D array
Console.WriteLine("Input File Contain: \n");
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
Console.Write(INPUT[i, j]);
}
Console.WriteLine();
}
// Getting Total 1's Count Only
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
if (INPUT[i, j] == 1)
{
count = count + 1;
}
}
Console.WriteLine();
}
// Getting Total 1's Colony Only
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
if (INPUT[i, j] == 1)
{
INPUT[i, j] = 0;
colony = GetColony(i, j);
}
}
}
Console.WriteLine("Counts Of 1 Are: " + count);
Console.WriteLine("Counts Of 1's Colony Are: " + colony );
}
static public int GetColony(int i, int j)
{
if ((i < rows) && (j < cols))
{
if ((INPUT[i - 1, j] == 1) || (INPUT[i + 1, j] == 1) || (INPUT[i, j - 1] == 1) || (INPUT[i, j + 1] == 1))
{
INPUT[i, j] = 0;
return GetColony(i, j);
}
else
{
return 1;
}
}
else
{
return 1;
}
}
}