import tensorflow.keras as keras
import tensorflow as tf

import numpy as np
import matplotlib.pyplot as plt

# Load the fashion-mnist pre-shuffled train data and test data
(x_train, y_train), (x_test, y_test) = keras.datasets.fashion_mnist.load_data()

print("x_train shape:", x_train.shape, "y_train shape:", y_train.shape)

# Print training set shape - note there are 60,000 training data of image size of 28x28, 60,000 train labels)
print("x_train shape:", x_train.shape, "y_train shape:", y_train.shape)

# Print the number of training and test datasets
print(x_train.shape[0], 'train set')
print(x_test.shape[0], 'test set')

# Define the text labels
fashion_mnist_labels = ["T-shirt/top",  # index 0
                        "Trouser",      # index 1
                        "Pullover",     # index 2 
                        "Dress",        # index 3 
                        "Coat",         # index 4
                        "Sandal",       # index 5
                        "Shirt",        # index 6 
                        "Sneaker",      # index 7 
                        "Bag",          # index 8 
                        "Ankle boot"]   # index 9

# Image index, you can pick any number between 0 and 59,999
img_index = np.random.randint(0, 59999)
# y_train contains the lables, ranging from 0 to 9
label_index = y_train[img_index]
# Print the label, for example 2 Pullover
print ("y = " + str(label_index) + " " +(fashion_mnist_labels[label_index]))
# # Show one of the images from the training dataset
plt.imshow(x_train[img_index])

from tensorflow.keras.layers import Input, Flatten, Dense
from tensorflow.keras.models import Model

from tensorflow.keras.losses import sparse_categorical_crossentropy
from tensorflow.keras.optimizers import Adam

keras.backend.clear_session()

# x_train = x_train.reshape(-1, 28 * 28 * 1).astype("float32") / 255.0
# x_test  = x_test.reshape(-1, 28 * 28 * 1).astype("float32") / 255.0
# y_train = tf.one_hot(y_train, 10)
# y_test  = tf.one_hot(y_test , 10)
# print(x_train.shape, y_train.shape, x_test.shape, y_test.shape)

il = Input(shape=(28,28), name="input_layer")
fl = Flatten(name='Flatten_layer')(il)
hl = Dense(512, activation="relu",name="H1")(fl)
hl = Dense(512, activation="relu",name="H2")(hl)
hl = Dense(512, activation="relu",name="H3")(hl)
hl = Dense(512, activation="relu",name="H4")(hl)
hl = Dense(512, activation="relu",name="H5")(hl)
hl = Dense(512, activation="relu",name="H6")(hl)
ol = Dense(10 , activation="softmax" ,name="output_layer")(hl)

model = Model(inputs=il, outputs=ol )
model.compile(loss='sparse_categorical_crossentropy', optimizer=Adam(lr=0.001), metrics=['accuracy'])
model.summary()
model.fit(x_train, y_train, epochs=5, batch_size=128, verbose=1)
score = model.evaluate(x_test, y_test)

# Print test accuracy
print('\n', 'Test accuracy:', score[1])
y_hat = model.predict(x_test)

 

'도서관 I > AI' 카테고리의 다른 글

기본 keras 사용법  (0) 2020.06.01
top_K  (0) 2020.06.01
softmax keras  (0) 2020.05.29
MNIST 데이터 불러오기  (0) 2020.05.29
python 정리  (0) 2020.05.28
import numpy as np
from keras.datasets import mnist
from keras.utils    import np_utils
from keras.models   import Sequential
from keras.layers   import Dense, Conv2D, pooling, Flatten

(tri, trl), (tei, tel) = mnist.load_data()
print(tri.shape, trl.shape, tei.shape, tel.shape)
tri = tri.reshape(tri.shape[0],28,28,1).astype(np.float32) / 255.0
tei = tei.reshape(tei.shape[0],28,28,1).astype(np.float32) / 255.0
trl = np_utils.to_categorical(trl)
tel = np_utils.to_categorical(tel)
# trl = tf.one_hot(trl, 10)
print(tri.shape, trl.shape, tei.shape, tel.shape)

