# Keras

> R keras package for deep learning. Use for neural networks with TensorFlow backend.

- Skill: `leolin990405/keras` (Agent Skill)
- Install (CLI): `npx get-skill add leolin990405/keras`
- Raw SKILL.md: https://api.skillmd.com/api/skills/leolin990405/keras/raw
- Safety review: pending
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Coding & Dev Tools
- Author: LeoLin990405 (https://skillmd.com/u/leolin990405)
- Updated: 2026-11-14
- Page: https://skillmd.com/skills/leolin990405/keras

---


# keras Package

Deep learning with TensorFlow backend.

## Setup

```r
library(keras)
# install_keras()  # First time only
```

## Sequential Model

```r
model <- keras_model_sequential() %>%
  layer_dense(units = 64, activation = "relu", input_shape = c(10)) %>%
  layer_dropout(rate = 0.3) %>%
  layer_dense(units = 32, activation = "relu") %>%
  layer_dropout(rate = 0.3) %>%
  layer_dense(units = 1, activation = "sigmoid")

model %>% compile(
  optimizer = "adam",
  loss = "binary_crossentropy",
  metrics = c("accuracy")
)

summary(model)
```

## Training

```r
history <- model %>% fit(
  x_train, y_train,
  epochs = 50,
  batch_size = 32,
  validation_split = 0.2,
  callbacks = list(
    callback_early_stopping(patience = 5),
    callback_model_checkpoint("best_model.h5", save_best_only = TRUE)
  )
)

plot(history)
```

## Evaluation

```r
model %>% evaluate(x_test, y_test)
predictions <- model %>% predict(x_test)
```

## CNN for Images

```r
model <- keras_model_sequential() %>%
  layer_conv_2d(filters = 32, kernel_size = c(3, 3), activation = "relu",
    input_shape = c(28, 28, 1)) %>%
  layer_max_pooling_2d(pool_size = c(2, 2)) %>%
  layer_conv_2d(filters = 64, kernel_size = c(3, 3), activation = "relu") %>%
  layer_max_pooling_2d(pool_size = c(2, 2)) %>%
  layer_flatten() %>%
  layer_dense(units = 64, activation = "relu") %>%
  layer_dense(units = 10, activation = "softmax")
```

## LSTM for Sequences

```r
model <- keras_model_sequential() %>%
  layer_lstm(units = 50, return_sequences = TRUE, input_shape = c(timesteps, features)) %>%
  layer_lstm(units = 50) %>%
  layer_dense(units = 1)
```

## Save/Load

```r
save_model_hdf5(model, "model.h5")
model <- load_model_hdf5("model.h5")
```

