Exemple #1
0
        public RegressionTrainer(ILinearAlgebraProvider lap, IDataTable table)
        {
            _lap = lap;
            var numRows          = table.RowCount;
            var numCols          = table.ColumnCount;
            int classColumnIndex = table.TargetColumnIndex;

            var data = table.GetNumericColumns(Enumerable.Range(0, numCols).Where(c => c != classColumnIndex));

            _feature = lap.Create(numRows, numCols, (i, j) => j == 0 ? 1 : data[j - 1][i]);
            _target  = lap.Create(table.GetColumn <float>(classColumnIndex));
        }
        public static void Load()
        {
            _cpu             = BrightWireProvider.CreateLinearAlgebra(false);
            var(graph, data) = MakeGraphAndData();
            var engine      = graph.CreateTrainingEngine(data);
            var errorMetric = new CustomErrorMetric();

            graph.Connect(engine).AddFeedForward(1).Add(graph.SigmoidActivation()).
            AddBackpropagation(errorMetric);
            engine.Train(300, data, errorMetric, bn => bestNetwork = bn);
            AssertEngineGetsGoodResults(engine, data);
        }
Exemple #3
0
        public Convolutional(ILinearAlgebraProvider lap, ConvolutionalNetwork.Layer layer)
        {
            _lap          = lap;
            _padding      = layer.Padding;
            _filterWidth  = layer.FilterWidth;
            _filterHeight = layer.FilterHeight;
            _stride       = layer.Stride;

            var activation = lap.NN.GetActivation(layer.Data.Activation);

            _layer = new StandardFeedForward(lap.CreateMatrix(layer.Data.Weight), lap.CreateVector(layer.Data.Bias), activation);
        }
Exemple #4
0
 public KNNClassifier(ILinearAlgebraProvider lap, KNearestNeighbours model, int k,
                      DistanceMetric distanceMetric = DistanceMetric.Euclidean)
 {
     _k              = k;
     _lap            = lap;
     _model          = model;
     _distanceMetric = distanceMetric;
     for (int i = 0, len = model.Instance.Length; i < len; i++)
     {
         _instance.Add(lap.CreateVector(model.Instance[i].Data));
     }
 }
        public TensorDataSource(ILinearAlgebraProvider lap, IReadOnlyList <FloatTensor> data)
        {
            _lap  = lap;
            _data = data;
            var first = data.First();

            InputSize   = first.Size;
            OutputSize  = -1;
            _rows       = first.RowCount;
            _columns    = first.ColumnCount;
            _depth      = first.Depth;
            _matrixSize = _rows * _columns;
        }
Exemple #6
0
        public TensorBasedDataTableAdaptor(ILinearAlgebraProvider lap, IDataTable dataTable)
            : base(lap, dataTable)
        {
            var firstRow = dataTable.GetRow(0);
            var input    = (FloatTensor)firstRow.Data[_dataColumnIndex[0]];
            var output   = (FloatVector)firstRow.Data[_dataTargetIndex];

            _outputSize = output.Size;
            _inputSize  = input.Size;
            Height      = input.RowCount;
            Width       = input.ColumnCount;
            Depth       = input.Depth;
        }
        //readonly List<IRow> _data = new List<IRow>();

        public RowClassifier(ILinearAlgebraProvider lap, IRowClassifier classifier, IDataTable dataTable, IDataTableAnalysis analysis, string name = null)
            : base(name)
        {
            _lap         = lap;
            _dataTable   = dataTable;
            _classifier  = classifier;
            _targetLabel = analysis.ColumnInfo
                           .First(ci => dataTable.Columns[ci.ColumnIndex].IsTarget)
                           .DistinctValues
                           .Select((v, i) => (v.ToString(), i))
                           .ToDictionary(d => d.Item1, d => d.Item2)
            ;
        }