model = Sequential()
model.add(Conv2D(32, (3,3), strides=(1,1), padding="same", activation="relu", input_shape=(28,28,1)))
model.add(Conv2D(32, (3,3), strides=(1,1), padding="same", activation="relu"))
model.add(pooling.MaxPooling2D(pool_size=(2,2)))
model.add(Conv2D(64, (3,3), strides=(1,1), padding="same", activation="relu"))
model.add(Conv2D(64, (3,3), strides=(1,1), padding="same", activation="relu"))
model.add(pooling.MaxPooling2D(pool_size=(2,2)))
model.add(Flatten())
model.add(Dense(10, activation="softmax"))

model.compile(loss="categorical_crossentropy", optimizer="adam", metrics=['accuracy'])

model.fit(tri, trl, epochs=5, batch_size=32 , verbose=1)

_, a = model.evaluate(tei, tel)

print("acc : " , a)

model.summary()

'도서관 I > AI' 카테고리의 다른 글

Fashion-MNIST 예제  (0) 2020.06.01
top_K  (0) 2020.06.01
softmax keras  (0) 2020.05.29
MNIST 데이터 불러오기  (0) 2020.05.29
python 정리  (0) 2020.05.28
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import tensorflow as tf

from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data", one_hot=True)

n_train_img = 5000
n_test_img = 200

pixel_train, onehot_train = mnist.train.next_batch(n_train_img)
print(pixel_train.shape, onehot_train.shape)
pixel_test, onehot_test   = mnist.test.next_batch(n_test_img)
print(pixel_test.shape, onehot_test.shape)

TRAIN = tf.placeholder("float", (None,784))
TEST  = tf.placeholder("float", (784))

#d = tf.reduce_sum(tf.abs(tf.add(TRAIN, tf.negative(TEST))), reduction_indices=1)
d = tf.sqrt(tf.reduce_sum(tf.square(tf.subtract(TRAIN, TEST)), reduction_indices=1))
print (d)

val, idx = tf.nn.top_k(-d, k=20, sorted=False)
print (val, idx)
acc = 0.0
tot = 0.0

with tf.Session() as sess:
  sess.run(tf.global_variables_initializer())
  for i in range(n_test_img):
    knn_idx = sess.run(idx, feed_dict={TRAIN: pixel_train, TEST: pixel_test[i,:]})
    
    look_up = np.zeros(10)
    for j in np.argmax(onehot_train[knn_idx], axis=1):
      look_up[j] += 1
    pred = np.argmax(look_up)

    if (pred == np.argmax(onehot_test[i])):
      acc += 1
    tot += 1
    print("Test : " , i , " , Pred : " , pred , " , Real : ", np.argmax(onehot_test[i]), " , acc : " , acc/tot)

    
    
a = np.array(list(pixel_test[197,:]))  # 0,6
a = a.reshape(28,28)
plt.imshow(a, cmap=plt.get_cmap('gray'))
a = np.array(list(pixel_test[169,:]))    # 5,8
a = a.reshape(28,28)
plt.imshow(a, cmap=plt.get_cmap('gray'))
a = np.array(list(pixel_test[162,:]))    # Pred :  9  , Real :  4 
a = a.reshape(28,28)
plt.imshow(a, cmap=plt.get_cmap('gray'))
a = np.array(list(pixel_test[161,:]))    # Pred :  0  , Real :  8 
a = a.reshape(28,28)
plt.imshow(a, cmap=plt.get_cmap('gray'))

'도서관 I > AI' 카테고리의 다른 글

Fashion-MNIST 예제  (0) 2020.06.01
기본 keras 사용법  (0) 2020.06.01
softmax keras  (0) 2020.05.29
MNIST 데이터 불러오기  (0) 2020.05.29
python 정리  (0) 2020.05.28

import

from keras.utils    import np_utils
from keras.datasets import mnist
from keras.models   import Sequential
from keras.layers   import Dense

 

mnist 데이터 변형하기

tri = tri.reshape(tri.shape[0],784).astype("float32")/255.0
tei = tei.reshape(tei.shape[0],784).astype("float32")/255.0
trl = np_utils.to_categorical(trl, num_classes=10, dtype='float32')
tel = np_utils.to_categorical(tel, num_classes=10, dtype='float32')
print(tri.shape,trl.shape,tei.shape,tel.shape)

model

