internal float dropout_rate = 0.2f; //遗忘概率 //创建层 public void Create(int neuron_num, string name = "dense", string activation = "relu", string optimizer = "GD") { DenseLayer layer; if (this.Layers.Count == 0) { layer = new DenseLayer(neuron_num, null, name, activation, optimizer); } else { layer = new DenseLayer(neuron_num, this.Layers.Last(), name, activation, optimizer); } 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(DenseLayer layer) { this.Layers.Add(layer); layer.Father = this;//设置父对象 Update(); }
//私有方法 internal void Update()//更新连接 { if (input != null) { //如果存在平化层,对其后层进行初始化 for (int i = 0; i < Layers.Count; i++) { if (Layers[i].GetType() == typeof(FlattenLayer) && Layers[Math.Min(i + 1, Layers.Count - 1)].GetType() == typeof(DenseLayer)) { int num = 0; DenseLayer denseLayer = (Layers[Math.Min(i + 1, Layers.Count - 1)] as DenseLayer); Layer preLayer = Layers[Math.Max(i - 1, 0)]; if (preLayer.GetType() == typeof(ConvLayer)) { num = 1; num *= (preLayer as ConvLayer).__kernelNum__; } else if (preLayer.GetType() == typeof(DenseLayer)) { num = 1; num *= (preLayer as DenseLayer).__neurons__; } if (input.GetType() == typeof(List <float>)) { num *= (input as List <float>).Count; } else if (input.GetType() == typeof(float[, ])) { num *= (input as float[, ]).GetLength(0) * (input as float[, ]).GetLength(1); } else if (input.GetType() == typeof(List <float[, ]>)) { num *= (input as List <float[, ]>)[0].GetLength(0) * (input as List <float[, ]>)[0].GetLength(1); } (Layers[i] as FlattenLayer).__outputNum__ = num; denseLayer.LoadKernel(denseLayer.__kernelPath__); denseLayer.LoadBias(denseLayer.__biasPath__); break; } } if (input.GetType() == typeof(System)) { if (Layers.First().GetType() == typeof(DenseLayer)) { var layer = Layers.First() as DenseLayer; layer.Input = (this.input.Layers[this.input.Layers.Count - 1] as DenseLayer).__neurons__; } } else if (input.GetType() == typeof(List <float>)) { if (Layers.First().GetType() == typeof(DenseLayer)) { var layer = Layers.First() as DenseLayer; layer.Input = input; } } else if (input.GetType() == typeof(List <float[, ]>)) { if (Layers.First().GetType() == typeof(ConvLayer)) { var layer = Layers.First() as ConvLayer; layer.Input = input; } } } for (int i = 0; i < Layers.Count; i++) { if (Layers[i].GetType() == typeof(ConvLayer) && Layers[i].Input != null && (Layers[i] as ConvLayer).__kernelPath__ != "") { Layers[i].LoadKernel((Layers[i] as ConvLayer).__kernelPath__); } if (Layers[i].GetType() == typeof(ConvLayer) && Layers[i].Input != null && (Layers[i] as ConvLayer).__biasPath__ != "") { Layers[i].LoadBias((Layers[i] as ConvLayer).__biasPath__); } } }