Advertisement
Guest User

Untitled

a guest
Feb 25th, 2020
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.52 KB | None | 0 0
  1. import keras
  2. from keras.layers import Input, Conv2D, MaxPool2D, Dense, Flatten, Dropout, Reshape, UpSampling2D
  3. from keras.models import Model
  4. from keras.datasets import mnist
  5. from sklearn.datasets import fetch_olivetti_faces
  6. from tensorflow.keras.utils import to_categorical
  7. import numpy as np
  8. import matplotlib.pyplot as plt
  9. import seaborn as sns
  10. from sklearn.model_selection import train_test_split
  11.  
  12. (x_train, y_train), (x_test, y_test) = mnist.load_data()
  13. x_train = np.reshape(x_train, (len(x_train), 28, 28, 1))
  14. x_test  = np.reshape(x_test,  (len(x_test),  28, 28, 1))
  15.  
  16. x_train / 255.0
  17. x_test / 255.0
  18.  
  19. y_train = np.reshape(y_train, (len(y_train), 1, 1, 1))
  20. y_test  = np.reshape(y_test,  (len(y_test), 1, 1, 1))
  21.  
  22. y_train.shape
  23.  
  24. input_layer = Input(shape=(28, 28, 1))
  25. conv1 = Conv2D(256, (1,1), activation='relu')(input_layer)
  26. pool1 = MaxPool2D()(conv1)
  27. conv2 = Conv2D(128, (1,1), activation='elu')(pool1)
  28. pool2 = MaxPool2D()(conv2)
  29. encoded = Dense(1,activation='softmax')(pool2)
  30.  
  31. input_layer2 = Input(shape=(28, 28, 1))
  32. x = Conv2D(128, (1,1), activation='relu')(input_layer2)
  33. x = MaxPool2D()(x)
  34. x = Conv2D(256, (1,1),activation='elu')(x)
  35. x = MaxPool2D()(x)
  36. decoded = Dense(1, activation='softmax')(x)
  37.  
  38. encoder = Model(input_layer, encoded)
  39. decoder = Model(input_layer2, decoded)
  40. autoencoder = Model(input_layer, decoder(encoder(input_layer)))
  41.  
  42. autoencoder.compile(optimizer='adam', loss='binary_crossentropy')
  43.  
  44. autoencoder.fit(x_train, y_train, epochs=2,
  45.           batch_size=64, validation_data=[x_test, y_test])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement