Esempio n. 1
0
        /// <summary>
        /// Deserializes an <see cref="IEnumerable{T}"/> of type <see cref="NnSerializationContext"/> from a file.
        /// </summary>
        /// <returns>A list of serialization contexts.</returns>
        /// <exception cref="ArgumentOutOfRangeException"></exception>
        /// <exception cref="NullReferenceException"></exception>
        public IEnumerable <NnSerializationContext> Deserialize()
        {
            int count = DeserializeContextCount();

            Contracts.ValueGreaterThanZero(count, nameof(count));

            var contexts = new List <NnSerializationContext>();
            var contextInfoDictionary = new Dictionary <int, NnSerializationContextInfo>();

            for (var i = 0; i < count; i++)
            {
                NnSerializationContextInfo contextInfo = DeserializeContextInfo();

                contextInfoDictionary.Add(i, contextInfo);
            }

            for (var i = 0; i < count; i++)
            {
                if (!contextInfoDictionary.TryGetValue(i, out NnSerializationContextInfo contextInfo))
                {
                    throw new NullReferenceException("No context info with was found for the given key.");
                }

                double[] contextData = DeserializeContextData(contextInfo.DataSizeInBytes);

                var context = new NnSerializationContext(contextData, contextInfo);

                contexts.Add(context);
            }

            return(contexts);
        }
        /// <summary>
        /// Writes the specified <see cref="NnSerializationContextInfo"/> to a file.
        /// </summary>
        /// <param name="serializationContextInfo">The context info being serialized.</param>
        private void SerializeContextInfo(NnSerializationContextInfo serializationContextInfo)
        {
            _writer.Write(serializationContextInfo.WeightMatrixRowCount);
            _writer.Write(serializationContextInfo.WeightMatrixColCount);
            _writer.Write(serializationContextInfo.BiasLength);

            byte[] bytes = Encoding.Unicode.GetBytes(serializationContextInfo.ActivationFunctionName);
            _writer.Write(serializationContextInfo.ActivationFunctionNameSizeInBytes);
            _writer.Write(bytes);
        }
Esempio n. 3
0
        /// <summary>
        /// Deserialize a <see cref="NnSerializationContextInfo"/> from a file.
        /// </summary>
        /// <returns>A context info.</returns>
        private NnSerializationContextInfo DeserializeContextInfo()
        {
            int weightMatrixRowCount = _reader.ReadInt32();
            int weightMatrixColCount = _reader.ReadInt32();
            int biasLength           = _reader.ReadInt32();

            int activationFunctionNameSizeInBytes = _reader.ReadInt32();

            byte[] activationFunctionNameBytes = _reader.ReadBytes(activationFunctionNameSizeInBytes);
            string activationFunctionName      = Encoding.Unicode.GetString(activationFunctionNameBytes);

            var contextInfo = new NnSerializationContextInfo(weightMatrixRowCount, weightMatrixColCount, biasLength, activationFunctionName);

            return(contextInfo);
        }
Esempio n. 4
0
        /// <summary>
        /// Deserializes a <see cref="NnSerializationContext"/> from a file.
        /// </summary>
        /// <returns>A serialization context.</returns>
        /// <exception cref="NotSupportedException"></exception>
        /// <exception cref="ArgumentOutOfRangeException"></exception>
        public NnSerializationContext DeserializeSingle()
        {
            int count = DeserializeContextCount();

            Contracts.ValueGreaterThanZero(count, nameof(count));

            if (count > 1)
            {
                throw new NotSupportedException("Can not deserialize a single context, from a file that stores multiple contexts.");
            }

            NnSerializationContextInfo contextInfo = DeserializeContextInfo();

            double[] contextData = DeserializeContextData(contextInfo.DataSizeInBytes);

            var context = new NnSerializationContext(contextData, contextInfo);

            return(context);
        }