public ConvolutionalLayer(int fsize, int numf, int stride, SpatialLayer prev, ActivationFunction <Matrix <float> > activation) : base(prev.SideLength / stride, numf) { Activation = activation; var vb = Vector <float> .Build; var mb = Matrix <float> .Build; _values = new Matrix <float> [ChannelCount]; for (int i = 0; i < _values.Length; i++) { _values[i] = Matrix <float> .Build.Dense(SideLength, SideLength); } Biases = vb.Dense(ChannelCount, Functions.BiasInitValue(activation)); Weights = new Matrix <float> [prev.ChannelCount][]; for (int i = 0; i < Weights.Length; i++) { Weights[i] = new Matrix <float> [ChannelCount]; for (int j = 0; j < Weights[i].Length; j++) { Weights[i][j] = mb.Random(fsize, fsize, Normal.WithMeanStdDev(0.0, Functions.WeightInitStdDev(2 * SideLength * SideLength, activation))); } } Prev = prev; Stride = stride; FilterSize = fsize; _cache = mb.Dense(fsize, fsize); _conv = mb.Dense(prev.SideLength + fsize - 1, prev.SideLength + fsize - 1); _offset = fsize / 2; if (prev.ChannelCount > 1) // No buffer needed if there is only 1 input channel. { _buffer = mb.Dense(SideLength, SideLength); } }
public FlattenLayer(SpatialLayer prev) { Prev = prev; X = prev.SideLength; Y = prev.SideLength; Z = prev.ChannelCount; _size = prev.SideLength * prev.SideLength * prev.ChannelCount; _values = Vector <float> .Build.Dense(_size); }
public MeanPoolLayer(int size, SpatialLayer prev) : base(prev.SideLength / size, prev.ChannelCount) { _values = new Matrix <float> [ChannelCount]; for (int i = 0; i < _values.Length; i++) { _values[i] = Matrix <float> .Build.Dense(SideLength, SideLength); } Prev = prev; PoolSize = size; _area = size * size; }
public MaxPoolLayer(int size, SpatialLayer prev) : base(prev.SideLength / size, prev.ChannelCount) { _values = new Matrix <float> [ChannelCount]; for (int i = 0; i < _values.Length; i++) { _values[i] = Matrix <float> .Build.Dense(SideLength, SideLength); } Prev = prev; PoolSize = size; Distribution = new Matrix <float> [prev.ChannelCount]; for (int i = 0; i < Distribution.Length; i++) { Distribution[i] = Matrix <float> .Build.Sparse(prev.SideLength, prev.SideLength); } }
public ConvolutionalNetwork(int matsize, int vecsize, int depth, int labels, params CNNArgs[] args) { _matsize = matsize; _vecsize = vecsize; _depth = depth; _labels = labels; _args = args; InputLayer = new SpatialLayer(matsize, depth); ConvolutionalLayers = new ConvolutionalLayer[args.Length]; SubSampleLayers = new MeanPoolLayer[args.Length]; ConvolutionalLayers[0] = new ConvolutionalLayer(args[0].FilterSize, args[0].FilterCount, args[0].Stride, InputLayer, Functions.Rectifier2D); SubSampleLayers[0] = new MeanPoolLayer(args[0].PoolLayerSize, ConvolutionalLayers[0]); for (int i = 1; i < args.Length; i++) { ConvolutionalLayers[i] = new ConvolutionalLayer(args[i].FilterSize, args[i].FilterCount, args[i].Stride, SubSampleLayers[i - 1], Functions.Rectifier2D); SubSampleLayers[i] = new MeanPoolLayer(args[i].PoolLayerSize, ConvolutionalLayers[i]); } FlattenLayer = new FlattenLayer(SubSampleLayers[SubSampleLayers.Length - 1]); VectorInput = new InputLayer(vecsize); LinearHiddenLayer = new DenseLayer(vecsize, VectorInput, Functions.Sigmoid); CombinationLayer = new TreeLayer(FlattenLayer.Size(), vecsize); OutputLayer = new DenseLayer(labels, CombinationLayer, Functions.Identity); }