Package com.cloudera.oryx.ml.serving.als.model

Examples of com.cloudera.oryx.ml.serving.als.model.ALSServingModel


  @Path("{userID}/{itemID : .+}")
  @Produces({CSVMessageBodyWriter.TEXT_CSV, MediaType.APPLICATION_JSON})
  public List<Double> get(@PathParam("userID") String userID,
                          @PathParam("itemID") List<PathSegment> pathSegmentsList)
      throws OryxServingException {
    ALSServingModel model = getALSServingModel();
    float[] userFeatures = model.getUserVector(userID);
    checkExists(userFeatures != null, userID);
    List<Double> results = new ArrayList<>(pathSegmentsList.size());
    for (PathSegment pathSegment : pathSegmentsList) {
      float[] itemFeatures = model.getItemVector(pathSegment.getPath());
      if (itemFeatures == null) {
        results.add(0.0);
      } else {
        double value = VectorMath.dot(itemFeatures, userFeatures);
        Preconditions.checkState(!(Double.isInfinite(value) || Double.isNaN(value)), "Bad estimate");
View Full Code Here


  @Produces({CSVMessageBodyWriter.TEXT_CSV, MediaType.APPLICATION_JSON})
  public List<Double> get(
      @PathParam("toItemID") String toItemID,
      @PathParam("itemID") List<PathSegment> pathSegmentsList) throws OryxServingException {

    ALSServingModel alsServingModel = getALSServingModel();
    float[] toItemFeatures = alsServingModel.getItemVector(toItemID);
    checkExists(toItemFeatures != null, toItemID);

    double toItemFeaturesNorm = VectorMath.norm(toItemFeatures);
    List<Double> results = new ArrayList<>(pathSegmentsList.size());
    for (PathSegment item : pathSegmentsList) {
      float[] itemFeatures = alsServingModel.getItemVector(item.getPath());
      if (itemFeatures == null) {
        results.add(0.0);
      } else {
        double value = VectorMath.dot(itemFeatures, toItemFeatures) /
            (toItemFeaturesNorm * VectorMath.norm(itemFeatures));
 
View Full Code Here

  @GET
  @Produces({CSVMessageBodyWriter.TEXT_CSV, MediaType.APPLICATION_JSON})
  public List<IDCount> get(@DefaultValue("10") @QueryParam("howMany") int howMany,
                           @DefaultValue("0") @QueryParam("offset") int offset) {

    ALSServingModel model = getALSServingModel();
    ObjectIntMap<String> itemCounts = model.getItemCounts();

    Iterable<Pair<String,Integer>> countPairs =
        Iterables.transform(itemCounts,
            new Function<ObjectIntCursor<String>, Pair<String,Integer>>() {
              @Override
View Full Code Here

      @QueryParam("rescorerParams") List<String> rescorerParams) throws OryxServingException {

    check(howMany > 0, "howMany must be positive");
    check(offset >= 0, "offset must be nonnegative");

    ALSServingModel model = getALSServingModel();
    float[] userVector = model.getUserVector(userID);
    checkExists(userVector != null, userID);

    Predicate<String> allowedFn = null;
    if (!considerKnownItems) {
      ObjectSet<String> knownItems = model.getKnownItems(userID);
      if (knownItems != null && !knownItems.isEmpty()) {
        synchronized (knownItems) {
          allowedFn = new NotKnownPredicate(new ObjectOpenHashSet<>(knownItems));
        }
      }
    }

    List<Pair<String,Double>> topIDDots = model.topN(
        new DotsFunction(userVector),
        howMany + offset,
        allowedFn);
    return toIDValueResponse(topIDDots, howMany, offset);
  }
View Full Code Here

    check(!pathSegmentsList.isEmpty(), "Need at least 1 user");
    check(howMany > 0, "howMany must be positive");
    check(offset >= 0, "offset must be non-negative");

    ALSServingModel alsServingModel = getALSServingModel();
    double[][] userFeaturesVectors = new double[pathSegmentsList.size()][];
    ObjectSet<String> userKnownItems = new ObjectOpenHashSet<>();

    for (int i = 0; i < userFeaturesVectors.length; i++) {
      String userID = pathSegmentsList.get(i).getPath();
      float[] userFeatureVector = alsServingModel.getUserVector(userID);
      checkExists(userFeatureVector != null, userID);
      userFeaturesVectors[i] = VectorMath.toDoubles(userFeatureVector);
      if (!considerKnownItems) {
        ObjectSet<String> knownItems = alsServingModel.getKnownItems(userID);
        if (knownItems != null && !knownItems.isEmpty()) {
          synchronized (knownItems) {
            for (ObjectCursor<String> knownItem : knownItems) {
              userKnownItems.add(knownItem.value);
            }
          }
        }
      }
    }

    List<Pair<String,Double>> topIDDots = alsServingModel.topN(
        new DotsFunction(userFeaturesVectors),
        howMany + offset,
        new NotKnownPredicate(userKnownItems));

    return toIDValueResponse(topIDDots, howMany, offset);
View Full Code Here

    check(!pathSegmentsList.isEmpty(), "Need at least 1 item to determine similarity");
    check(howMany > 0, "howMany must be positive");
    check(offset >= 0, "offset must be non-negative");

    ALSServingModel alsServingModel = getALSServingModel();
    float[][] itemFeatureVectors = new float[pathSegmentsList.size()][];
    ObjectSet<String> knownItems = new ObjectOpenHashSet<>();

    for (int i = 0; i < itemFeatureVectors.length; i++) {
      String itemID = pathSegmentsList.get(i).getPath();
      float[] itemVector = alsServingModel.getItemVector(itemID);
      checkExists(itemVector != null, itemID);
      itemFeatureVectors[i] = itemVector;
      knownItems.add(itemID);
    }

    List<Pair<String,Double>> topIDCosines = alsServingModel.topN(
        new CosineAverageFunction(itemFeatureVectors),
        howMany + offset,
        new NotKnownPredicate(knownItems));

    return toIDValueResponse(topIDCosines, howMany, offset);
View Full Code Here

      @QueryParam("rescorerParams") List<String> rescorerParams) throws OryxServingException {

    check(howMany > 0, "howMany must be positive");
    check(offset >= 0, "offset must be nonnegative");

    ALSServingModel model = getALSServingModel();
    double[] anonymousUserFeatures =
        EstimateForAnonymous.buildAnonymousUserFeatures(model, pathSegments);

    ObjectSet<String> knownItems = new ObjectOpenHashSet<>();
    for (Pair<String,?> itemValue : EstimateForAnonymous.parsePathSegments(pathSegments)) {
      knownItems.add(itemValue.getFirst());
    }

    List<Pair<String,Double>> topIDDots = model.topN(
        new DotsFunction(anonymousUserFeatures),
        howMany + offset,
        new NotKnownPredicate(knownItems));
    return toIDValueResponse(topIDDots, howMany, offset);
  }
View Full Code Here

      @DefaultValue("0") @QueryParam("offset") int offset) throws OryxServingException {

    check(howMany > 0, "howMany must be positive");
    check(offset >= 0, "offset must be non-negative");

    ALSServingModel model = getALSServingModel();
    float[] itemVector = model.getItemVector(itemID);
    checkExists(itemVector != null, itemID);
    List<Pair<String,float[]>> knownItemVectors = model.getKnownItemVectorsForUser(userID);
    checkExists(knownItemVectors != null, userID);

    Iterable<Pair<String,Double>> idSimilarities =
        Iterables.transform(knownItemVectors, new CosineSimilarityFunction(itemVector));
View Full Code Here

  @Produces({CSVMessageBodyWriter.TEXT_CSV, MediaType.APPLICATION_JSON})
  public Double get(
      @PathParam("toItemID") String toItemID,
      @PathParam("itemID") List<PathSegment> pathSegments) throws OryxServingException {

    ALSServingModel model = getALSServingModel();
    float[] toItemVector = model.getItemVector(toItemID);
    checkExists(toItemVector != null, toItemID);

    double[] anonymousUserFeatures = buildAnonymousUserFeatures(model, pathSegments);
    return VectorMath.dot(anonymousUserFeatures, toItemVector);
  }
View Full Code Here

      @DefaultValue("0") @QueryParam("offset") int offset) throws OryxServingException {

    check(howMany > 0, "howMany must be positive");
    check(offset >= 0, "offset must be nonnegative");

    ALSServingModel model = getALSServingModel();
    float[] userVector = model.getUserVector(userID);
    checkExists(userVector != null, userID);
    List<Pair<String,float[]>> knownItemVectors = model.getKnownItemVectorsForUser(userID);
    checkExists(knownItemVectors != null, userID);

    Iterable<Pair<String,Double>> idDots =
        Iterables.transform(knownItemVectors, new DotsFunction(userVector));
View Full Code Here

TOP

Related Classes of com.cloudera.oryx.ml.serving.als.model.ALSServingModel

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.