Exemple #8
0
            public InternalLayer(ILinearAlgebraProvider lap, int inputSize, int outputSize, IActivationFunction activation, ConvolutionDescriptor descriptor, bool disableUpdate)
            {
                _inputSize     = inputSize;
                _outputSize    = outputSize;
                _activation    = activation;
                _descriptor    = descriptor;
                _disableUpdate = disableUpdate;

                var weightInit = lap.NN.GetWeightInitialisation(descriptor.WeightInitialisation);

                _bias   = lap.Create(outputSize, x => weightInit.GetBias());
                _weight = lap.Create(inputSize, outputSize, (x, y) => weightInit.GetWeight(inputSize, outputSize, x, y));
            }
        public ConvolutionalTrainingDataProvider(ILinearAlgebraProvider lap, ConvolutionDescriptor descriptor, IReadOnlyList <Tuple <I3DTensor, float[]> > data, IReadOnlyList <IConvolutionalLayer> layer, bool isTraining)
        {
            _lap        = lap;
            _data       = data;
            _descriptor = descriptor;
            _layer      = layer;
            _isTraining = isTraining;
            var first       = data.First();
            var firstTensor = first.Item1;
            var extent      = descriptor.CalculateExtent(firstTensor.ColumnCount, firstTensor.RowCount);

            _inputSize  = extent.Item1 * extent.Item2 * firstTensor.Depth * descriptor.FilterDepth;
            _outputSize = first.Item2.Length;
        }
Exemple #10
0
 public Gaussian(
     ILinearAlgebraProvider lap,
     bool zeroInitialBias = true,
     double stdDev        = 0.1,
     GaussianVarianceCalibration varianceCalibration = GaussianVarianceCalibration.SquareRootN,
     GaussianVarianceCount varianceCount             = GaussianVarianceCount.FanIn
     )
 {
     _lap                 = lap;
     _zeroBias            = zeroInitialBias;
     _varianceCalibration = varianceCalibration;
     _varianceCount       = varianceCount;
     _distribution        = lap.IsStochastic ? new Normal(0, stdDev) : new Normal(0, stdDev, new Random(0));
 }
Exemple #11
0
        public Lstm(int inputSize, int hiddenSize, INeuralNetworkFactory factory, LayerDescriptor template)
        {
            _lap        = factory.LinearAlgebraProvider;
            _activation = factory.GetActivation(template.Activation);

            _wc = CreateLayer(inputSize, hiddenSize, factory, template);
            _wi = CreateLayer(inputSize, hiddenSize, factory, template);
            _wf = CreateLayer(inputSize, hiddenSize, factory, template);
            _wo = CreateLayer(inputSize, hiddenSize, factory, template);

            _uc = CreateLayer(hiddenSize, hiddenSize, factory, template);
            _ui = CreateLayer(hiddenSize, hiddenSize, factory, template);
            _uf = CreateLayer(hiddenSize, hiddenSize, factory, template);
            _uo = CreateLayer(hiddenSize, hiddenSize, factory, template);
        }
 public ConvolutionalExecution(ILinearAlgebraProvider lap, ConvolutionalNetwork network)
 {
     foreach (var layer in network.ConvolutionalLayer)
     {
         if (layer.Type == ConvolutionalNetwork.ConvolutionalLayerType.Convolutional)
         {
             _convolutional.Add(new Convolutional(lap, layer));
         }
         else if (layer.Type == ConvolutionalNetwork.ConvolutionalLayerType.MaxPooling)
         {
             _convolutional.Add(new MaxPooling(layer.FilterWidth, layer.FilterHeight, layer.Stride));
         }
     }
     _feedForward = lap.NN.CreateFeedForward(network.FeedForward);
 }
Exemple #13
0
        public BidirectionalManager(ILinearAlgebraProvider lap,
                                    INeuralNetworkBidirectionalBatchTrainer trainer,
                                    string dataFile,
                                    ISequentialTrainingDataProvider testData,
                                    int memorySize,
                                    int?autoAdjustOnNoChangeCount = 5) : base(testData, autoAdjustOnNoChangeCount)
        {
            _lap      = lap;
            _trainer  = trainer;
            _dataFile = dataFile;

            var memory = _Load(_trainer, dataFile, memorySize);

            _forwardMemory  = memory.Item1;
            _backwardMemory = memory.Item2;
        }
