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

# linear regression

model = tf.matmul(X,W) + b
cost = tf.reduce_mean(tf.square(model - Y))
train = tf.train.GradientDescentOptimizer(0.01).minimize(cost)

# binary classification

model = tf.sigmoid(tf.add(tf.matmul(X,W), b))
cost  = tf.reduce_mean((-1)*Y*tf.log(model) + (-1)*(1-Y)*tf.log(1 - model))
train = tf.train.GradientDescentOptimizer(0.01).minimize(cost)

prediction = tf.cast(model > 0.5, dtype=tf.float32)
accuracy = tf.reduce_mean(tf.cast(tf.equal(prediction, Y), dtype=tf.float32))

# softmax 

model_LC=tf.add(tf.matmul(X,W),b)
model = tf.nn.softmax(model_LC)
#cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=model_LC, labels=Y))
cost = tf.reduce_mean(-tf.reduce_sum(Y*tf.log(model), axis=1))
train = tf.train.GradientDescentOptimizer(0.1).minimize(cost)

acc = tf.reduce_mean(tf.cast(tf.equal(tf.argmax(model, 1), tf.argmax(Y,1)), tf.float32))

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

너무 좋아서... 자주 보다가 그냥 퍼왔습니다.


자세가... 환상이네요~~~ ^^ 언제 저런 스윙 함 해보나요... ㅋ












내가 맡고 있는 시스템에 문제가 있었다.

나에게 오기전부터 한달에 한번씩 오류나면 재기동을 하면된단다...

내가 멀아나? 그냥 시키는 대로했지... ㅋ

그러나... 왠지 찜찜하다..

다음 오류부터 분석에들어갔다 ㅋ
heapdump 볼줄도 몰랐던 내가 하나하나 인터넷 뒤지고 벤더사 지원을 받아가며 막 뒤졌다
그러니 조금씩보이기 시작했다.

특정 객체가 메모리의 99%를 차지하며 죽더란말이지...
왜일까...??? 개발자에게 말해도 모르쇠요... 벤더사 물어봐도 모르쇠...

결국 두번째 덤프를 통해 개발자 코드에서 java 객체 생성후 reference를 남긴채 종료 된다는 것을 찾았다.

즉, 개발자들은 자원해제가 다 되었다고( 사용했던 객체는 모두 자원해제하였다고) 생각했지만 실제로 배열에 reference 변수들이 남아있었고, java garbage collector는 해당 자원들을 정리하지 않았던 것이였다.

날이 지날수록 점점 메모리에 쓰레기 자원만 늘어갔고, 결국 메모리 full이 발생했을 땐 (여기선 약 한달) 실제로 신규 접속정보를 저장할 공간을 할당할수 없는 상태가 되었다.

즉, ... connection fail...

모르는 운영자에겐 최선의 방법은 재기동일 뿐이다 ㅋㅋㅋ



이렇게 아무도 인정해주지 않는 곳에서 오늘 하루도 보람을 느낀다... ㅋㅋㅋ



iPhone 에서 작성된 글입니다.

'바람돌이 이야기' 카테고리의 다른 글

술을마셔따...  (0) 2011.02.24
서현역 롯데리아... 안 가!  (2) 2011.02.09
잠이오지 않는밤  (0) 2010.11.08
내가 아는이의 뽀~~옥씽 모습  (0) 2010.07.10
[바람이] 뉴맥북 화이트 질럿습니다. @^-^@  (2) 2010.03.11

+ Recent posts