/** * Build a recommender from a configuration. The recommender is immediately usable. This is * mostly useful for evaluations and test programs; more sophisticated applications that need * to build multiple recommenders from the same model should use a {@linkplain LenskitRecommenderEngine * recommender engine}. * * @param config The configuration. * @param dao The data access object. * @return The recommender. * @throws RecommenderBuildException If there is an error building the recommender. * @since 2.0 */ public static LenskitRecommender build(LenskitConfiguration config, DataAccessObject dao) throws RecommenderBuildException { return LenskitRecommenderEngine.build(config, dao).createRecommender(dao); } }
if (modelFile == null) { logger.info("creating fresh recommender"); LenskitRecommenderEngineBuilder builder = LenskitRecommenderEngine.newBuilder(); for (LenskitConfiguration config: environment.loadConfigurations(getConfigFiles())) { builder.addConfiguration(config); } else { logger.info("loading recommender from {}", modelFile); LenskitRecommenderEngineLoader loader = LenskitRecommenderEngine.newLoader(); loader.setClassLoader(environment.getClassLoader()); for (LenskitConfiguration config: environment.loadConfigurations(getConfigFiles())) {
@Override public void execute(Namespace opts) throws LenskitCommandException { Context ctx = new Context(opts); LenskitConfiguration dataConfig = ctx.input.getConfiguration(); LenskitRecommenderEngineBuilder builder = LenskitRecommenderEngine.newBuilder(); try { for (LenskitConfiguration config: ctx.environment.loadConfigurations(ctx.getConfigFiles())) { builder.addConfiguration(config); } } catch (IOException e) { throw new LenskitCommandException("error loading LensKit configuration", e); } builder.addConfiguration(dataConfig, ModelDisposition.EXCLUDED); Stopwatch timer = Stopwatch.createStarted(); LenskitRecommenderEngine engine = builder.build(ctx.input.getDAO()); timer.stop(); logger.info("built model in {}", timer); File output = ctx.getOutputFile(); CompressionMode comp = CompressionMode.autodetect(output); logger.info("writing model to {}", output); try (OutputStream raw = new FileOutputStream(output); OutputStream stream = comp.wrapOutput(raw)) { engine.write(stream); } catch (IOException e) { throw new LenskitCommandException("could not write output file", e); } }
/** * Load a configuration graph from a recommender model. * @param file The model file. * @return The recommender graph. * @throws IOException if there is an error loading the model. * @throws RecommenderConfigurationException if the model fails to configure. */ private DAGNode<Component, Dependency> loadModel(Context ctx, File file) throws IOException, RecommenderConfigurationException { logger.info("loading model from {}", file); LenskitRecommenderEngineLoader loader = LenskitRecommenderEngine.newLoader(); loader.setValidationMode(EngineValidationMode.DEFERRED) .addConfiguration(makeDataConfig(ctx)); for (LenskitConfiguration config: ctx.environment.loadConfigurations(ctx.getConfigFiles())) { loader.addConfiguration(config); } LenskitRecommenderEngine engine = loader.load(file); return engine.getGraph(); }
private LenskitRecommender buildRecommender(LenskitRecommenderEngine engine, @Nonnull DataAccessObject train, @Nullable DataAccessObject runtime) throws RecommenderBuildException { if (runtime == null) { runtime = train; } return engine.createRecommender(runtime); }
lre = LenskitRecommenderEngine.newBuilder() .addConfiguration(algorithm.getConfigurations().get(0)) .build(limitedDao); recommender.close(); recommender = lre.createRecommender(limitedDao);
@SuppressWarnings("deprecation") @Before public void setup() throws RecommenderBuildException { List<Rating> rs = new ArrayList<>(); rs.add(Rating.create(1, 5, 2)); rs.add(Rating.create(1, 7, 4)); rs.add(Rating.create(8, 4, 5)); rs.add(Rating.create(8, 5, 4)); StaticDataSource source = StaticDataSource.fromList(rs); LenskitConfiguration config = new LenskitConfiguration(); config.bind(DataAccessObject.class).toProvider(source); config.bind(ItemScorer.class).to(ItemItemScorer.class); config.bind(ItemBasedItemScorer.class).to(ItemItemItemBasedItemScorer.class); // this is the default // factory.setComponent(UserVectorNormalizer.class, VectorNormalizer.class, // IdentityVectorNormalizer.class); engine = LenskitRecommenderEngine.build(config); }
/** * Build a LensKit recommender engine from a configuration. The resulting recommender is * independent of any subsequent modifications to the configuration. * * @param config The configuration. * @param dao The data access object * @return The recommender engine. */ public static LenskitRecommenderEngine build(LenskitConfiguration config, DataAccessObject dao) throws RecommenderBuildException { return newBuilder().addConfiguration(config).build(dao); }
/** * Create a new recommender engine by reading a previously serialized engine from the * given input stream. The new engine will be identical to the old. It is assumed that the file * was created by using {@link #write(OutputStream)}. Classes will be loaded using a default * class loader. * * @param input The stream from which to load the engine. * @return The loaded recommender engine. * @throws IOException If there is an error reading from the file. * @throws RecommenderConfigurationException * If the configuration cannot be used. */ public static LenskitRecommenderEngine load(InputStream input) throws IOException, RecommenderConfigurationException { return newLoader().load(input); }
/** * Write the state of this recommender engine to the given file so * that it can be recreated later using another DAOFactory. This uses * default object serialization so if the factory has a PicoContainer or * session bindings containing non-serializable types, this will fail. * * @param file The file to write the rec engine to. * @throws IOException if there is an error serializing the engine. * @see #write(OutputStream) */ public void write(@Nonnull File file) throws IOException { write(file, CompressionMode.NONE); }
/** * Build the recommender engine. * * @param dao The data access object to use. Can be `null` to build without a DAO, but this is only useful in * special cases. * @return The built recommender engine, with {@linkplain ModelDisposition#EXCLUDED excluded} * components removed. * @throws RecommenderBuildException if there is an error building the engine. */ public LenskitRecommenderEngine build(DataAccessObject dao) throws RecommenderBuildException { DAGNode<Component, Dependency> graph = buildRecommenderGraph(dao); graph = instantiateGraph(graph); graph = rewriteExcludedComponents(graph, dao); boolean instantiable = GraphtUtils.getPlaceholderNodes(graph).isEmpty(); return new LenskitRecommenderEngine(graph, instantiable); }
/** * Create a LensKit recommender. * @param dao The data access object * @return The constructed recommender. */ public LenskitRecommender createRecommender(@WillNotClose DataAccessObject dao) throws RecommenderBuildException { LenskitConfiguration config = new LenskitConfiguration(); config.addComponent(dao); return createRecommender(config); }
/** * Build a recommender. * * @param defaults Additional configuration. This configuration comes <em>before</em> the * algorithm's configuration, so it is overridden if appropriate. It is used * for providing things such as DAOs. * @return The instantiated recommender. * @throws RecommenderBuildException */ public LenskitRecommender buildRecommender(@Nullable LenskitConfiguration defaults) throws RecommenderBuildException { LenskitRecommenderEngineBuilder builder = LenskitRecommenderEngine.newBuilder(); if (defaults != null) { builder.addConfiguration(defaults); } for (LenskitConfiguration cfg: configurations) { builder.addConfiguration(cfg); } return builder.build().createRecommender(); }
@SuppressWarnings("deprecation") @Before public void setup() throws RecommenderBuildException { List<Rating> rs = new ArrayList<>(); rs.add(Rating.create(1, 5, 2)); rs.add(Rating.create(1, 7, 4)); rs.add(Rating.create(8, 4, 5)); rs.add(Rating.create(8, 5, 4)); StaticDataSource source = StaticDataSource.fromList(rs); DataAccessObject dao = source.get(); LenskitConfiguration config = new LenskitConfiguration(); config.bind(DataAccessObject.class).to(dao); config.bind(ItemScorer.class).to(UserUserItemScorer.class); config.bind(NeighborFinder.class).to(LiveNeighborFinder.class); engine = LenskitRecommenderEngine.build(config); }
/** * Build a LensKit recommender engine from a configuration. The resulting recommender is * independent of any subsequent modifications to the configuration. * * @param config The configuration. * @return The recommender engine. */ @SuppressWarnings("deprecation") public static LenskitRecommenderEngine build(LenskitConfiguration config) throws RecommenderBuildException { return newBuilder().addConfiguration(config).build(); }
/** * Create a new recommender engine by reading a previously serialized engine from the * given file. The new engine will be identical to the old except it will use the new * DAOFactory. It is assumed that the file was created by using {@link #write(OutputStream)}. * Classes will be loaded using a default class loader. * * @param file The file from which to load the engine. * @return The loaded recommender engine. * @throws IOException If there is an error reading from the file. * @throws RecommenderConfigurationException * If the configuration cannot be used. */ public static LenskitRecommenderEngine load(File file) throws IOException, RecommenderConfigurationException { return newLoader().load(file); }
/** * Write the state of this recommender engine to the given file so * that it can be recreated later using another DAOFactory. This uses * default object serialization so if the factory has a PicoContainer or * session bindings containing non-serializable types, this will fail. * * @param file The file to write the rec engine to. * @param compressed Whether to compress the output file. * @throws IOException if there is an error serializing the engine. * @see #write(OutputStream) */ public void write(@Nonnull File file, CompressionMode compressed) throws IOException { try (OutputStream out = new FileOutputStream(file); OutputStream zout = compressed.getEffectiveCompressionMode(file.getName()).wrapOutput(out)) { write(zout); } }
return new LenskitRecommenderEngine(graph, instantiable);
/** * Build a recommender from a configuration. The recommender is immediately usable. This is * mostly useful for evaluations and test programs; more sophisticated applications that need * to build multiple recommenders from the same model should use a {@linkplain LenskitRecommenderEngine * recommender engine}. * * @param config The configuration. * @return The recommender. * @throws RecommenderBuildException If there is an error building the recommender. * @since 2.0 * @deprecated Use {@link #build(LenskitConfiguration, DataAccessObject)} */ @Deprecated @SuppressWarnings("deprecation") public static LenskitRecommender build(LenskitConfiguration config) throws RecommenderBuildException { return LenskitRecommenderEngine.build(config).createRecommender(); }
final int n = ctx.options.getInt("num_recs"); try (LenskitRecommender rec = engine.createRecommender(ctx.input.getDAO())) { ItemRecommender irec = rec.getItemRecommender(); DataAccessObject dao = rec.getDataAccessObject();