generate_stan_code_cat: Internal function to generate Stan Code for Neural Networks...

View source: R/generate_stan_code.R

generate_stan_code_catR Documentation

Internal function to generate Stan Code for Neural Networks with Categorical Response

Description

This function generates Stan code for modeling a categorical response using neural networks with multiple layers. The generated code supports customizable activation functions for each layer and softmax-based prediction for the categorical output.

Usage

generate_stan_code_cat(num_layers, nodes)

Arguments

num_layers

Integer. Number of layers in the neural network.

nodes

Integer vector. Number of nodes in each layer. The length of this vector must match num_layers, and all values must be positive.

Details

The Stan code includes the following components:

  • Data Block: Defines inputs, response variable, layer configurations, and activation functions.

  • Parameters Block: Declares weights and biases for all layers and the output layer.

  • Transformed Parameters Block: Computes intermediate outputs (z and a) for each layer and calculates the final predictions (y_hat) using the softmax function.

  • Model Block: Specifies priors for parameters and models the categorical response using categorical_logit.

Supported activation functions for the hidden layers:

  • 1: Tanh

  • 2: Sigmoid

  • 3: Softplus

  • 4: ReLU

  • 5: linear

The categorical response (y) is assumed to take integer values from 1 to K, where K is the total number of categories.

Value

A string containing the Stan code for the specified neural network architecture and categorical response model.

See Also

generate_stan_code_bin(), generate_stan_code_cont()

Examples

# Generate Stan code for a neural network with 3 layers
num_layers <- 3
nodes <- c(10, 8, 6) # 10 nodes in the first layer, 8 in the second, 6 in the third
stan_code <- generate_stan_code_cat(num_layers, nodes)
cat(stan_code)


bnns documentation built on April 3, 2025, 6:12 p.m.