Package gnu.trove.map.hash

Examples of gnu.trove.map.hash.TObjectDoubleHashMap$MapBackedView


       /////
       String line = value.toString();
       // id gold_label injected_labels estimated_labels neighbors rw_probabilities
       String[] fields = line.split(_kDelim);      
       TObjectDoubleHashMap neighbors = CollectionUtil.String2Map(fields[4]);
             
       boolean isSeedNode = fields[2].length() > 0 ? true : false;
      
       // If the current node is a seed node but there is no
       // estimate label information yet, then transfer the seed label
       // to the estimated label distribution. Ideally, this is likely
       // to be used in the map of the very first iteration.
       if (isSeedNode && fields[3].length() == 0) {
         fields[3] = fields[2];
       }

       // Send two types of messages:
       //   -- self messages which will store the injection labels and
       //        random walk probabilities.
       //   -- messages to neighbors about current estimated scores
       //        of the node.
       //
       // message to self
       output.collect(new Text(fields[0]), new Text(line));

       // message to neighbors
       TObjectDoubleIterator neighIterator = neighbors.iterator();
       while (neighIterator.hasNext()) {
         neighIterator.advance();
        
         // message (neighbor_node, current_node + DELIM + curr_node_label_scores
         output.collect(new Text((String) neighIterator.key()),
View Full Code Here


 
  public static void Split(Graph g, double trainFract) {   
    Random r = new Random(_kDeterministicSeed);
    // Random r = new Random();
   
    TObjectDoubleHashMap instanceVertices = new TObjectDoubleHashMap();
    Iterator vIter = g.vertices().keySet().iterator();
    while (vIter.hasNext()) {
      Vertex v = g.vertices().get(vIter.next());
     
      // nodes without feature prefix and those with at least one
      // gold labels are considered valid instances
      if (!v.name().startsWith(Constants.GetFeatPrefix()) &&
          v.goldLabels().size() > 0) {
        instanceVertices.put(v, r.nextDouble());
      }
    }
   
    ArrayList<ObjectDoublePair> sortedRandomInstances =
      CollectionUtil.ReverseSortMap(instanceVertices);
View Full Code Here

   }
  
     public void reduce(Text key, Iterator<Text> values,
               OutputCollector<Text, Text> output, Reporter reporter) throws IOException {      
       // new scores estimated for the current node
       TObjectDoubleHashMap newEstimatedScores = new TObjectDoubleHashMap();
      
       // set to true only if the message sent to itself is found.
       boolean isSelfMessageFound = false;
      
       String vertexId = key.toString();
       String vertexString = "";
      
       TObjectDoubleHashMap neighbors = null;
       TObjectDoubleHashMap randWalkProbs = null;
      
       HashMap<String, String> neighScores =
                 new HashMap<String, String>();
      
       int totalMessagesReceived = 0;
      
       // iterate over all the messages received at the node
       while (values.hasNext()) {
         ++totalMessagesReceived;

         String val = values.next().toString();
         String[] fields = val.split(_kDelim);
        
         // System.out.println("src: " + fields[0] + " dest: " + vertexId +
         //       "MESSAGE>>" + val + "<<");

         // self-message check
         if (vertexId.equals(fields[0])) {
           isSelfMessageFound = true;
           vertexString = val;
          
           // System.out.println("Reduce: " + vertexId + " " + val + " " + fields.length);

           TObjectDoubleHashMap injLabels = CollectionUtil.String2Map(fields[2]);
           neighbors = CollectionUtil.String2Map(neighbors, fields[4]);
           randWalkProbs = CollectionUtil.String2Map(fields[5]);
          
           if (injLabels.size() > 0) {          
             // add injected labels to the estimated scores.
             ProbUtil.AddScores(newEstimatedScores,
                        mu1 * randWalkProbs.get(Constants._kInjProb),
                        injLabels);
           }
         } else {
           // an empty second field represents that the
           // neighbor has no valid label assignment yet.
           if (fields.length > 1) {
             neighScores.put(fields[0], fields[1]);
           }
         }
       }

       // terminate if message from self is not received.
       if (!isSelfMessageFound) {
         throw new RuntimeException("Self message not received for node " + vertexId);
       }
      
       // collect neighbors label distributions and create one single
       // label distribution
       TObjectDoubleHashMap weightedNeigLablDist = new TObjectDoubleHashMap();
       Iterator<String> neighIter = neighScores.keySet().iterator();
       while (neighIter.hasNext()) {
         String neighName = neighIter.next();
         ProbUtil.AddScores(weightedNeigLablDist, // newEstimatedScores,
                   mu2 * randWalkProbs.get(Constants._kContProb) * neighbors.get(neighName),
 
View Full Code Here

       /////
       String line = value.toString();
       // id gold_label injected_labels estimated_labels neighbors rw_probabilities
       String[] fields = line.split(_kDelim);      
       TObjectDoubleHashMap neighbors = CollectionUtil.String2Map(fields[4]);
      
       boolean isSeedNode = fields[2].length() > 0 ? true : false;
      
       // If the current node is a seed node but there is no
       // estimate label information yet, then transfer the seed label
       // to the estimated label distribution. Ideally, this is likely
       // to be used in the map of the very first iteration.
       if (isSeedNode && fields[3].length() == 0) {
         fields[3] = fields[2];
       }

       // Send two types of messages:
       //   -- self messages which will store the injection labels and
       //        random walk probabilities.
       //   -- messages to neighbors about current estimated scores
       //        of the node.
       //
       // message to self
       output.collect(new Text(fields[0]), new Text(line));

       // message to neighbors
       TObjectDoubleIterator neighIterator = neighbors.iterator();
       while (neighIterator.hasNext()) {
         neighIterator.advance();
        
         // message (neighbor_node, current_node + DELIM + curr_node_label_scores
         output.collect(new Text((String) neighIterator.key()),
View Full Code Here

   }
  
     public void reduce(Text key, Iterator<Text> values,
               OutputCollector<Text, Text> output, Reporter reporter) throws IOException {      
       // new scores estimated for the current node
       TObjectDoubleHashMap newEstimatedScores = new TObjectDoubleHashMap();
      
       // set to true only if the message sent to itself is found.
       boolean isSelfMessageFound = false;
      
       String vertexId = key.toString();
       String vertexString = "";
      
       TObjectDoubleHashMap neighbors = null;
       TObjectDoubleHashMap randWalkProbs = null;
      
       HashMap<String, String> neighScores =
                 new HashMap<String, String>();
      
       int totalMessagesReceived = 0;
       boolean isSeedNode = false;
      
       // iterate over all the messages received at the node
       while (values.hasNext()) {
         ++totalMessagesReceived;

         String val = values.next().toString();
         String[] fields = val.split(_kDelim);
        
         // System.out.println("src: " + fields[0] + " dest: " + vertexId +
         //       "MESSAGE>>" + val + "<<");

         // self-message check
         if (vertexId.equals(fields[0])) {
           isSelfMessageFound = true;
           vertexString = val;
          
           // System.out.println("Reduce: " + vertexId + " " + val + " " + fields.length);

           TObjectDoubleHashMap injLabels = CollectionUtil.String2Map(fields[2]);
           neighbors = CollectionUtil.String2Map(neighbors, fields[4]);
           randWalkProbs = CollectionUtil.String2Map(fields[5]);
          
           if (injLabels.size() > 0) {
             isSeedNode = true;

             // add injected labels to the estimated scores.
             ProbUtil.AddScores(newEstimatedScores,
                            mu1, injLabels);
           }
         } else {
           // an empty second field represents that the
           // neighbor has no valid label assignment yet.
           if (fields.length > 1) {
             neighScores.put(fields[0], fields[1]);
           }
         }
       }

       // terminate if message from self is not received.
       if (!isSelfMessageFound) {
         throw new RuntimeException("Self message not received for node " + vertexId);
       }
      
       // Add neighbor label scores to current node's label estimates only if the
       // current node is not a seed node. In case of seed nodes, clamp back the
       // injected label distribution, which is already done above when processing
       // the self messages
       if (!isSeedNode) {
         // collect neighbors label distributions and create one single
         // label distribution
         TObjectDoubleHashMap weightedNeigLablDist = new TObjectDoubleHashMap();
         Iterator<String> neighIter = neighScores.keySet().iterator();
         while (neighIter.hasNext()) {
           String neighName = neighIter.next();
           ProbUtil.AddScores(weightedNeigLablDist, // newEstimatedScores,
                         mu2 * neighbors.get(neighName),
 
View Full Code Here

      /////
      String line = value.toString();
      // id gold_label injected_labels estimated_labels neighbors rw_probabilities
      String[] fields = line.split(_kDelim);      
      TObjectDoubleHashMap neighbors = CollectionUtil.String2Map(fields[4]);      
      TObjectDoubleHashMap rwProbabilities = CollectionUtil.String2Map(fields[5]);
      
      // If the current node is a seed node but there is no
      // estimate label information yet, then transfer the seed label
      // to the estimated label distribution. Ideally, this is likely
      // to be used in the map of the very first iteration.
      boolean isSeedNode = fields[2].length() > 0 ? true : false;
      if (isSeedNode && fields[3].length() == 0) {
  fields[3] = fields[2];
      }

      // TODO(partha): move messages to ProtocolBuffers
      
      // Send two types of messages:
      //   -- self messages which will store the injection labels and
      //        random walk probabilities.
      //   -- messages to neighbors about current estimated scores
      //        of the node.
      //
      // message to self
      output.collect(new Text(fields[0]), new Text("labels" + _kDelim + line));

      // message to neighbors
      TObjectDoubleIterator neighIterator = neighbors.iterator();
      while (neighIterator.hasNext()) {
  neighIterator.advance();
        
  // message (neighbor_node, current_node + DELIM + curr_node_label_scores
  output.collect(new Text((String) neighIterator.key()),
           new Text("labels" + _kDelim + fields[0] + _kDelim + fields[3]));
        
  // message (neighbor_node, curr_node + DELIM + curr_node_edge_weights + DELIM curr_node_cont_prob
  assert(neighbors.containsKey((String) neighIterator.key()));
  output.collect(new Text((String) neighIterator.key()),
           new Text("edge_info" + _kDelim +
        fields[0] + _kDelim +
        neighbors.get((String) neighIterator.key()) + _kDelim +
        rwProbabilities.get(Constants._kContProb)));
      }
    }
View Full Code Here

    }
  
    public void reduce(Text key, Iterator<Text> values,
           OutputCollector<Text, Text> output, Reporter reporter) throws IOException {      
      // new scores estimated for the current node
      TObjectDoubleHashMap newEstimatedScores = new TObjectDoubleHashMap();
      
      // set to true only if the message sent to itself is found.
      boolean isSelfMessageFound = false;
      
      String vertexId = key.toString();
      String vertexString = "";
      
      TObjectDoubleHashMap neighbors = null;
      TObjectDoubleHashMap randWalkProbs = null;
      
      HashMap<String, String> neighScores =
  new HashMap<String, String>();
      
      TObjectDoubleHashMap incomingEdgeWeights = new TObjectDoubleHashMap();
      TObjectDoubleHashMap neighborContProb = new TObjectDoubleHashMap();
      
      int totalMessagesReceived = 0;
      
      // iterate over all the messages received at the node
      while (values.hasNext()) {
  ++totalMessagesReceived;

  String val = values.next().toString();
  String[] fields = val.split(_kDelim);
        
  // first field represents the type of message
  String msgType = fields[0];
        
  if (fields[0].equals("labels")) {          
    // self-message check
    if (vertexId.equals(fields[1])) {
      isSelfMessageFound = true;
      vertexString = val;          
 
      TObjectDoubleHashMap injLabels = CollectionUtil.String2Map(fields[3]);
      neighbors = CollectionUtil.String2Map(neighbors, fields[5]);
      randWalkProbs = CollectionUtil.String2Map(fields[6]);
            
      if (injLabels.size() > 0) {          
        // add injected labels to the estimated scores.
        ProbUtil.AddScores(newEstimatedScores,
         mu1 * randWalkProbs.get(Constants._kInjProb),
         injLabels);
      }
    } else {
      // an empty third field represents that the
      // neighbor has no valid label assignment yet.
      if (fields.length > 2) {
        neighScores.put(fields[1], fields[2]);
      }
    }
  } else if (msgType.equals("edge_info")) {
    // edge_info neigh_vertex incoming_edge_weight cont_prob
    String neighId = fields[1];
          
    if (!incomingEdgeWeights.contains(neighId)) {
      incomingEdgeWeights.put(neighId, Double.parseDouble(fields[2]));
    }
          
    if (!neighborContProb.contains(neighId)) {
      neighborContProb.put(neighId, Double.parseDouble(fields[3]));
    }
  } else {
      throw new RuntimeException("Invalid message: " + val);
  }
      }

      // terminate if message from self is not received.
      if (!isSelfMessageFound) {
          throw new RuntimeException("Self message not received for node " + vertexId);
      }
      
      // collect neighbors' label distributions and create one single
      // label distribution
      TObjectDoubleHashMap weightedNeigLablDist = new TObjectDoubleHashMap();
      Iterator<String> neighIter = neighScores.keySet().iterator();
      while (neighIter.hasNext()) {
  String neighName = neighIter.next();
        
  double mult = randWalkProbs.get(Constants._kContProb) * neighbors.get(neighName) +
View Full Code Here

    int totalLabels = labels.size();
    assert (totalLabels > 0);
    double prior = 1.0 / totalLabels;
    assert (prior > 0);

    TObjectDoubleHashMap retMap = new TObjectDoubleHashMap();
    for (int li = 0; li < totalLabels; ++li) {
      retMap.put(labels.get(li), prior);
    }
    return (retMap);
  }
View Full Code Here

    return (sum);
  }

  public static double GetDifferenceNorm2Squarred(TObjectDoubleHashMap m1,
                                                  double m1Mult, TObjectDoubleHashMap m2, double m2Mult) {
    TObjectDoubleHashMap diffMap = new TObjectDoubleHashMap();

    // copy m1 into the difference map
    TObjectDoubleIterator iter = m1.iterator();
    while (iter.hasNext()) {
      iter.advance();
      diffMap.put(iter.key(), m1Mult * iter.value());
    }

    iter = m2.iterator();
    while (iter.hasNext()) {
      iter.advance();
      diffMap.adjustOrPutValue(iter.key(), -1 * m2Mult * iter.value(), -1
                               * m2Mult * iter.value());
    }

    double val = 0;
    iter = diffMap.iterator();
    while (iter.hasNext()) {
      iter.advance();
      val += iter.value() * iter.value();
    }

View Full Code Here

    ArrayList<ArrayList> results = new ArrayList<ArrayList>();
    HashSet<String> uniqueConfigs = new HashSet<String>();
   
    // map from algo to the current best scores and the corresponding config
    HashMap<String,Hashtable> algo2BestConfig = new HashMap<String,Hashtable>();
    TObjectDoubleHashMap algo2BestScore = new TObjectDoubleHashMap();
   
    // store console
    PrintStream consoleOut = System.out;
    PrintStream consoleErr = System.err;

    for (int ci = 0; ci < configs.size(); ++ci) {
      Hashtable c = configs.get(ci);
     
      // if this a post-tune config, then generate seed and test files
      if (Defaults.GetValueOrDefault((String) c.get("is_final_run"), false)) {
        String splitId = Defaults.GetValueOrDie(c, "split_id");
        c.put("seed_file", c.remove("seed_base") + "." + splitId + ".train");
        c.put("test_file", c.remove("test_base") + "." + splitId + ".test");
      }
     
      // output file name is considered a unique identifier of a configuration
      String outputFile = GetOutputFileName(c, opDir, idenStr);
      if (uniqueConfigs.contains(outputFile)) {
        continue;
      }
      uniqueConfigs.add(outputFile);
      if (opDir != null) {
        c.put("output_file", outputFile);
      }

      System.out.println("Working with config: " + c.toString());

      try {
        // reset System.out so that the log printed using System.out.println
        // is directed to the right log file
        String logFile = GetLogFileName(c, logDir, idenStr);
       
        // if the log file exists, then don't repeat
        File lf = new File(logFile);
        if (skipExistingConfigs && lf.exists()) {
          continue;
        }
       
        FileOutputStream fos = new FileOutputStream(new File(logFile));
        PrintStream ps = new PrintStream(fos);
        System.setOut(ps);
        System.setErr(ps);
     
        results.add(new ArrayList());
        JuntoConfigRunner.apply(c, results.get(results.size() - 1));
        UpdateBestConfig((String) c.get("algo"), algo2BestScore,
                         algo2BestConfig, c, results.get(results.size() - 1));

        // reset System.out back to the original console value
        System.setOut(consoleOut);
        System.setErr(consoleErr);
       
        // close log file
        fos.close();

      } catch (FileNotFoundException fnfe) {
        fnfe.printStackTrace();
      } catch (IOException ioe) {
        ioe.printStackTrace();
      }
    }
   
    // print out the best parameters for each algorithm
    Iterator algoIter = algo2BestConfig.keySet().iterator();
    while (algoIter.hasNext()) {
      String algo = (String) algoIter.next();
      System.out.println("\n#################\n" +
                         "BEST_CONFIG_FOR " + algo + " " +
                         algo2BestScore.get(algo) + "\n" +
                         CollectionUtil.Map2StringPrettyPrint(algo2BestConfig.get(algo)));
     
      // run test with tuned parameters, if requested
      if (finalTestConfigFile != null) {
        Hashtable finalTestConfig = (Hashtable) algo2BestConfig.get(algo).clone();
View Full Code Here

TOP

Related Classes of gnu.trove.map.hash.TObjectDoubleHashMap$MapBackedView

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.