Professional Documents
Culture Documents
C 19 AmanAgarwal
C 19 AmanAgarwal
Roll No. 19
Regn. No. 201800119
7th Semester
Section C
Code :
import numpy as np
%matplotlibinline
train_noise = np.clip(train_noise, 0, 1)
test_noise = np.clip(test_noise, 0, 1)
# Encoder
inputs = Input(shape=(28,28,1))
# Decoder
Output:
Model: "model"
=================================================================
Total params: 28,353
Trainable params: 28,353
Non-trainable params: 0
epochs = 10
batch_size = 256
history = autoencoder.fit(train_noise,
train,
epochs=epochs,
batch_size=batch_size,
shuffle=True,
validation_data=(test_noise, test)
)
# Defining Figure
f = plt.figure(figsize=(10,7))
f.add_subplot()
# Adding Subplot
plt.plot(history.epoch, history.history['loss'], label = "loss") # Loss
curve for training set
plt.plot(history.epoch, history.history['val_loss'], label = "val_loss"
) # Loss curve for validation set
plt.title("Loss Curve",fontsize=18)
plt.xlabel("Epochs",fontsize=15)
plt.ylabel("Loss",fontsize=15)
plt.grid(alpha=0.3)
plt.legend()
plt.savefig("Loss_curve.png")
plt.show()
# Select few random test images
num_imgs = 16
rand = np.random.randint(1, 100)
f.suptitle("Autoencoder Results",fontsize=18)
plt.savefig("test_results.png")
plt.show()
epochs1 = 15
batch_size = 256
history1 = autoencoder.fit(train_noise,
train,
epochs=epochs1,
batch_size=batch_size,
shuffle=True,
validation_data=(test_noise, test)
)
# Defining Figure
f1 = plt.figure(figsize=(10,7))
f1.add_subplot()
# AddingSubplot
plt.plot(history1.epoch, history1.history['loss'], label = "loss") # Lo
ss curve for training set
plt.plot(history1.epoch, history1.history['val_loss'], label = "val_los
s") # Loss curve for validation set
plt.title("Loss Curve",fontsize=18)
plt.xlabel("Epochs",fontsize=15)
plt.ylabel("Loss",fontsize=15)
plt.grid(alpha=0.3)
plt.legend()
plt.savefig("Loss_curve.png")
plt.show()
f1 = plt.figure(figsize=(2*cols,2*rows*2)) # Defining a figure
f1.suptitle("Autoencoder Results",fontsize=18)
plt.savefig("test_results.png")
plt.show()
epochs2 = 20
batch_size = 256
history2 = autoencoder.fit(train_noise,
train,
epochs=epochs2,
batch_size=batch_size,
shuffle=True,
validation_data=(test_noise, test)
)
Output:
Epoch 1/20
235/235 [==============================] - 156s 664ms/step - loss:
0.0849 - val_loss: 0.0794
Epoch 2/20
235/235 [==============================] - 157s 670ms/step - loss:
0.0848 - val_loss: 0.0801
Epoch 3/20
235/235 [==============================] - 158s 671ms/step - loss:
0.0847 - val_loss: 0.0790
Epoch 4/20
235/235 [==============================] - 158s 671ms/step - loss:
0.0846 - val_loss: 0.0793
Epoch 5/20
235/235 [==============================] - 159s 675ms/step - loss:
0.0845 - val_loss: 0.0799
Epoch 6/20
235/235 [==============================] - 159s 675ms/step - loss:
0.0844 - val_loss: 0.0797
Epoch 7/20
235/235 [==============================] - 159s 677ms/step - loss:
0.0843 - val_loss: 0.0791
Epoch 8/20
235/235 [==============================] - 159s 676ms/step - loss:
0.0843 - val_loss: 0.0789
Epoch 9/20
235/235 [==============================] - 158s 672ms/step - loss:
0.0842 - val_loss: 0.0791
Epoch 10/20
235/235 [==============================] - 157s 670ms/step - loss:
0.0841 - val_loss: 0.0791
Epoch 11/20
235/235 [==============================] - 157s 670ms/step - loss:
0.0840 - val_loss: 0.0800
Epoch 12/20
235/235 [==============================] - 157s 667ms/step - loss:
0.0840 - val_loss: 0.0816
Epoch 13/20
235/235 [==============================] - 158s 673ms/step - loss:
0.0840 - val_loss: 0.0790
Epoch 14/20
235/235 [==============================] - 158s 673ms/step - loss:
0.0839 - val_loss: 0.0788
Epoch 15/20
235/235 [==============================] - 157s 669ms/step - loss:
0.0838 - val_loss: 0.0796
Epoch 16/20
235/235 [==============================] - 157s 669ms/step - loss:
0.0838 - val_loss: 0.0788
Epoch 17/20
235/235 [==============================] - 158s 670ms/step - loss:
0.0838 - val_loss: 0.0791
Epoch 18/20
235/235 [==============================] - 157s 670ms/step - loss:
0.0837 - val_loss: 0.0796
Epoch 19/20
235/235 [==============================] - 158s 675ms/step - loss:
0.0836 - val_loss: 0.0786
Epoch 20/20
235/235 [==============================] - 159s 678ms/step - loss:
0.0836 - val_loss: 0.0786
# Defining Figure
f2 = plt.figure(figsize=(10,7))
f2.add_subplot()
# AddingSubplot
plt.plot(history2.epoch, history2.history['loss'], label = "loss") # Lo
ss curve for training set
plt.plot(history2.epoch, history2.history['val_loss'], label = "val_los
s") # Loss curve for validation set
plt.title("Loss Curve",fontsize=18)
plt.xlabel("Epochs",fontsize=15)
plt.ylabel("Loss",fontsize=15)
plt.grid(alpha=0.3)
plt.legend()
plt.savefig("Loss_curve.png")
plt.show()
f2.suptitle("Autoencoder Results",fontsize=18)
plt.savefig("test_results.png")
plt.show()
Autoencoder Results