Exemple #14
0
        I4DTensor _FormIntoTensor(ILinearAlgebraProvider lap, IVector vector, I4DTensor tensor)
        {
            var indexableVector = vector.AsIndexable();
            var tensorList      = new List <I3DTensor>();

            for (var i = 0; i < tensor.Count; i++)
            {
                var matrixList = new List <IMatrix>();
                for (var j = 0; j < tensor.Depth; j++)
                {
                    matrixList.Add(lap.CreateMatrix(tensor.RowCount, tensor.ColumnCount, indexableVector[j]));
                }
                tensorList.Add(lap.Create3DTensor(matrixList));
            }
            return(lap.Create4DTensor(tensorList));
        }
Exemple #15
0
        public NNMF(ILinearAlgebraProvider lap, IReadOnlyList <IIndexableVector> data, int numClusters, IErrorMetric costFunction = null)
        {
            _lap          = lap;
            _data         = data;
            _numClusters  = numClusters;
            _costFunction = costFunction ?? ErrorMetricType.RMSE.Create();

            // create the main matrix
            var rand = new Random();

            _dataMatrix = _lap.Create(data.Count, data.First().Count, (x, y) => data[x][y]);

            // create the weights and features
            _weights  = _lap.Create(_dataMatrix.RowCount, _numClusters, (x, y) => Convert.ToSingle(rand.NextDouble()));
            _features = _lap.Create(_numClusters, _dataMatrix.ColumnCount, (x, y) => Convert.ToSingle(rand.NextDouble()));
        }
        public SequenceToSequenceDataTableAdaptor(ILinearAlgebraProvider lap, ILearningContext learningContext, GraphFactory factory, IDataTable dataTable, Action <WireBuilder> dataConversionBuilder)
            : base(lap, learningContext, dataTable)
        {
            _Initialise(dataTable);

            var wireBuilder = factory.Connect(_inputSize, _input);

            dataConversionBuilder(wireBuilder);

            // execute the graph to find the input size (which is the size of the adaptive graph's output)
            using (var executionContext = new ExecutionContext(lap)) {
                var output = _Encode(executionContext, new[] { 0 });
                _inputSize = output.Item1.ColumnCount;
                _learningContext.Clear();
            }
        }
Exemple #17
0
        public IReadOnlyList <IVector> GetNumericColumns(ILinearAlgebraProvider lap, IEnumerable <int> columns = null)
        {
            var columnTable = (columns ?? Enumerable.Range(0, ColumnCount)).ToDictionary(i => i, i => new float[RowCount]);

            int index = 0;

            _Iterate(row => {
                foreach (var item in columnTable)
                {
                    item.Value[index] = row.GetField <float>(item.Key);
                }
                ++index;
                return(true);
            });

            return(columnTable.OrderBy(kv => kv.Key).Select(kv => lap.Create(kv.Value)).ToList());
        }
        public SequentialDataSource(ILinearAlgebraProvider lap, IReadOnlyList <FloatMatrix> matrixList)
        {
            _lap       = lap;
            _data      = matrixList;
            OutputSize = -1;
            int index = 0;

            _rowDepth = new int[matrixList.Count];
            foreach (var item in matrixList)
            {
                if (index == 0)
                {
                    InputSize = item.ColumnCount;
                }
                _rowDepth[index++] = item.RowCount;
            }
        }
        public DenseMatrixProduct()
        {
            foreach (var m in new[] {8, 64, 128})
            foreach (var n in new[] {8, 64, 128})
            {
                var key = Key(m, n);
                _data[key] = Matrix<double>.Build.Random(m, n);
            }

            Control.NativeProviderPath = @"..\..\..\..\out\MKL\Windows\";
            _mathnetMkl = new MklLinearAlgebraProvider();
            _mathnetManaged = new ManagedLinearAlgebraProvider();
            _mathnetExperimental = new ManagedLinearAlgebraProvider(Variation.Experimental);

            _mathnetMkl.InitializeVerify();
            _mathnetManaged.InitializeVerify();
            _mathnetExperimental.InitializeVerify();
        }
