Beispiel #1
0
  void TestNode::serialize(BundleIO& bundle)
  {
    {
      std::ofstream& f = bundle.getOutputStream("main");
      // There is more than one way to do this. We could serialize to YAML, which
      // would make a readable format, or we could serialize directly to the stream
      // Choose the easier one.
      f << "TestNode-v2" << " "
        << nodeCount_ << " "
        << int32Param_ << " "
        << uint32Param_ << " "
        << int64Param_ << " "
        << uint64Param_ << " "
        << real32Param_ << " "
        << real64Param_ << " "
        << boolParam_ << " "
        << outputElementCount_ << " "
        << delta_ << " "
        << iter_ << " ";

      arrayOut(f, real32ArrayParam_, "real32ArrayParam_");
      arrayOut(f, int64ArrayParam_, "int64ArrayParam_");
      arrayOut(f, boolArrayParam_, "boolArrayParam_");
      arrayOut(f, unclonedParam_, "unclonedParam_");
      f << shouldCloneParam_ << " ";

      // outer vector needs to be done by hand.
      f << "unclonedArray ";
      f << unclonedInt64ArrayParam_.size() << " "; // number of nodes
      for (size_t i = 0; i < unclonedInt64ArrayParam_.size(); i++)
      {
        std::stringstream name;
        name << "unclonedInt64ArrayParam[" << i << "]";
        arrayOut(f, unclonedInt64ArrayParam_[i], name.str());
      }
      f.close();
    }  // main file


    // auxilliary file using stream
    {
      std::ofstream& f = bundle.getOutputStream("aux");
      f << "This is an auxilliary file!\n";
      f.close();
    }

    // auxilliary file using path
    {
      std::string path = bundle.getPath("aux2");
      std::ofstream f(path.c_str());
      f << "This is another auxilliary file!\n";
      f.close();
    }
  }
Beispiel #2
0
void ScalarSensor::deserialize(BundleIO &bundle) {
  std::istream &f = bundle.getInputStream();
  std::string tag;
  f >> tag;
  NTA_CHECK(tag == "ScalerSensor");
  f.ignore(1);
  f.read((char *)&params_, sizeof(params_));
  f >> tag;
  NTA_CHECK(tag == "~ScalerSensor");
  f.ignore(1);

  if (params_.periodic) {
    encoder_ = new PeriodicScalarEncoder(params_.w, 
                                  params_.minValue, 
                                  params_.maxValue, 
                                  params_.n, 
                                  params_.radius, 
                                  params_.resolution);
  } else {
    encoder_ = new ScalarEncoder( params_.w, 
                                  params_.minValue, 
                                  params_.maxValue, 
                                  params_.n, 
                                  params_.radius, 
                                  params_.resolution, 
                                  params_.clipInput);
  }
  initialize();
  encodedOutput_->initialize();
  bucketOutput_->initialize();
  compute();
}
Beispiel #3
0
void SpatialPoolerNode::deserialize(BundleIO& bundle)
{
  std::ifstream& f = bundle.getInputStream("spmain");
  std::string version;
  f >> version;
  NTA_CHECK(version == current_spatial_pooler_node_version_);
  {
    // f >> mode_ doesn't work. 
    Int32 mode;
    f >> mode;
    mode_ = (Mode)mode;
  }
  f >> clonedNodes_;
  f >> nodeCount_;
  f >> segmentSize_;
  {
    Int32 mode;
    f >> mode;
    sparsificationMode_ = (SparsePooler::SparsificationMode) mode;
    f >> mode;
    inferenceMode_ = (SparsePooler::InferenceMode) mode;
  }
  f >> normalize_;
  f >> norm_;
  f >> kWinners_;
  f >> maxDistance_;
  f >> minAcceptNorm_;
  f >> minProtoSum_;
  f >> sigma_;
  // don't use this value to seed; use the one saved from rgen, below
  f >> seed_;
  f >> maxNAttempts_;
  f >> maxNPrototypes_;
  f >> acceptanceProbability_;
  {
    UInt32 actualSeed;
    f >> actualSeed;
    // TODO: this isn't really useful unless the network hasn't been used. 
    // The seed is the same, but internal state is lost.
    rgen_ = nta::Random(actualSeed);
  }
  
  f >> poolersAllocated_;
  if (poolersAllocated_)
  {
    UInt poolerCount = clonedNodes_ ? 1 : nodeCount_;
    poolers_.resize(poolerCount);
    for (UInt i = 0; i != poolerCount; ++i) {
      poolers_[i] = new SparsePooler();
      poolers_[i]->readState(f);
    }
  }
  f.close();


}
Beispiel #4
0
void TestNode::serialize(BundleIO &bundle) {
  {
    std::ostream &f = bundle.getOutputStream();
    // There is more than one way to do this. We could serialize to YAML, which
    // would make a readable format, or we could serialize directly to the
    // stream Choose the easier one.
    f << "TestNode-v2"
      << " " << nodeCount_ << " " << int32Param_ << " " << uint32Param_ << " "
      << int64Param_ << " " << uint64Param_ << " " << real32Param_ << " "
      << real64Param_ << " " << boolParam_ << " " << outputElementCount_ << " "
      << delta_ << " " << iter_ << " " << dim_ << " ";

    arrayOut(f, real32ArrayParam_, "real32ArrayParam_");
    arrayOut(f, int64ArrayParam_, "int64ArrayParam_");
    arrayOut(f, boolArrayParam_, "boolArrayParam_");
    arrayOut(f, unclonedParam_, "unclonedParam_");
    f << shouldCloneParam_ << " ";

    // outer vector needs to be done by hand.
    f << "unclonedArray ";
    f << unclonedInt64ArrayParam_.size() << " "; // number of nodes
    for (size_t i = 0; i < unclonedInt64ArrayParam_.size(); i++) {
      std::stringstream name;
      name << "unclonedInt64ArrayParam[" << i << "]";
      arrayOut(f, unclonedInt64ArrayParam_[i], name.str());
    }
      // save the output buffers
      f << "outputs [";
      std::map<std::string, Output *> outputs = region_->getOutputs();
      for (auto iter : outputs) {
        const Array &outputBuffer = iter.second->getData();
        if (outputBuffer.getCount() != 0) {
          f << iter.first << " ";
          outputBuffer.save(f);
        }
      }
      f << "] "; // end of all output buffers
  } // main file

 }
