Exemplo n.º 1
0
        public static INeuralNetwork TryLoad([NotNull] Stream stream, ExecutionModePreference preference)
        {
            try
            {
                using (GZipStream gzip = new GZipStream(stream, CompressionMode.Decompress))
                {
                    if (!gzip.TryRead(out NetworkType model))
                    {
                        return(null);
                    }
                    switch (model)
                    {
                    case NetworkType.Sequential: return(SequentialNetwork.Deserialize(gzip, preference));

                    case NetworkType.ComputationGraph: return(ComputationGraphNetwork.Deserialize(gzip, preference));

                    default: return(null);
                    }
                }
            }
            catch
            {
                // Locked or invalid file
                return(null);
            }
        }
        // Tries to process a random batch through the network (this method should just not crash)
        private static void ValidateGraph([NotNull] INeuralNetwork network)
        {
            float[,]
            x = new float[200, network.InputInfo.Size],
            y = new float[200, network.OutputInfo.Size];
            for (int i = 0; i < 200; i++)
            {
                for (int j = 0; j < x.GetLength(1); j++)
                {
                    x[i, j] = ThreadSafeRandom.NextFloat();
                }
                y[i, ThreadSafeRandom.NextInt(max: y.GetLength(1))] = 1;
            }
            _ = network.Forward(x);
            SamplesBatch            batch = new SamplesBatch(x, y);
            ComputationGraphNetwork graph = network.To <INeuralNetwork, ComputationGraphNetwork>();

            graph.Backpropagate(batch, 0.5f, WeightsUpdaters.AdaDelta(TrainingAlgorithms.AdaDelta(), graph));
            _ = network.ExtractDeepFeatures(x);
        }