# HTM do not perform well when learning a simple function like y=x!

**URL:** <https://discourse.numenta.org/t/htm-do-not-perform-well-when-learning-a-simple-function-like-y-x/3364>\
**Category:** NuPIC\
**Created:** [January 24, 2018, 4:12am UTC](https://discourse.numenta.org/t/htm-do-not-perform-well-when-learning-a-simple-function-like-y-x/3364 "2018-01-24T04:12:08Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jiede1](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.numenta.org/jiede1/32/1978_2.png) [@Jiede1](https://discourse.numenta.org/u/Jiede1)\
**Post date:** [January 24, 2018, 4:12am UTC](https://discourse.numenta.org/t/htm-do-not-perform-well-when-learning-a-simple-function-like-y-x/3364/1 "2018-01-24T04:12:08Z")

</div>

I am a beginner using HTM.when I run the quick-start example in the website([http://nupic.docs.numenta.org/stable/quick-start/opf.html](http://nupic.docs.numenta.org/stable/quick-start/opf.html)) with the datasets I made,I didn’t change any parameters,and I suffered an problem:

the quick-start example codes:

```python
import yaml
from nupic.frameworks.opf.model_factory import ModelFactory

_PARAMS_PATH = "model.yaml"

with open(_PARAMS_PATH, "r") as f:
  modelParams = yaml.safe_load(f)
  print modelParams

model = ModelFactory.create(modelParams)

# This tells the model the field to predict.
model.enableInference({'predictedField': 'consumption'})

import csv
import datetime
#file = open('data.csv','w+')
# Open the file to loop over each row
with open ("realMode.csv") as fileIn:
  reader = csv.reader(fileIn)
  # The first three rows are not data, but we'll need the field names when
  # passing data into the model.
  headers = reader.next()
  reader.next()
  reader.next()

  for record in reader:
    # Create a dictionary with field names as keys, row values as values.
    modelInput = dict(zip(headers, record))
    # Convert string consumption to float value.
    modelInput = dict(zip(headers, record))
    # Convert string consumption to float value.
    modelInput["consumption"] = float(modelInput["consumption"])
    # Convert timestamp string to Python datetime.
    modelInput["timestamp"] = datetime.datetime.strptime(
      modelInput["timestamp"], "%m/%d/%y %H:%M"
    )
    # Push the data into the model and get back results.
    result = model.run(modelInput)

    #print result
    bestPredictions = result.inferences['multiStepBestPredictions']
    allPredictions = result.inferences['multiStepPredictions']
    oneStep = bestPredictions[1]
    twoStep = bestPredictions[2]
    fiveStep = bestPredictions[5]
    # Confidence values are keyed by prediction value in multiStepPredictions.
    oneStepConfidence = allPredictions[1][oneStep]
    fiveStepConfidence = allPredictions[5][fiveStep]
    twoStepConfidence = allPredictions[2][twoStep]

    result = (oneStep, oneStepConfidence * 100,twoStep, twoStepConfidence*100,
          fiveStep, fiveStepConfidence * 100)
    #esult = (fifStep,fifStepConfidence * 100)
    print "1-step: {:16} ({:4.4}%)\t 2-step: {:16} ({:4.4}%)\t 5-step: {:16} ({:4.4}%)".format(*result)

```

Datasets of mine:

```auto
timestamp,consumption
float,float
,
08/02/10 00:00,0
08/02/10 01:00,1
08/02/10 02:00,2
08/02/10 03:00,3
08/02/10 04:00,4
08/02/10 05:00,5
08/02/10 06:00,6
08/02/10 07:00,7
08/02/10 08:00,8
08/02/10 09:00,9
08/02/10 10:00,10
08/02/10 11:00,11
08/02/10 12:00,12
08/02/10 13:00,13
08/02/10 14:00,14
08/02/10 15:00,15
08/02/10 16:00,16
08/02/10 17:00,17
08/02/10 18:00,18
08/02/10 19:00,19
08/02/10 20:00,20
08/02/10 21:00,21
08/02/10 22:00,22
08/02/10 23:00,23
.......
02/26/11 05:00,4997
02/26/11 06:00,4998
02/26/11 07:00,4999

```

it is a sequence like the function **y=x** ,total 5000 rows

and the result:

```auto
1-step: 4978.66666667 (99.99%) 2-step: 4978.66666667 (99.99%) 5-step: 4978.66666667 (99.99%)
1-step: 4979.66666667 (100.0%) 2-step: 4979.66666667 (100.0%) 5-step: 4979.66666667 (100.0%)
1-step: 4980.66666667 (99.97%) 2-step: 4980.66666667 (99.85%) 5-step: 4980.66666667 (99.97%)
1-step: 4981.66666667 (92.65%) 2-step: 4981.66666667 (92.41%) 5-step: 4981.66666667 (92.26%)
1-step: 4982.66666667 (89.2%) 2-step: 4982.66666667 (89.03%) 5-step: 4982.66666667 (88.6%)
1-step: 4983.66666667 (92.78%) 2-step: 4983.66666667 (92.77%) 5-step: 4983.66666667 (92.77%)
1-step: 4984.66666667 (99.99%) 2-step: 4984.66666667 (99.98%) 5-step: 4984.66666667 (99.99%)
1-step: 4985.66666667 (99.95%) 2-step: 4985.66666667 (99.96%) 5-step: 4985.66666667 (99.96%)
1-step: 4986.66666667 (93.35%) 2-step: 4986.66666667 (92.99%) 5-step: 4986.66666667 (94.28%)
1-step: 4987.66666667 (98.29%) 2-step: 4987.66666667 (98.3%) 5-step: 4987.66666667 (98.45%)
1-step: 4992.0 (49.19%) 2-step: 4992.0 (48.18%) 5-step: 4992.0 (47.94%)
1-step: 4989.66666667 (97.96%) 2-step: 4989.66666667 (97.98%) 5-step: 4989.66666667 (97.96%)
1-step: 4990.66666667 (98.76%) 2-step: 4990.66666667 (98.78%) 5-step: 4990.66666667 (98.78%)
1-step: 4991.66666667 (98.54%) 2-step: 4991.66666667 (98.57%) 5-step: 4991.66666667 (98.54%)
1-step: 4992.66666667 (98.48%) 2-step: 4992.66666667 (98.46%) 5-step: 4992.66666667 (98.44%)
1-step: 4993.66666667 (97.8%) 2-step: 4993.66666667 (97.79%) 5-step: 4993.66666667 (97.82%)
1-step: 4994.66666667 (97.47%) 2-step: 4994.66666667 (97.52%) 5-step: 4994.66666667 (97.49%)
1-step: 4995.66666667 (96.86%) 2-step: 4995.66666667 (96.87%) 5-step: 4995.66666667 (96.87%)

```

as we see, the prediction value is always less than the orgin value(and it should be equal with the origin data) ,and values of 1-step,2-step,and 5-step are almost the [same.So](http://same.So) I assume that the algorithm didn’t learn the function y=x well .But the function is so simple,why HTM can not learn such a simple function ?And I assume the fuction is too simple,so the paramters do not need to change . if I use Linear regression of scikit-learn ,it will perform perfectly! So I don’t know what is the problem.

---

<div class="post-metadata">

**Author:** ![abshej](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.numenta.org/abshej/32/1993_2.png) [@abshej](https://discourse.numenta.org/u/abshej)\
**Post date:** [January 24, 2018, 4:47am UTC](https://discourse.numenta.org/t/htm-do-not-perform-well-when-learning-a-simple-function-like-y-x/3364/2 "2018-01-24T04:47:37Z")

</div>

I am not very familiar with the NuPIC framework but what happens if you change the field to be predicted to Int?

---

<div class="post-metadata">

**Author:** ![Jiede1](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.numenta.org/jiede1/32/1978_2.png) [@Jiede1](https://discourse.numenta.org/u/Jiede1)\
**Post date:** [January 24, 2018, 5:29am UTC](https://discourse.numenta.org/t/htm-do-not-perform-well-when-learning-a-simple-function-like-y-x/3364/3 "2018-01-24T05:29:36Z")

</div>

i haven’t tried yet ,but I think it doesn’t help

---

<div class="post-metadata">

**Author:** ![abshej](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.numenta.org/abshej/32/1993_2.png) [@abshej](https://discourse.numenta.org/u/abshej)\
**Post date:** [January 24, 2018, 5:38am UTC](https://discourse.numenta.org/t/htm-do-not-perform-well-when-learning-a-simple-function-like-y-x/3364/4 "2018-01-24T05:38:45Z")

</div>

I see. Please try it once. It might be a rounding problem or something related to how the SDRs are formed depending on the dtype.

---

<div class="post-metadata">

**Author:** ![Jiede1](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.numenta.org/jiede1/32/1978_2.png) [@Jiede1](https://discourse.numenta.org/u/Jiede1)\
**Post date:** [January 24, 2018, 5:50am UTC](https://discourse.numenta.org/t/htm-do-not-perform-well-when-learning-a-simple-function-like-y-x/3364/5 "2018-01-24T05:50:53Z")

</div>

Thanks,I will try at once,and I would like to re-explain my problem——I created a dataset that satisfies the law of y = x，and I wish that HTM could learn this law.But it works not well.For example,when the origin data x=4999,HTM will predict the 1-step value is 4995.66666667,and the 2-step value is 4995.66666667，5-step value is 4995.66666667,rather than 5000,5001,5004.

---

<div class="post-metadata">

**Author:** ![abshej](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.numenta.org/abshej/32/1993_2.png) [@abshej](https://discourse.numenta.org/u/abshej)\
**Post date:** [January 24, 2018, 5:56am UTC](https://discourse.numenta.org/t/htm-do-not-perform-well-when-learning-a-simple-function-like-y-x/3364/6 "2018-01-24T05:56:23Z")

</div>

I think for representing functions, you should give two values, y and x.  
Then merge them into an SDR. Note that the system doesn’t have any understanding of the number system itself.  
So train it on the values of y and x(since function can be x^2 as well… And so x and y both are needed I suppose) and then map the predictions.  
I think the problem is the type of output HTM gives.

---

<div class="post-metadata">

**Author:** ![Jiede1](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.numenta.org/jiede1/32/1978_2.png) [@Jiede1](https://discourse.numenta.org/u/Jiede1)\
**Post date:** [January 24, 2018, 6:11am UTC](https://discourse.numenta.org/t/htm-do-not-perform-well-when-learning-a-simple-function-like-y-x/3364/7 "2018-01-24T06:11:11Z")

</div>

Currently，HTM can learn that sequence is always increasing by adding 1 each time,as following shown  
1-step: 4978.66666667 (99.99%)  
1-step: 4979.66666667 (100.0%)  
1-step: 4980.66666667 (99.97%)

but my biggest question is ,its prediction is not coorect, when x=500,and 1-step,2-step,5-step values should be 501,502,505,but the prediction are 500,500,500

and this are some of my model parameters:

#Classifier parameters. For detailed descriptions of each parameter, see  
#the API docs for nupic.algorithms.sdr\_classifier.SDRClassifier.  
clParams:  
verbosity: 0  
regionName: SDRClassifierRegion  
alpha: 0.1  
steps: '1,2,5’  
maxCategoryCount: 1000  
implementation: cpp

---

<div class="post-metadata">

**Author:** ![abshej](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.numenta.org/abshej/32/1993_2.png) [@abshej](https://discourse.numenta.org/u/abshej)\
**Post date:** [January 24, 2018, 6:17am UTC](https://discourse.numenta.org/t/htm-do-not-perform-well-when-learning-a-simple-function-like-y-x/3364/8 "2018-01-24T06:17:36Z")

</div>

I see. Maybe it isn’t learning the sequences and the incrementing output predictions you are getting are because of the changing nature of the input itself.  
How is the accuracy so high if the next input is not the predicted output?  
Are you sure you are to use the **SDR Classifier** region for this particular problem?

---

<div class="post-metadata">

**Author:** ![Jiede1](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.numenta.org/jiede1/32/1978_2.png) [@Jiede1](https://discourse.numenta.org/u/Jiede1)\
**Post date:** [January 24, 2018, 6:29am UTC](https://discourse.numenta.org/t/htm-do-not-perform-well-when-learning-a-simple-function-like-y-x/3364/9 "2018-01-24T06:29:38Z")

</div>

my input is a dataset satisfying the law y=x ,like

08/02/10 00:00, **0**  
08/02/10 01:00,1  
08/02/10 02:00,2  
08/02/10 03:00, **3**  
08/02/10 04:00,4  
08/02/10 05:00,5  
08/02/10 06:00,6  
08/02/10 07:00,7  
08/02/10 08:00, **8**  
08/02/10 09:00, **9**  
08/02/10 10:00, **10**

you can see the field 1 is increasing

and the prediction has the similar structure with the input.like:

1-step: 4978.66666667 (99.99%)  
1-step: 4979.66666667 (100.0%)  
1-step: 4980.66666667 (99.97%)

So I think SDR Classifier is OK for this question.But the predict Value for 1-step,2-step,5-step is not correct ,because it is different with the origin data at that moment

---

<div class="post-metadata">

**Author:** ![abshej](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.numenta.org/abshej/32/1993_2.png) [@abshej](https://discourse.numenta.org/u/abshej)\
**Post date:** [January 24, 2018, 6:36am UTC](https://discourse.numenta.org/t/htm-do-not-perform-well-when-learning-a-simple-function-like-y-x/3364/10 "2018-01-24T06:36:06Z")

</div>

I see. So I guess a single field is fine.  
What time input are you giving to check the predictions? What happens if you remove the temporal content from the data set? That is, only using the incrementing values over time without any actual encoding of time.  
I am not yet clear on how the SDR Classifier region works. So I guess it’s right.

---

<div class="post-metadata">

**Author:** ![Jiede1](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.numenta.org/jiede1/32/1978_2.png) [@Jiede1](https://discourse.numenta.org/u/Jiede1)\
**Post date:** [January 24, 2018, 6:43am UTC](https://discourse.numenta.org/t/htm-do-not-perform-well-when-learning-a-simple-function-like-y-x/3364/11 "2018-01-24T06:43:11Z")

</div>

I think the temporal content is [needed.In](http://needed.In) the official website ,its dataset also use it ,and the complete model parameter file also includes the parameters for the temporal content.

---

<div class="post-metadata">

**Author:** ![abshej](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.numenta.org/abshej/32/1993_2.png) [@abshej](https://discourse.numenta.org/u/abshej)\
**Post date:** [January 24, 2018, 6:47am UTC](https://discourse.numenta.org/t/htm-do-not-perform-well-when-learning-a-simple-function-like-y-x/3364/12 "2018-01-24T06:47:02Z")

</div>

Oh. But do they encode mathematical functions in their datasets? While trying to extract sequences of patterns from an unknown, albeit real time varying data, time step encoding is required, but we already have a definition of the function and thus it’s output that we are trying predict here. Is this relevant here?  
Did you check what happens for integer type inputs and predictions?

---

<div class="post-metadata">

**Author:** ![Jiede1](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.numenta.org/jiede1/32/1978_2.png) [@Jiede1](https://discourse.numenta.org/u/Jiede1)\
**Post date:** [January 24, 2018, 6:56am UTC](https://discourse.numenta.org/t/htm-do-not-perform-well-when-learning-a-simple-function-like-y-x/3364/13 "2018-01-24T06:56:56Z")

</div>

yes, it is relevant.when the timestamp value goes to the next step ,the value for prediction will add 1.  
08/02/10 00:00,0  
08/02/10 01:00,1  
08/02/10 02:00,2

and this is the meaning of the law y=x.

---

<div class="post-metadata">

**Author:** ![abshej](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.numenta.org/abshej/32/1993_2.png) [@abshej](https://discourse.numenta.org/u/abshej)\
**Post date:** [January 24, 2018, 7:07am UTC](https://discourse.numenta.org/t/htm-do-not-perform-well-when-learning-a-simple-function-like-y-x/3364/14 "2018-01-24T07:07:45Z")

</div>

That is true for the type of dataset you have created. y=x applies for any randomly iterating values of x as well. Even though after the entire forward and backward iteration, the actual function map will end up being continuous over time.  
For example, if you were training the system using random values of x and its corresponding y, using two fields and also using time encoding then it should still figure out the linear relationship between x and y which is x=y. This is the kind of training I was thinking about. 🤔  
So if you are using time steps I would suggest using multistep increments and decrements in x and y over time. Sorry if this is very vague. I am also trying to get a hold of the theory. Don’t know much about NuPIC though.

---

<div class="post-metadata">

**Author:** ![Jiede1](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.numenta.org/jiede1/32/1978_2.png) [@Jiede1](https://discourse.numenta.org/u/Jiede1)\
**Post date:** [January 24, 2018, 7:29am UTC](https://discourse.numenta.org/t/htm-do-not-perform-well-when-learning-a-simple-function-like-y-x/3364/15 "2018-01-24T07:29:43Z")

</div>

But I have seen the quick-start codes ,the model of HTM depends on the parameters we give,and it use the timestamp value ,as an Encoder input,and another field , as the RandomDistributedScalarEncoder(as following shown)，so I think the situation you assume does not exist

part of the model parameters I use for the quick-start example:  
#List of encoders and their parameters.  
encoders:  
consumption:  
fieldname: consumption  
name: consumption  
resolution: 0.88  
seed: 1  
type: RandomDistributedScalarEncoder  
timestamp\_timeOfDay:  
fieldname: timestamp  
name: timestamp\_timeOfDay  
timeOfDay: [21, 1]  
type: DateEncoder  
timestamp\_weekend:  
fieldname: timestamp  
name: timestamp\_weekend  
type: DateEncoder  
weekend: 21

---

<div class="post-metadata">

**Author:** ![abshej](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.numenta.org/abshej/32/1993_2.png) [@abshej](https://discourse.numenta.org/u/abshej)\
**Post date:** [January 24, 2018, 7:34am UTC](https://discourse.numenta.org/t/htm-do-not-perform-well-when-learning-a-simple-function-like-y-x/3364/16 "2018-01-24T07:34:38Z")

</div>

Oh, okay. 🤔 My bad. Do tell when it works along with the corrections.

---

<div class="post-metadata">

**Author:** ![Jiede1](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.numenta.org/jiede1/32/1978_2.png) [@Jiede1](https://discourse.numenta.org/u/Jiede1)\
**Post date:** [January 24, 2018, 8:11am UTC](https://discourse.numenta.org/t/htm-do-not-perform-well-when-learning-a-simple-function-like-y-x/3364/17 "2018-01-24T08:11:45Z")

</div>

😁ok

---

<div class="post-metadata">

**Author:** ![rhyolight](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.numenta.org/rhyolight/32/3922_2.png) [@rhyolight](https://discourse.numenta.org/u/rhyolight)\
**Post date:** [January 24, 2018, 2:53pm UTC](https://discourse.numenta.org/t/htm-do-not-perform-well-when-learning-a-simple-function-like-y-x/3364/18 "2018-01-24T14:53:10Z")

</div>

First of all, NuPIC will never predict a sequence like x=y or x=y! or x=y2 because those functions don’t create repeating sequences. They just go on in one direction forever, there are never any patterns to understand. It does better with a repeating pattern like `x=sin(y)` but still not good (for [other](https://discourse.numenta.org/t/what-does-one-step-ahead-prediction-mean-in-the-sine-wave-prediction-example/2362) reasons)

2nd thing is that you can [format your code](https://discourse.numenta.org/t/posting-code-on-htm-forum/1412) in your posts and it looks much better. I’ve already done this for you.

---

<div class="post-metadata">

**Author:** ![chhenning](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.numenta.org/chhenning/32/3821_2.png) [@chhenning](https://discourse.numenta.org/u/chhenning)\
**Post date:** [January 24, 2018, 3:24pm UTC](https://discourse.numenta.org/t/htm-do-not-perform-well-when-learning-a-simple-function-like-y-x/3364/19 "2018-01-24T15:24:39Z")

</div>

> [@rhyolight](#):
>
> those functions don’t create repeating sequences.

QQ, would it work when you provide the differential in a separate column?

---

<div class="post-metadata">

**Author:** ![abshej](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.numenta.org/abshej/32/1993_2.png) [@abshej](https://discourse.numenta.org/u/abshej)\
**Post date:** [January 24, 2018, 4:04pm UTC](https://discourse.numenta.org/t/htm-do-not-perform-well-when-learning-a-simple-function-like-y-x/3364/20 "2018-01-24T16:04:51Z")

</div>

This depends on the encoding, right? If we find a way to encode the property of “increment”(for y=x, but similar properties for other functions) into SDRs, then will it work? For example, if we activate a bit in an SDR row for every increment and after reaching the end of the row, we again activate the first bit of the row. And given that we have proper representations for numerical quantities in the SDR, the layer could hypothetically predict the next number. Specially if we use _buckets_ for encoding numbers. I think. 🤔

[Next page](https://discourse.numenta.org/t/htm-do-not-perform-well-when-learning-a-simple-function-like-y-x/3364.md?page=2)