Beispiel #5
0
void SpatialPoolerNode::serialize(BundleIO& bundle)
{
  std::ofstream& f = bundle.getOutputStream("spmain");
  f   << current_spatial_pooler_node_version_ << " "
      << (UInt32) mode_ << " "
      << (UInt32) clonedNodes_ << " "
      << nodeCount_ << " "
      << segmentSize_ << " "
      << (UInt32) sparsificationMode_ << " "
      << (UInt32) inferenceMode_ << " "
      // patchMasksStr_ not serialized
      << (UInt32) normalize_ << " "
      << norm_ << " "
      << kWinners_ << " "
      << maxDistance_ << " "
      << minAcceptNorm_ << " "
      << minProtoSum_ << " "
      << sigma_ << " "
      << seed_ << " "
      << maxNAttempts_ << " "
      << maxNPrototypes_ << " "
      << acceptanceProbability_ << " "
      // save actual seed separately from seed_ in case it was seeded with "0"
      // TODO: actually serialize rgen_;
      << (UInt32) rgen_.getSeed() << " " 
      << (UInt32) poolersAllocated_ << " ";

  if (poolersAllocated_)
  {
    // If cloned, there is a single pooler. 
    // If not cloned, everybody is saved here. 
    UInt actualNumNodes = clonedNodes_ ? 1 : nodeCount_;
    for (UInt i = 0; i != actualNumNodes; ++i) {
      poolers_[i]->saveState(f);
      f << " ";
    }
  }
  f.close();
}
Beispiel #6
0
  void TestNode::deserialize(BundleIO& bundle)
  {
    {
      std::ifstream& f = bundle.getInputStream("main");
      // There is more than one way to do this. We could serialize to YAML, which
      // would make a readable format, or we could serialize directly to the stream
      // Choose the easier one.
      std::string versionString;
      f >> versionString;
      if (versionString != "TestNode-v2")
      {
        NTA_THROW << "Bad serialization for region '" << region_->getName()
                  << "' of type TestNode. Main serialization file must start "
                  << "with \"TestNode-v2\" but instead it starts with '"
                  << versionString << "'";

      }
      f >> nodeCount_;
      f >> int32Param_;
      f >> uint32Param_;
      f >> int64Param_;
      f >> uint64Param_;
      f >> real32Param_;
      f >> real64Param_;
      f >> boolParam_;
      f >> outputElementCount_;
      f >> delta_;
      f >> iter_;

      arrayIn(f, real32ArrayParam_, "real32ArrayParam_");
      arrayIn(f, int64ArrayParam_, "int64ArrayParam_");
      arrayIn(f, int64ArrayParam_, "boolArrayParam_");
      arrayIn(f, unclonedParam_, "unclonedParam_");

      f >> shouldCloneParam_;

      std::string label;
      f >> label;
      if (label != "unclonedArray")
        NTA_THROW << "Missing label for uncloned array. Got '" << label << "'";
      size_t vecsize;
      f >> vecsize;
      unclonedInt64ArrayParam_.clear();
      unclonedInt64ArrayParam_.resize(vecsize);
      for (size_t i = 0; i < vecsize; i++)
      {
        std::stringstream name;
        name << "unclonedInt64ArrayParam[" << i << "]";
        arrayIn(f, unclonedInt64ArrayParam_[i], name.str());
      }
      f.close();
    }  // main file

    // auxilliary file using stream
    {
      std::ifstream& f = bundle.getInputStream("aux");
      char line1[100];
      f.read(line1, 100);
      line1[f.gcount()] = '\0';
      if (std::string(line1) != "This is an auxilliary file!\n")
      {
        NTA_THROW << "Invalid auxilliary serialization file for TestNode";
      }
      f.close();
    }

    // auxilliary file using path
    {
      std::string path = bundle.getPath("aux2");
      std::ifstream f(path.c_str());
      char line1[100];
      f.read(line1, 100);
      line1[f.gcount()] = '\0';
      if (std::string(line1) != "This is another auxilliary file!\n")
      {
        NTA_THROW << "Invalid auxilliary2 serialization file for TestNode";
      }

      f.close();
    }
  }
