/** * Ordinary feed forward pass of a neural network, evaluating the function * f(x) by propagating the activity forward through f. * * @param inputActivation Input data used for evaluating the specified * activity function. * @param outputActivation Datatype to store the resulting output activation. */ void FeedForward(const VecType& inputActivation, VecType& outputActivation) { if (inGate.n_cols < seqLen) { inGate = arma::zeros<MatType>(layerSize, seqLen); inGateAct = arma::zeros<MatType>(layerSize, seqLen); inGateError = arma::zeros<MatType>(layerSize, seqLen); outGate = arma::zeros<MatType>(layerSize, seqLen); outGateAct = arma::zeros<MatType>(layerSize, seqLen); outGateError = arma::zeros<MatType>(layerSize, seqLen); forgetGate = arma::zeros<MatType>(layerSize, seqLen); forgetGateAct = arma::zeros<MatType>(layerSize, seqLen); forgetGateError = arma::zeros<MatType>(layerSize, seqLen); state = arma::zeros<MatType>(layerSize, seqLen); stateError = arma::zeros<MatType>(layerSize, seqLen); cellAct = arma::zeros<MatType>(layerSize, seqLen); } // Split up the inputactivation into the 3 parts (inGate, forgetGate, // outGate). inGate.col(offset) = inputActivation.subvec(0, layerSize - 1); forgetGate.col(offset) = inputActivation.subvec( layerSize, (layerSize * 2) - 1); outGate.col(offset) = inputActivation.subvec( layerSize * 3, (layerSize * 4) - 1); if (peepholes && offset > 0) { inGate.col(offset) += inGatePeepholeWeights % state.col(offset - 1); forgetGate.col(offset) += forgetGatePeepholeWeights % state.col(offset - 1); } VecType inGateActivation = inGateAct.unsafe_col(offset); GateActivationFunction::fn(inGate.unsafe_col(offset), inGateActivation); VecType forgetGateActivation = forgetGateAct.unsafe_col(offset); GateActivationFunction::fn(forgetGate.unsafe_col(offset), forgetGateActivation); VecType cellActivation = cellAct.unsafe_col(offset); StateActivationFunction::fn(inputActivation.subvec(layerSize * 2, (layerSize * 3) - 1), cellActivation); state.col(offset) = inGateAct.col(offset) % cellActivation; if (offset > 0) state.col(offset) += forgetGateAct.col(offset) % state.col(offset - 1); if (peepholes) outGate.col(offset) += outGatePeepholeWeights % state.col(offset); VecType outGateActivation = outGateAct.unsafe_col(offset); GateActivationFunction::fn(outGate.unsafe_col(offset), outGateActivation); OutputActivationFunction::fn(state.unsafe_col(offset), outputActivation); outputActivation = outGateAct.col(offset) % outputActivation; offset = (offset + 1) % seqLen; }
void operator()(VecType x, VecType& y) { // y = x; // printf("Calling Preconditioners::LocalInnerSolver\n"); std::fill(y.begin(),y.end(),typename VecType::value_type(0.)); GMRES(plan, y, x, options, M, context); context.reset(); }
always_inline VecType vec_exp_tanh_float(VecType const & arg) { typedef typename VecType::int_vec int_vec; /* Express e**x = e**g 2**n * = e**g e**( n loge(2) ) * = e**( g + n loge(2) ) */ // black magic VecType x = arg; VecType z = round(VecType(1.44269504088896341f) * x); int_vec n = z.truncate_to_int(); x -= z*VecType(0.693359375f); x -= z*VecType(-2.12194440e-4f); /* Theoretical peak relative error in [-0.5, +0.5] is 3.5e-8. */ VecType p = 1.f + x * (1.00000035762786865234375f + x * (0.4999996721744537353515625f + x * (0.16665561497211456298828125f + x * (4.167006909847259521484375e-2f + x * (8.420792408287525177001953125e-3f + x * 1.386119984090328216552734375e-3f))))); /* multiply by power of 2 */ VecType approx = ldexp_float(p, n); return approx; }
//Read in a KeyGroup (see http://niftools.sourceforge.net/doc/nif/NiKeyframeData.html) void read(NIFStream *nif, bool force=false) { assert(nif); size_t count = nif->getUInt(); if(count == 0 && !force) return; //If we aren't forcing things, make sure that read clears any previous keys if(!force) mKeys.clear(); mInterpolationType = nif->getUInt(); KeyT<T> key; NIFStream &nifReference = *nif; if(mInterpolationType == sLinearInterpolation) { for(size_t i = 0;i < count;i++) { readTimeAndValue(nifReference, key); mKeys.push_back(key); } } else if(mInterpolationType == sQuadraticInterpolation) { for(size_t i = 0;i < count;i++) { readQuadratic(nifReference, key); mKeys.push_back(key); } } else if(mInterpolationType == sTBCInterpolation) { for(size_t i = 0;i < count;i++) { readTBC(nifReference, key); mKeys.push_back(key); } } //XYZ keys aren't actually read here. //data.hpp sees that the last type read was sXYZInterpolation and: // Eats a floating point number, then // Re-runs the read function 3 more times, with force enabled so that the previous values aren't cleared. // When it does that it's reading in a bunch of sLinearInterpolation keys, not sXYZInterpolation. else if(mInterpolationType == sXYZInterpolation) { //Don't try to read XYZ keys into the wrong part if ( count != 1 ) nif->file->fail("XYZ_ROTATION_KEY count should always be '1' . Retrieved Value: "+Ogre::StringConverter::toString(count)); } else if (0 == mInterpolationType) { if (count != 0) nif->file->fail("Interpolation type 0 doesn't work with keys"); } else nif->file->fail("Unhandled interpolation type: "+Ogre::StringConverter::toString(mInterpolationType)); }
hduPlane<T>::hduPlane(const VecType &pt1, const VecType &pt2, const VecType &p3) { const VecType v1 = pt2 - pt1; const VecType v2 = p3 - pt1; m_normal = v1.crossProduct(v2); m_normal.normalize(); m_d = -(m_normal.dotProduct(pt1)); }
/** Dimensionality check during initialization */ bool dimCheck(){ if( Sx_.rows() != Sx_.cols() ){ std::cerr << "Error: MatType must be a square matrix \n"; return false; } if( Sx_.rows() != x_.size() ){ std::cerr << "Error: VecType and MatType dimension mismatch \n"; return false; } nDim_ = x_.size(); return true; }
inline typename VecType::value_type entry_or_zero(const VecType &v, unsigned i) { if (i >= v.size()) return 0; else return v[i]; }
/* * Calculate the output class using the specified input activation. * * @param inputActivations Input data used to calculate the output class. * @param output Output class of the input activation. */ void outputClass(const VecType& inputActivations, VecType& output) { output = arma::zeros<VecType>(inputActivations.n_elem); arma::uword maxIndex; inputActivations.max(maxIndex); output(maxIndex) = 1; }
always_inline VecType vec_sin_float(VecType const & arg) { typedef typename VecType::int_vec int_vec; const typename VecType::float_type four_over_pi = 1.27323954473516268615107010698011489627567716592367; VecType sign = arg & VecType::gen_sign_mask(); VecType abs_arg = arg & VecType::gen_abs_mask(); VecType y = abs_arg * four_over_pi; int_vec j = y.truncate_to_int(); /* cephes: j=(j+1) & (~1) */ j = (j + int_vec(1)) & int_vec(~1); y = j.convert_to_float(); /* sign based on quadrant */ VecType swap_sign_bit = slli(j & int_vec(4), 29); sign = sign ^ swap_sign_bit; /* polynomial mask */ VecType poly_mask = VecType (mask_eq(j & int_vec(2), int_vec(0))); /* black magic */ static float DP1 = 0.78515625; static float DP2 = 2.4187564849853515625e-4; static float DP3 = 3.77489497744594108e-8; VecType base = ((abs_arg - y * DP1) - y * DP2) - y * DP3; /* [0..pi/4] */ VecType z = base * base; VecType p1 = (( 2.443315711809948E-005 * z - 1.388731625493765E-003) * z + 4.166664568298827E-002) * z * z -0.5f * z + 1.0 ; /* [pi/4..pi/2] */ VecType p2 = ((-1.9515295891E-4 * z + 8.3321608736E-3) * z - 1.6666654611E-1) * z * base + base; VecType approximation = select(p1, p2, poly_mask); return approximation ^ sign; }
//========================== // NW N N NE // \ | | / // a-----------b // / | | \ // SW S S SE //========================== int Stick::extrudeLines(const PathType& path, float width) { if (path.size() < 2) { return EXTRUDE_FAIL; } int pointSize = path.size(); int lineSize = pointSize - 1; _indexList.resize( lineSize*IDEX_FACTOR ); for (int idx=0; idx < lineSize; idx++) { const VecType& a = path[idx]; const VecType& b = path[idx+1]; VecType e = (b-a); e.normalize(); e *= width; VecType N = VecType(-e.y(), e.x(), 0); VecType S = -N; VecType NE = N + e; VecType NW = N - e; VecType SW = -NE; VecType SE = -NW; _vertexList.push_back( Vertex(a + SW) ); _vertexList.push_back( Vertex(a + NW) ); _vertexList.push_back( Vertex(a + S) ); _vertexList.push_back( Vertex(a + N) ); _vertexList.push_back( Vertex(b + S) ); _vertexList.push_back( Vertex(b + N) ); _vertexList.push_back( Vertex(b + SE) ); _vertexList.push_back( Vertex(b + NE) ); } _generateTriangleTexCoord(); _generateTriangesIndices(); return EXTRUDE_SUCCESS; }
inline typename boost::disable_if_c<VecType::has_compare_bitmask, VecType >::type perform(VecType arg) const { typedef VecType vec; vec result; for (int i = 0; i != result.size; ++i) result.set(i, sc_scurve(arg.get(i))); return result; }
always_inline VecType vec_exp_float(VecType const & arg) { typedef typename VecType::int_vec int_vec; /* Express e**x = e**g 2**n * = e**g e**( n loge(2) ) * = e**( g + n loge(2) ) */ // black magic VecType x = arg; VecType z = round(VecType(1.44269504088896341f) * x); int_vec n = z.truncate_to_int(); x -= z*VecType(0.693359375f); x -= z*VecType(-2.12194440e-4f); /* Theoretical peak relative error in [-0.5, +0.5] is 3.5e-8. */ VecType p = VecType(VecType::gen_one()) + x * (1.00000035762786865234375f + x * (0.4999996721744537353515625f + x * (0.16665561497211456298828125f + x * (4.167006909847259521484375e-2f + x * (8.420792408287525177001953125e-3f + x * 1.386119984090328216552734375e-3f))))); /* multiply by power of 2 */ VecType approx = ldexp_float(p, n); /* handle min/max boundaries */ const VecType maxlogf(88.72283905206835f); // const VecType minlogf(-103.278929903431851103f); const VecType minlogf = -maxlogf; const VecType max_float(std::numeric_limits<float>::max()); const VecType zero = VecType::gen_zero(); VecType too_large = mask_gt(arg, maxlogf); VecType too_small = mask_lt(arg, minlogf); VecType ret = select(approx, max_float, too_large); ret = select(ret, zero, too_small); return ret; }
void compare_float_mask(VecType const & v, unsigned int mask) { for (int i = 0; i != v.size; ++i) { union { float f; unsigned int i; } x; x.f = v.get(i); BOOST_REQUIRE_EQUAL( x.i, mask); } }
always_inline VecType vec_tan_float(VecType const & arg) { typedef typename VecType::int_vec int_vec; const typename VecType::float_type four_over_pi = 1.27323954473516268615107010698011489627567716592367; VecType sign = arg & VecType::gen_sign_mask(); VecType abs_arg = arg & VecType::gen_abs_mask(); VecType y = abs_arg * four_over_pi; int_vec j = y.truncate_to_int(); /* cephes: j=(j+1) & (~1) */ j = (j + int_vec(1)) & int_vec(~1); y = j.convert_to_float(); /* approximation mask */ VecType poly_mask = VecType (mask_eq(j & int_vec(2), int_vec(0))); /* black magic */ static float DP1 = 0.78515625; static float DP2 = 2.4187564849853515625e-4; static float DP3 = 3.77489497744594108e-8; VecType base = ((abs_arg - y * DP1) - y * DP2) - y * DP3; VecType x = base; VecType x2 = x*x; // sollya: fpminimax(tan(x), [|3,5,7,9,11,13|], [|24...|], [-pi/4,pi/4], x); VecType approx = x + x * x2 * (0.3333315551280975341796875 + x2 * (0.1333882510662078857421875 + x2 * (5.3409568965435028076171875e-2 + x2 * (2.443529665470123291015625e-2 + x2 * (3.1127030961215496063232421875e-3 + x2 * 9.3892104923725128173828125e-3))))); //VecType recip = -reciprocal(approx); VecType recip = -1.0 / approx; VecType approximation = select(recip, approx, poly_mask); return approximation ^ sign; }
PartialVolumeAnalysisClusteringCalculator::ClusterResultType PartialVolumeAnalysisClusteringCalculator::CalculateCurves(ParamsType params, VecType xVals) const { int arraysz = xVals.size(); ClusterResultType result(arraysz); for( int j=0; j<2; j++) { for(int i=0; i<arraysz; i++) { double d = xVals(i)-params.means[j]; double amp = params.ps[j]/sqrt(2*PVA_PI*params.sigmas[j]); result.vals[j](i) = amp*exp(-0.5 * (d*d)/params.sigmas[j]); } } for(int i=0; i<arraysz; i++) { result.mixedVals[0](i) = 0; } for(double t=0; t<=1; t = t + 1.0/(m_StepsNumIntegration-1.0)) { for(int i=0; i<arraysz; i++) { double d = xVals(i)-(t*params.means[0]+(1-t)*params.means[1]); double v = t*params.sigmas[0]+(1-t)*params.sigmas[1]; double p = 1.0 - params.ps[0] - params.ps[1]; double amp = (1.0/m_StepsNumIntegration) * p / sqrt(2.0*PVA_PI*v); result.mixedVals[0](i) = result.mixedVals[0](i) + amp*exp(-0.5 * (d*d)/v); // MITK_INFO << "d=" << d << "v=" <<v << "p=" << p << "amp=" << amp << "result=" << amp*exp(-0.5 * (d*d)/v) << std::endl; } // MITK_INFO << "t=" << t << std::endl; // params.Print(); // result.Print(); } for(int i=0; i<arraysz; i++) { result.combiVals(i) = result.mixedVals[0](i) + result.vals[0](i) + result.vals[1](i); } return result; }
void read(NIFStream *nif, bool force=false) { size_t count = nif->getInt(); if(count == 0 && !force) return; mInterpolationType = nif->getInt(); mKeys.resize(count); if(mInterpolationType == sLinearInterpolation) { for(size_t i = 0;i < count;i++) { KeyT<T> &key = mKeys[i]; key.mTime = nif->getFloat(); key.mValue = (nif->*getValue)(); } } else if(mInterpolationType == sQuadraticInterpolation) { for(size_t i = 0;i < count;i++) { KeyT<T> &key = mKeys[i]; key.mTime = nif->getFloat(); key.mValue = (nif->*getValue)(); key.mForwardValue = (nif->*getValue)(); key.mBackwardValue = (nif->*getValue)(); } } else if(mInterpolationType == sTBCInterpolation) { for(size_t i = 0;i < count;i++) { KeyT<T> &key = mKeys[i]; key.mTime = nif->getFloat(); key.mValue = (nif->*getValue)(); key.mTension = nif->getFloat(); key.mBias = nif->getFloat(); key.mContinuity = nif->getFloat(); } } else nif->file->warn("Unhandled interpolation type: "+Ogre::StringConverter::toString(mInterpolationType)); }
void Warp::resize_1d(const VecType &in, const InterpType interp_type, double inv_scale, VecType *out) const { // For the anti-aliasing filter double scale = 1.0f/inv_scale; if(scale>1){ scale = 1; } int length = in.rows(); switch(interp_type){ case Warp::NEAREST: { for(double i = 0; i < out->rows(); ++i ) { double u = i*inv_scale + 0.5f*(-1.0f+inv_scale); int l = floor(u); int r = l+1; if(u-l > r-u ) { if((r >=0) && (r<length)){ (*out)(i) = in(r); } }else { if((l >=0) && (l<length)){ (*out)(i) = in(l); } } } break; } case Warp::BILINEAR: { int kernel_width = 1; for(double i = 0; i < out->rows(); ++i ) { double u = i*inv_scale + 0.5f*(-1.0f+inv_scale); int l = floor(u-kernel_width/2); int r = l+kernel_width; double val = 0; // if(i == out->rows()-1){ // printf("%f,%d,%d\n",u,l,r); // } double w = 0; double weight = 0; for(int x = l; x<=r; ++x) { if(x<0 || x>=length){ continue; } w = kernel_linear(u-x,scale); val += w*in(x); weight += w; } if(weight>0) { val/= weight; } (*out)(i) = val; } break; } case Warp::BICUBIC: { int kernel_width = 4; for(double i = 0; i < out->rows(); ++i ) { double u = i*inv_scale + 0.5f*(-1.0f+inv_scale); int l = floor(u-kernel_width/2); int r = l+kernel_width; double val = 0; double w = 0; double weight = 0; for(int x = l; x<=r; ++x) { if(x<0 || x>=length){ continue; } w = kernel_cubic(u-x,scale); val += w*in(x); weight += w; // printf("%d, %.2f ", x, w); } // printf("\n"); if(weight>0) { val/= weight; } (*out)(i) = val; } break; } } // switch }
int RoadComposer::extrude_lines(const Point* point_list, int point_num, int width, float (&tex_coord)[4], V2F2F* vertex_page, int vertex_size, IType* index_page, int index_size, int index_off) { if (point_num < 2) { return EXTRUDE_FAIL; } int line_size = point_num - 1; int v_cursor = 0; for (int idx=0; idx < line_size; idx++) { const Point& p0 = point_list[idx]; const Point& p1 = point_list[idx+1]; VecType a(p0.x, p0.y); VecType b(p1.x, p1.y); VecType e = (b-a); e.normalize(); e *= width; VecType N = VecType(-e.y(), e.x()); VecType S = -N; VecType NE = N + e; VecType NW = N - e; VecType SW = -NE; VecType SE = -NW; if (v_cursor + LINE_MAX_VERT_SIZE > vertex_size) { return EXTRUDE_FAIL; } ASSIGN_2F_ARRAY( vertex_page[v_cursor].xy, VecType(a + SW).getValue() ); v_cursor++; ASSIGN_2F_ARRAY( vertex_page[v_cursor].xy, VecType(a + NW).getValue() ); v_cursor++; ASSIGN_2F_ARRAY( vertex_page[v_cursor].xy, VecType(a + S).getValue() ); v_cursor++; ASSIGN_2F_ARRAY( vertex_page[v_cursor].xy, VecType(a + N).getValue() ); v_cursor++; ASSIGN_2F_ARRAY( vertex_page[v_cursor].xy, VecType(b + S).getValue() ); v_cursor++; ASSIGN_2F_ARRAY( vertex_page[v_cursor].xy, VecType(b + N).getValue() ); v_cursor++; if (idx==line_size-1) { // 最后一段增加尾部 ASSIGN_2F_ARRAY( vertex_page[v_cursor].xy, VecType(b + S).getValue() ); v_cursor++; ASSIGN_2F_ARRAY( vertex_page[v_cursor].xy, VecType(b + N).getValue() ); v_cursor++; ASSIGN_2F_ARRAY( vertex_page[v_cursor].xy, VecType(b + SE).getValue() ); v_cursor++; ASSIGN_2F_ARRAY( vertex_page[v_cursor].xy, VecType(b + NE).getValue() ); v_cursor++; } } _generate_triangle_texCoord(line_size, vertex_size, vertex_page, tex_coord); return _generate_trianges_indices(line_size, index_size, index_page, index_off);; }
VecType2 topLeft() const { return position.xy(); }
bool Legalizer::runOnMachineFunction(MachineFunction &MF) { // If the ISel pipeline failed, do not bother running that pass. if (MF.getProperties().hasProperty( MachineFunctionProperties::Property::FailedISel)) return false; DEBUG(dbgs() << "Legalize Machine IR for: " << MF.getName() << '\n'); init(MF); const TargetPassConfig &TPC = getAnalysis<TargetPassConfig>(); MachineOptimizationRemarkEmitter MORE(MF, /*MBFI=*/nullptr); LegalizerHelper Helper(MF); // FIXME: an instruction may need more than one pass before it is legal. For // example on most architectures <3 x i3> is doubly-illegal. It would // typically proceed along a path like: <3 x i3> -> <3 x i8> -> <8 x i8>. We // probably want a worklist of instructions rather than naive iterate until // convergence for performance reasons. bool Changed = false; MachineBasicBlock::iterator NextMI; using VecType = SmallSetVector<MachineInstr *, 8>; VecType WorkList; VecType CombineList; for (auto &MBB : MF) { for (auto MI = MBB.begin(); MI != MBB.end(); MI = NextMI) { // Get the next Instruction before we try to legalize, because there's a // good chance MI will be deleted. NextMI = std::next(MI); // Only legalize pre-isel generic instructions: others don't have types // and are assumed to be legal. if (!isPreISelGenericOpcode(MI->getOpcode())) continue; unsigned NumNewInsns = 0; WorkList.clear(); CombineList.clear(); Helper.MIRBuilder.recordInsertions([&](MachineInstr *MI) { // Only legalize pre-isel generic instructions. // Legalization process could generate Target specific pseudo // instructions with generic types. Don't record them if (isPreISelGenericOpcode(MI->getOpcode())) { ++NumNewInsns; WorkList.insert(MI); CombineList.insert(MI); } }); WorkList.insert(&*MI); LegalizerCombiner C(Helper.MIRBuilder, MF.getRegInfo(), Helper.getLegalizerInfo()); bool Changed = false; LegalizerHelper::LegalizeResult Res; do { assert(!WorkList.empty() && "Expecting illegal ops"); while (!WorkList.empty()) { NumNewInsns = 0; MachineInstr *CurrInst = WorkList.pop_back_val(); Res = Helper.legalizeInstrStep(*CurrInst); // Error out if we couldn't legalize this instruction. We may want to // fall back to DAG ISel instead in the future. if (Res == LegalizerHelper::UnableToLegalize) { Helper.MIRBuilder.stopRecordingInsertions(); if (Res == LegalizerHelper::UnableToLegalize) { reportGISelFailure(MF, TPC, MORE, "gisel-legalize", "unable to legalize instruction", *CurrInst); return false; } } Changed |= Res == LegalizerHelper::Legalized; // If CurrInst was legalized, there's a good chance that it might have // been erased. So remove it from the Combine List. if (Res == LegalizerHelper::Legalized) CombineList.remove(CurrInst); #ifndef NDEBUG if (NumNewInsns) for (unsigned I = WorkList.size() - NumNewInsns, E = WorkList.size(); I != E; ++I) DEBUG(dbgs() << ".. .. New MI: " << *WorkList[I];); #endif } // Do the combines. while (!CombineList.empty()) { NumNewInsns = 0; MachineInstr *CurrInst = CombineList.pop_back_val(); SmallVector<MachineInstr *, 4> DeadInstructions; Changed |= C.tryCombineInstruction(*CurrInst, DeadInstructions); for (auto *DeadMI : DeadInstructions) { DEBUG(dbgs() << ".. Erasing Dead Instruction " << *DeadMI); CombineList.remove(DeadMI); WorkList.remove(DeadMI); DeadMI->eraseFromParent(); } #ifndef NDEBUG if (NumNewInsns) for (unsigned I = CombineList.size() - NumNewInsns, E = CombineList.size(); I != E; ++I) DEBUG(dbgs() << ".. .. Combine New MI: " << *CombineList[I];); #endif } } while (!WorkList.empty()); Helper.MIRBuilder.stopRecordingInsertions(); }
/** * Ordinary feed backward pass of a neural network, calculating the function * f(x) by propagating x backwards trough f. Using the results from the feed * forward pass. * * @param inputActivation Input data used for calculating the function f(x). * @param error The backpropagated error. * @param delta The calculating delta using the partial derivative of the * error with respect to a weight. */ void FeedBackward(const VecType& /* unused */, const VecType& error, VecType& delta) { size_t queryOffset = seqLen - offset - 1; VecType outGateDerivative; GateActivationFunction::deriv(outGateAct.unsafe_col(queryOffset), outGateDerivative); VecType stateActivation; StateActivationFunction::fn(state.unsafe_col(queryOffset), stateActivation); outGateError.col(queryOffset) = outGateDerivative % error % stateActivation; VecType stateDerivative; StateActivationFunction::deriv(stateActivation, stateDerivative); stateError.col(queryOffset) = error % outGateAct.col(queryOffset) % stateDerivative; if (queryOffset < (seqLen - 1)) { stateError.col(queryOffset) += stateError.col(queryOffset + 1) % forgetGateAct.col(queryOffset + 1); if (peepholes) { stateError.col(queryOffset) += inGateError.col(queryOffset + 1) % inGatePeepholeWeights; stateError.col(queryOffset) += forgetGateError.col(queryOffset + 1) % forgetGatePeepholeWeights; } } if (peepholes) { stateError.col(queryOffset) += outGateError.col(queryOffset) % outGatePeepholeWeights; } VecType cellDerivative; StateActivationFunction::deriv(cellAct.col(queryOffset), cellDerivative); VecType cellError = inGateAct.col(queryOffset) % cellDerivative % stateError.col(queryOffset); if (queryOffset > 0) { VecType forgetGateDerivative; GateActivationFunction::deriv(forgetGateAct.col(queryOffset), forgetGateDerivative); forgetGateError.col(queryOffset) = forgetGateDerivative % stateError.col(queryOffset) % state.col(queryOffset - 1); } VecType inGateDerivative; GateActivationFunction::deriv(inGateAct.col(queryOffset), inGateDerivative); inGateError.col(queryOffset) = inGateDerivative % stateError.col(queryOffset) % cellAct.col(queryOffset); if (peepholes) { outGateDerivative += outGateError.col(queryOffset) % state.col(queryOffset); if (queryOffset > 0) { inGatePeepholeDerivatives += inGateError.col(queryOffset) % state.col(queryOffset - 1); forgetGatePeepholeDerivatives += forgetGateError.col(queryOffset) % state.col(queryOffset - 1); } } delta = arma::zeros<VecType>(layerSize * 4); delta.subvec(0, layerSize - 1) = inGateError.col(queryOffset); delta.subvec(layerSize, (layerSize * 2) - 1) = forgetGateError.col(queryOffset); delta.subvec(layerSize * 2, (layerSize * 3) - 1) = cellError; delta.subvec(layerSize * 3, (layerSize * 4) - 1) = outGateError.col(queryOffset); offset = (offset + 1) % seqLen; if (peepholes && offset == 0) { inGatePeepholeGradient = (inGatePeepholeWeights.t() * (inGateError.col(queryOffset) % inGatePeepholeDerivatives)) * inGate.col(queryOffset).t(); forgetGatePeepholeGradient = (forgetGatePeepholeWeights.t() * (forgetGateError.col(queryOffset) % forgetGatePeepholeDerivatives)) * forgetGate.col(queryOffset).t(); outGatePeepholeGradient = (outGatePeepholeWeights.t() * (outGateError.col(queryOffset) % outGatePeepholeDerivatives)) * outGate.col(queryOffset).t(); inGatePeepholeOptimizer->UpdateWeights(inGatePeepholeWeights, inGatePeepholeGradient.t(), 0); forgetGatePeepholeOptimizer->UpdateWeights(forgetGatePeepholeWeights, forgetGatePeepholeGradient.t(), 0); outGatePeepholeOptimizer->UpdateWeights(outGatePeepholeWeights, outGatePeepholeGradient.t(), 0); inGatePeepholeDerivatives.zeros(); forgetGatePeepholeDerivatives.zeros(); outGatePeepholeDerivatives.zeros(); } }
void read(NIFStream *nif, bool force=false) { size_t count = nif->getInt(); if(count == 0 && !force) return; mInterpolationType = nif->getInt(); mKeys.resize(count); if(mInterpolationType == sLinearInterpolation) { for(size_t i = 0;i < count;i++) { KeyT<T> &key = mKeys[i]; key.mTime = nif->getFloat(); key.mValue = (nif->*getValue)(); } } else if(mInterpolationType == sQuadraticInterpolation) { for(size_t i = 0;i < count;i++) { KeyT<T> &key = mKeys[i]; key.mTime = nif->getFloat(); key.mValue = (nif->*getValue)(); key.mForwardValue = (nif->*getValue)(); key.mBackwardValue = (nif->*getValue)(); } } else if(mInterpolationType == sTBCInterpolation) { for(size_t i = 0;i < count;i++) { KeyT<T> &key = mKeys[i]; key.mTime = nif->getFloat(); key.mValue = (nif->*getValue)(); key.mTension = nif->getFloat(); key.mBias = nif->getFloat(); key.mContinuity = nif->getFloat(); } } //\FIXME This now reads the correct amount of data in the file, but doesn't actually do anything with it. else if(mInterpolationType == sXYZInterpolation) { if (count != 1) { nif->file->fail("count should always be '1' for XYZ_ROTATION_KEY. Retrieved Value: "+Ogre::StringConverter::toString(count)); return; } //KeyGroup (see http://niftools.sourceforge.net/doc/nif/NiKeyframeData.html) //Chomp unknown and possibly unused float nif->getFloat(); for(size_t i=0;i<3;++i) { unsigned int numKeys = nif->getInt(); if(numKeys != 0) { int interpolationTypeAgain = nif->getInt(); if( interpolationTypeAgain != sLinearInterpolation) { nif->file->fail("XYZ_ROTATION_KEY's KeyGroup keyType must be '1' (Linear Interpolation). Retrieved Value: "+Ogre::StringConverter::toString(interpolationTypeAgain)); return; } for(size_t j = 0;j < numKeys;j++) { //For now just chomp these nif->getFloat(); nif->getFloat(); } } nif->file->warn("XYZ_ROTATION_KEY read, but not used!"); } } else if (mInterpolationType == 0) { if (count != 0) nif->file->fail("Interpolation type 0 doesn't work with keys"); } else nif->file->fail("Unhandled interpolation type: "+Ogre::StringConverter::toString(mInterpolationType)); }
VecType2 bottomLeft() const { return position.xy() + VecType2(0, size.y);}
void writeGlobalArray(std::ostream& os, const string& prefix, const VecType& g) { for (size_t i = 0; i < g.size(); ++i) { os << prefix << g[i].type << " " << g[i].name << ";\n"; } }
void DecisionStump<MatType>::TrainOnDim(const VecType& dimension, const arma::Row<size_t>& labels) { size_t i, count, begin, end; arma::rowvec sortedSplitDim = arma::sort(dimension); arma::uvec sortedSplitIndexDim = arma::stable_sort_index(dimension.t()); arma::Row<size_t> sortedLabels(dimension.n_elem); sortedLabels.fill(0); for (i = 0; i < dimension.n_elem; i++) sortedLabels(i) = labels(sortedSplitIndexDim(i)); arma::rowvec subCols; double mostFreq; i = 0; count = 0; while (i < sortedLabels.n_elem) { count++; if (i == sortedLabels.n_elem - 1) { begin = i - count + 1; end = i; mostFreq = CountMostFreq(sortedLabels.cols(begin, end)); split.resize(split.n_elem + 1); split(split.n_elem - 1) = sortedSplitDim(begin); binLabels.resize(binLabels.n_elem + 1); binLabels(binLabels.n_elem - 1) = mostFreq; i++; } else if (sortedLabels(i) != sortedLabels(i + 1)) { if (count < bucketSize) { // Test for different values of bucketSize, especially extreme cases. begin = i - count + 1; end = begin + bucketSize - 1; if (end > sortedLabels.n_elem - 1) end = sortedLabels.n_elem - 1; } else { begin = i - count + 1; end = i; } // Find the most frequent element in subCols so as to assign a label to // the bucket of subCols. mostFreq = CountMostFreq(sortedLabels.cols(begin, end)); split.resize(split.n_elem + 1); split(split.n_elem - 1) = sortedSplitDim(begin); binLabels.resize(binLabels.n_elem + 1); binLabels(binLabels.n_elem - 1) = mostFreq; i = end + 1; count = 0; } else i++; } // Now trim the split matrix so that buckets one after the after which point // to the same classLabel are merged as one big bucket. MergeRanges(); }
VecType2 topRight() const { return position.xy() + VecType2(size.x, 0); }
inline void setVec(DblVec& x, const VarVector& vars, const VecType& vals) { assert(vars.size() == vals.size()); for (int i = 0; i < vars.size(); ++i) { x[vars[i].var_rep->index] = vals[i]; } }
VecType2 bottomRight() const { return position.xy() + size.xy(); }