2

I am stuck with one engine and multiple algorithms and need help further.

I have one engine and 2 algorithms, let's call AL1 and AL2. In src/main/scala having 2 algorithm scala files, name AL1Algorithm.scala and AL2Algorithm.scala each algorithm have its own param and model (AL1AlgorithmParams and AL1Model for AL1 and AL2AlgorithmParams and AL2Model for AL2). Therefore there are 2 train functions and 2 predict functions.

I can train 2 algorithms by calling pio train. When I call send_query to test, the program always return the result of the algorithm I put on the top of engine.json file

For instance, if I put AL1Algorithm on top, send_query will run AL1Algorithm

"algorithms": [
    {
      "name": "AL1Algorithm",
      "params": {
        "n": 100
      }
    },
    {
      "name": "AL2Algorithm",
      "params": {
        "n": 100
      }
    }

if I put AL2Algorithm on top, send_query will run AL2Algorithm'

"algorithms": [
    {
      "name": "AL2Algorithm",
      "params": {
        "n": 100
      }
    },
    {
      "name": "AL1Algorithm",
      "params": {
        "n": 100
      }
    }

My question is, can I call 2 algorithms for one send_query, how to distinguish which algorithm should be called in send_query instead of engine.json. There is something weird here, I think there are problems with my configuration. Will Serving.scala produce results of 2 algorithms at the same time. Can I split the results of each algorithm in serving.

Thank you very much

aval
  • 57
  • 8
Le Kim Trang
  • 369
  • 2
  • 5
  • 17

1 Answers1

0

You also need to modify the serve() function In Serving.scala. the serve() function has 2 arguments. see below

def serve(query: Query,
   predictedResults: Seq[PredictedResult]): PredictedResult

The query is sent to both algorithms, and the PredictedResult returned by both algorithms are passed to serve() function as Seq[PredictedResult]. The Seq[] order is the same as the order of algorithm defined in engine.json. You need to modify this to make use of all PredictedResult output by algorithm. Some of the template examples by default only take the 1st result (predictedResults.head)

Please also see "Step 4. Modify Serving to combine multiple algorithms' outputs" in this doc as example.

GitaarLAB
  • 14,536
  • 11
  • 60
  • 80
  • Dear Kenneth, Thank you very much for your reply. I can get all of the result of 2 algorithms now by override def serve(query: Query, predictedResults: Seq[PredictedResult]): PredictedResult = { val standard: Seq[Array[ItemScore]] = predictedResults.map(_.itemScores) new PredictedResult(standard.flatten.toArray) } Can you help me the way to split them, I have one parameter in query named "eventType", if eventType == "AL1" then I need the result of the first algorithm, if eventType == "AL2", outcome of algorithm 2 should return, and so on, if eventType == "ALn", then I need algorithm n. – Le Kim Trang Sep 17 '15 at 09:50
  • One way to do it is to define PredictedResult as – Kenneth Chan Sep 18 '15 at 06:50
  • (sorry, hit enter by mistake in previous comment) One way to do it is to add a field in your PredictedResult to indicate the result is coming from which algorithm. e.g. `case class PredictedResult(algoId: String, itemScores: Array[ItemScore]) extends Serializable`, then in serving you can tell the PredictedResult is from which algorithm. – Kenneth Chan Sep 18 '15 at 06:56