Пример #1
0
        public Layer(GPUModule gpuModule, Layer previousLayer = null,int size = 0, string id = "", int miniBatchSize = Int32.MinValue)
        {
            if (previousLayer != null) MinibatchSize = previousLayer.MinibatchSize;
            if (miniBatchSize != Int32.MinValue) MinibatchSize = miniBatchSize;

            LayerIndex = IdCounter++;
            Id = id;
            if (String.IsNullOrEmpty(Id))
            {
                Id = "ID" + LayerIndex.ToString().PadLeft(2, '0');
            }

            _gpuModule = gpuModule;
            _gpu = _gpuModule.Gpu;
            PreviousLayer = previousLayer;
            if (size != 0)
            {
                this.Size = size;
                AddArray(ArrayName.Outputs, MinibatchSize, this.Size);                
            }

            if ((previousLayer != null) && (size > 0))
            {
                AddArray(ArrayName.Gradients, MinibatchSize, size);
            }
        }
Пример #2
0
 public MaxoutLayer(GPUModule gpuModule, Layer previousLayer, int groupSize = 2, string id = "") : base(gpuModule, previousLayer, id, createOutputs: false)
 {
     GroupSize = groupSize;
     if (previousLayer.Size % GroupSize != 0) throw new ArgumentException("Invalid groupsize");
     this.Size = previousLayer.Size / GroupSize;
     AddArray(ArrayName.Outputs, previousLayer.MinibatchSize, this.Size);
     AddArray(ArrayName.Gradients, previousLayer.MinibatchSize, this.Size);
     AddIntArray(ArrayName.Winners, previousLayer.MinibatchSize, this.Size);
 }
Пример #3
0
 public ActivationLayer(GPUModule gpuModule, Layer previousLayer, string id = "", bool createOutputs = true) : base(gpuModule, previousLayer, 0, id)
 {
     if (createOutputs)
     {
         this.Size = previousLayer.Size;
         AddArray(ArrayName.Outputs, previousLayer.MinibatchSize, this.Size);
         AddArray(ArrayName.Gradients, previousLayer.MinibatchSize, this.Size);
     }
 }
Пример #4
0
        public void DumpArray(Layer layer, ArrayName name, string filePrefix, string dir)
        {
            var arr = layer.GetArray(name);
            if (arr != null)
            {
                var txt = arr.GetTxt();
                var fileName = filePrefix + "_" + name.ToString() + ".txt";
                var path = Path.Combine(dir, fileName);
                File.WriteAllText(path, txt);
            }

        }
Пример #5
0
 public void DumpLayer(Layer layer, string dir)
 {
     layer.CopyToHost();
     var fileprefix = layer.Id + "_" + layer.GetType();
     DumpArray(layer, ArrayName.Outputs, fileprefix, dir);
     DumpArray(layer, ArrayName.Gradients, fileprefix, dir);
     DumpArray(layer, ArrayName.Weights, fileprefix, dir);
     DumpArray(layer, ArrayName.WeightUpdates, fileprefix, dir);
     DumpArray(layer, ArrayName.LastWeightUpdates, fileprefix, dir);
     DumpArray(layer, ArrayName.CorrectlyPredictedLabels, fileprefix, dir);
     DumpArray(layer, ArrayName.BiasWeights, fileprefix, dir);
     DumpArray(layer, ArrayName.BiasWeightUpdates, fileprefix, dir);
     DumpArray(layer, ArrayName.LastBiasWeightUpdates, fileprefix, dir);
 }
Пример #6
0
        public FullyConnectedLayer(GPUModule gpuModule, Layer previousLayer, int size, string id = "") : base(gpuModule, previousLayer, size, id)
        {
            if (previousLayer != null)
            {
                AddArray(ArrayName.WeightUpdates, InputSize, this.Size);
                AddArray(ArrayName.LastWeightUpdates, InputSize, this.Size);
                AddArray(ArrayName.Weights, InputSize, this.Size);
                AddArray(ArrayName.BiasWeights, this.Size);
                AddArray(ArrayName.BiasWeightUpdates, this.Size);
                AddArray(ArrayName.LastBiasWeightUpdates, this.Size);

                var biasMultipliers = AddArray(ArrayName.BiasMultiplier, this.MinibatchSize, 1);
                biasMultipliers.FillCPU(1f);
            }
        }
Пример #7
0
 public static void SerializeLayer(XElement parent, Layer layer)
 {
     var layerElement = new XElement("Layer");
     layerElement.AddElement("Id", layer.Id);
     layerElement.AddElement("Size", layer.Size.ToString());
     if (layer.HasWeights)
     {
         var weights = layer.GetArray(ArrayName.Weights);
         var biasWeights = layer.GetArray(ArrayName.BiasWeights);
         weights.CopyToHost();
         biasWeights.CopyToHost();
         layerElement.AddElement("Weights", SerializeArrayValues(weights));
         layerElement.AddElement("BiasWeights", SerializeArrayValues(biasWeights));
     }
     parent.Add(layerElement);
 }
Пример #8
0
        public static void DeserializeLayer(XElement layerElement, Layer layer, params SerializationOptions[] options)
        {
            if (layer.HasWeights)
            {
                var weightsElement = layerElement.Element("Weights");
                if (weightsElement == null) throw new Exception("Weights not found for layer : " + layer.Id);
                var weightArray = layer.GetArray(ArrayName.Weights);
                var newData = weightsElement.Value.Split('\n').Select(x => float.Parse(x, CultureInfo.InvariantCulture)).ToArray();
                if (newData.Length != weightArray.CPUArray.Length) throw new Exception("Size of weight arrays dont match");
                weightArray.CPUArray = newData;


                var biasWeightElement = layerElement.Element("BiasWeights");
                if (biasWeightElement == null) throw new Exception("BiasWeights not found for layer : " + layer.Id);
                var biasArray = layer.GetArray(ArrayName.BiasWeights);
                newData = biasWeightElement.Value.Split('\n').Select(x => float.Parse(x, CultureInfo.InvariantCulture)).ToArray();
                if (newData.Length != biasArray.CPUArray.Length) throw new Exception("Size of weight arrays dont match");
                biasArray.CPUArray = newData;
            }
        }
Пример #9
0
 public TanhLayer(GPUModule gpuModule, Layer previousLayer, string id = "") : base(gpuModule, previousLayer, id)
 {
 }
Пример #10
0
 public DropoutLayer(GPUModule gpuModule, Layer previousLayer, string id = "") : base(gpuModule, previousLayer, previousLayer.Size, id)
 {
     AddArray(ArrayName.DropoutMask, MinibatchSize, this.Size);
 }
Пример #11
0
 public CostLayer(GPUModule gpuModule, Layer previousLayer, DataLayer groundThruthLayer, int size, string id = "") : base(gpuModule, previousLayer, size, id)
 {
     GroundTruthLayer = groundThruthLayer;
 }