rstudio / keras3

R Interface to Keras
https://keras3.posit.co/
Other
833 stars 282 forks source link

Error in py_call_impl(callable, dots$args, dots$keywords) : ValueError: in user code: #1153

Open Gfuentesmo99 opened 3 years ago

Gfuentesmo99 commented 3 years ago

Hello guys, I'm getting this error all the time while trying to execute my code. Im new to R and to Keras so I was trying to do my own code with other people code, but somewhere I have written something in a bad way and when I try to fit the model, it prints the error. Btw, I was copying the code from a python example of a machine translator, and it uses the function that I called encode_output with array reshape but I was not able to implement so that may be the error. The whole error is this one: Error in py_call_impl(callable, dots$args, dots$keywords) : ValueError: in user code: /Users/Administrador/Library/r-miniconda/envs/r-reticulate/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py:805 train_function * return step_function(self, iterator) /Users/Administrador/Library/r-miniconda/envs/r-reticulate/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py:795 step_function ** outputs = model.distribute_strategy.run(run_step, args=(data,)) /Users/Administrador/Library/r-miniconda/envs/r-reticulate/lib/python3.6/site-packages/tensorflow/python/distribute/distribute_lib.py:1259 run return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs) /Users/Administrador/Library/r-miniconda/envs/r-reticulate/lib/python3.6/site-packages/tensorflow/python/distribute/distribute_lib.py:2730 call_for_each_replica return self._call_for_each_replica(fn, args, kwargs) /Users/Administrador/Library/r-miniconda/envs/r-reticulate/lib/python3.6/si

