コード例 #1
0
        public void Init()
        {
            // number of categories includes zero'th category: "unknown"
            ncategories = categoryList == null ? 0 : categoryList.Count + 1;
            minVal      = 0;
            maxVal      = ncategories - 1;

            try
            {
                scalarEncoder = (ScalarEncoder)ScalarEncoder.GetBuilder()
                                .N(this.n)
                                .W(this.w)
                                .Radius(this.radius)
                                .MinVal(this.minVal)
                                .MaxVal(this.maxVal)
                                .Periodic(this.periodic)
                                .Forced(this.forced).Build();
            }
            catch (Exception e)
            {
                string msg = null;
                int    idx = -1;
                if ((idx = (msg = e.Message).IndexOf("ScalarEncoder")) != -1)
                {
                    msg = msg.Substring(0, idx) + "CategoryEncoder";
                    throw new InvalidOperationException(msg);
                }
            }

            indexToCategory.Add(0, "<UNKNOWN>");
            if (categoryList != null && categoryList.Any())
            {
                int len = categoryList.Count;
                for (int i = 0; i < len; i++)
                {
                    categoryToIndex.Add(categoryList[i], i + 1);
                    indexToCategory.Add(i + 1, categoryList[i]);
                }
            }


            width = n = w * ncategories;

            //TODO this is what the CategoryEncoder was doing before I added the ScalarEncoder delegate.
            //I'm concerned because we're changing n without calling init again on the scalar encoder.
            //In other words, if I move the scalarEncoder = ...build() from to here, the test cases fail
            //which indicates significant fragility and at some level a violation of encapsulation.
            //scalarEncoder.N = n;
            scalarEncoder.SetN(n);



            if (GetWidth() != width)
            {
                throw new InvalidOperationException(
                          "Width != w (num bits to represent output item) * #categories");
            }

            description.Add(new Tuple(name, 0));
        }
コード例 #2
0
ファイル: LogEncoder.cs プロジェクト: ArtiDi/HTM.Net
        /**
         *   w -- number of bits to set in output
         *   minval -- minimum input value. must be greater than 0. Lower values are
         *             reset to this value
         *   maxval -- maximum input value (input is strictly less if periodic == True)
         *   periodic -- If true, then the input value "wraps around" such that minval =
         *             maxval For a periodic value, the input must be strictly less than
         *             maxval, otherwise maxval is a true upper bound.
         *
         *   Exactly one of n, radius, resolution must be set. "0" is a special
         *   value that means "not set".
         *   n -- number of bits in the representation (must be > w)
         *   radius -- inputs separated by more than this distance in log space will have
         *             non-overlapping representations
         *   resolution -- The minimum change in scaled value needed to produce a change
         *                 in encoding. This should be specified in log space. For
         *                 example, the scaled values 10 and 11 will be distinguishable
         *                 in the output. In terms of the original input values, this
         *                 means 10^1 (1) and 10^1.1 (1.25) will be distinguishable.
         *   name -- an optional string which will become part of the description
         *   clipInput -- if true, non-periodic inputs smaller than minval or greater
         *                 than maxval will be clipped to minval/maxval
         *   forced -- (default False), if True, skip some safety checks
         */
        public void Init()
        {
            double lowLimit = 1e-07;

            // w defaults to 5
            if (GetW() == 0)
            {
                SetW(5);
            }

            // maxVal defaults to 10000.
            if (GetMaxVal() == 0.0)
            {
                SetMaxVal(10000);
            }

            if (GetMinVal() < lowLimit)
            {
                SetMinVal(lowLimit);
            }

            if (GetMinVal() >= GetMaxVal())
            {
                throw new InvalidOperationException("Max val must be larger than min val or the lower limit " +
                                                    "for this encoder " + string.Format("{0:#.0000000}", lowLimit));
            }

            _minScaledValue = Math.Log10(GetMinVal());
            _maxScaledValue = Math.Log10(GetMaxVal());

            if (_minScaledValue >= _maxScaledValue)
            {
                throw new InvalidOperationException("Max val must be larger, in log space, than min val.");
            }

            // There are three different ways of thinking about the representation. Handle
            // each case here.
            _encoder = (ScalarEncoder)ScalarEncoder.GetBuilder()
                       .W(GetW())
                       .MinVal(_minScaledValue)
                       .MaxVal(_maxScaledValue)
                       .Periodic(false)
                       .N(GetN())
                       .Radius(GetRadius())
                       .Resolution(GetResolution())
                       .ClipInput(ClipInput())
                       .Forced(IsForced())
                       .Name(GetName())
                       .Build();

            SetN(_encoder.GetN());
            SetResolution(_encoder.GetResolution());
            SetRadius(_encoder.GetRadius());
        }
コード例 #3
0
        /**
         * Open up for internal Network API use.
         * Returns an {@link Encoder.Builder} which corresponds to the specified name.
         * @param encoderName
         * @return
         */
        public IBuilder GetBuilder(string encoderName)
        {
            switch (encoderName)
            {
            case "CategoryEncoder":
                return(CategoryEncoder.GetBuilder());

            case "CoordinateEncoder":
                return(CoordinateEncoder.GetBuilder());

            case "GeospatialCoordinateEncoder":
                return(GeospatialCoordinateEncoder.GetGeobuilder());

            case "LogEncoder":
                return(LogEncoder.GetBuilder());

            case "PassThroughEncoder":
                return(PassThroughEncoder <int[]> .GetBuilder());

            case "ScalarEncoder":
                return(ScalarEncoder.GetBuilder());

            case "AdaptiveScalarEncoder":
                return(AdaptiveScalarEncoder.GetAdaptiveBuilder());

            case "SparsePassThroughEncoder":
                return(SparsePassThroughEncoder.GetSparseBuilder());

            case "SDRCategoryEncoder":
                return(SDRCategoryEncoder.GetBuilder());

            case "RandomDistributedScalarEncoder":
                return(RandomDistributedScalarEncoder.GetBuilder());

            case "DateEncoder":
                return(DateEncoder.GetBuilder());

            case "DeltaEncoder":
                return(DeltaEncoder.GetDeltaBuilder());

            case "SDRPassThroughEncoder":
                return(SDRPassThroughEncoder.GetSptBuilder());

            default:
                throw new ArgumentException("Invalid encoder: " + encoderName);
            }
        }
