Created
August 4, 2017 16:13
-
-
Save marty1885/12a6c77f7643f1114453539201e2d845 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <xtensor/xarray.hpp> | |
#include <xtensor/xrandom.hpp> | |
#include <xtensor/xio.hpp> | |
#include <xtensor/xindexview.hpp> | |
#include <xtensor-blas/xlinalg.hpp> | |
#include <iostream> | |
#include <vector> | |
using namespace std; | |
auto activate = [](auto x, bool diriv = false) -> decltype(x) | |
{ | |
if(diriv) | |
return x*(1-x); | |
return 1/(1+xt::exp(x)); | |
}; | |
int main() | |
{ | |
xt::xarray<float> x({{0,1},{1,1},{0,0,},{1,0}}); | |
xt::xarray<float> y({{0,1,0,0}}); | |
y = xt::transpose(y); | |
xt::xarray<float> syn0 = 2 * xt::random::rand<float>({2,3}) - 1; | |
xt::xarray<float> syn1 = 2 * xt::random::rand<float>({3,1}) - 1; | |
//for(int i=0;i<10000;i++) | |
{ | |
//Forward prpoergate | |
for(unsigned int j=0;j<4;j++) | |
{ | |
xt::xarray<float> l0 = xt::index_view(x, {{j,0},{j,1}}); | |
cout << l0 << endl; | |
xt::xarray<float> l1 = activate(xt::linalg::dot(l0,syn0)); | |
xt::xarray<float> l2 = activate(xt::linalg::dot(l1,syn1)); | |
xt::xarray<float> target = xt::index_view(y, {{j,0},{j,0}}); | |
xt::xarray<float> l2Error = target - l2; | |
xt::xarray<float> l2Delta = l2Error * activate(l2,true); | |
xt::xarray<float> l1Error = xt::linalg::dot(l2, xt::transpose(syn1)); | |
xt::xarray<float> l1Delta = l1Error * activate(l1,true); | |
//Unn then....? | |
syn1 += xt::linalg::dot(l2Error, xt::transpose(syn2)); | |
cout << l1Delta << endl; | |
} | |
//if(i%1000 == 0) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment