Example #1
0
 public NeuralNetworkImage(
     Layer[] layers, IErrorFunction error_fnc,
     IDataConvertor input_convertor, IDataConvertor output_convertor,
     IRegularization regularization)
 {
     CheckImageError(layers, error_fnc);
     this.layers           = layers;
     this.error_fnc        = error_fnc;
     this.input_convertor  = input_convertor;
     this.output_convertor = output_convertor;
     this.regularization   = regularization;
 }
        private NeuralNetworkImage CloseCurrentImage()
        {
            var image = new NeuralNetworkImage(
                layers.ToArray(),
                error_func, in_cvrt, out_cvrt, regularization);

            last_layer_input_count = 0;
            layers         = null;
            error_func     = null;
            in_cvrt        = out_cvrt = null;
            regularization = null;

            return(image);
        }
        public void Serialize(IRegularization regularization)
        {
            ushort code;

            byte[] parameters;
            if (regularization == null)
            {
                code       = 0;
                parameters = null;
            }
            else
            {
                switch (regularization)
                {
                case RegularizationL1 _:
                    code       = 1;
                    parameters = null;
                    break;

                case RegularizationL2 _:
                    code       = 2;
                    parameters = null;
                    break;

                default:
                    var attr = GetAttribute(regularization.GetType());
                    if (!registered_functions_via_code.ContainsKey(attr.code | REGULARIZATION_SIGN))
                    {
                        throw new ArgumentException(
                                  nameof(regularization), "this type of IRegularization is not registered.");
                    }
                    var serializer = registered_functions_via_code[attr.code | REGULARIZATION_SIGN];
                    code       = serializer.Code;
                    parameters = serializer.Serialize(regularization);
                    if ((parameters?.Length ?? 0) != serializer.ParameterLength)
                    {
                        throw new Exception("invalid parameters' length.");
                    }
                    break;
                }
            }

            // serialaize type and parameters
            Serialize(code, parameters);
        }
        public NeuralNetworkInitializer SetCorrection(
            IErrorFunction error_func, IRegularization regularization = null)
        {
            if (layers == null)
            {
                throw new Exception("The layers input is not set yet.");
            }

            if (layers.Count < 1)
            {
                throw new Exception("The layers output is not set yet.");
            }

            this.error_func = error_func ??
                              throw new ArgumentNullException(nameof(error_func),
                                                              "The error function is undefined.");

            last_layer_input_count = -1;
            this.regularization    = regularization;

            return(this);
        }