내용

글번호 1004
작성자 허진경
작성일 2019-12-13 12:35:49
제목 [Re]딥러닝(케라스)을 이용한 패션MNIST 데이터 분류
내용 import tensorflow tensorflow.__version__ # import tensorflow as tf import tensorflow.compat.v1 as tf tf.disable_v2_behavior() import numpy as np import pandas as pd import matplotlib.pyplot as plt from tensorflow import keras fashion_mnist = keras.datasets.fashion_mnist (train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data() class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat', 'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot'] # train_images.shape # train_labels plt.figure() plt.imshow(train_images[0]) plt.colorbar() plt.grid(False) plt.show() # train_images = train_images / 255.0 # test_images = test_images / 255.0 plt.figure(figsize=(10,10)) for i in range(25): plt.subplot(5,5,i+1) plt.xticks([]) plt.yticks([]) plt.grid(False) plt.imshow(train_images[i], cmap=plt.cm.binary) plt.xlabel(class_names[train_labels[i>) plt.show() from sklearn.preprocessing import OneHotEncoder enc = OneHotEncoder() enc.fit(train_labels.reshape(-1, 1)) train_labels_oh = enc.transform(train_labels.reshape(-1, 1)) ######################################### model = keras.Sequential([ keras.layers.Flatten(input_shape=(28,28)), keras.layers.Dense(128, activation=tf.nn.relu), keras.layers.Dense(10, activation=tf.nn.softmax) ]) model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy']) model.fit(train_images, train_labels, epochs=8) test_loss, test_acc = model.evaluate(test_images, test_labels) predictions = model.predict(test_images) [round(p, 4) for p in predictions[0> np.argmax(predictions[0]), class_names[np.argmax(predictions[0])] plt.imshow(test_images[0], cmap=plt.cm.binary) plt.show()