When I build a Spark model and call it, the predictions take tens of ms to return. However, when I save that same model, then load it, the predictions take much longer. Is there some sort of cache I should be using?
model.cache()
after loading does not work, as the model is not an RDD.
This works great:
from pyspark.mllib.recommendation import ALS
from pyspark import SparkContext
import time
sc = SparkContext()
# Some example data
r = [(1, 1, 1.0),
(1, 2, 2.0),
(2, 1, 2.0)]
ratings = sc.parallelize(r)
model = ALS.trainImplicit(ratings, 1, seed=10)
# Call model and time it
now = time.time()
for t in range(10):
model.predict(2, 2)
elapsed = (time.time() - now)*1000/(t+1)
print "Average time for model call: {:.2f}ms".format(elapsed)
model.save(sc, 'my_spark_model')
Output: Average time for model call: 71.18ms
If I run the following, the predictions take much more time:
from pyspark.mllib.recommendation import MatrixFactorizationModel
from pyspark import SparkContext
import time
sc = SparkContext()
model_path = "my_spark_model"
model = MatrixFactorizationModel.load(sc, model_path)
# Call model and time it
now = time.time()
for t in range(10):
model.predict(2, 2)
elapsed = (time.time() - now)*1000/(t+1)
print "Average time for loaded model call: {:.2f}ms".format(elapsed)
The output: Average time for loaded model call: 180.34ms
For BIG models, I'm seeing prediction times over 10 seconds for a single call after loading a saved model.