예제 #1
0
 WeightList NeuralNet::getWeights() const
 {
     WeightList ret;
     LayerList::const_iterator it;
     for(it = m_layers.begin(); it != m_layers.end(); it++)
     {
         NeuronLayer::const_iterator it2;
         for(it2 = it->begin(); it2 != it->end(); it2++)
         {
             Neuron::const_iterator it3;
             for(it3 = it2->begin(); it3 != it2->end(); it3++)
             {
                 ret.push_back(*it3);
             }
         }
     }
     return ret;
 }
예제 #2
0
// condition |is_central(roots,t)| means $t=\exp(2pi\pi)$ with $<p,roots>$ int
bool is_central(const WeightList& alpha, const TorusElement& t)
{
  const RatWeight& rw = t.as_Qmod2Z(); // using $\exp(i\pi.)$ is faster, but
  arithmetic::Numer_t d = 2*rw.denominator(); // it requires even pairings
  for (weyl::Generator s=0; s<alpha.size(); ++s)
    if (rw.numerator().dot(alpha[s])%d != 0) // see if division is exact
      return false;

  return true;
}
예제 #3
0
bool is_central(const WeightList& alpha, const TorusElement& t)
{
  RatWeight rw = t.as_Qmod2Z();
  arithmetic::Numer_t d = 2*rw.denominator();
  for (weyl::Generator s=0; s<alpha.size(); ++s)
    if (alpha[s].dot(rw.numerator())%d != 0) // see if division is exact
      return false;

  return true;
}
예제 #4
0
  bool init(const LayerMap& layerMap,
            const ParameterMap& parameterMap) override {
    /* Initialize the basic parent class */
    Layer::init(layerMap, parameterMap);

    /* initialize the weightList */
    size_t i;
    for (i = 0; i < inputLayers_.size(); i++) {
      if (!parameters_[i]) break;
      size_t width = inputLayers_[i]->getSize();
      // create a new weight
      CHECK_EQ(parameters_[i]->getSize(), width * numClasses_);
      Weight* w = new Weight(numClasses_, width, parameters_[i]);

      // append the new weight to the list
      weights_.emplace_back(w);
    }

    CHECK_EQ(1U, getSize());

    numInputs_ = i;
    CHECK_GE(numInputs_, 1)
        << "Must have at least one input besides label and weight";
    CHECK_LT(i, inputLayers_.size()) << "Missing label layer";
    labelLayer_ = inputLayers_[i];
    if (++i < inputLayers_.size()) {
      weightLayer_ = inputLayers_[i];
      ++i;
    }
    CHECK_EQ(i, inputLayers_.size());

    /* initialize biases_ */
    if (biasParameter_.get() != NULL) {
      CHECK_EQ(biasParameter_->getSize(), (size_t)numClasses_);
      biases_.reset(new Weight(1, numClasses_, biasParameter_));
    }

    if (config_.neg_sampling_dist_size()) {
      CHECK_EQ(numClasses_, config_.neg_sampling_dist_size());
      sampler_.reset(MultinomialSampler::create(
          config_.neg_sampling_dist().data(), numClasses_));
    }

    return true;
  }
예제 #5
0
 void NeuralNet::setWeights(const WeightList& ws)
 {
     int index = 0;
     LayerList::iterator it;
     for(it = m_layers.begin(); it != m_layers.end(); it++)
     {
         NeuronLayer::iterator it2;
         for(it2 = it->begin(); it2 != it->end(); it2++)
         {
             Neuron::iterator it3;
             for(it3 = it2->begin(); it3 != it2->end(); it3++)
             {
                 *it3 = ws.at(index);
                 index++;
             }
         }
     }
 }
RecipeDB::ConversionStatus RecipeDB::convertIngredientUnits( const Ingredient &from, const Unit &to, Ingredient &result )
{
	result = from;

	if ( from.units.id() == to.id() )
		return Success;

	if ( from.units.type() == to.type() && to.type() != Unit::Other ) {
		double ratio = unitRatio( from.units.id(), to.id() );
		if ( ratio > 0 ) {
			result.amount = from.amount * ratio;
			result.units = to;

			return Success;
		}
		else {
			kDebug()<<"Unit conversion failed, you should probably update your unit conversion table.";
			kDebug()<<from.units.id()<<" to "<<to.id();
			return MissingUnitConversion;
		}
	}
	else if ( to.type() == Unit::Mass || from.units.type() == Unit::Mass ) {
		if ( from.ingredientID == -1 )
			return MissingIngredient;

		double fromToWeightRatio, weightToToRatio;
		int unitID = -1;

		WeightList idList = ingredientWeightUnits( from.ingredientID );

		if ( idList.count() == 0 )
			return MissingIngredientWeight;

		for ( WeightList::const_iterator it = idList.constBegin(); it != idList.constEnd(); ++it ) {
			//get conversion order correct (i.e., Mass -> Volume instead of Volume -> Mass, depending on unit type)
			int first   = (to.type() == Unit::Mass)?(*it).perAmountUnitId():(*it).weightUnitId();
			int second  = (to.type() == Unit::Mass)?(*it).weightUnitId():(*it).perAmountUnitId();
			double tryFromToWeightRatio = unitRatio( from.units.id(), first );
			if ( tryFromToWeightRatio > 0 ) {
				weightToToRatio = unitRatio( second, to.id() );
				fromToWeightRatio = tryFromToWeightRatio;
				unitID = first;

				if ( from.prepMethodList.containsId( (*it).prepMethodId() ) ) {
					break;
				}
			}
		}
		if ( unitID == -1 )
			return MissingUnitConversion;

		bool wasApproximated;

		Ingredient i;
		i.ingredientID = from.ingredientID;
		i.units.setId(unitID);
		i.amount = from.amount * fromToWeightRatio;
		i.prepMethodList = from.prepMethodList;
		result.amount = ingredientWeight( i, &wasApproximated ) * weightToToRatio;
		result.units = to;

		if ( result.amount < 0 )
			return MismatchedPrepMethod;
		else if ( wasApproximated )
			return MismatchedPrepMethodUsingApprox;

		return Success;
	}
	else {
		QString to_str;
		switch ( to.type() ) {
		case Unit::Other: to_str = "Other"; break;
		case Unit::Mass: to_str = "Mass"; break;
		case Unit::Volume: to_str = "Volume"; break;
		case Unit::All: kDebug()<<"Code error: trying to convert to unit of type 'All'"; return InvalidTypes;
		}
		QString from_str;
		switch ( from.units.type() ) {
		case Unit::Other: from_str = "Other"; break;
		case Unit::Mass: from_str = "Mass"; break;
		case Unit::Volume: from_str = "Volume"; break;
		case Unit::All: kDebug()<<"Code error: trying to convert from unit of type 'All'"; return InvalidTypes;
		}
		kDebug()<<"Can't handle conversion from "<<from_str<<'('<<from.units.id()<<") to "<<to_str<<'('<<to.id()<<')';

		return InvalidTypes;
	}
}