Ejemplo n.º 1
0
void RNN<
LayerTypes, OutputLayerType, InitializationRuleType, PerformanceFunction
>::Gradient(const arma::mat& /* unused */,
            const size_t i,
            arma::mat& gradient)
{
  if (gradient.is_empty())
  {
    gradient = arma::zeros<arma::mat>(parameter.n_rows, parameter.n_cols);
  }
  else
  {
    gradient.zeros();
  }

  Evaluate(parameter, i, false);

  arma::mat currentGradient = arma::mat(gradient.n_rows, gradient.n_cols);
  NetworkGradients(currentGradient, network);

  const arma::mat input = arma::mat(predictors.colptr(i), predictors.n_rows,
      1, false, true);

  // Iterate through the input sequence and perform the feed backward pass.
  for (seqNum = seqLen - 1; seqNum >= 0; seqNum--)
  {
    // Load the network activation for the upcoming backward pass.
    LoadActivations(input.rows(seqNum * inputSize, (seqNum + 1) *
        inputSize - 1), network);

    // Perform the backward pass.
    if (seqOutput)
    {
      arma::mat seqError = error.unsafe_col(seqNum);
      Backward(seqError, network);
    }
    else
    {
      Backward(error, network);
    }

    // Link the parameters and update the gradients.
    LinkParameter(network);
    UpdateGradients<>(network);

    // Update the overall gradient.
    gradient += currentGradient;

    if (seqNum == 0) break;
  }
}
Ejemplo n.º 2
0
std::vector<int> HighOrderMeshGenerator::
insertNewNodes(const unique_element_ptr& el,
	       const arma::mat& newelnodes,
	       const arma::mat& parametric_coords,
	       int order){

  const int type = el->getElementType();

  const NodeIndexer* ni_old = 
    index_factory->getNodeIndexer(type,el->getOrder());

  const NodeIndexer* ni_new = 
    index_factory->getNodeIndexer(type,order);
  
  std::vector<int> newnode_indices(ni_new->Ndof(),-1);
  const std::vector<indtype>& corner_new = ni_new->getCornerNodes();
  
  const gind* old_nodes = el->getNodes();
  const std::vector<indtype>& corner_old = ni_old->getCornerNodes();
  
  for(int i = 0; i < el->numCornerNodes(); i++){
    //std::cout << "corner node: " << corner_new[i] << " " << 
    //  corner_old[i] << std::endl;
    newnode_indices[corner_new[i]] = old_nodes[corner_old[i]];
  }
 


    //std::cout << el->getDim() << std::endl;
  for(int ch = 0; ch < el->NumChildren(); ch++){
    const MEl* child_old = el->getChild(ch);
    if(el->getDim() > 1) assert(child_old);
    if(child_old){
      //int child_dim = child_old->getDim();
      auto child_it = elmap[el->getDim()-1].find(child_old);
      assert(child_it != elmap[el->getDim()-1].end());
   
      const MEl* child_new = child_it->second;
      const gind* child_nodes = child_new->getNodes();
      int orient = el->getChildOrientation(ch);
      
      const std::vector<indtype>& child_node_indices = 
	ni_new->getChildNodes(ch);

      const NodeIndexer* ni_child =
	index_factory->getNodeIndexer(child_new->getElementType(),
				      child_new->getOrder());

      const std::vector<indtype>& orient_indices = 
	ni_child->getOrientedNodes(orient);
      assert(child_node_indices.size() == child_new->NumNodes());
      for(int nd = 0; nd < child_new->NumNodes(); nd++){
	newnode_indices[child_node_indices[nd]] = 
	  child_nodes[orient_indices[nd]];
      }
    }
  } // end child loop

  
  // delete the old interior nodes from map
  
  const std::vector<indtype>& old_interior_indices = ni_old->getInteriorNodes();
  for(int i = 0; i < old_interior_indices.size(); i++){
    meshcurr->getNodesNC().erase(old_nodes[old_interior_indices[i]]);
  }
  

  // insert new nodes into node map
  //std::cout << "ni_new type: " << ni_new->getType() << std::endl;
  //arma::uvec new_interior_indices;
  //std::cout << "el type: " << el->getElementType() << std::endl;
  arma::uvec new_interior_indices(ni_new->getInteriorNodes());
  //std::cout << "h3.1" << std::endl;
  //std::cout << new_interior_indices << std::endl;
  //std::cout << newelnodes << std::endl;
  arma::mat new_interior_nodes = newelnodes.cols(new_interior_indices);
  //std::cout << "h3.2" << std::endl;
  //std::cout << new_interior_indices << std::endl;
  //std::cout << parametric_coords << std::endl;
  arma::mat new_interior_parametric_coords;
  if(!parametric_coords.is_empty()){
    new_interior_parametric_coords = 
      parametric_coords.cols(new_interior_indices);
  }

  //std::cout << "h4" << std::endl;

  nodeFactory* node_factory = nodeFactory::Instance();

  int node_type = 3;
  if(el->hasGeoEntity()) node_type = el->getGeoType()+1;
     
  //std::cout << new_interior_nodes << std::endl;

  if(el->getElementType() == 1){
    //std::cout << el->getGeoType() << std::endl;
    //if(!el->hasGeoEntity()) std::cout << "Line does not have a geo entity!" << std::endl;
  }
  for(int i = 0; i < new_interior_indices.size(); i++){
    double uv[2];
    for(int j = 0; j < parametric_coords.n_rows; j++){
      uv[j] = new_interior_parametric_coords.at(j,i);
    }
    int nd_count = node_factory->GetNodeCount();
    newnode_indices[new_interior_indices[i]] = nd_count;
    auto newnode = 
      node_factory->CreateNode(node_type,
			       new_interior_nodes.colptr(i),
			       el->getGeoEntity(),
			       uv[0],uv[1]);
    meshcurr->getNodesNC()[nd_count] = std::move(newnode);

  }

  //std::cout << "h5" << std::endl;

  return newnode_indices;
  
}