<jupyter_start><jupyter_text>4. We then compile and fit our model.<jupyter_code># compiling the model
autoencoder %>% compile(optimizer='adadelta', loss='binary_crossentropy')

# training the model
autoencoder %>% fit(x_train, x_train,
                epochs=50,
                batch_size=256,
                shuffle=TRUE,
                validation_data=list(x_test, x_test))
# predicting the output of the model on test data
predicted <- autoencoder %>% predict(x_test)
options(repr.plot.width=10, repr.plot.height=1)
grid = array_reshape(x_test[20,],dim = c(28,28))
for(i in seq(1,5)){
    grid = abind(grid,array_reshape(x_test[i,],dim = c(28,28)),along = 2)
}

grid.raster(grid,interpolate=FALSE)
grid1 = array_reshape(predicted[20,],dim = c(28,28))
for(i in seq(1,5)){
    grid1 = abind(grid1,array_reshape(predicted[i,],dim = c(28,28)),along = 2)
}

grid.raster(grid1, interpolate=FALSE)<jupyter_output><empty_output><jupyter_text>There is  more...  Convolutional autoencoder<jupyter_code>x_train = x_train/ 255
x_test = x_test / 255

x_train = array_reshape(x_train, c(nrow(x_train), 28,28,1))
x_test = array_reshape(x_test, c(nrow(x_test), 28,28,1))
input_img = layer_input(shape=c(28, 28, 1)) 
x = input_img %>% layer_conv_2d(32, c(3, 3), activation='relu', padding='same')
x = x %>% layer_max_pooling_2d(c(2, 2), padding='same')
x = x %>% layer_conv_2d(18, c(3, 3), activation='relu', padding='same')
x = x %>%layer_max_pooling_2d(c(2, 2), padding='same')
x = x %>% layer_conv_2d(8, c(3, 3), activation='relu', padding='same')
encoded = x %>% layer_max_pooling_2d(c(2, 2), padding='same')

x = encoded %>% layer_conv_2d(8, c(3, 3), activation='relu', padding='same')
x = x %>% layer_upsampling_2d(c(2, 2))
x = x %>% layer_conv_2d(8, c(3, 3), activation='relu', padding='same')
x = x %>% layer_upsampling_2d(c(2, 2))
x = x %>% layer_conv_2d(16, c(3, 3), activation='relu')
x = x %>% layer_upsampling_2d(c(2, 2))
decoded = x %>% layer_conv_2d(1, c(3, 3), activation='sigmoid', padding='same')

autoencoder = keras_model(input_img, decoded)
summary(autoencoder)
autoencoder %>% compile(optimizer='adadelta', loss='binary_crossentropy')
autoencoder %>% fit(x_train, x_train,
                epochs=20,
                batch_size=128,
                validation_data=list(x_test, x_test))
save_model_hdf5(autoencoder,"convolutional_autoencoder.h5")
predicted <- autoencoder %>% predict(x_test)
dim(x_test)
options(repr.plot.width=10, repr.plot.height=1)
grid = x_test[20,,,]
for(i in seq(1,5)){
    grid = abind(grid,x_test[i,,,],along = 2)
}

grid.raster(grid, interpolate=FALSE)
grid1 = predicted[20,,,]
for(i in seq(1,5)){
    grid1 = abind(grid1,predicted[i,,,],along = 2)
}

grid.raster(grid1, interpolate=FALSE)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1764065335753176, 'fraction_numerical': 0.04827586206896552, 'mean_word_length': 5.530805687203792, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '470496', 'license': 'MIT', 'n_tokens_mistral': 1182, 'n_tokens_neox': 1084, 'n_words': 199, 'path': 'Chapter04/Vanila autoencoder.ipynb', 'repo_name': 'PacktPublishing/Deep-Learning-with-R-Cookbook', 'size': '2755'}