{"id":31536708,"url":"https://github.com/davidrsch/kerasnip","last_synced_at":"2026-05-03T07:16:02.345Z","repository":{"id":306619393,"uuid":"847727913","full_name":"davidrsch/kerasnip","owner":"davidrsch","description":"A bridge between the keras and tidymodels frameworks","archived":false,"fork":false,"pushed_at":"2025-09-18T11:19:28.000Z","size":12030,"stargazers_count":8,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-09-18T11:19:32.987Z","etag":null,"topics":["deep-learning","deep-learning-workflow","keras","parsnip","r","tidymodels"],"latest_commit_sha":null,"homepage":"https://davidrsch.github.io/kerasnip/","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/davidrsch.png","metadata":{"files":{"readme":"README.md","changelog":"NEWS.md","contributing":".github/CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":".github/CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":".github/CODEOWNERS","security":null,"support":".github/SUPPORT.md","governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2024-08-26T12:37:35.000Z","updated_at":"2025-09-18T08:01:52.000Z","dependencies_parsed_at":"2025-07-26T20:51:59.193Z","dependency_job_id":"07d3b48f-3455-4e2f-a5dd-e27e79ccb483","html_url":"https://github.com/davidrsch/kerasnip","commit_stats":null,"previous_names":["davidrsch/kerasnip"],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/davidrsch/kerasnip","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/davidrsch%2Fkerasnip","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/davidrsch%2Fkerasnip/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/davidrsch%2Fkerasnip/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/davidrsch%2Fkerasnip/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/davidrsch","download_url":"https://codeload.github.com/davidrsch/kerasnip/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/davidrsch%2Fkerasnip/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":278283493,"owners_count":25961310,"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","status":"online","status_checked_at":"2025-10-04T02:00:05.491Z","response_time":63,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["deep-learning","deep-learning-workflow","keras","parsnip","r","tidymodels"],"created_at":"2025-10-04T07:47:34.164Z","updated_at":"2026-05-03T07:16:02.318Z","avatar_url":"https://github.com/davidrsch.png","language":"R","funding_links":[],"categories":[],"sub_categories":[],"readme":"# kerasnip\n\n\u003c!-- badges: start --\u003e\n\n[![R-CMD-check](https://github.com/davidrsch/kerasnip/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/davidrsch/kerasnip/actions/workflows/R-CMD-check.yaml)\n[![Codecov test\ncoverage](https://codecov.io/gh/davidrsch/kerasnip/graph/badge.svg)](https://app.codecov.io/gh/davidrsch/kerasnip)\n[![CRAN_Status_Badge](https://www.r-pkg.org/badges/version/kerasnip)](https://cran.r-project.org/package=kerasnip)\n[![Downloads](https://cranlogs.r-pkg.org/badges/last-month/kerasnip)](https://cran.r-project.org/package=kerasnip)\n[![Downloads](https://cranlogs.r-pkg.org/badges/grand-total/kerasnip)](https://cran.r-project.org/package=kerasnip)\n\n\u003c!-- badges: end --\u003e\n\nThe goal of `kerasnip` is to provide a seamless bridge between the `keras` and `tidymodels` frameworks. It allows for the dynamic creation of `parsnip` model specifications for Keras models, making them fully compatible with `tidymodels` workflows.\n\n## Installation\n\nYou can install the development version of `kerasnip` from GitHub with:\n\n```r\n# install.packages(\"pak\")\npak::pak(\"davidrsch/kerasnip\")\n```\n\n## Example\n\n### Example 1: Building a Sequential MLP\n\nThis example shows the core workflow for building a simple, linear stack of layers using `create_keras_sequential_spec()`.\n\n```r\nlibrary(kerasnip)\nlibrary(tidymodels)\nlibrary(keras3)\n\n# 1. Define Keras layer blocks\n# The first block initializes the model.\ninput_block \u003c- function(model, input_shape) {\n  keras_model_sequential(input_shape = input_shape)\n}\n# Subsequent blocks add layers.\ndense_block \u003c- function(model, units = 32) {\n  model |\u003e layer_dense(units = units, activation = \"relu\")\n}\n# The final block creates the output layer.\noutput_block \u003c- function(model) {\n  model |\u003e\n    layer_dense(units = 1)\n}\n\n# 2. Create a spec from the layer blocks\n# This creates a new model function, `basic_mlp()`, in your environment.\ncreate_keras_sequential_spec(\n  model_name = \"basic_mlp\",\n  layer_blocks = list(\n    input = input_block,\n    dense = dense_block,\n    output = output_block\n  ),\n  mode = \"regression\"\n)\n\n# 3. Use the generated spec to define a model.\n# We can set the number of dense layers (`num_dense`) and their parameters (`dense_units`).\nspec \u003c- basic_mlp(\n  num_dense = 2,\n  dense_units = 64,\n  fit_epochs = 10,\n  learn_rate = 0.01\n) |\u003e\n  set_engine(\"keras\")\n\n# 4. Fit the model within a tidymodels workflow\nrec \u003c- recipe(mpg ~ ., data = mtcars) |\u003e\n  step_normalize(all_numeric_predictors())\n\nwf \u003c- workflow(rec, spec)\n\nset.seed(123)\nfit_obj \u003c- fit(wf, data = mtcars)\n\n# 5. Make predictions\npredict(fit_obj, new_data = mtcars[1:5, ])\n#\u003e # A tibble: 5 × 1\n#\u003e   .pred\n#\u003e   \u003cdbl\u003e\n#\u003e 1  21.3\n#\u003e 2  21.3\n#\u003e 3  22.8\n#\u003e 4  21.4\n#\u003e 5  18.7\n```\n\n### Example 2: Building a Functional \"Fork-Join\" Model\n\nFor complex, non-linear architectures, use `create_keras_functional_spec()`. This example builds a model where the input is forked into two paths, which are then concatenated.\n\n```r\nlibrary(kerasnip)\nlibrary(tidymodels)\nlibrary(keras3)\n\n# 1. Define blocks. For the functional API, blocks are nodes in a graph.\ninput_block \u003c- function(input_shape) layer_input(shape = input_shape)\npath_block \u003c- function(tensor, units = 16) tensor |\u003e layer_dense(units = units)\nconcat_block \u003c- function(input_a, input_b) layer_concatenate(list(input_a, input_b))\noutput_block \u003c- function(tensor) layer_dense(tensor, units = 1)\n\n# 2. Create the spec. The graph is defined by block names and their arguments.\ncreate_keras_functional_spec(\n  model_name = \"forked_mlp\",\n  layer_blocks = list(\n    main_input = input_block,\n    path_a = inp_spec(path_block, \"main_input\"),\n    path_b = inp_spec(path_block, \"main_input\"),\n    concatenated = inp_spec(concat_block, c(input_a = \"path_a\", input_b = \"path_b\")),\n    output = inp_spec(output_block, \"concatenated\")\n  ),\n  mode = \"regression\"\n)\n\n# 3. Use the new spec. Arguments are prefixed with their block name.\nspec \u003c- forked_mlp(path_a_units = 16, path_b_units = 8, fit_epochs = 10) |\u003e\n  set_engine(\"keras\")\n\n# Fit and predict as usual\nset.seed(123)\nfit(spec, mpg ~ ., data = mtcars) |\u003e\n  predict(new_data = mtcars[1:5, ])\n#\u003e # A tibble: 5 × 1\n#\u003e   .pred\n#\u003e   \u003cdbl\u003e\n#\u003e 1  19.4\n#\u003e 2  19.5\n#\u003e 3  21.9\n#\u003e 4  18.6\n#\u003e 5  17.9\n```\n\n### Example 3: Tuning a Sequential MLP Architecture\n\nThis example demonstrates how to tune the number of dense layers and the rate of a final dropout layer, showcasing how to tune both architecture and block hyperparameters simultaneously.\n\n```r\nlibrary(kerasnip)\nlibrary(tidymodels)\nlibrary(keras3)\n\n# 1. Define Keras layer blocks for a tunable MLP\ninput_block \u003c- function(model, input_shape) {\n  keras_model_sequential(input_shape = input_shape)\n}\ndense_block \u003c- function(model, units = 32) {\n  model |\u003e layer_dense(units = units, activation = \"relu\")\n}\ndropout_block \u003c- function(model, rate = 0.2) {\n  model |\u003e layer_dropout(rate = rate)\n}\noutput_block \u003c- function(model) {\n  model |\u003e layer_dense(units = 1)\n}\n\n# 2. Create a spec from the layer blocks\ncreate_keras_sequential_spec(\n  model_name = \"tunable_mlp\",\n  layer_blocks = list(\n    input = input_block,\n    dense = dense_block,\n    dropout = dropout_block,\n    output = output_block\n  ),\n  mode = \"regression\"\n)\n\n# 3. Define a tunable model specification\ntune_spec \u003c- tunable_mlp(\n  num_dense = tune(),\n  dense_units = tune(),\n  num_dropout = 1,\n  dropout_rate = tune(),\n  fit_epochs = 10\n) |\u003e\n  set_engine(\"keras\")\n\n# 4. Set up and run a tuning workflow\nrec \u003c- recipe(mpg ~ ., data = mtcars) |\u003e\n  step_normalize(all_numeric_predictors())\n\nwf_tune \u003c- workflow(rec, tune_spec)\n\n# Define the tuning grid.\nparams \u003c- extract_parameter_set_dials(wf_tune) |\u003e\n  update(\n    num_dense = dials::num_terms(c(1, 3)),\n    dense_units = dials::hidden_units(c(8, 64)),\n    dropout_rate = dials::dropout(c(0.1, 0.5))\n  )\ngrid \u003c- grid_regular(params, levels = 2)\n\n# 5. Run the tuning\nset.seed(456)\nfolds \u003c- vfold_cv(mtcars, v = 3)\n\ntune_res \u003c- tune_grid(\n  wf_tune,\n  resamples = folds,\n  grid = grid,\n  control = control_grid(verbose = FALSE)\n)\n\n# 6. Show the best architecture\nshow_best(tune_res, metric = \"rmse\")\n#\u003e # A tibble: 5 × 7\n#\u003e   num_dense dense_units dropout_rate .metric .estimator .mean .config\n#\u003e       \u003cint\u003e       \u003cint\u003e        \u003cdbl\u003e \u003cchr\u003e   \u003cchr\u003e      \u003cdbl\u003e \u003cchr\u003e\n#\u003e 1         1          64          0.1 rmse    standard    2.92 Preprocessor1_Model02\n#\u003e 2         1          64          0.5 rmse    standard    3.02 Preprocessor1_Model08\n#\u003e 3         3          64          0.1 rmse    standard    3.15 Preprocessor1_Model04\n#\u003e 4         1           8          0.1 rmse    standard    3.20 Preprocessor1_Model01\n#\u003e 5         3           8          0.1 rmse    standard    3.22 Preprocessor1_Model03\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdavidrsch%2Fkerasnip","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdavidrsch%2Fkerasnip","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdavidrsch%2Fkerasnip/lists"}