Skip to content

Instantly share code, notes, and snippets.

View marty1885's full-sized avatar
πŸ‘¨β€πŸ’»
Writing code

Martin Chang marty1885

πŸ‘¨β€πŸ’»
Writing code
View GitHub Profile
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
size_t hidden_size = 30;
size_t seq_len = 3; //The length of traning srquence
network<sequential> nn;
nn << recurrent(lstm(3, hidden_size), seq_len);
nn << leaky_relu() << fc(hidden_size) << softmax();
xt::xarray<float> compute(xt::xarray<float> input)
{
assert(input.size() == 3);
//save data for traning
if(last_input_.size() != 0) {
for(auto v : last_input_)
input_.push_back(v);
for(auto v : input)
output_.push_back(v);
}
TemporalMemory tm(3*ENCODE_WIDTH, TP_DEPTH);
tm->setMaxNewSynapseCount(64);
tm->setPermanenceIncrement(0.1);
tm->setPermanenceDecrement(0.045);
tm->setConnectedPermanence(0.4);
tm->setPredictedSegmentDecrement(0.3*2.0f*tm_->getPermanenceIncrement());
xt::xarray<float> compute(int last_oppo_move, bool learn = true)
{
auto out = tm.compute(encode(last_oppo_move), true); //true means enable learning
return categroize(3, ENCODE_WIDTH, out); //Convert from SDR to properablity distribution
}
@marty1885
marty1885 / main.cpp
Last active December 10, 2018 05:09
int main()
{
//Initialize both AI
RNNPlayer player1;
HTMPlayer player2;
int rnn_last_move = 0;
int htm_last_move = 0;
size_t rnn_win = 0;
//build: c++ tmperf.cpp -o tmperf -O3 -lnupic_core
//usage: ./tmperf
#include <nupic/algorithms/TemporalMemory.hpp>
#include <chrono>
#include <vector>
#include <random>
using TemporalMemory = nupic::algorithms::temporal_memory::TemporalMemory; //Yeah.... This is too redundent
#include "HTMHelper.hpp"
using namespace HTM;
int main()
{
TemporalMemory tm({16}, 8);
CategoryEncoder encoder(2, 8);
std::vector<size_t> seq = {0,0,1,1};
[marty@zack scripts]$ make clean
[marty@zack scripts]$ make -j6
[ 1%] Building CXX object src/CMakeFiles/nupic_core.dir/nupic/algorithms/BacktrackingTMCpp.cpp.o
[ 3%] Building CXX object src/CMakeFiles/nupic_core.dir/nupic/algorithms/Anomaly.cpp.o
[ 3%] Building CXX object src/CMakeFiles/nupic_core.dir/nupic/algorithms/AnomalyLikelihood.cpp.o
[ 4%] Building CXX object src/CMakeFiles/nupic_core.dir/nupic/algorithms/ClassifierResult.cpp.o
[ 5%] Building CXX object src/CMakeFiles/nupic_core.dir/nupic/algorithms/Cells4.cpp.o
[ 6%] Building CXX object src/CMakeFiles/nupic_core.dir/nupic/algorithms/Cell.cpp.o
[ 7%] Building CXX object src/CMakeFiles/nupic_core.dir/nupic/algorithms/Connections.cpp.o
[ 7%] Building CXX object src/CMakeFiles/nupic_core.dir/nupic/algorithms/InSynapse.cpp.o