void VectorFileSensor::deserialize(BundleIO &bundle) {
  std::ifstream &f = bundle.getInputStream("vfs");
  f >> repeatCount_ >> activeOutputCount_ >> filename_ >> scalingMode_;
  f.close();
}
void VectorFileSensor::serialize(BundleIO &bundle) {
  std::ofstream &f = bundle.getOutputStream("vfs");
  f << repeatCount_ << " " << activeOutputCount_ << " " << filename_ << " "
    << scalingMode_ << " ";
  f.close();
}
Beispiel #9
0
void TestNode::deserialize(BundleIO &bundle) {
  {
    std::istream &f = bundle.getInputStream();
    // There is more than one way to do this. We could serialize to YAML, which
    // would make a readable format, or we could serialize directly to the
    // stream Choose the easier one.
    std::string versionString;
    f >> versionString;
    if (versionString != "TestNode-v2") {
      NTA_THROW << "Bad serialization for region '" << region_->getName()
                << "' of type TestNode. Main serialization file must start "
                << "with \"TestNode-v2\" but instead it starts with '"
                << versionString << "'";
    }
    f >> nodeCount_;
    f >> int32Param_;
    f >> uint32Param_;
    f >> int64Param_;
    f >> uint64Param_;
    f >> real32Param_;
    f >> real64Param_;
    f >> boolParam_;
    f >> outputElementCount_;
    f >> delta_;
    f >> iter_;
    f >> dim_;

    arrayIn(f, real32ArrayParam_, "real32ArrayParam_");
    arrayIn(f, int64ArrayParam_, "int64ArrayParam_");
    arrayIn(f, int64ArrayParam_, "boolArrayParam_");
    arrayIn(f, unclonedParam_, "unclonedParam_");

    f >> shouldCloneParam_;

      std::string tag;
      f >> tag;
      if (tag != "unclonedArray")
        NTA_THROW << "Missing label for uncloned array. Got '" << tag << "'";
      size_t vecsize;
      f >> vecsize;
      unclonedInt64ArrayParam_.clear();
      unclonedInt64ArrayParam_.resize(vecsize);
      for (size_t i = 0; i < vecsize; i++)
      {
        std::stringstream name;
        name << "unclonedInt64ArrayParam[" << i << "]";
        arrayIn(f, unclonedInt64ArrayParam_[i], name.str());
      }

	    // Restore outputs
	    f >> tag;
	    NTA_CHECK(tag == "outputs");
	    f.ignore(1);
	    NTA_CHECK(f.get() == '['); // start of outputs

	    while (true) {
	      f >> tag;
	      f.ignore(1);
	      if (tag == "]")
	        break;
	      getOutput(tag)->getData().load(f);
	    }
	  }

  }
Beispiel #10
0
void ScalarSensor::serialize(BundleIO &bundle) {
    std::ostream &f = bundle.getOutputStream();
    f << "ScalerSensor ";
    f.write((char*)&params_, sizeof(params_));
    f << "~ScalerSensor" << std::endl;
}