KERAS选择层号/输入层单位

时间:2019-07-02 01:21:19

标签: r tensorflow keras neural-network

我有以下体重模型。我输入的形状是64,64,3。我使用连续的,我有6个类别(每个类别3000张图片)。准确率超过90%,但是当我尝试预测新图像时,效果不佳。

如何选择层数? 是否有任何规则来确定单位数量,特别是对于输入层? 形状和数量之间有关系吗?

conv_base <- application_vgg16(
  weights = "imagenet",
  include_top = FALSE,
  input_shape = c(64, 64, 3)
)

model <- keras_model_sequential() %>% 
      conv_base %>% 
      layer_flatten() %>% 
      layer_dense(units = 256, activation = "relu") %>%
      layer_batch_normaization() %>%
      layer_dense(units = 3, activation = "softmax")

train_datagen = image_data_generator(
  rescale = 1/255,
  rotation_range = 40,
  width_shift_range = 0.2,
  height_shift_range = 0.2,
  shear_range = 0.2,
  zoom_range = 0.2,
  horizontal_flip = TRUE,
  fill_mode = "nearest"
)

test_datagen <- image_data_generator(rescale = 1/255)  

train_generator <- flow_images_from_directory(
  train_dir,                   
  train_datagen,              
  target_size = c(64, 64),  
  batch_size = 20,
  class_mode = "categorical"    
)

validation_generator <- flow_images_from_directory(
  validation_dir,
  test_datagen,
  target_size = c(64, 64),
  batch_size = 20,
  class_mode = "categorical"
)
model %>% compile(
  loss = 'categorical_crossentropy',
  optimizer = optimizer_rmsprop(lr = 2e-5),
  metrics = c("accuracy")
)

##Plot
history <- model %>% fit_generator(
  train_generator,
  steps_per_epoch = 100,
  epochs = 5,
  validation_data = validation_generator,
  validation_steps = 50
)

0 个答案:

没有答案