Ejemplo n.º 1
0
 protected AdaptiveDataTableAdaptorBase(ILinearAlgebraProvider lap,
                                        ILearningContext learningContext, IDataTable dataTable) : base(lap, dataTable)
 {
     Debug.Assert(learningContext == null || learningContext.DeferUpdates);
     _learningContext = learningContext;
     _input           = new InputFeeder(0);
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Creates a graph training engine
        /// </summary>
        /// <param name="dataSource">Data source with training data</param>
        /// <param name="learningContext">Previously created training context</param>
        /// <param name="graph">The serialised graph to execute (optional)</param>
        /// <returns></returns>
        public IGraphTrainingEngine CreateTrainingEngine(IDataSource dataSource,
                                                         ILearningContext learningContext, Models.ExecutionGraph graph = null)
        {
            var input = this.CreateFrom(graph);

            return(new TrainingEngine(LinearAlgebraProvider, dataSource, learningContext, input));
        }
Ejemplo n.º 3
0
		public override void Update(IMatrix source, IMatrix delta, ILearningContext context)
		{
			using var previousVelocity = _cache.Clone();
			_cache.AddInPlace(delta, _momentum);
			previousVelocity.AddInPlace(_cache, -_momentum, 1 + _momentum);
			_updater.Update(source, previousVelocity, context);
		}
Ejemplo n.º 4
0
        public override void Update(IMatrix source, IMatrix delta, ILearningContext context)
        {
            var l1 = context.BatchLearningRate * _lambda;

            source.L1Regularisation(l1);
            base.Update(source, delta, context);
        }
Ejemplo n.º 5
0
 public void UpdateBias(IMatrix delta, ILearningContext context)
 {
     using (var columnSums = delta.ColumnSums()) {
         columnSums.Multiply(1f / delta.RowCount);
         _bias.AddInPlace(columnSums, 1f, context.BatchLearningRate);
     }
 }
 public SequenceToSequenceDataTableAdaptor(ILinearAlgebraProvider lap, ILearningContext learningContext, IDataTable dataTable, INode input, DataSourceModel dataSource)
     : base(lap, learningContext, dataTable)
 {
     _Initialise(dataTable);
     _input      = input;
     _inputSize  = dataSource.InputSize;
     _outputSize = dataSource.OutputSize;
 }
 private SequenceToSequenceDataTableAdaptor(ILinearAlgebraProvider lap, ILearningContext learningContext, IDataTable dataTable, INode input, int inputSize, int outputSize)
     : base(lap, learningContext, dataTable)
 {
     _Initialise(dataTable);
     _input      = input;
     _inputSize  = inputSize;
     _outputSize = outputSize;
 }
Ejemplo n.º 8
0
 public virtual void Update(IMatrix source, IMatrix delta, ILearningContext context)
 {
     using var deltaSquared = delta.PointwiseMultiply(delta);
     _cache.AddInPlace(deltaSquared);
     using var cachedSqrt = _cache.Sqrt();
     using var delta2     = delta.PointwiseDivide(cachedSqrt);
     _updater.Update(source, delta2, context);
 }
Ejemplo n.º 9
0
        public override void Update(IMatrix source, IMatrix delta, ILearningContext context)
        {
            using (var deltaSquared = delta.PointwiseMultiply(delta)) {
                _cache.AddInPlace(deltaSquared, _decayRate, 1 - _decayRate);

                using (var cachedSqrt = _cache.Sqrt(1e-8f))
                    using (var delta2 = delta.PointwiseDivide(cachedSqrt)) {
                        _updater.Update(source, delta2, context);
                    }
            }
        }
Ejemplo n.º 10
0
        public override void Update(IMatrix source, IMatrix delta, ILearningContext context)
        {
            var t = context.CurrentEpoch;

            using var deltaSquared = delta.PointwiseMultiply(delta);
            _cache.AddInPlace(delta, _decayRate, 1 - _decayRate);
            _cache2.AddInPlace(deltaSquared, _decayRate2, 1 - _decayRate2);
            using var mb = _cache.Clone();
            using var vb = _cache2.Clone();
            mb.Multiply(1f / (1f - Convert.ToSingle(Math.Pow(_decayRate, t))));
            vb.Multiply(1f / (1f - Convert.ToSingle(Math.Pow(_decayRate2, t))));
            using var vbSqrt = vb.Sqrt();
            using var delta2 = mb.PointwiseDivide(vbSqrt);
            _updater.Update(source, delta2, context);
        }
        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();
            }
        }
        /// <summary>
        /// Creates an adaptive data source (that uses the output from a preliminary graph)
        /// </summary>
        /// <param name="dataTable">Data that will be sent to the preliminary graph</param>
        /// <param name="learningContext">Learning context to use while training the preliminary graph</param>
        /// <param name="dataConversionBuilder">Callback to build the preliminary graph</param>
        /// <returns></returns>
        public IDataSource CreateDataSource(IDataTable dataTable, ILearningContext learningContext, Action <WireBuilder> dataConversionBuilder)
        {
            var columns = dataTable.Columns;

            if (columns.Count == 2)
            {
                var column1 = columns[0].Type;
                var column2 = columns[1].Type;

                // sequence to sequence
                if (column1 == ColumnType.Matrix && column2 == ColumnType.Matrix)
                {
                    return(new SequenceToSequenceDataTableAdaptor(_lap, learningContext, this, dataTable, dataConversionBuilder));
                }
            }
            throw new ArgumentException($"{nameof(dataTable)} does not contain a recognised data format");
        }
