public static void Serialize(FileStream stream, NeuralNetworkLineImage image)
        {
            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }
            if (image == null)
            {
                throw new ArgumentNullException(nameof(image));
            }

            byte[] buffer;

            // new version signal
            buffer = BitConverter.GetBytes(SectionType.SECTION_START_SIGNAL); // 2-bytes
            stream.Write(buffer, 0, buffer.Length);

            // serialize section type
            buffer = new byte[] { SECTION_TYPE }; // 1-bytes
            stream.Write(buffer, 0, buffer.Length);

            // serialize version
            buffer = BitConverter.GetBytes(VERSION); // 2-bytes
            stream.Write(buffer, 0, buffer.Length);

            // serialize index
            buffer = BitConverter.GetBytes(image.index); // 4-bytes
            stream.Write(buffer, 0, buffer.Length);

            // functions serializer
            var function = new FunctionSerializerCore(stream);

            // serialize images count
            buffer = BitConverter.GetBytes(image.images.Length); // 4-bytes
            stream.Write(buffer, 0, buffer.Length);

            // serialize line
            NeuralNetworkSerializer.Serialize(stream, image.images[0]);
            for (int i = 0; i < image.combiners.Length;)
            {
                function.Serialize(image.combiners[i++]);
                NeuralNetworkSerializer.Serialize(stream, image.images[i]);
            }
        }
Ejemplo n.º 2
0
        public FunctionSerializer()
        {
            FunctionType = typeof(T);

            var attr = FunctionSerializerCore.GetAttribute(FunctionType);

            if (attr.code < 0x8000)
            {
                throw new ArgumentException(FunctionType.Name,
                                            $"Invalid code({attr.code}). The code must be greater than 32768 (0x8000).");
            }

            Ucode = Code = attr.code;
            if (typeof(IErrorFunction).IsAssignableFrom(FunctionType))
            {
                Ucode |= FunctionSerializerCore.ERROR_FUNCTION_SIGN;
            }
            if (typeof(IDataConvertor).IsAssignableFrom(FunctionType))
            {
                Ucode |= FunctionSerializerCore.DATA_CONVERTOR_SIGN;
            }
            if (typeof(IRegularization).IsAssignableFrom(FunctionType))
            {
                Ucode |= FunctionSerializerCore.REGULARIZATION_SIGN;
            }
            if (typeof(IDataCombiner).IsAssignableFrom(FunctionType))
            {
                Ucode |= FunctionSerializerCore.DATA_COMBINER_SIGN;
            }

            if (Code == Ucode)
            {
                throw new ArgumentException(FunctionType.Name,
                                            $"The type if serializer ({FunctionType.Name}) is invalid.");
            }

            if (attr.parameter_length < 0)
            {
                throw new ArgumentException(FunctionType.Name,
                                            $"Invalid parameter's length for type({FunctionType.Name}).");
            }
            ParameterLength = attr.parameter_length;
        }
Ejemplo n.º 3
0
        public static void Serialize(FileStream stream, NeuralNetworkImage image)
        {
            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }
            if (image == null)
            {
                throw new ArgumentNullException(nameof(image));
            }

            byte[] buffer;

            // 1: new version signal
            buffer = BitConverter.GetBytes(SectionType.SECTION_START_SIGNAL); // 2-bytes
            stream.Write(buffer, 0, buffer.Length);

            // 2: serialize section type
            buffer = new byte[] { SECTION_TYPE }; // 1-bytes
            stream.Write(buffer, 0, buffer.Length);

            // 3: serialize version
            buffer = BitConverter.GetBytes(VERSION); // 2-bytes
            stream.Write(buffer, 0, buffer.Length);

            // 2: serializer layers
            LayerSerializer.Serialize(stream, image.layers);

            // functions serializer
            var function = new FunctionSerializerCore(stream);

            // 3: serialize error function
            function.Serialize(image.error_fnc);

            // 4: serialize regularization function
            function.Serialize(image.regularization);

            // 5: serialize data input convertor
            function.Serialize(image.input_convertor);

            // 6: serialize data output convertor
            function.Serialize(image.output_convertor);
        }
Ejemplo n.º 4
0
 public void Register()
 {
     FunctionSerializerCore.RegisterFunction(this);
 }