And here is my code: `#Traductor Automatico

Cargamos las funciones y librerias necesarias.

source("../Scripts/translator_functions.R", echo = TRUE, max.deparse.length=1e3)
library(stringr)
library(utf8)
library(keras)

Preparacion de los datos.

Cargamos los datasets.

Lo primero de todo sera cargar los datasets descargados.

spFile <- file("../dataset/es-en/europarl-v7.es-en.es", encoding = "UTF-8")
enFile <- file("../dataset/es-en/europarl-v7.es-en.en", encoding="UTF-8")
spLines <- readLines(spFile)
enLines <- readLines(enFile)
close(spFile)
close(enFile)

Lmpieza de datasets.

En este apartado vamos a "limpiar" los datos para que puedan ser usados por la red neuronal. Y a la vez juntarlos con la función "Pair".

pair_esen<- Pair(clean_lines(spLines), clean_lines(enLines))

Reducción del tamaño del modelo a las primeras 100000 frases.

Hacemos esto para reducir la complejidad del modelo, aunque su exactitud y vocabulario sera menor pero de la otra forma no tendriamos los requisitos computacionales necesarios.

numSentences <- 20000
randomSentences <- sample(1:length(enLines),numSentences)
dataset <- pair_esen[randomSentences,]
dataset <- matrix(unlist(dataset), ncol=2)

Cogemos un 85% de la muestra para entrenar y un 15% para probar.

randomSentencesTest <- sample(1:numSentences, numSentences*0.85)
train_dataset <- dataset[randomSentencesTest,]
test_dataset <- dataset[-randomSentencesTest,]

Codificamos las frases a enteros.

Vamos a codificar las frases a enteros y a rellenarlas hasta la maxima longitud de frase que tengamos.

en_tokenizer <- text_tokenizer()  %>% fit_text_tokenizer(dataset[,2])
es_tokenizer <- text_tokenizer() %>%fit_text_tokenizer(dataset[,1])
es_vocab_size <- length(es_tokenizer$word_index)+1
en_vocab_size <- length(en_tokenizer$word_index)+1

es_maxlen <- get_longest(dataset[,1])
en_maxlen <- get_longest(dataset[,2])

x_train <- encode_seq(es_tokenizer,es_maxlen,train_dataset[,1])
y_train <- encode_seq(en_tokenizer,en_maxlen,train_dataset[,2])

x_test <- encode_seq(es_tokenizer, es_maxlen, test_dataset[,1])
y_test <- encode_seq(en_tokenizer, en_maxlen, test_dataset[,2])
#y_test <- encode_output(y_test, en_vocab_size)

Entrenamiento de la red neuronal

Definimos el modelo:

model <- keras_model_sequential()
model %>% layer_embedding(input_dim = es_vocab_size,output_dim = 512, 
                            input_length = es_maxlen,mask_zero = TRUE )%>%
          layer_lstm(units=512)%>%layer_repeat_vector(en_maxlen)%>%
          layer_lstm(units = 512, return_sequences = TRUE)%>%
          time_distributed(layer_dense(units=en_vocab_size, activation="softmax"))

Creamos el checkpoint y entrenamos el modelo:

checkpoint <- callback_model_checkpoint("traductor",verbose=1, save_best_only = TRUE, mode = "min")
model %>% compile(
  loss = "categorical_crossentropy",
  optimizer = "adam"
)
summary(model)
model %>% fit(x_train,y_train,batch_size <- 64, epochs<-30, callbacks=checkpoint, verbose=2, validation_data = list(x_test,y_test) )

And the functions I created in a separate file are these ones:

clean_lines <- function(lines){
  lines <- utf8_normalize(lines)
  lines <- remove_nonAlphanumericChar(lines)
  trimws(lines)
  lines <- add_space_before_punc(lines)
  lines<-tolower(lines)
  line <- split_in_Line(lines)

}

split_in_Line <- function(lines){
  str_split(lines,"\n")
}
add_space_before_punc <- function(lines){
  str_replace_all(lines,"([?.!])", "\\1")
}
remove_nonAlphanumericChar <- function(lines){
  str_replace_all(lines, "[[:punct:]]", " ")
}

encode_seq <- function(tokenizer, maxlength, lines){
  integerFromSeq <- tokenizer$texts_to_sequences(lines) 
  integerFromSeq <- pad_sequences(integerFromSeq, maxlen = maxlength, padding="post")
  return(integerFromSeq)
}
encoder_output <- function(sequence, vocabulary){
  auxlist <- list()
  for (i in sequence) {
    encoded <- to_categorical(i, num_classes=vocabulary)
    append(list, encoded)
  }
  arrOutput <- array(unlist(auxlist),dim =c(nrow(sequence),ncol(sequence),vocabulary))
  return(arrOutput)  
}

get_longest <- function(dataset){
  pos <-0
  maxlen <- 0
  iter<-1
  for (i in dataset) {
    if(nchar(i)>maxlen){
      pos <-iter
      maxlen <- nchar(i)
    }
    iter <- iter+1
  }
  return(maxlen)

}
dfalbel commented 3 years ago

Can you file a reproducible example? Without your dataset there's no way to reproduce the error.

shai14 commented 2 years ago

Hi, I am a newbie into deep learning and trying to solve a Multiclass text classification problem using Keras in R. I am getting error - Error in py_call_impl(callable, dots$args, dots$keywords) : ValueError: in user code: while fitting my model. I have checked my data types, the class(train_x), class(train_y) returns me "matrix" "array". the dim(train_x) and dim(train_y) are -

dim(train_x) [1] 169 12 dim(train_y) [1] 169 13 the code is given below, please help me understand the issue and what should I do to fix it, TIA. -

rm(list=ls())

library(keras) library(dplyr) library(ggplot2)

reading the data

textdata <- read.csv("text_data.csv", header= T) textdata$product_form <- as.factor(textdata$product_form) levels(textdata$product_form) textdata$product_form <- as.numeric(as.factor(textdata$product_form))-1 str(textdata)

sampling

set.seed(123) index <- sample(2,nrow(textdata), replace=T, prob = c(.8,.2)) train <- textdata[index==1, c(1,2,4)] test <- textdata[index==2, c(1,2,4)] train_y <- textdata[index==1, c(3)]# vector numeric test_y <- textdata[index==2, c(3)]

str(train_y) is.vector(train_y)# true dim(train_y)# Null length(train_y) #169 str(train) # dataframe

converting data into matrix

train<- as.matrix(train) dimnames(train)<- NULL test<- as.matrix(test) dimnames(test)<- NULL train_y<- as.matrix(train_y) test_y<- as.matrix(test_y) dimnames(train_y)<- NULL dimnames(test_y)<- NULL

is.matrix(train_y)#true dim(train_y) # 169 1 str(train_y)

now the target class is numerical we need to do the one hot encoding

train_y <- to_categorical(train_y, num_classes = 13) test_y <- to_categorical(test_y, num_classes = 13) class(train_y)# matrix array dim(train_y) #[1] 169 13 str(test_y) # num [1:39, 1:13]

tokenize train and test text part

train[,2] tokenTR <- text_tokenizer(num_words = 200) %>% fit_text_tokenizer(train[,2]) ## 2nd var is product_name tokenTS <- text_tokenizer(num_words = 200) %>% fit_text_tokenizer(test[,2]) class(tokenTR)

convert prod names to sequence of integer

tokenTR$index_word[1:3] seqTR <- texts_to_sequences(tokenTR, train[,2]) seqTR seqTS <- texts_to_sequences(tokenTS, test[,2]) seqTS class(seqTR)##list

spread is used to judge how much maxlen for sequence would be good

z <- NULL for (i in 1:length(seqTR)) {z[i] <- print(length(seqTR[[i]]))} summary(z) hist(z)

padding and truncating

train_x <- pad_sequences(seqTR, maxlen = 12, padding = 'post') dim(train_x) test_x <- pad_sequences(seqTS, maxlen = 12, padding = 'post') dim(test_x)

model Architecture # input length will be equal to padding sequence maxlen and

last dense layer units will be equal to the number of labels of target var.

model <- keras_model_sequential()

model %>% layer_embedding(input_dim =200, output_dim = 16, input_length = 12) %>% layer_flatten() %>% layer_dense(units = 16, activation = 'relu') %>% layer_dense(units = 13, activation = "softmax") summary(model)

compiling ### use adam optimiser when multiclass

model %>% compile(optimizer = "adam", loss = "categorial_crossentropy", metrics = list("accuracy"))

class(train_x) class(train_y) dim(train_y) is.array(train_x)

fitting the model

mymodel <- model %>% fit(train_x,train_y, epochs = 10, batch_size = 16, validation_split = 0.2) plot(mymodel)