Ejemplo n.º 13
0
        /// <summary>
        /// Creates an adaptive data source from a serialised model
        /// </summary>
        /// <param name="dataTable">Data that will be sent to the preliminary graph</param>
        /// <param name="dataSource">The serialised preliminary graph</param>
        /// <param name="learningContext">Learning context to use while training the preliminary graph</param>
        /// <returns></returns>
        public IDataSource CreateDataSource(IDataTable dataTable, DataSourceModel dataSource,
                                            ILearningContext learningContext = null)
        {
            var input   = this.CreateFrom(dataSource.Graph);
            var columns = dataTable.Columns;

            if (columns.Count == 2)
            {
                var column1 = columns[0].Type;
                var column2 = columns[1].Type;
                if (column1 == ColumnType.Matrix && column2 == ColumnType.Matrix)
                {
                    return(new SequenceToSequenceDataTableAdaptor(LinearAlgebraProvider, learningContext,
                                                                  dataTable, input, dataSource));
                }
            }

            throw new ArgumentException($"{nameof(dataTable)} does not contain a recognised data format");
        }
Ejemplo n.º 14
0
 public void UpdateBias(IMatrix delta, ILearningContext context)
 {
     using var columnSums = delta.ColumnSums();
     _bias.AddInPlace(columnSums, 1f / columnSums.Count, context.BatchLearningRate);
 }
 protected void _Update(IMatrix source, IMatrix delta, ILearningContext context, float coefficient1, float coefficient2)
 {
     source.AddInPlace(delta, coefficient1, coefficient2);
 }
Ejemplo n.º 16
0
 public void UpdateWeights(IMatrix delta, ILearningContext context)
 {
     _updater.Update(_weight, delta, context);
 }
Ejemplo n.º 17
0
        public override void Update(IMatrix source, IMatrix delta, ILearningContext context)
        {
            var l2 = 1.0f - (context.BatchLearningRate * _lambda);

            _Update(source, delta, context, l2, context.BatchLearningRate);
        }
Ejemplo n.º 18
0
 public override void Update(IMatrix source, IMatrix delta, ILearningContext context)
 {
     _cache.AddInPlace(delta, 1f, _momentum);
     _updater.Update(source, _cache, context);
 }
Ejemplo n.º 19
0
 public void Update(IMatrix delta, ILearningContext context)
 {
     _updater.Update(_filter, delta, context);
 }
Ejemplo n.º 20
0
 void _UpdateBias(IVector delta, ILearningContext context)
 {
     _source._bias.AddInPlace(delta, 1f, context.BatchLearningRate);
 }
 public virtual void Update(IMatrix source, IMatrix delta, ILearningContext context)
 {
     _Update(source, delta, context, 1f, context.BatchLearningRate);
 }