/// includes tests for getNumberOfStates/Inputs/Outputs() /// and confirms generate(), removeTransition(), removeState() void tGenerateSaveLoad() { DEBUG_MSG("Generate: %s", machineTypeNames[fsm->getType()]); fsm->generate(0, 0, 0);// = create(1,1,1) is minimum /// ERR: <type>::generate - the number of states needs to be greater than 0 (set to 1) /// ERR: <type>::generate - the number of inputs needs to be greater than 0 (set to 1) /// ERR: <type>::generate - the number of outputs needs to be greater than 0 (set to 1) ARE_EQUAL(fsm->getNumberOfStates(), state_t(1), "The number of states is not correct."); ARE_EQUAL(fsm->getNumberOfInputs(), input_t(1), "The number of inputs is not correct."); ARE_EQUAL(fsm->getNumberOfOutputs(), output_t(1), "The number of outputs is not correct."); tSaveLoad(); fsm->generate(5, 3, 2); ARE_EQUAL(fsm->getNumberOfStates(), state_t(5), "The number of states is not correct."); ARE_EQUAL(fsm->getNumberOfInputs(), input_t(3), "The number of inputs is not correct."); ARE_EQUAL(fsm->getNumberOfOutputs(), output_t(2), "The number of outputs is not correct."); tSaveLoad(); fsm->removeTransition(0, 0); fsm->removeTransition(1, 1); fsm->removeTransition(2, 2); /// 3 transitions * 2 machines = 6 ERRs if fsm->isOutputTransition() /// <type>::getOutput - there is no such transition fsm->removeState(3); /// if fsm->isOutputTransition() && there was a transition to state 3, then 2 ERRs: /// <type>::getOutput - there is no such transition tSaveLoad(); }
/// includes tests for getNumberOfStates/Inputs/Outputs() /// and confirms create(), removeTransition() void tCreateSaveLoad() { DEBUG_MSG("Create: %s", machineTypeNames[fsm->getType()]); fsm->create(0, 0, 0);// = create(1,0,0) is minimum /// ERR: <type>::create - the number of states needs to be greater than 0 (set to 1) ARE_EQUAL(fsm->getNumberOfStates(), state_t(1), "The number of states is not correct."); ARE_EQUAL(fsm->getNumberOfInputs(), input_t(0), "The number of inputs is not correct."); ARE_EQUAL(fsm->getNumberOfOutputs(), output_t(0), "The number of outputs is not correct."); tSaveLoad(); fsm->create(5, 3, 2); ARE_EQUAL(fsm->getNumberOfStates(), state_t(5), "The number of states is not correct."); ARE_EQUAL(fsm->getNumberOfInputs(), input_t(3), "The number of inputs is not correct."); ARE_EQUAL(fsm->getNumberOfOutputs(), output_t(2), "The number of outputs is not correct."); /// 5 states * 3 inputs * 2 machines = 30 ERRs if fsm->isOutputTransition() /// <type>::getOutput - there is no such transition tSaveLoad(); fsm->setTransition(0, 0, 0); fsm->setTransition(0, 1, 1); fsm->setTransition(0, 2, 2); fsm->setTransition(1, 0, 3); fsm->setTransition(2, 1, 4); fsm->setTransition(3, 2, 0); fsm->setTransition(4, 0, 2); /// (5 states * 3 inputs - 7 transitions) * 2 machines = 16 ERRs if fsm->isOutputTransition() /// <type>::getOutput - there is no such transition tSaveLoad(); }
bool input() { if ( ! ( input_nk() ) ) return false; input_t(); input_e(); return true; }
IdentifierModel<Adapter>::IdentifierModel( const RCP<const Adapter> &ia, const RCP<const Environment> &env, const RCP<const Comm<int> > &comm, modelFlag_t &modelFlags): numGlobalIdentifiers_(), env_(env), comm_(comm), gids_(), nUserWeights_(0), weights_() { // Get the local and global problem size size_t nLocalIds = ia->getLocalNumIDs(); gno_t lsum = nLocalIds; reduceAll<int, gno_t>(*comm_, Teuchos::REDUCE_SUM, 1, &lsum, &numGlobalIdentifiers_); // Get the number of weights // Use max number of weights over all processes as nUserWeights_ int tmp = ia->getNumWeightsPerID(); Teuchos::reduceAll<int, int>(*comm, Teuchos::REDUCE_MAX, 1, &tmp, &nUserWeights_); // Prepare to store views from input adapter // TODO: Do we have to store these views, or can we get them on an // TODO: as-needed basis? Array<const scalar_t *> wgts(nUserWeights_, (const scalar_t *)NULL); Array<int> wgtStrides(nUserWeights_, 0); if (nUserWeights_ > 0){ input_t *w = new input_t [nUserWeights_]; weights_ = arcp<input_t>(w, 0, nUserWeights_); } const gno_t *gids=NULL; // Get the input adapter's views try{ ia->getIDsView(gids); for (int idx=0; idx < nUserWeights_; idx++) ia->getWeightsView(wgts[idx], wgtStrides[idx], idx); } Z2_FORWARD_EXCEPTIONS; if (nLocalIds){ gids_ = arcp(gids, 0, nLocalIds, false); if (nUserWeights_ > 0){ for (int idx=0; idx < nUserWeights_; idx++){ ArrayRCP<const scalar_t> wgtArray(wgts[idx], 0, nLocalIds*wgtStrides[idx], false); weights_[idx] = input_t(wgtArray, wgtStrides[idx]); } } } env_->memory("After construction of identifier model"); }
int main(int argc, char* argv[]) { Mnist mnist; auto in = mnist.readInputFile("./resources/train-images-idx3-ubyte"); auto out = mnist.readLabelFile("./resources/train-labels-idx1-ubyte"); auto in_t = mnist.readInputFile("./resources/t10k-images-idx3-ubyte"); auto out_t = mnist.readLabelFile("./resources/t10k-labels-idx1-ubyte"); std::vector<std::vector<float>> input(in.size()); std::vector<std::vector<float>> label(out.size()); std::vector<std::vector<float>> input_t(in_t.size()); for(int i = 0; i < input.size(); ++i) { input[i] = std::vector<float>(in[i].size()); for(int j = 0; j < in[i].size(); ++j) { input[i][j] = in[i][j] / 255.0; } } for(int i = 0; i < input_t.size(); ++i) { input_t[i] = std::vector<float>(in_t[i].size()); for(int j = 0; j < in_t[i].size(); ++j) { input_t[i][j] = in_t[i][j] / 255.0; } } for(int i = 0; i < label.size(); ++i) { label[i] = Utility::to_categorical((int)out[i], 10); } Container model; model.add(new Dense(300, input[0].size())); model.add(new Relu()); model.add(new Dense(label[0].size())); model.add(new Softmax()); model.compile(new CrossEntropy(), new Adam(0.001)); model.train(input, label, 50, 50); auto result = model.predict(input_t); int acr = 0; for(int i = 0; i < result.size(); ++i) { auto max = std::max_element(result[i].begin(), result[i].end()); auto max_id = std::distance(result[i].begin(), max); if(std::abs(max_id - out_t[i]) < 1e-7) { ++acr; } } printf("accuracy = %lf\n", 100.0 * acr / result.size()); return 0; }
IdentifierModel<IdentifierInput<User> >::IdentifierModel( const IdentifierInput<User> *ia, const RCP<const Environment> &env, const RCP<const Comm<int> > &comm, modelFlag_t &modelFlags): gnosAreGids_(false), numGlobalIdentifiers_(), env_(env), comm_(comm), gids_(), userWeightDim_(0), weights_(), gnos_(), gnosConst_() { userWeightDim_ = ia->getNumberOfWeights(); size_t nLocalIds = ia->getLocalNumberOfIdentifiers(); Model<IdentifierInput<User> >::maxCount(*comm, userWeightDim_); Array<const scalar_t *> wgts(userWeightDim_, (const scalar_t *)NULL); Array<int> wgtStrides(userWeightDim_, 0); Array<lno_t> weightArrayLengths(userWeightDim_, 0); if (userWeightDim_ > 0){ input_t *w = new input_t [userWeightDim_]; weights_ = arcp<input_t>(w, 0, userWeightDim_); } const gid_t *gids=NULL; try{ ia->getIdentifierList(gids); for (int dim=0; dim < userWeightDim_; dim++) ia->getIdentifierWeights(dim, wgts[dim], wgtStrides[dim]); } Z2_FORWARD_EXCEPTIONS; if (nLocalIds){ gids_ = arcp(gids, 0, nLocalIds, false); if (userWeightDim_ > 0){ for (int i=0; i < userWeightDim_; i++){ if (wgts[i] != NULL){ ArrayRCP<const scalar_t> wgtArray( wgts[i], 0, nLocalIds*wgtStrides[i], false); weights_[i] = input_t(wgtArray, wgtStrides[i]); weightArrayLengths[i] = nLocalIds; } } } } this->setWeightArrayLengths(weightArrayLengths, *comm_); RCP<const idmap_t> idMap; try{ if (modelFlags.test(IDS_MUST_BE_GLOBALLY_CONSECUTIVE)) idMap = rcp(new idmap_t(env_, comm_, gids_, true)); else idMap = rcp(new idmap_t(env_, comm_, gids_, false)); } Z2_FORWARD_EXCEPTIONS; gnosAreGids_ = idMap->gnosAreGids(); this->setIdentifierMap(idMap); gno_t lsum = nLocalIds; reduceAll<int, gno_t>(*comm_, Teuchos::REDUCE_SUM, 1, &lsum, &numGlobalIdentifiers_); if (!gnosAreGids_ && nLocalIds>0){ gno_t *tmpGno = new gno_t [nLocalIds]; env_->localMemoryAssertion(__FILE__, __LINE__, nLocalIds, tmpGno); gnos_ = arcp(tmpGno, 0, nLocalIds); try{ ArrayRCP<gid_t> gidsNonConst = arcp_const_cast<gid_t>(gids_); idMap->gidTranslate( gidsNonConst(0,nLocalIds), gnos_(0,nLocalIds), TRANSLATE_APP_TO_LIB); } Z2_FORWARD_EXCEPTIONS; }