// Layer ver 2.0 #include "Layer.h" #include void Layer::softmax() { int y; int overflow=0; Float max=A[0]; int amax=0; Float norm=0; for (y=0; y85) { overflow=1; } else { norm += (Float)exp(A[y]); } if (A[y]>max) { max = A[y]; amax = y; } } if (overflow) { memset(Y, 0, NY*sizeof(Float)); Y[amax]=1.0; return; } else { for (y=0; y> NY; is >> NU; is >> NUr; NK=new int[NU+NUr]; for (u=0; u> NK[u]; alloc(NY,NU+NUr,NK); for (y=0; y> W[y][u][k]; } } is >> B[y]; } level=0; } void Layer::read(istream& is) { int y,u,k; is >> NY; is >> NU; is >> NUr; for (u=0; u> NK[u]; for (y=0; y> W[y][u][k]; } } is >> B[y]; } level=0; } void Layer::write(ostream& os) { int y,u,k; os << NY << "\n"; os << NU << "\n"; os << NUr << "\n"; for (u=0; u=0) a += W[y][u][I[u]]; } Y[y]=A[y]=a; } } void Layer::forward(Float* I) { int y,u,k,i; for (y=0; y=0) a += W[y][u][I1[u]]; } for (u=NU; u=2 this is an output layer // so rbackprop[] is the target vector. else { for (y=0; y=2) { err=f_cost(rbackprop); } else { for (int yyy=0;yyy=0) { dW[y][u][I[u]] += delta[y]; } } dB[y] += delta[y]; } } void Layer::gradient(Float* I) { int y,u,k; int i; for (y=0; y=0) { dW[y][u][I1[u]] += delta[y]; } } i=0; for (u=NU; u0) return 1.0; if (a<0) return -1.0; return 0.0; } void Layer::resetGradient() { int y,u; for (y=0; y80) { overflow=1; } else sum += (Float)exp(A[y]); } if (qua == -1) cerr << " AMBIGUITY!! "; Float parz; if (overflow) { parz=0.0; } else parz = (Float)exp(a)/sum; return (parz - parz*parz); } Float Layer_soft::f_cost(Float* t) { return Layer::log_cost(t); } // Layer_tanh Float Layer_tanh::f1(Float a) { return 1.0-(Float)(tanh(a)*tanh(a)); } Float Layer_tanh::f_cost(Float* t) { return Layer::sq_cost(t); } // Selector void Selector::forward(Float* I) { int y; for (y=0; ymaxW)) { maxW=W[y][u][0]; arg=u; } } past[arg]=1; edges[y]=arg; // cout << y << " " << edges[y] << "\n"; } delete[] past; } Float Selector::f1(Float a) { return 1.0; } Float Selector::f_cost(Float* t) { return Layer::sq_cost(t); }