Spark-Keras Integration
This package enables seamless integration of PySpark DataFrames with Keras models, allowing users to efficiently train and predict using distributed data.
Quickstart Guide
Setting Up the PySpark DataFrame
Start by creating a PySpark DataFrame as shown below:
import pandas as pd
from pyspark.sql import SparkSession
import tensorflow as tf
# Initialize a Spark session
spark = SparkSession.builder.appName("spark_keras").getOrCreate()
# Create a PySpark DataFrame
spark_df = spark.createDataFrame(
pd.DataFrame({
"feature1": tf.random.normal([100]).numpy().tolist(),
"label1": tf.random.normal([100]).numpy().tolist(),
"partition_id": [0 for _ in range(100)]
})
)
Training and Predicting with KerasSparkModel
You can fit and predict with KerasSparkModel using the standard Keras API:
import tensorflow as tf
from keras_spark.models import KerasSparkModel as Model
# Define the Keras model
input_layer = tf.keras.Input(shape=[1], name="feature1")
output_layer = tf.keras.layers.Dense(1, name="label1")(input_layer)
model = Model(input_layer, output_layer)
model.compile("adam","mean_squared_error")
# Train the model using the PySpark DataFrame
model.fit(spark_df, batch_size=10, epochs=100,partition_col="partition_id")
# Use Spark for distributed scoring on the PySpark DataFrame
predictions = model.predict(spark_df).select("model_output.label1")
Important Considerations
-
Naming Conventions:
- Input Names: Each Keras input must have a specified name that corresponds to the respective PySpark DataFrame column.
- Output Names: Output names are inferred from the Keras output layers and must match the PySpark column names if using
.fit().
-
Data Type Compatibility:
- Ensure that the data types of Keras inputs and the corresponding PySpark columns are compatible.
-
Partitioning Requirements:
- The PySpark DataFrame must include a
partition_idcolumn, with values ranging from0tonr_partitions. - Choose
nr_partitionscarefully to ensure that the Spark driver can handle the workload. - Parallel processing of partitions is handled using
.interleave(), with the degree of parallelism set bynum_parallel_calls.
- The PySpark DataFrame must include a
-
Prediction Output:
- The
.predict()method generates an additional struct column namedmodel_output. - To access specific outputs, reference them using their keys, e.g.,
model_output.label1.
- The
-
Keras Version:
- This package is compatible with Keras version 3.0 and above.
This integration empowers users to leverage distributed data processing with PySpark while taking full advantage of Keras's deep learning capabilities.
Metadata
Release files for keras-spark 0.99
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| keras_spark-0.99.tar.gz | 12.6 kB | Without provenance | Details for keras_spark-0.99.tar.gz |
Built distributions (wheels)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| keras_spark-0.99-py3-none-any.whl | Python 3 | none | any | Details for keras_spark-0.99-py3-none-any.whl |
| keras_spark-0.99-py2.py3-none-any.whl | Python 2, Python 3 | none | any | Details for keras_spark-0.99-py2.py3-none-any.whl |
Total release size: 39.4 kB
Release files / keras_spark-0.99.tar.gz
| Download URL | keras_spark-0.99.tar.gz |
|---|---|
| Size | 12.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d46b509e3287cfdfee9cf34aa80d07e8968a6be50bc1b799cc1fb3972d0d7eb8
|
|
BLAKE2b-256 checksum How to use checksums |
e6982c7e4b72731326c06cf85c7974423ee96dfccf0d8c5a6c4c4cd3fa8f956f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.9.18
|
Release files / keras_spark-0.99-py3-none-any.whl
| Download URL | keras_spark-0.99-py3-none-any.whl |
|---|---|
| Size | 13.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
8c9a66f7e1c05c38fa4b50c218f68a70ddf4d165010332436d0738fca302bc74
|
|
BLAKE2b-256 checksum How to use checksums |
9a9a53847281a9a2eb5a509239f05eff05ea692c859e297cfa4ec6a75adf444f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.9.18
|
Release files / keras_spark-0.99-py2.py3-none-any.whl
| Download URL | keras_spark-0.99-py2.py3-none-any.whl |
|---|---|
| Size | 13.4 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
911f26ac02aff5f7195a25912f24a0811fbe981397e8a221459b76d33e492a77
|
|
BLAKE2b-256 checksum How to use checksums |
e1b7998f21365b14b41fbaeb484bdb37dcddcf83e981129e84e673ae23ed10b1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.9.18
|
Security for keras-spark 0.99
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.