Exemple #20
0
        static bool TryUse(ILinearAlgebraProvider provider)
        {
            try
            {
                if (!provider.IsAvailable())
                {
                    return(false);
                }

                Provider = provider;
                return(true);
            }
            catch
            {
                // intentionally swallow exceptions here - use the explicit variants if you're interested in why
                return(false);
            }
        }
Exemple #21
0
        public IReadOnlyList <IVector> GetNumericRows(ILinearAlgebraProvider lap, IEnumerable <int> columns = null)
        {
            var columnList = new List <int>(columns ?? Enumerable.Range(0, ColumnCount));

            var ret = new List <IVector>();

            _Iterate(row => {
                int index  = 0;
                var buffer = new float[columnList.Count];
                foreach (var item in columnList)
                {
                    buffer[index++] = row.GetField <float>(item);
                }
                ret.Add(lap.Create(buffer));
                return(true);
            });

            return(ret);
        }
Exemple #22
0
        public DenseMatrixProduct()
        {
            foreach (var m in new[] { 8, 64, 128 })
            {
                foreach (var n in new[] { 8, 64, 128 })
                {
                    var key = Key(m, n);
                    _data[key] = Matrix <double> .Build.Random(m, n);
                }
            }

            Control.NativeProviderPath = @"..\..\..\..\out\MKL\Windows\";
            _mathnetMkl     = LinearAlgebraControl.CreateNativeMKL();
            _mathnetManaged = LinearAlgebraControl.CreateManaged();
            //_mathnetExperimental = new ManagedLinearAlgebraProvider(Variation.Experimental);

            _mathnetMkl.InitializeVerify();
            _mathnetManaged.InitializeVerify();
            //_mathnetExperimental.InitializeVerify();
        }
        public SparseSequentialTrainingDataProvider(ILinearAlgebraProvider lap, IReadOnlyList <Tuple <Dictionary <uint, float>, Dictionary <uint, float> >[]> data)
        {
            _lap        = lap;
            _totalCount = data.Count;

            // group the data by sequence size
            _inputData = data
                         .Select((a, ind) => Tuple.Create(a, ind))
                         .GroupBy(s => s.Item1.Length)
                         .OrderBy(g => g.Key)
                         .ToDictionary(g => g.Key, g => g.ToList())
            ;
            _sequenceLength = _inputData.Select(s => new SequenceInfo(s.Key, s.Value.Count)).ToArray();

            // find the dimensions of the input and output
            var firstItem = _inputData.First().Value.First().Item1.First();

            _inputSize  = firstItem.Item1.Count;
            _outputSize = firstItem.Item2.Count;
        }
