Пример #1
0
// Deserialize region
Region::Region(const std::string& name, 
               const std::string& nodeType,
               const Dimensions& dimensions,
               BundleIO& bundle,
               Network * network) :
  name_(name), 
  type_(nodeType), 
  initialized_(false), 
  enabledNodes_(NULL),
  network_(network)
{
  // Set region info before creating the RegionImpl so that the 
  // Impl has access to the region info in its constructor.
  RegionImplFactory & factory = RegionImplFactory::getInstance();
  spec_ = factory.getSpec(nodeType);

  // Dimensions start off as unspecified, but if
  // the RegionImpl only supports a single node, we 
  // can immediately set the dimensions. 
  if (spec_->singleNodeOnly)
    if (!dimensions.isDontcare() && !dimensions.isUnspecified() &&
        !dimensions.isOnes())
      NTA_THROW << "Attempt to deserialize region of type " << nodeType
                << " with dimensions " << dimensions
                << " but region supports exactly one node.";

  dims_ = dimensions;

  impl_ = factory.deserializeRegionImpl(nodeType, bundle, this);
  createInputsAndOutputs_();
}
Пример #2
0
// Create region from parameter spec
Region::Region(const std::string& name, 
               const std::string& nodeType, 
               const std::string& nodeParams,
               Network * network) :
  name_(name), 
  type_(nodeType), 
  initialized_(false), 
  enabledNodes_(NULL),
  network_(network)
{
  // Set region info before creating the RegionImpl so that the 
  // Impl has access to the region info in its constructor.
  RegionImplFactory & factory = RegionImplFactory::getInstance();
  spec_ = factory.getSpec(nodeType);

  // Dimensions start off as unspecified, but if
  // the RegionImpl only supports a single node, we 
  // can immediately set the dimensions. 
  if (spec_->singleNodeOnly)
    dims_.push_back(1);
  // else dims_ = []

  impl_ = factory.createRegionImpl(nodeType, nodeParams, this);
  createInputsAndOutputs_();

}
Пример #3
0
 Region::Region(std::string name, RegionProto::Reader& proto,
                Network* network) :
   name_(std::move(name)),
   type_(proto.getNodeType().cStr()),
   initialized_(false),
   enabledNodes_(nullptr),
   network_(network)
 {
   read(proto);
   createInputsAndOutputs_();
 }
Пример #4
0
void Region::load(std::istream &f) {
  char bigbuffer[5000];
  std::string tag;
  Size count;
  Dimensions d;

  // Each region is a map -- extract the 5 values in the map
  f >> tag;
  NTA_CHECK(tag == "{") << "bad region entry (not a map)";

  // 1. name
  f >> tag;
  NTA_CHECK(tag == "name:");
  f.ignore(1);
  f.getline(bigbuffer, sizeof(bigbuffer));
  name_ = bigbuffer;

  // 2. nodeType
  f >> tag;
  NTA_CHECK(tag == "nodeType:");
  f.ignore(1);
  f.getline(bigbuffer, sizeof(bigbuffer));
  type_ = bigbuffer;

  // 3. phases
  f >> tag;
  NTA_CHECK(tag == "phases:");
  f >> tag;
  NTA_CHECK(tag == "[") << "Expecting a sequence of phases.";
  f >> count;
  phases_.clear();
  for (Size i = 0; i < count; i++)
  {
    UInt32 val;
    f >> val;
    phases_.insert(val);
  }
  f >> tag;
  NTA_CHECK(tag == "]") << "Expected end of sequence of phases.";

  // create inputs and outputs
  RegionImplFactory &factory = RegionImplFactory::getInstance();
  spec_ = factory.getSpec(type_);
  createInputsAndOutputs_();

  // 4. Restore dimensions on outputs
  {
    f >> tag;
    NTA_CHECK(tag == "outputs:");
    f >> tag;
    NTA_CHECK(tag == "[") << "Expecting a sequence of outputs.";
    f >> tag;
    while(!f.eof() && tag != "]") {
      f >> d;
      auto itr = outputs_.find(tag);
      if (itr != outputs_.end())
        itr->second->setDimensions(d);
      f >> tag;
    }
    NTA_CHECK(tag == "]") << "Expected end of sequence of outputs.";
  }

  // 5. Restore dimensions on inputs
  {
    f >> tag;
    NTA_CHECK(tag == "inputs:");
    f >> tag;
    NTA_CHECK(tag == "[") << "Expecting a sequence of inputs.";
    f >> tag;
    while(!f.eof() && tag != "]") {
      f >> d;
      auto itr = inputs_.find(tag);
      if (itr != inputs_.end())
        itr->second->setDimensions(d);
      f >> tag;
    }
    NTA_CHECK(tag == "]") << "Expected end of sequence of inputs.";
  }

  // 6. impl
  f >> tag;
  NTA_CHECK(tag == "RegionImpl:") << "Expected beginning of RegionImpl.";
  f.ignore(1);

  BundleIO bundle(&f);
  impl_.reset(factory.deserializeRegionImpl(type_, bundle, this));

  f >> tag;
  NTA_CHECK(tag == "}") << "Expected end of region. Found '" << tag << "'.";
}