public void Create(string name = "flatten") { FlattenLayer layer; if (this.Layers.Count == 0) { layer = new FlattenLayer(null, name); } else { layer = new FlattenLayer(this.Layers.Last(), name); } Add(layer); }
//公有字段 public DenseLayer(int neuron_num, dynamic input = null, string name = null, string activation = "relu", string optimizer = "GD") { //---- 创建层 ---- this.Name = name == null ? "Undefined" : name; this.Input = input; this.Activation = activation; this.Optimizer = optimizer; //---- ---- //---- 创建神经元 ---- this.__weights__ = null; this.__neurons__ = neuron_num; //---- ---- //判断输入类型 if (input != null) { if (input.GetType() == typeof(DenseLayer))//非头层 { DenseLayer Input = input; this.__inputNum__ = Input.__neurons__; this.__weights__ = new float[__inputNum__, __neurons__]; this.__bias__ = new float[__neurons__]; } if (input.GetType() == typeof(FlattenLayer)) { FlattenLayer Input = input; this.__inputNum__ = Input.__outputNum__; this.__weights__ = new float[__inputNum__, __neurons__]; this.__bias__ = new float[__neurons__]; } else if (input.GetType() == typeof(List <float>))//头层 { List <float> Input = input; this.__inputNum__ = Input.Count; this.__weights__ = new float[__inputNum__, __neurons__]; this.__bias__ = new float[__neurons__]; } } else { for (int i = 0; i < neuron_num; i++) { DenseLayer Input = input; this.__inputNum__ = Input.__neurons__; this.__weights__ = null; this.__bias__ = new float[__neurons__]; } } }
//公有方法 public new List <float> GetOutput() { //耗时计算 Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); List <float> Output = new List <float>(); //---- 判断输入类型 ---- Type input_type = this.Input.GetType(); if (input_type == typeof(List <float>)) { List <float> input = this.Input; var _input = input.ToArray(); var res = Wineforever.Mathematics.client.dense(_input, __weights__, __bias__, Activation); Output = res.ToList(); } else if (input_type == typeof(FlattenLayer)) { FlattenLayer input = this.Input; var _input = input.GetOutput().ToArray(); var res = Wineforever.Mathematics.client.dense(_input, __weights__, __bias__, Activation); Output = res.ToList(); } else if (input_type == typeof(DenseLayer)) { DenseLayer input = this.Input; var _input = input.GetOutput().ToArray(); var res = Wineforever.Mathematics.client.dense(_input, __weights__, __bias__, Activation); Output = res.ToList(); } if (this.Activation == "softmax") { float sum = Output.Sum(i => (float)Math.Pow(Math.E, i - Output.Max())); Output = Output.Select(i => (float)Math.Pow(Math.E, i - Output.Max()) / sum).ToList(); } //打印 stopwatch.Stop(); if (this.Father.__debug__) { Console.WriteLine("{0}用时(毫秒):{1}", this.Name, stopwatch.ElapsedMilliseconds); } return(Output); }
public void Add(FlattenLayer layer) { this.Layers.Add(layer); layer.Father = this;//设置父对象 Update(); }