Error in UseMethod("fit")

Hello!

I tried to create a neural network with keras and the glove embedding.

model <- keras_model_sequential() %>%
layer_embedding(input_dim = num_words, output_dim = embedding_dim,
input_length = max_length) %>%
layer_flatten() %>%
layer_dense(units = 32, activation = "relu") %>%
layer_dense(units = 1, activation = "sigmoid")

get_layer(model, index = 1) %>%
set_weights(list(embedding_matrix)) %>%
freeze_weights()

I am able to use the compile function, however, I get the following error message when I try to use the fit function:

Error in UseMethod("fit"): no applicable method for 'fit' applied to an object of class "c('keras.engine.sequential.Sequential', 'keras.engine.functional.Functional', 'keras.engine.training.Model', 'keras.engine.base_layer.Layer', 'tensorflow.python.module.module.Module', 'tensorflow.python.training.tracking.tracking.AutoTrackable', 'tensorflow.python.training.tracking.base.Trackable', 'keras.utils.version_utils.LayerVersionSelector', 'keras.utils.version_utils.ModelVersionSelector', 'python.builtin.object')"

Also, for the evaluation I get an error:

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

I am looking forward to your help.

Hi @student5,

Providing a reproducible example will greatly help. In your case, sharing a minimal dataset as well as the list of packages you use would greatly help.

I am using the following packages:

library(readxl)
library(readr)
library(tensorflow)
library(keras)
library(text2vec)

My data set are some tweets I extracted via the API and cleaned with the tm package in a previous step.

The code looks the following:

tokens = word_tokenizer(model_data$clean_text)
v = create_vocabulary(itoken(tokens))
v = prune_vocabulary(v, term_count_min = 5, doc_proportion_max = 0.5)
it = itoken(tokens)
vectorizer = vocab_vectorizer(v)
word_index = tokens$word_index

dtm = create_dtm(it, vectorizer)
tcm = create_tcm(it, vectorizer, skip_grams_window = 5)
glove_model = GloVe$new(rank = 50, x_max = 10)
wv_main = glove_model$fit_transform(tcm, n_iter = 5)

word_vectors <- wv_main + t(glove_model$components)
rwmd_model = RelaxedWordMoversDistance$new(dtm, word_vectors)
rwms = rwmd_model$sim2(dtm[1:10, ])
head(sort(rwms[1, ], decreasing = T))

training_id <- sample.int(nrow(model_data), size=nrow(model_data)*0.8)
training <- model_data[training_id,]
testing <- model_data[-training_id,]

embeddings_index <- new.env(hash = TRUE, parent = emptyenv())

embedding_dim <- 100
embedding_matrix <- array(0, c(num_words, embedding_dim))
for (word in names(word_index)) {
index <- word_index[[word]]
if (index < max_words) {
embedding_vector <- embeddings_index[[word]]
if (!is.null(embedding_vector))
# Words not found in the embedding index will be all zeros.
embedding_matrix[index+1,] <- embedding_vector
}
}

model <- keras_model_sequential() %>%
layer_embedding(input_dim = num_words, output_dim = embedding_dim,
input_length = max_length) %>%
layer_flatten() %>%
layer_dense(units = 32, activation = "relu") %>%
layer_dense(units = 1, activation = "sigmoid")

get_layer(model, index = 1) %>%
set_weights(list(embedding_matrix)) %>%
freeze_weights()

model %>% compile(
optimizer = 'adam',
loss = 'binary_crossentropy',
metrics = list('accuracy')
)

history <- model %>% fit(
training$clean_text,
training$hate,
epochs = 100,
batch_size = 32,
validation_split = 0.2,
verbose = 2
)

results <- model %>% evaluate(testing$clean_text, testing$hate, verbose = 0)
results

Everything works fine except for the last two steps.

Actually, it's really difficult to just look at the code (especially due to its length) and determine what's wrong. It would greatly help if you could also share your dataset. You could upload your dataset and share a link or post the data with the dput() function. If you want to see how dput() works, check this: https://ask.xiaolee.net/questions/2153421

This is the data set:

list(text = c("!!! RT mayasolovely: As a woman you shouldn't complain about cleaning up your house. and; as a man you should always take the trash out...",
"!!!!! RT mleew17: boy dats cold...tyga dwn bad for cuffin dat hoe in the 1st place!!",
"!!!!!!! RT UrKindOfBrand Dawg!!!! RT @80sbaby4life: You ever ■■■■ a ■■■■■ and she start to cry? You be confused as ■■■■",
"!!!!!!!!! RT C_G_Anderson: @viva_based she look like a tranny",
"!!!!!!!!!!!!! RT ShenikaRoberts: The ■■■■ you hear about me might be true or it might be faker than the ■■■■■ who told it to ya ",
"!!!!!!!!!!!!!!!!!!T_Madison_x: The ■■■■ just blows me..claim you so faithful and down for somebody but still ■■■■ing with hoes! :joy::joy::joy:",
"!!!!!!@_BrighterDays: I can not just sit up and HATE on another ■■■■■ .. I got too much ■■■■ going on!",
"!!!!“selfiequeenbri: cause I'm tired of you big ■■■■■es coming for us skinny girls!!”",
"and; you might not get ya ■■■■■ back and; thats that", "@rhythmixx
:hobbies include: fighting Mariam ■■■■■",
"Keeks is a ■■■■■ she curves everyone lol I walked into a conversation like this. Smh",
"Murda Gang ■■■■■ its Gang Land", "So hoes that smoke are losers ? yea ... go on IG",
"bad ■■■■■es is the only thing that i like", "■■■■■ get up off me",
"■■■■■ ■■■■■ miss me with it", "■■■■■ plz whatever", "■■■■■ who do you love",
"■■■■■es get cut off everyday B", "black bottle and; a bad ■■■■■"
), hate = c(0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1))

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.