Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SDR Classifier without densematrix #170

Merged
merged 18 commits into from
Dec 19, 2018
Merged

Conversation

breznak
Copy link
Member

@breznak breznak commented Dec 17, 2018

as DenseMatrix is to be removed.
Uses map<UInt, map<UInt, Real>> instead.

Blocks more cleanup in #169

as DenseMatrix is to be removed.
Uses map<UInt, map<UInt, Real>> instead.
@breznak breznak added the code code enhancement, optimization, cleanup..programmer stuff label Dec 17, 2018
@breznak breznak added this to the cleanup milestone Dec 17, 2018
Copy link
Member Author

@breznak breznak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First draft, looking for comments, esp for Matrix typedef.

  • already compiles
  • c++ tests fail on SDR Classifier (expected)
    • py tests pass (no testing :( )

src/nupic/algorithms/SDRClassifier.cpp Outdated Show resolved Hide resolved
src/nupic/algorithms/SDRClassifier.cpp Outdated Show resolved Hide resolved
src/nupic/algorithms/SDRClassifier.cpp Outdated Show resolved Hide resolved
src/nupic/algorithms/SDRClassifier.cpp Outdated Show resolved Hide resolved
src/nupic/algorithms/SDRClassifier.cpp Outdated Show resolved Hide resolved
src/nupic/algorithms/SDRClassifier.cpp Outdated Show resolved Hide resolved
src/nupic/algorithms/SDRClassifier.cpp Outdated Show resolved Hide resolved

typedef Dense<UInt, Real64> Matrix;
typedef std::map<UInt, std::map<UInt, Real64>> Matrix; //Matrix[r][c] = 0.0d
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm looking for comments on implementation of this 2D matrix. It's a mapping from 2d [UInt][UInt] -> Real64;

  • used to be DenseMatrix
  • now considering:
    • map<int, map<int, double>>
    • Eigen: Matrix<Real64, Dynamic, Dynamic>
    • plain Real64[][] //we sometimes have to grow it
    • vector<vector>

I'm inclining to eigen::Matrix as we need here concept of rows, cols.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for now implemented using native map<int, map<int, double>> map2d;
Eigen would be optional improvement for the future, if needed.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this matrix type. I prefer the standard library types because they're understood by everyone.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree std:: classes are always good but I don't have any experience with eigen so I cannot say which is better.

@breznak breznak mentioned this pull request Dec 17, 2018
11 tasks
@breznak breznak self-assigned this Dec 17, 2018
Copy link
Member Author

@breznak breznak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RFC on softmax_()

for (auto itr = begin; itr != end; ++itr) {
*itr -= *maxItr;

void SDRClassifier::softmax_(const vector<Real64>::iterator begin,
Copy link
Member Author

@breznak breznak Dec 18, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@htm-community/nupic-cpp I'd like your help reviewing this softmax_ function. I think I found an error in upstream implementation. It is not a big deal (didn't even have a test to spot it, but let's make sure it's right)

  • I'm reworking SDRClassifier, and its softmax_()

  • wrote test SDRClassifierTest::testSoftmax to compare results (with results obtained from master) -> and we diff!

  • the "original" softmax_ version from master uses:

    • range_exp() from math/ArrayAlgo.hpp
      • Exp() from math/Math.hpp
    • accumulate() - I'm not sure if std::, or something from SparseMatrix::accumulate

Debug results from master:

max=2.018
in=0
in=1
in=1.337
in=2.018
in=1.1
in=0.5
in=0.9
exp=0.132921
exp=0.361317
exp=0.506111
exp=1
exp=3.00417
exp=1.64872
exp=2.4596
sum=9.11284
div=0.0145861
div=0.0396492
div=0.0555382
div=0.109735
div=0.329663
div=0.180923
div=0.269905
[       OK ] SDRClassifierTest.testSoftmax (1 ms)
[----------] 1 test from SDRClassifierTest (1 ms total)

and from this PR:

max=2.018
in=0
exp=0.132921
in=1
exp=0.361317
in=1.337
exp=0.506111
in=2.018
exp=1
in=1.1
exp=0.399317
in=0.5
exp=0.21915
in=0.9
exp=0.326933
sum=2.94575
div=0.045123
div=0.122657
div=0.171811
div=0.339472
div=0.135557
div=0.0743953
div=0.110985
[  FAILED  ] SDRClassifierTest.testSoftmax

 1 FAILED TEST

You can see a strange behavior in the old/master implementation after "2.018 aka max elemen aka where exp=1":
the old function gives exp > 1, while our does return < 1 (as it should). Because we normalized the values to the max element( x[i]-maxVal) , all values should be <= 1.

TL;DR:

  • not too critical (no test even before)
  • I think ours new is correct, just want to doublecheck

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I went ahead with our version

@breznak breznak added the ready label Dec 18, 2018
@breznak breznak added bug Something isn't working backport_upstream What could be useful for upstream backporting labels Dec 18, 2018
breznak added a commit that referenced this pull request Dec 18, 2018
unused since PR #170 - SDR Classifier uses 2d map
map<int, map<int, double>>
@breznak
Copy link
Member Author

breznak commented Dec 18, 2018

@ctrl-z-9000-times @dkeeney this is now ready, please review when you have time

Copy link

@dkeeney dkeeney left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see anything I object to. Again I am not very familiar with these modules so I have to rely on you two to get it right. :)

@@ -318,20 +340,18 @@ void SDRClassifier::load(istream &inStream) {
// Check the version.
UInt version;
inStream >> version;
NTA_CHECK(version <= 1);
NTA_CHECK(version == 2);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agree, we don't need to be backward compatible with old versions of serialization.


typedef Dense<UInt, Real64> Matrix;
typedef std::map<UInt, std::map<UInt, Real64>> Matrix; //Matrix[r][c] = 0.0d
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree std:: classes are always good but I don't have any experience with eigen so I cannot say which is better.

@dkeeney
Copy link

dkeeney commented Dec 19, 2018

Now that I have a fix for Circle CI PR #176 , perhaps we should push this one more time after the fix is in the master so that we can check this PR in debug mode.

@breznak breznak merged commit 16a5717 into master Dec 19, 2018
@breznak breznak deleted the sdrclassifier_without_densematrix branch December 19, 2018 07:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport_upstream What could be useful for upstream backporting bug Something isn't working code code enhancement, optimization, cleanup..programmer stuff ready
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants