using UnityEngine; using System.Collections; using System.Xml.Serialization; public class Neuron { [XmlAttribute("weight")] public string data; [XmlIgnore] public int[,] weight; // [XmlIgnore] public int minimum = 50; // [XmlIgnore] public int row = 64,column = 64; /** * , */ public Neuron() { weight = new int[row,column]; randomizeWeights(); } /** * , * @param input - * @return 0 1 */ public int transferHard(int[,] input) { int power = 0; for(int r = 0; r < row;r++) { for(int c = 0; c < column;c++) { power += weight[r,c]*input[r,c]; } } //Debug.Log("Power: " + power); return power >= minimum ? 1 : 0; } /** * * @param input - * @return n */ public int transfer(int[,] input) { int power = 0; for(int r = 0; r < row;r++) for(int c = 0; c < column;c++) power += weight[r,c]*input[r,c]; //Debug.Log("Power: " + power); return power; } /** * */ void randomizeWeights() { for(int r = 0; r < row;r++) for(int c = 0; c < column;c++) weight[r,c] = Random.Range(0,10); } /** * * @param input - * @param d - */ public void changeWeights(int[,] input,int d) { for(int r = 0; r < row;r++) for(int c = 0; c < column;c++) weight[r,c] += d*input[r,c]; } public void prepareForSerialization() { data = ""; for(int r = 0; r < row;r++) { for(int c = 0; c < column;c++) { data += weight[r,c] + " "; } data += "\n"; } } public void onDeserialize() { weight = new int[row,column]; string[] rows = data.Split(new char[]{'\n'}); for(int r = 0; r < row;r++) { string[] columns = rows[r].Split(new char[]{' '}); for(int c = 0; c < column;c++) { weight[r,c] = int.Parse(columns[c]); } } } }
using UnityEngine; using System.Collections; using System.Xml.Serialization; using System.Xml; using System.IO; public class NeuralNetwork { [XmlArray("Neurons")] public Neuron[] neurons; /** * */ public NeuralNetwork() { neurons = new Neuron[10]; for(int i = 0;i<neurons.Length;i++) neurons[i] = new Neuron(); } /** * , * @param input - * @return , */ int[] handleHard(int[,] input) { int[] output = new int[neurons.Length]; for(int i = 0;i<output.Length;i++) output[i] = neurons[i].transferHard(input); return output; } /** * , * @param input - * @return , */ int[] handle(int[,] input) { int[] output = new int[neurons.Length]; for(int i = 0;i<output.Length;i++) output[i] = neurons[i].transfer(input); return output; } /** * * @param input - * @return */ public int getAnswer(int[,] input) { int[] output = handle(input); int maxIndex = 0; for(int i = 1; i < output.Length;i++) if(output[i] > output[maxIndex]) maxIndex = i; return maxIndex; } /** * * @param input - * @param correctAnswer - */ public void study(int[,] input,int correctAnswer) { int[] correctOutput = new int[neurons.Length]; correctOutput[correctAnswer] = 1; int[] output = handleHard(input); while(!compareArrays(correctOutput,output)) { for(int i = 0; i < neurons.Length;i++) { int dif = correctOutput[i]-output[i]; neurons[i].changeWeights(input,dif); } output = handleHard(input); } } /** * * @param true - , false - */ bool compareArrays(int[] a,int[] b) { if(a.Length != b.Length) return false; for(int i = 0;i<a.Length;i++) if(a[i] != b[i]) return false; return true; } void prepareForSerialization() { foreach(Neuron n in neurons) n.prepareForSerialization(); } void onDeserialize() { foreach(Neuron n in neurons) n.onDeserialize(); } public void saveLocal() { prepareForSerialization(); XmlSerializer serializer = new XmlSerializer(this.GetType()); FileStream stream = new FileStream(Application.dataPath + "/NeuralNetwork.txt", FileMode.Create); XmlWriter writer = new XmlTextWriter(stream, new System.Text.ASCIIEncoding()); using(writer) { serializer.Serialize(writer, this); } } public static NeuralNetwork fromXml() { string xml = ""; FileStream fStream = new FileStream(Application.dataPath + "/NeuralNetwork.txt", FileMode.OpenOrCreate); if(fStream.Length > 0) { byte[] tempData = new byte[fStream.Length]; fStream.Read(tempData, 0, tempData.Length); xml = System.Text.Encoding.ASCII.GetString(tempData); } fStream.Close(); if(string.IsNullOrEmpty(xml)) return new NeuralNetwork(); NeuralNetwork data; XmlSerializer serializer = new XmlSerializer(typeof(NeuralNetwork)); using(TextReader reader = new StringReader(xml)) { data = serializer.Deserialize(reader) as NeuralNetwork; } data.onDeserialize(); return data; } }
Source: https://habr.com/ru/post/308448/
All Articles