# Proportional Scalar Encoder?

**URL:** <https://discourse.numenta.org/t/proportional-scalar-encoder/10104>\
**Category:** Implementations\
**Created:** [September 15, 2022, 3:27pm UTC](https://discourse.numenta.org/t/proportional-scalar-encoder/10104 "2022-09-15T15:27:25Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![cezar\_t](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.numenta.org/cezar_t/32/4451_2.png) [@cezar\_t](https://discourse.numenta.org/u/cezar_t)\
**Post date:** [September 15, 2022, 3:27pm UTC](https://discourse.numenta.org/t/proportional-scalar-encoder/10104/1 "2022-09-15T15:27:26Z")

</div>

I searched for such a beast, which bit overlap between any two encoded numbers is higher as their ratio is closer to 1.

e.g. overlap between 900 and 1000 would be the same as the one between 0.9 and 1

PS “biological justification” is we perceive sizes relatively - in terms of sizes, a horse-ox distance is “smaller” than the sparrow-duck one while measuring absolute values are the opposite.  
encoding logarithmic values doesn’t seem to solve it.

Is anyone aware of such an encoder, and which one is that?

---

<div class="post-metadata">

**Author:** ![JarvisGoBrr](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.numenta.org/jarvisgobrr/32/6147_2.png) [@JarvisGoBrr](https://discourse.numenta.org/u/JarvisGoBrr)\
**Post date:** [September 15, 2022, 5:49pm UTC](https://discourse.numenta.org/t/proportional-scalar-encoder/10104/2 "2022-09-15T17:49:44Z")

</div>

This is one I made for this purpose sometime ago:

```auto
def hash_scalar_encoder(value, resolution=0.001, curving=2, n=1000, p=10):
    sdr = set()
    value **= 1 / curving
    value /= resolution
    for i in range(p):
        sdr.add(hash((round(value + i),)) % n)
    return sdr

```

---

<div class="post-metadata">

**Author:** ![cezar\_t](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.numenta.org/cezar_t/32/4451_2.png) [@cezar\_t](https://discourse.numenta.org/u/cezar_t)\
**Post date:** [September 15, 2022, 7:07pm UTC](https://discourse.numenta.org/t/proportional-scalar-encoder/10104/3 "2022-09-15T19:07:19Z")

</div>

Thanks, I’ll look into it.

---

<div class="post-metadata">

**Author:** ![roboto](https://avatars.discourse-cdn.com/v4/letter/r/b77776/32.png) [@roboto](https://discourse.numenta.org/u/roboto)\
**Post date:** [September 16, 2022, 4:29pm UTC](https://discourse.numenta.org/t/proportional-scalar-encoder/10104/4 "2022-09-16T16:29:20Z")

</div>

Thanks for sharing. Looks useful. Just wondering if you’ve considered using log function instead since we can use additions in logs? Below is my attempt but haven’t tested it yet.

```auto
def hash_scalar_encoder(value, resolution=2, n=1000, p=10):
    sdr = set()
    scale = p / (2 * math.log(resolution))
    value = math.log(value)
    value = value * scale
    for i in range(p):
        sdr.add(hash((round(value + i),)) % n)
    return sdr

```

Some examples of what the variable ‘resolution’ does:  
E.g. resolution of 2 means that a value and its double (e.g. 3 & 6 or 7 & 14) will share 50% of bits in their SDRs  
E.g. resolution of 5 means that a value and its quintuple (e.g. 3 & 15 or 7 & 35) will share 50% of bits in their SDRs

---

<div class="post-metadata">

**Author:** ![cezar\_t](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.numenta.org/cezar_t/32/4451_2.png) [@cezar\_t](https://discourse.numenta.org/u/cezar_t)\
**Post date:** [September 16, 2022, 4:38pm UTC](https://discourse.numenta.org/t/proportional-scalar-encoder/10104/5 "2022-09-16T16:38:08Z")

</div>

Thanks, I assumed encoding logarithms won’t work which isn’t correct.

---

<div class="post-metadata">

**Author:** ![JarvisGoBrr](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.numenta.org/jarvisgobrr/32/6147_2.png) [@JarvisGoBrr](https://discourse.numenta.org/u/JarvisGoBrr)\
**Post date:** [September 16, 2022, 5:10pm UTC](https://discourse.numenta.org/t/proportional-scalar-encoder/10104/6 "2022-09-16T17:10:00Z")

</div>

> [@roboto](#):
>
> Just wondering if you’ve considered using log function instead since we can use additions in logs

I dont have a good intuition for those math concepts so I havent thought about doing it that way,

---

<div class="post-metadata">

**Author:** ![roboto](https://avatars.discourse-cdn.com/v4/letter/r/b77776/32.png) [@roboto](https://discourse.numenta.org/u/roboto)\
**Post date:** [September 16, 2022, 5:53pm UTC](https://discourse.numenta.org/t/proportional-scalar-encoder/10104/7 "2022-09-16T17:53:02Z")

</div>

Oh ok that log thing I thought was a natural extension to your code so i thought that maybe it was more expensive in terms of computation and that’s why it wasn’t used. Anyways it was a clever code.