model = Sequential()
model.add(Dense(256, activation="relu"))
model.add(Dense(512, activation="relu"))
model.add(Dense(10, activation="softmax"))
model.compile(loss="categorical_crossentropy", optimizer="sgd", metrics=['accuracy'])
model.fit(tri, trl, epochs=5, batch_size=128, verbose=1)

c, a = model.evaluate(tei, tel)
print("acc : " , a, " , c : " , c)

'도서관 I > AI' 카테고리의 다른 글

Fashion-MNIST 예제  (0) 2020.06.01
기본 keras 사용법  (0) 2020.06.01
top_K  (0) 2020.06.01
MNIST 데이터 불러오기  (0) 2020.05.29
python 정리  (0) 2020.05.28

tutorials 로 가져오기

from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("mnist_data", one_hot=True)

img_train, lbl_train = mnist.train.next_batch(batch_size)
img_test , lbl_test  = mnist.test.next_batch(mnist.test.num_examples)

 

keras로 가져오기

from keras.datasets import mnist

(train_img, train_lbl), (test_img, test_lbl) = mnist.load_data()
print (train_img.shape, train_lbl.shape,test_img.shape, test_lbl.shape)
train_img = train_img.reshape(-1,28,28,1).astype(np.float32)
test_img  = test_img.reshape(-1,28,28,1).astype(np.float32)
train_lbl = np_utils.to_categorical(train_lbl)
test_lbl  = np_utils.to_categorical(test_lbl)

pandas로 가져오기

data = pd.read_csv('https://raw.githubusercontent.com/DA4BAM/dataset/master/diamonds.csv'
                 , sep=',', skipinitialspace=True)  


df = pd.read_csv('data-diabetes.csv', delimiter=',',index_col=False)
df.head()
x_data = df.iloc[:,0:-1]
y_data = df.iloc[:,-1]
print (x_data.shape, y_data.shape)

'도서관 I > AI' 카테고리의 다른 글

Fashion-MNIST 예제  (0) 2020.06.01
기본 keras 사용법  (0) 2020.06.01
top_K  (0) 2020.06.01
softmax keras  (0) 2020.05.29
python 정리  (0) 2020.05.28

기본 연산자

a / b  => 나누기
a // b  => 나눈 몫
a % b =>  나눈 나머지
a ** b => a의 b제곱  ex) 2 ** 3 => 2*2*2

문자열 print format

hello = 'hello' 
world = 'world'

# {} 안에 위치 값 지정 --> 지정한 위치의 변수가 대입 됨
print('{0} {1} {2}'.format(hello, world, 12)) 
print('{2} {0} {1}'.format(hello, world, 12))

문자열 공백 제거 (trim())

strip(x) 메소드: x 문자 주변의 공백을 제거합니다.

# 문자열 분리 
s = '    I have a Dream! '
s = s.strip()
print(s)

list 관련 함수들

1) count(), index() 메소드: 요소 갯수와 위치 조회
2) append() 메소드: 요소 하나 추가
3) reverse(), sort() 메소드: 요소 순서 변경
4) insert() 메소드: 지정한 위치에 요소 추가
5) remove(), pop(), clear() 메소드: 요소 삭제
6) extend() 메소드: 리스트 연결

Pandas 

data.dtypes

carat      float64
cut         object
color       object
clarity     object
depth      float64
table      float64
price        int64
x          float64
y          float64
z          float64
dtype: object

#조건 추출시에는 소괄호 넣기
data[ (data.color == 'E') & (data['price'] > 5000)]

# 조건에 맞는 여러 열 조회
graduate.loc[graduate['gpa'] > 3.0, ['admit', 'gpa', 'gre']].head()

# 특정 컬럼값중 가장큰값을 가지는 행 출력
data[ data.price == data.price.max() ]

# cut별 평균 carat을 조회하시오
data.groupby(by=['cut'], as_index=False)[['carat']].mean().rename(columns={'carat':'avgCarat'})

plot 그리기

# 산점도 그리기

data.plot('carat', 'price', kind = 'scatter')
plt.show()

'도서관 I > AI' 카테고리의 다른 글

Fashion-MNIST 예제  (0) 2020.06.01
기본 keras 사용법  (0) 2020.06.01
top_K  (0) 2020.06.01
softmax keras  (0) 2020.05.29
MNIST 데이터 불러오기  (0) 2020.05.29

+ Recent posts