1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
| # Initialize model input <- layer_input(shape = c(maxlen)) model <- input %>% # Creates dense embedding layer; outputs 3D tensor # with shape (batch_size, sequence_length, output_dim)
layer_embedding(max_features, 300, input_length=maxlen, trainable = TRUE)
conv1 = model %>% layer_conv_1d(filters=128, kernel_size =3, padding ='valid', activation='relu') %>% layer_global_max_pooling_1d()
conv2 = model %>% layer_conv_1d(filters=128, kernel_size =4, padding ='valid', activation='relu') %>% layer_global_max_pooling_1d()
conv3 = model %>% layer_conv_1d(filters=128, kernel_size =5, padding ='valid', activation='relu') %>% layer_global_max_pooling_1d()
merge.layer <- layer_concatenate(c(conv1, conv2, conv3))
output <- merge.layer %>% layer_dense(units = 1, activation = 'sigmoid')
model <- keras_model( inputs = input, outputs = output )
# Optimizer model %>% compile( loss = 'binary_crossentropy', optimizer = 'adam', metrics = c('accuracy') )
# Train model model %>% fit( x_train, y_train, batch_size = 100, epochs = 3, validation_split = 0.2 ) # Prediction & ConfusionMatrix m3_cnn_y_pred <- model %>% predict(x_test) m3_cnn_y_pred <- as.numeric(m3_cnn_y_pred > 0.5) m3_cnn_results <- model %>% evaluate(x_test, y_test) m3_cnn_cMatrix <- table(m3_cnn_y_pred,y_test)
|