コード例 #4
0
            public override IEncoder Build()
            {
                //Must be instantiated so that super class can initialize
                //boilerplate variables.
                encoder = new ScalarEncoder();

                //Call super class here
                base.Build();

                ////////////////////////////////////////////////////////
                //  Implementing classes would do setting of specific //
                //  vars here together with any sanity checking       //
                ////////////////////////////////////////////////////////

                ((ScalarEncoder)encoder).Init();

                return((ScalarEncoder)encoder);
            }
コード例 #5
0
ファイル: DateEncoder.cs プロジェクト: ArtiDi/HTM.Net
 protected void AddChildEncoder(ScalarEncoder encoder)
 {
     AddEncoder(GetName() ?? string.Empty, encoder.GetName(), encoder);
 }
コード例 #6
0
ファイル: DateEncoder.cs プロジェクト: ArtiDi/HTM.Net
        /**
         * Init the {@code DateEncoder} with parameters
         */
        public void Init()
        {
            width = 0;

            // Because most of the ScalarEncoder fields have less than 21 bits(recommended in
            // ScalarEncoder.checkReasonableSettings), so for now we set forced to be true to
            // override.
            // TODO figure out how to remove this
            SetForced(true);

            // Note: The order of adding encoders matters, must be in the following
            // season, dayOfWeek, weekend, customDays, holiday, timeOfDay

            if (IsValidEncoderPropertyTuple(season))
            {
                seasonEncoder = (ScalarEncoder)ScalarEncoder.GetBuilder()
                                .W((int)season.Get(0))
                                .Radius((double)season.Get(1))
                                .MinVal(0)
                                .MaxVal(366)
                                .Periodic(true)
                                .Name("season")
                                .Forced(this.IsForced())
                                .Build();
                AddChildEncoder(seasonEncoder);
            }

            if (IsValidEncoderPropertyTuple(dayOfWeek))
            {
                dayOfWeekEncoder = (ScalarEncoder)ScalarEncoder.GetBuilder()
                                   .W((int)dayOfWeek.Get(0))
                                   .Radius((double)dayOfWeek.Get(1))
                                   .MinVal(0)
                                   .MaxVal(7)
                                   .Periodic(true)
                                   .Name("day of week")
                                   .Forced(this.IsForced())
                                   .Build();
                AddChildEncoder(dayOfWeekEncoder);
            }

            if (IsValidEncoderPropertyTuple(weekend))
            {
                weekendEncoder = (ScalarEncoder)ScalarEncoder.GetBuilder()
                                 .W((int)weekend.Get(0))
                                 .Radius((double)weekend.Get(1))
                                 .MinVal(0)
                                 .MaxVal(1)
                                 .Periodic(false)
                                 .Name("weekend")
                                 .Forced(this.IsForced())
                                 .Build();
                AddChildEncoder(weekendEncoder);
            }

            if (IsValidEncoderPropertyTuple(customDays))
            {
                List <string> days = (List <string>)customDays.Get(1);

                StringBuilder customDayEncoderName = new StringBuilder();

                if (days.Count == 1)
                {
                    customDayEncoderName.Append(days[0]);
                }
                else
                {
                    foreach (string day in days)
                    {
                        customDayEncoderName.Append(day).Append(" ");
                    }
                }

                customDaysEncoder = (ScalarEncoder)ScalarEncoder.GetBuilder()
                                    .W((int)customDays.Get(0))
                                    .Radius(1)
                                    .MinVal(0)
                                    .MaxVal(1)
                                    .Periodic(false)
                                    .Name(customDayEncoderName.ToString())
                                    .Forced(this.IsForced())
                                    .Build();
                //customDaysEncoder is special in naming
                AddEncoder(GetName() ?? string.Empty, "customdays", customDaysEncoder);
                AddCustomDays(days);
            }

            if (IsValidEncoderPropertyTuple(holiday))
            {
                holidayEncoder = (ScalarEncoder)ScalarEncoder.GetBuilder()
                                 .W((int)holiday.Get(0))
                                 .Radius((double)holiday.Get(1))
                                 .MinVal(0)
                                 .MaxVal(1)
                                 .Periodic(false)
                                 .Name("holiday")
                                 .Forced(this.IsForced())
                                 .Build();
                AddChildEncoder(holidayEncoder);
            }

            if (IsValidEncoderPropertyTuple(timeOfDay))
            {
                timeOfDayEncoder = (ScalarEncoder)ScalarEncoder.GetBuilder()
                                   .W((int)timeOfDay.Get(0))
                                   .Radius((double)timeOfDay.Get(1))
                                   .MinVal(0)
                                   .MaxVal(24)
                                   .Periodic(true)
                                   .Name("time of day")
                                   .Forced(this.IsForced())
                                   .Build();
                AddChildEncoder(timeOfDayEncoder);
            }
        }