Exemple #24
0
        public static MultinomialLogisticRegression Train(IDataTable table, ILinearAlgebraProvider lap, int trainingIterations, float trainingRate, float lambda)
        {
            var trainingData = table.ConvertToBinaryClassification();

            // train the classifiers on each training data set
            var classifier = new List <LogisticRegression>();
            var label      = new List <string>();

            foreach (var item in trainingData)
            {
                classifier.Add(item.Table.TrainLogisticRegression(lap, trainingIterations, trainingRate, lambda));
                label.Add(item.Classification);
            }

            // build the model
            return(new MultinomialLogisticRegression {
                Classification = label.ToArray(),
                Model = classifier.ToArray(),
                FeatureColumn = Enumerable.Range(0, table.ColumnCount).Where(c => c != table.TargetColumnIndex).ToArray()
            });
        }
        public ConnectionistFactory(ILinearAlgebraProvider lap, bool stochastic = true)
        {
            _lap           = lap;
            _stochastic    = stochastic;
            _trainer       = new TrainerFactory(lap);
            _weightUpdater = new UpdaterFactory(lap);

            _activation.Add(ActivationType.Relu, new Relu());
            _activation.Add(ActivationType.LeakyRelu, new LeakyRelu());
            _activation.Add(ActivationType.Sigmoid, new Sigmoid());
            _activation.Add(ActivationType.Tanh, new Tanh());
            //_activation.Add(ActivationType.Softmax, new Softmax());
            _activation.Add(ActivationType.None, null);

            _weightInitialisation.Add(WeightInitialisationType.Gaussian, new Gaussian(stochastic));
            _weightInitialisation.Add(WeightInitialisationType.Xavier, new Xavier(stochastic));
            _weightInitialisation.Add(WeightInitialisationType.Identity, new Identity());
            _weightInitialisation.Add(WeightInitialisationType.Identity1, new Identity(0.1f));
            _weightInitialisation.Add(WeightInitialisationType.Identity01, new Identity(0.01f));
            _weightInitialisation.Add(WeightInitialisationType.Identity001, new Identity(0.001f));
        }
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="lap">The linear algebra provider to use</param>
        /// <param name="propertySet">A property set with initialisation data (optional)</param>
        public GraphFactory(ILinearAlgebraProvider lap, IPropertySet propertySet = null)
        {
            _lap = lap;
            WeightInitialisation = new WeightInitialisationProvider(_lap);
            GraphOperation       = new GraphOperationProvider();
            _defaultPropertySet  = propertySet ?? new PropertySet(_lap)
            {
                WeightInitialisation = WeightInitialisation.Gaussian,
                TemplateGradientDescentDescriptor = _rmsProp
            };

            // add the gradient descent descriptors
            //_Add(typeof(L1RegularisationDescriptor), PropertySet.GRADIENT_DESCENT_DESCRIPTOR);
            //_Add(typeof(L2RegularisationDescriptor), PropertySet.GRADIENT_DESCENT_DESCRIPTOR);

            //// add the template based gradient descent descriptors
            //_Add(typeof(AdaGradDescriptor), PropertySet.TEMPLATE_GRADIENT_DESCENT_DESCRIPTOR);
            //_Add(typeof(AdamDescriptor), PropertySet.TEMPLATE_GRADIENT_DESCENT_DESCRIPTOR);
            //_Add(typeof(MomentumDescriptor), PropertySet.TEMPLATE_GRADIENT_DESCENT_DESCRIPTOR);
            //_Add(typeof(NesterovMomentumDescriptor), PropertySet.TEMPLATE_GRADIENT_DESCENT_DESCRIPTOR);
            //_Add(typeof(RmsPropDescriptor), PropertySet.TEMPLATE_GRADIENT_DESCENT_DESCRIPTOR);
        }
        public SequentialDataTableAdaptor(ILinearAlgebraProvider lap, IDataTable dataTable) : base(lap, dataTable)
        {
            if (_dataColumnIndex.Length > 1)
            {
                throw new NotImplementedException("Sequential datasets not supported with more than one input data column");
            }

            _rowDepth = new int[dataTable.RowCount];

            FloatMatrix inputMatrix = null, outputMatrix = null;

            dataTable.ForEach((row, i) => {
                inputMatrix  = row.GetField <FloatMatrix>(_dataColumnIndex[0]);
                outputMatrix = row.GetField <FloatMatrix>(_dataTargetIndex);
                _rowDepth[i] = inputMatrix.RowCount;
                if (outputMatrix.RowCount != inputMatrix.RowCount)
                {
                    throw new ArgumentException("Rows between input and output data tables do not match");
                }
            });
            _inputSize  = inputMatrix.ColumnCount;
            _outputSize = outputMatrix.ColumnCount;
        }
 public static void UseManaged()
 {
     Provider = CreateManaged();
 }
 public static void Load(TestContext context)
 {
     _cuda = BrightWireGpuProvider.CreateLinearAlgebra(false);
     _cpu  = BrightWireProvider.CreateLinearAlgebra(false);
 }
 public RegressionPredictor(ILinearAlgebraProvider lap, IVector theta)
 {
     _lap   = lap;
     _theta = theta;
 }
 public NonNegativeMatrixFactorisation(ILinearAlgebraProvider lap, int numClusters, IErrorMetric costFunction = null)
 {
     _lap          = lap;
     _numClusters  = numClusters;
     _costFunction = costFunction ?? new RMSE();
 }
        static bool TryUse(ILinearAlgebraProvider provider)
        {
            try
            {
                if (!provider.IsAvailable())
                {
                    return false;
                }

                Control.LinearAlgebraProvider = provider;
                return true;
            }
            catch
            {
                // intentionally swallow exceptions here - use the explicit variants if you're interested in why
                return false;
            }
        }