I am running the code below for Neural Network in R. I have already installed TensorFlow which is working fine. I am facing error “Error in py_call_impl(callable, call_args$unnamed, call_args$named) :
KeyError: 0” while running the response(). I think it is because of the inputs shape of response. The inputs are LogVol and Network. Both have the same shape i.e. TensorShape([None, 1]). Yet I am facing the above Error. Can someone PLEASE help me to resolve this issue?
# My Code:
# Required Package
library(tidyverse)
#install.packages("keras")
library(keras)
library(reticulate)
library(ggplot2)
library(cluster)
#install.packages("ClusterR")
library(ClusterR)
library(readxl)
library(magrittr)
library(dplyr)
# Data
# Sample Data
[enter image description here](https://i.sstatic.net/cxkqQRgY.png)
dat=readr::read_csv("freMTPL2freq.csv") # Data is an CSV file
str(dat)
for(i in seq_along(dat)){ if(is.character(dat[[i]])){dat[[i]] <- factor(dat[[i]])}}
dat$ClaimNb <- pmin(dat$ClaimNb, 4)
dat$Exposure <- pmin(dat$Exposure, 1)
## NN
#To do: crate the following matrices
# - Design Matrix for continuous variables
# - Matrices for the categorical variables
# - Matrix for offset
MM_scaling <- function(x){ 2*(x-min(x))/(max(x)-min(x)) - 1}
dat_NN=data.frame(ClaimNb=dat$ClaimNb)
dat_NN$DriveAge <- MM_scaling(dat$DrivAge)
dat_NN$BonusMalus <- MM_scaling(dat$BonusMalus)
dat_NN$Area <- MM_scaling(as.integer(dat$Area))
dat_NN$VehPower <- MM_scaling(as.numeric(dat$VehPower))
dat_NN$VehAge <- MM_scaling(as.numeric(dat$VehAge))
dat_NN$Density <- MM_scaling(dat$Density)
dat_NN$VehGas <- MM_scaling(as.integer(dat$VehGas))
str(dat_NN)
summary(dat_NN)
#Test Split
learn_idx <- sample(1:nrow(dat), round(0.9*nrow(dat)), replace = FALSE)
learn <- dat[learn_idx, ]
test <- dat[-learn_idx, ]
n_l <- nrow(learn)
n_t <- nrow(test)
learn_NN=dat_NN[learn_idx,]
test_NN=dat_NN[-learn_idx,]
Design_learn <- as.matrix(learn_NN[ , -1])
Design_test <- as.matrix(test_NN[ ,-1])
Br_learn <- as.matrix(as.integer(learn$VehBrand)) - 1
Br_test <- as.matrix(as.integer(test$VehBrand)) - 1
Re_learn <- as.matrix(as.integer(learn$Region)) - 1
Re_test <- as.matrix(as.integer(test$Region)) - 1
Vol_learn <- as.matrix(learn$Exposure)
Vol_test <- as.matrix(test$Exposure)
LogVol_learn <- log(Vol_learn)
LogVol_test <- log(Vol_test)
Y_learn <- as.matrix(learn_NN$ClaimNb)
Y_test <- as.matrix(test_NN$ClaimNb)
# Step 2: Neural Network
# Step 2.1: Hyperparameters initialization
# the dimension of the 1st hidden layer
q1 <- 25
# the dimension of the 2nd hidden layer
q2 <- 20
# the dimension of the 3rd hidden layer
q3 <- 15
# the dimension of the embedded layer for "VehBrand" and "Region"
qEmb <- 2
# number of epochs to train the model
epochs <- 1000
# number of samples per gradient update
batchsize <- 10000
# Step 2.2: Input layer
# Input layer for continuous features
DesignShape <- ncol(learn_NN) - 1 # the number of the continuous predictors
Design <- layer_input(shape = DesignShape, dtype = 'float32', name = 'Design')
# Input layer for categorical features
Br_ndistinct <- length(unique(learn$VehBrand)) # number of vehicle brands = 11
Re_ndistinct <- length(unique(learn$Region)) # number of regions = 22
VehBrand <- layer_input(shape = 1, dtype = 'int32', name = 'VehBrand')
Region <- layer_input(shape = 1, dtype = 'int32', name = 'Region')
# Input layer for Exposure (as the offset)
#LogVol <- layer_input(shape = 1, dtype = 'float32', name = 'LogVol')
LogVol <- layer_input(shape = 1, dtype = 'float32', name = 'LogVol_Input')
#LogVol <- layer_input(shape = 2, dtype = 'float32', name = 'LogVol_Input')
Vol <- layer_input(shape = 1, dtype = 'float32', name = 'Vol')
# Step 2.3: Embedding layer
# Create embedding layer for categorical predictors (dimension: qEmb = 2)
# Vehicle Brand: 11 -> 2
# Region: 22 -> 2
BrEmb = VehBrand %>%
layer_embedding(input_dim = Br_ndistinct, output_dim = qEmb, input_length = 1, name = 'BrEmb') %>%
layer_flatten(name = 'Br_flat')
ReEmb = Region %>%
layer_embedding(input_dim = Re_ndistinct, output_dim = qEmb, input_length = 1, name = 'ReEmb') %>%
layer_flatten(name = 'Re_flat')
# Step 2.4: Main architecture and Output layer
# Main architecture with 3 hidden layers
Network <- list(Design, BrEmb, ReEmb) %>% layer_concatenate(name = 'concate') %>%
# 1st hidden layer
layer_dense(units = q1, activation = 'tanh', name = 'hidden1') %>%
# 2nd hidden layer
layer_dense(units = q2, activation = 'tanh', name = 'hidden2') %>%
# 3rd hidden layer
layer_dense(units = q3, activation = 'tanh', name = 'hidden3') %>%
# provide one neuron in the output layer
layer_dense(units = 1, activation = 'linear', name = 'Network')
# Output layer to combine the main architecture and the offset layer (Exposure)
Response = list(Network, LogVol) %>%
# add the exposure and the last neuron
layer_add(name = 'Add') %>%
# give the response
layer_dense(units = 1,
activation = 'exponential',
name = 'Response',
trainable = FALSE,
weights = list(array(1, dim = c(1,1)), array(0, dim = c(1))))
# Step 2.5: Model configuration and fitting
# Model assembly
model <- keras_model(inputs = c(Design, VehBrand, Region, LogVol_Input), outputs = c(Response))
summary(model)
# Model configuration
model %>% compile(
loss = 'poisson', # set poisson deviance loss function as the objective loss function
optimizer = 'nadam'
)
# Model fitting by running gradient descent method to minimize the objective loss function
{
t1 <- proc.time()
fit <- model %>% fit(
list(Design_learn, Br_learn, Re_learn, LogVol_learn), # all predictors
Y_learn, # response
verbose = 1, # verbose = 0 silences the progress bar for the process
# verbose = 1 shows the fitting process, incl. learning loss and validation loss, epoch by epoch
epochs = epochs, # epochs = 1,000
batch_size = batchsize, # batchsize = 10,000
validation_split = 0.2 # 20% as validation set
)
print(proc.time()-t1)
}
# Predicted value of the claim numbers
learn$nn0 <- as.vector(model %>% predict(list(Design_learn, Br_learn, Re_learn, LogVol_learn)))
test$nn0 <- as.vector(model %>% predict(list(Design_test, Br_test, Re_test, LogVol_test)))
dev.loss(y = learn$ClaimNb, mu = learn$nn0, dpois0)
# Code where Error is generating:
Response = list(Network, LogVol) %>%
layer_add(name = 'Add') %>%
layer_dense(units = 1,
activation = 'exponential',
name = 'Response',
trainable = FALSE,
weights = list(array(1, dim = c(1,1)), array(0, dim = c(1))))
# Error while running Response():
Error in py_call_impl(callable, call_args$unnamed, call_args$named) :
KeyError: 0
Run `reticulate::py_last_error()` for details.
# Error Details:
Error in py_call_impl(callable, call_args$unnamed, call_args$named) :
KeyError: 0
Run `reticulate::py_last_error()` for details.
> reticulate::py_last_error()
── Python Exception Message ───────────────────────────────────────────────────────────────────────────────────────────────
Traceback (most recent call last):
File "C:UserskushaOneDriveDOCUME~1VIRTUA~1R-TENS~1Libsite-packageskerassrclayersmergingadd.py", line 92, in add
return Add(**kwargs)(inputs)
^^^^^^^^^^^^^^^^^^^^^
File "C:UserskushaOneDriveDOCUME~1VIRTUA~1R-TENS~1Libsite-packageskerassrcutilstraceback_utils.py", line 70, in error_handler
raise e.with_traceback(filtered_tb) from None
File "C:UserskushaOneDriveDOCUME~1VIRTUA~1R-TENS~1Libsite-packageskerassrclayersmergingbase_merge.py", line 84, in build
if not isinstance(input_shape[0], tuple):
~~~~~~~~~~~^^^
KeyError: 0
── R Traceback ────────────────────────────────────────────────────────────────────────────────────────────────────────────
▆
1. ├─list(Network, LogVol) %>% layer_add(name = "Add") %>% ...
2. ├─keras::layer_dense(...)
3. │ └─keras::create_layer(...)
4. └─keras::layer_add(., name = "Add")
5. ├─base::do.call(keras$layers$add, c(list(inputs), dots$named))
6. └─reticulate (local) `<python.builtin.function>`(`<named list>`, name = "Add")
7. └─reticulate:::py_call_impl(callable, call_args$unnamed, call_args$named)
See `reticulate::py_last_error()$r_trace$full_call` for more details.
> reticulate::py_last_error()$r_trace$full_call
[[1]]
list(Network, LogVol) %>% layer_add(name = "Add") %>% layer_dense(units = 1,
activation = "exponential", name = "Response")
[[2]]
layer_dense(., units = 1, activation = "exponential", name = "Response")
[[3]]
create_layer(keras$layers$Dense, object, list(units = as.integer(units),
activation = activation, use_bias = use_bias, kernel_initializer = kernel_initializer,
bias_initializer = bias_initializer, kernel_regularizer = kernel_regularizer,
bias_regularizer = bias_regularizer, activity_regularizer = activity_regularizer,
kernel_constraint = kernel_constraint, bias_constraint = bias_constraint,
input_shape = normalize_shape(input_shape), batch_input_shape = normalize_shape(batch_input_shape),
batch_size = as_nullable_integer(batch_size), dtype = dtype,
name = name, trainable = trainable, weights = weights))
[[4]]
layer_add(., name = "Add")
[[5]]
do.call(keras$layers$add, c(list(inputs), dots$named))
[[6]]
(function (inputs, ...)
{
cl <- sys.call()
cl[[1L]] <- list2
call_args <- split_named_unnamed(eval(cl, parent.frame()))
result <- py_call_impl(callable, call_args$unnamed, call_args$named)
if (py_get_convert(callable))
result <- py_to_r(result)
if (is.null(result))
invisible(result)
else result
})(list(<environment>, <environment>), name = "Add")
[[7]]
py_call_impl(callable, call_args$unnamed, call_args$named)
Facing Error “Error in py_call_impl(callable, call_args$unnamed, call_args$named) :
KeyError: 0” while running code for response()
Kushal Kataria is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.