Example #1
0
      void operation_reduce(Parser& parser,
			    const feature_set_type& feats,
			    const Theta& theta,
			    const state_type& state,
			    const symbol_type& label)
      {
	const size_type offset1 = 0;
	const size_type offset2 = theta.hidden_;
      
	const state_type state_reduced = state.stack();
	const state_type state_stack   = state_reduced.stack();

	state_type state_new = parser.state_allocator_.allocate();
	  
	state_new.step()  = state.step() + 1;
	state_new.next()  = state.next();
	state_new.unary() = state.unary();
	  
	state_new.operation() = operation_type::REDUCE;
	state_new.label()     = label;
	state_new.head()      = symbol_type::EPSILON;
	state_new.span()      = span_type(state_reduced.span().first_, state.span().last_);
	  
	state_new.stack()      = state_stack;
	state_new.derivation() = state;
	state_new.reduced()    = state_reduced;
	
	state_new.feature_vector() = parser.feature_vector_allocator_.allocate();
	state_new.feature_state()  = feats.apply(state_new.operation(),
						 state_new.label(),
						 state.feature_state(),
						 state_reduced.feature_state(),
						 *state_new.feature_vector());
	
	const size_type index_operation       = theta.index_operation(state_new.operation());
	const size_type offset_operation      = index_operation * theta.hidden_;
	const size_type offset_classification = theta.offset_classification(label);
	const size_type offset_category       = theta.offset_category(label);
	  
	state_new.layer(theta.hidden_) = (theta.Bre_.block(offset_category, 0, theta.hidden_, 1)
					  + (theta.Wre_.block(offset_category, offset1, theta.hidden_, theta.hidden_)
					     * state.layer(theta.hidden_))
					  + (theta.Wre_.block(offset_category, offset2, theta.hidden_, theta.hidden_)
					     * state_reduced.layer(theta.hidden_))
					  ).array().unaryExpr(model_type::activation());
	  
	const double score = (theta.Wc_.block(offset_classification, offset_operation, 1, theta.hidden_) * state_new.layer(theta.hidden_)
			      + theta.Bc_.block(offset_classification, index_operation, 1, 1))(0, 0);
	
	state_new.score() = trance::dot_product(theta.Wfe_, *state_new.feature_vector()) + state.score() + score;
	
	parser.agenda_[state_new.step()].push_back(state_new);
      }
Example #2
0
      void operation_idle(Parser& parser,
			  const feature_set_type& feats,
			  const Theta& theta,
			  const state_type& state)
      {
	state_type state_new = parser.state_allocator_.allocate();

	state_new.step()  = state.step() + 1;
	state_new.next()  = state.next();
	state_new.unary() = state.unary();
      
	state_new.operation() = operation_type::IDLE;
	state_new.label()     = symbol_type::IDLE;
	state_new.head()      = symbol_type::EPSILON;
	state_new.span()      = state.span();
      
	state_new.stack()      = state.stack();
	state_new.derivation() = state;
	state_new.reduced()    = state_type();

	state_new.feature_vector() = parser.feature_vector_allocator_.allocate();
	state_new.feature_state()  = feats.apply(state_new.operation(),
						 state.feature_state(),
						 *state_new.feature_vector());

	const size_type index_operation       = theta.index_operation(state_new.operation());
	const size_type offset_operation      = index_operation * theta.hidden_;
	const size_type offset_classification = theta.offset_classification(symbol_type::IDLE);
      
	state_new.layer(theta.hidden_) = (theta.Bi_
					  + theta.Wi_ * state.layer(theta.hidden_)
					  ).array().unaryExpr(model_type::activation());
      
	const double score = (theta.Wc_.block(offset_classification, offset_operation, 1, theta.hidden_) * state_new.layer(theta.hidden_)
			      + theta.Bc_.block(offset_classification, index_operation, 1, 1))(0, 0);
      
	state_new.score() = trance::dot_product(theta.Wfe_, *state_new.feature_vector()) + state.score() + score;
      
	parser.agenda_[state_new.step()].push_back(state_new);
      }