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)