Trying to build a neural network

I am trying to build a neural network in RStudio using the keras and tensorflow packages. I am getting the error "You must compile your model before training/testing. Use model.compile(optimizer, loss)." which tells me there is something wrong in my compile function but after lots of googling, I cant seem to figure it out. I am still relatively new to RStudio

# Upload .csv
insectdata<- read.csv(file="C:\\Users\\Desktop\\Tea Insectv3.csv")

#data partition
set.seed(1234)
seed <- sample(2, nrow(insectdata), replace = T)
insect_train <- insectdata$Insect_Pest_Var[seed==1]
insect_test <- insectdata$Insect_Pest_Var[seed==2]
train_target <- insectdata$Insect_Pest_Var[seed==1]
test_tartget <- insectdata$Insect_Pest_Var[seed==2]

#one hot enconding
trainlabels <-to_categorical(train_target)
testlabels <-to_categorical(test_tartget)
print(testlabels)

#model
model <- keras_model_sequential() %>% 
model %>% 
        layer_dense(units=8, activation ='relu', input_shape = c(399)) %>%

#compile
model %>%
        compile(loss = 'categorical_crossentropy', 
                optimizer = 'adam', 
                metrics = 'accuracy') %>% 


#fit model
history <- model %>%
            fit(insect_train, 
                trainlabels, 
                epoch = 200, 
                batchsize = 32, 
                validation_split = 0.2)

plot(model)

I followed some code I found online and modified it to fit my needs. What is the correct format?

I ended up reworking my pipeline, thank you for the suggestion. But now i am getting this error error in py_call_impl(callable, dots$args, dots$keywords) : valueerror: in user code:

After tons of googling, I really cant figure out what the cause is. It seems to be an issue with calling python to the RStudio environment. I downloaded anaconda3 and created a tensorflow/keras/rstudio enviornment. other tensorflow/keras functions work, but this does not seem to work.

does anyone have any other pointers?

I solved this by removing the set.seed data and just running my train data because it was already numerical. i also removed the pipeline characters and used proper format.

This topic was automatically closed 7 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.