Exemplo n.º 1
0
  /**
   * Write a user or item index to the file.
   *
   * @param map The index to write.
   */
  private void writeIndex(Long2ObjectMap<IntList> map) throws IOException {
    LongSortedSet keys = LongUtils.packedSet(map.keySet());
    BinaryIndexTableWriter tableWriter =
        BinaryIndexTableWriter.create(format, channel, keys.size());

    SortComparator indexComparator = new SortComparator();

    LongIterator iter = keys.iterator();
    while (iter.hasNext()) {
      final long key = iter.nextLong();
      int[] indexes = map.get(key).toIntArray();
      if (needsSorting) {
        IntArrays.quickSort(indexes, indexComparator);
      }

      if (translationMap != null) {
        for (int i = 0; i < indexes.length; i++) {
          indexes[i] = translationMap[indexes[i]];
        }
      }

      logger.debug("writing {} indices for id {}", key, indexes.length);
      tableWriter.writeEntry(key, indexes);
    }
  }
Exemplo n.º 2
0
  @Override
  public void predict(long uid, @Nonnull MutableSparseVector predictions) {
    logger.debug("predicting {} items for {}", predictions.keyDomain().size(), uid);
    OrdRecModel params = new OrdRecModel(quantizer);
    SparseVector ratings = makeUserVector(uid, userEventDao);
    LongSet keySet = LongUtils.setUnion(ratings.keySet(), predictions.keyDomain());
    MutableSparseVector scores = MutableSparseVector.create(keySet);
    itemScorer.score(uid, scores);
    params.train(ratings, scores);
    logger.debug("trained parameters for {}: {}", uid, params);

    Vector probabilities = Vector.createLength(params.getLevelCount());
    Long2ObjectMap<IVector> distChannel = null;
    if (reportDistribution) {
      distChannel = predictions.addChannel(RATING_PROBABILITY_CHANNEL);
    }

    for (VectorEntry e : predictions.fast(VectorEntry.State.EITHER)) {
      long iid = e.getKey();
      double score = scores.get(iid);
      params.getProbDistribution(score, probabilities);

      int mlIdx = probabilities.maxElementIndex();

      predictions.set(e, quantizer.getIndexValue(mlIdx));
      if (distChannel != null) {
        distChannel.put(e.getKey(), probabilities.immutable());
      }
    }
  }
Exemplo n.º 3
0
 LongSortedSet itemSet(long item) {
   return LongUtils.packedSet(item);
 }