{"id":13791097,"url":"https://github.com/rstudio/sparktf","last_synced_at":"2025-10-15T22:39:12.165Z","repository":{"id":66205590,"uuid":"168762859","full_name":"rstudio/sparktf","owner":"rstudio","description":"R interface to Spark TensorFlow Connector","archived":false,"fork":false,"pushed_at":"2021-09-13T19:57:55.000Z","size":13,"stargazers_count":13,"open_issues_count":3,"forks_count":8,"subscribers_count":9,"default_branch":"main","last_synced_at":"2024-08-03T22:04:24.707Z","etag":null,"topics":["apache-spark","keras","r","rstats","sparklyr","sparklyr-extension","tensorflow"],"latest_commit_sha":null,"homepage":null,"language":"R","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rstudio.png","metadata":{"files":{"readme":"README.Rmd","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2019-02-01T21:31:54.000Z","updated_at":"2022-09-20T23:36:48.000Z","dependencies_parsed_at":"2023-05-29T19:30:39.146Z","dependency_job_id":null,"html_url":"https://github.com/rstudio/sparktf","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rstudio%2Fsparktf","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rstudio%2Fsparktf/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rstudio%2Fsparktf/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rstudio%2Fsparktf/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rstudio","download_url":"https://codeload.github.com/rstudio/sparktf/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224014256,"owners_count":17241281,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["apache-spark","keras","r","rstats","sparklyr","sparklyr-extension","tensorflow"],"created_at":"2024-08-03T22:00:55.592Z","updated_at":"2025-10-15T22:39:07.114Z","avatar_url":"https://github.com/rstudio.png","language":"R","funding_links":[],"categories":["Sparklyr Analysis Tools","Keras Cheatsheet"],"sub_categories":["Deep Learning","Data Pipeline"],"readme":"---\noutput: github_document\n---\n\n\u003c!-- README.md is generated from README.Rmd. Please edit that file --\u003e\n\n```{r, include = FALSE}\nknitr::opts_chunk$set(\n  collapse = TRUE,\n  comment = \"#\u003e\",\n  fig.path = \"man/figures/README-\",\n  out.width = \"100%\"\n)\n```\n# sparktf\n\n\u003c!-- badges: start --\u003e\n[![Travis build status](https://travis-ci.org/rstudio/sparktf.svg?branch=master)](https://travis-ci.org/rstudio/sparktf)\n\u003c!-- badges: end --\u003e\n\n## Overview\n\n**sparktf** is a [sparklyr](https://spark.rstudio.com/) extension that allows writing of Spark `DataFrame`s to `TFRecord`, the recommended format for persisting data to be used in training with TensorFlow. \n\n## Installation\n\nYou can install the development version of sparktf from GitHub with:\n\n``` r\ndevtools::install_github(\"rstudio/sparktf\")\n```\n\n## Example\n\nWe first attach the required packages and establish a Spark connection.\n\n```{r}\nlibrary(sparktf)\nlibrary(sparklyr)\nlibrary(keras)\nuse_implementation(\"tensorflow\")\nlibrary(tensorflow)\ntfe_enable_eager_execution()\nlibrary(tfdatasets)\n\nsc \u003c- spark_connect(master = \"local\")\n```\n\nCopied a sample dataset to Spark then write it to disk via `spark_write_tfrecord()`.\n\n```{r}\ndata_path \u003c- file.path(tempdir(), \"iris\")\niris_tbl \u003c- sdf_copy_to(sc, iris)\n\niris_tbl %\u003e%\n  ft_string_indexer_model(\n    \"Species\", \"label\",\n    labels = c(\"setosa\", \"versicolor\", \"virginica\")\n  ) %\u003e%\n  spark_write_tfrecord(\n    path = data_path,\n    write_locality = \"local\"\n  )\n```\n\nWe now read the saved `TFRecord` file and parse the contents to create a dataset object. For details, refer to the [package website for tfdatasets](https://tensorflow.rstudio.com/tools/tfdatasets/articles/introduction.html).\n\n```{r, message = FALSE}\ndataset \u003c- tfrecord_dataset(list.files(data_path, full.names = TRUE)) %\u003e%\n  dataset_map(function(example_proto) {\n    features \u003c- list(\n      label = tf$FixedLenFeature(shape(), tf$float32),\n      Sepal_Length = tf$FixedLenFeature(shape(), tf$float32),\n      Sepal_Width = tf$FixedLenFeature(shape(), tf$float32),\n      Petal_Length = tf$FixedLenFeature(shape(), tf$float32),\n      Petal_Width = tf$FixedLenFeature(shape(), tf$float32)\n    )\n\n    features \u003c- tf$parse_single_example(example_proto, features)\n    x \u003c- list(\n      features$Sepal_Length, features$Sepal_Width,\n      features$Petal_Length, features$Petal_Width\n      )\n    y \u003c- tf$one_hot(tf$cast(features$label, tf$int32), 3L)\n    list(x, y)\n  }) %\u003e%\n  dataset_shuffle(150) %\u003e%\n  dataset_batch(16)\n```\n\nNow, we can define a Keras model using the [keras package](https://keras.rstudio.com/) and fit it by feeding the `dataset` object defined above.\n\n```{r}\nmodel \u003c- keras_model_sequential() %\u003e%\n  layer_dense(32, activation = \"relu\", input_shape = 4) %\u003e%\n  layer_dense(3, activation = \"softmax\")\n\nmodel %\u003e%\n  compile(loss = \"categorical_crossentropy\", optimizer = tf$train$AdamOptimizer())\n\nhistory \u003c- model %\u003e%\n  fit(dataset, epochs = 100, verbose = 0)\n```\n\nFinally, we can use the trained model to make some predictions.\n\n```{r}\nnew_data \u003c- tf$constant(c(4.9, 3.2, 1.4, 0.2), shape = c(1, 4))\nmodel(new_data)\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frstudio%2Fsparktf","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frstudio%2Fsparktf","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frstudio%2Fsparktf/lists"}