File size: 1,450 Bytes
4f65a7c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# -*- coding: utf-8 -*-
"""Zadanie4_Semenov_II_DRPK47.ipynb

Automatically generated by Colaboratory.

Original file is located at
    https://colab.research.google.com/drive/11fNvzrVniDSjEVdE-ZnejpPvvA88ApfY
"""

import numpy as np
import matplotlib.pyplot as plt
import tensorflow.keras as keras
import tensorflow.keras.datasets
from tensorflow.keras.datasets import fashion_mnist
from tensorflow.keras.layers import Input, Dense

(train_x, train_y), (test_x, test_y) = fashion_mnist.load_data()

train_x = train_x / 255
test_x = test_x / 255

train_x = np.reshape(train_x, (len(train_x), 28 * 28))
test_x = np.reshape(test_x, (len(test_x), 28 * 28))

inputs = Input(shape = (28*28, ))
x = Dense(150, activation = 'relu')(inputs)
x = Dense(400, activation = 'relu')(x)
x = Dense(10, activation = 'relu')(x)
encoder = Dense(3, activation = 'linear')(x)


inputs_dec = Input(shape = (3, ))
x = Dense(10, activation = 'relu')(inputs_dec)
x = Dense(40, activation = 'relu')(x)
x = Dense(150, activation = 'relu')(x)
decoder = Dense(28*28, activation = 'relu')(x)

encoder_model = keras.Model(inputs, encoder)
decoder_model = keras.Model(inputs_dec, decoder)
autoenc = keras.Model(inputs, decoder_model(encoder_model(inputs)))

autoenc.compile(optimizer='adam', loss='mean_squared_error', metrics = ['accuracy'])

autoenc.fit(train_x, train_x, epochs = 20, batch_size=50)

y = autoenc.predict(test_x[:12])
plt.imshow(y[5].reshape(28, 28), cmap = 'gray')