我正在尝试在集群上分发Keras培训并使用Elephas。但是,当运行Elephas文档(https://github.com/maxpumperla/elephas)的基本示例时:
from elephas.utils.rdd_utils import to_simple_rdd
rdd = to_simple_rdd(sc, x_train, y_train)
from elephas.spark_model import SparkModel
from elephas import optimizers as elephas_optimizers
sgd = elephas_optimizers.SGD()
spark_model = SparkModel(sc, model, optimizer=sgd, frequency='epoch', mode='asynchronous', num_workers=2)
spark_model.train(rdd, nb_epoch=epochs, batch_size=batch_size, verbose=1, validation_split=0.1)
我收到以下错误:
ImportError: No module named elephas.spark_model
```Py4JJavaError: An error occurred while calling z:org.apache.spark.api.python.PythonRDD.collectAndServe.
: org.apache.spark.SparkException: Job aborted due to stage failure: Task 1 in stage 5.0 failed 4 times, most recent failure: Lost task 1.3 in stage 5.0 (TID 58, xxxx, executor 8): org.apache.spark.api.python.PythonException: Traceback (most recent call last):
File "/xx/xx/hadoop/yarn/local/usercache/xx/appcache/application_151xxx857247_19188/container_1512xxx247_19188_01_000009/pyspark.zip/pyspark/worker.py", line 163, in main
func, profiler, deserializer, serializer = read_command(pickleSer, infile)
File "/xx/xx/hadoop/yarn/local/usercache/xx/appcache/application_151xxx857247_19188/container_1512xxx247_19188_01_000009/pyspark.zip/pyspark/worker.py", line 54, in read_command
command = serializer._read_with_length(file)
File /yarn/local/usercache/xx/appcache/application_151xxx857247_19188/container_1512xxx247_19188_01_000009/pyspark.zip/pyspark/serializers.py", line 169, in _read_with_length
return self.loads(obj)
File "/yarn//local/usercache/xx/appcache/application_151xxx857247_19188/container_1512xxx247_19188_01_000009/pyspark.zip/pyspark/serializers.py", line 454, in loads
return pickle.loads(obj)
ImportError: No module named elephas.spark_model
at org.apache.spark.api.python.PythonRunner$$anon$1.read(PythonRDD.scala:193)
at org.apache.spark.api.python.PythonRunner$$anon$1.<init>(PythonRDD.scala:234)
at org.apache.spark.api.python.PythonRunner.compute(PythonRDD.scala:152)
at org.apache.spark.api.python.PythonRDD.compute(PythonRDD.scala:63)
at org.apache.spark.rdd.RDD.computeOrReadCheckpoint(RDD.scala:323)
at org.apache.spark.rdd.RDD.iterator(RDD.scala:287)
at org.apache.spark.scheduler.ResultTask.runTask(ResultTask.scala:87)
at org.apache.spark.scheduler.Task.run(Task.scala:99)
at org.apache.spark.executor.Executor$TaskRunner.run(Executor.scala:322)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)```
此外,该模型实际上已创建,我可以print(spark_model)
并获得此<elephas.spark_model.SparkModel object at 0x7efce0abfcd0>
。错误发生在spark_model.train
。
我使用pip2 install git+https://github.com/maxpumperla/elephas
安装了elephas,也许这是相关的。
我使用PySpark 2.1.1,Keras 2.1.4和Python 2.7。 我尝试使用 spark-submit 运行它:
PYSPARK_DRIVER_PYTHON=`which python` spark-submit --driver-memory 1G filname.py
还直接在Jupyter笔记本中。两者都会导致同样的问题。
任何人都可以给我任何指示吗?这个象是相关的还是PySpark问题?
编辑:我还上传了虚拟环境的zip文件并在脚本中调用它:
virtualenv spark_venv --relocatable
cd spark_venv
zip -qr ../spark_venv.zip *
PYSPARK_DRIVER_PYTHON=`which python` spark-submit --driver-memory 1G --py-files spark_venv.zip filename.py
然后在我做的文件中:
sc.addPyFile("spark_venv.zip")
导入此keras后没有任何问题,但我仍然从上面得到elephas
错误。
答案 0 :(得分:1)
您应该将elephas
库作为参数添加到spark-submit
命令中。
引用官方指南:
对于Python,您可以使用
--py-files
的{{1}}参数添加.py,.zip或.egg文件,以便与您的应用程序一起分发。如果您依赖多个Python文件,我们建议将它们打包成.zip或.egg。
答案 1 :(得分:1)
我找到了如何正确加载虚拟环境给主人和所有奴隶工人的解决方案:
virtualenv venv --relocatable
cd venv
zip -qr ../venv.zip *
PYSPARK_PYTHON=./SP/bin/python spark-submit --master yarn --deploy-mode cluster --conf spark.yarn.appMasterEnv.PYSPARK_PYTHON=./SP/bin/python --driver-memory 4G --archives venv.zip#SP filename.py
GitHub问题中的更多细节: https://github.com/maxpumperla/elephas/issues/80#issuecomment-371073492