-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsec4_hwk3_tensorflow_image-classf.py
44 lines (33 loc) · 1.24 KB
/
sec4_hwk3_tensorflow_image-classf.py
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
import matplotlib.pyplot as plt
import numpy as np
from keras.models import Sequential
from keras.layers import Dense
from keras.utils import np_utils
from keras.datasets import fashion_mnist
(x_train, y_train), (x_test, y_test)= fashion_mnist.load_data()
i = np.random.randint(0, len(X_train))
plt.imshow(X_train[i], cmap = 'gray')
plt.title('Class ' + str(y_train[i]))
x_train = x_train.reshape(60000, 28*28)
x_test = x_test.reshape(10000, 28*28)
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
y_train = np_utils.to_categorical(y_train)
y_test = np_utils.to_categorical(y_test)
network = Sequential()
network.add(Dense(input_shape=(784,), units=397, activation='relu'))
network.add(Dense(units=397, activation='relu'))
network.add(Dense(units=10, activation='softmax'))
network.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
history = network.fit(x_train, y_train, batch_size=128, epochs=40)
plt.plot(history.history['loss'])
plt.show()
plt.plot(history.history['accuracy'])
plt.show()
accuracy_test = network.evaluate(x_test, y_test)
predictions = network.predict(x_test)
plt.imshow(x_test[0].reshape(28,28), cmap='gray')
plt.title('Class: ' + str(y_test[0]))
plt.show()