Open Gfuentesmo99 opened 3 years ago
Can you file a reproducible example? Without your dataset there's no way to reproduce the error.
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)
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)
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
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)
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]
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)
tokenTR$index_word[1:3] seqTR <- texts_to_sequences(tokenTR, train[,2]) seqTR seqTS <- texts_to_sequences(tokenTS, test[,2]) seqTS class(seqTR)##list
z <- NULL for (i in 1:length(seqTR)) {z[i] <- print(length(seqTR[[i]]))} summary(z) hist(z)
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 <- 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)
model %>% compile(optimizer = "adam", loss = "categorial_crossentropy", metrics = list("accuracy"))
class(train_x) class(train_y) dim(train_y) is.array(train_x)
mymodel <- model %>% fit(train_x,train_y, epochs = 10, batch_size = 16, validation_split = 0.2) plot(mymodel)
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.
Preparacion de los datos.
Cargamos los datasets.
Lo primero de todo sera cargar los datasets descargados.
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".
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.
Cogemos un 85% de la muestra para entrenar y un 15% para probar.
Codificamos las frases a enteros.
Vamos a codificar las frases a enteros y a rellenarlas hasta la maxima longitud de frase que tengamos.
Entrenamiento de la red neuronal
Definimos el modelo:
Creamos el checkpoint y entrenamos el modelo:
And the functions I created in a separate file are these ones: