solstyle

공지 사항

temp 2020. 6. 1. 23:35 by solstyle

# fashion mnist

# 4 layer neural network 구성

 

import tensorflow as tf

 

# 데이터 준비

(x_train, y_train), (x_test, y_test) = tf.keras.datasets.fashion_mnist.load_data()

 

# shape를 조정

x_train = x_train.reshape(-1, 28 * 28)

x_test = x_test.reshape(-1, 28 * 28)

 

y_train = tf.keras.utils.to_categorical(y_train)

y_test = tf.keras.utils.to_categorical(y_test)

 

print(x_train.shape, y_train.shape)

print(x_test.shape, y_test.shape)

 

tf.reset_default_graph()

tf.random.set_random_seed(1)

 

X = tf.placeholder(tf.float32, shape=[None, 784])

Y = tf.placeholder(tf.float32, shape=[None, 10])

kp = tf.placeholder(tf.float32)

 

# layer 1 만들기

W1 = tf.Variable(tf.random_normal(shape=[784, 256]))

B1 = tf.Variable(tf.random_normal(shape=[256]))

H1 = tf.matmul(X, W1) + B1

H1 = tf.contrib.layers.batch_norm(H1)

H1 = tf.nn.relu(H1)

H1 = tf.nn.dropout(H1, keep_prob=kp)

# H1 shape = [None, 128]

 

# layer 2 만들기

W2 = tf.Variable(tf.random_normal(shape=[256, 256]))

B2 = tf.Variable(tf.random_normal(shape=[256]))

H2 = tf.matmul(H1, W2) + B2

H2 = tf.contrib.layers.batch_norm(H2)

H2 = tf.nn.relu(H2)

H2 = tf.nn.dropout(H2, keep_prob=kp)

 

# layer 3

W3 = tf.Variable(tf.random_normal(shape=[256, 128]))

B3 = tf.Variable(tf.random_normal(shape=[128]))

H3 = tf.matmul(H2, W3) + B3

H3 = tf.contrib.layers.batch_norm(H3)

H3 = tf.nn.relu(H3)

H3 = tf.nn.dropout(H3, keep_prob=kp)

 

# layer 4

W4 = tf.Variable(tf.random_normal(shape=[128, 10]))

B4 = tf.Variable(tf.random_normal(shape=[10]))

# H4 = tf.matmul(H3, W4) + B4

# H4 = tf.nn.relu(H4)

# H4 = tf.nn.dropout(H4, keep_prob=kp)

 

logit = tf.matmul(H3, W4) + B4

 

# loss

loss = tf.nn.softmax_cross_entropy_with_logits_v2(logits=logit, labels=Y)

loss = tf.reduce_mean(loss)

 

pred = tf.nn.softmax(logit)

acc = tf.equal(tf.argmax(pred, axis=1), tf.argmax(Y, axis=1))

acc = tf.reduce_mean(tf.cast(acc, tf.float32))

 

optimizer = tf.train.AdamOptimizer(0.01).minimize(loss)

 

 

# 모델 학습

 

sess = tf.Session()

sess.run(tf.global_variables_initializer())

 

epochs = 5

batch = 256

n_batch = len(x_train) // batch

 

for e in range(epochs):

for b in range(n_batch):

x = x_train[b * batch:(b + 1) * batch]

y = y_train[b * batch:(b + 1) * batch]

sess.run(optimizer, feed_dict={X: x, Y: y, kp: 0.8})

 

if b % 20 == 0:

print(sess.run([loss, acc], feed_dict={X: x, Y: y, kp: 1.0}))

 

accuracy = sess.run(acc, feed_dict={X: x_test, Y: y_test, kp: 1.0})

print("{0: .2f}%".format(accuracy * 100))

 

 

 

 

fashion mnist CNN

  • conv1 8장
  • pool
  • conv2 16장
  • pool
  • fc

import tensorflow as tf

 

tf.reset_default_graph()

 

(x_train, y_train), (x_test, y_test) = \

tf.keras.datasets.cifar10.load_data()

 

x_train = x_train / 255

x_test = x_test / 255

 

# X = (X - min) / (max - min)

 

y_train = tf.keras.utils.to_categorical(y_train)

y_test = tf.keras.utils.to_categorical(y_test)

 

print(x_train.shape, y_train.shape)

 

tf.reset_default_graph()

# tf.random.set_random_seed(1)

 

# conv1 준비

X = tf.placeholder(tf.float32, shape=[None, 32, 32, 3])

Y = tf.placeholder(tf.float32, shape=[None, 10])

 

W1 = tf.Variable(tf.random_normal(shape=[3, 3, 3, 16]))

B1 = tf.Variable(tf.random_normal(shape=[16]))

H1 = tf.nn.conv2d(X, W1, strides=[1, 1, 1, 1], padding="SAME")

H1 = tf.add(H1, B1)

H1 = tf.contrib.layers.batch_norm(H1)

HR1 = tf.nn.relu(H1)

 

# pool1

P1 = tf.nn.max_pool(HR1,

ksize=[1, 2, 2, 1],

strides=[1, 2, 2, 1],

padding="SAME")

 

# conv2

W2 = tf.Variable(tf.random_normal(shape=[3, 3, 16, 16]))

B2 = tf.Variable(tf.random_normal(shape=[16]))

H2 = tf.nn.conv2d(P1, W2, strides=[1, 1, 1, 1], padding="SAME")

H2 = tf.add(H2, B2)

H2 = tf.contrib.layers.batch_norm(H2)

HR2 = tf.nn.relu(H2)

 

# pool2

P2 = tf.nn.max_pool(HR2,

ksize=[1, 2, 2, 1],

strides=[1, 2, 2, 1],

padding="SAME")

 

# flatten

flat = tf.reshape(P2, [-1, 8 * 8 * 16])

 

# fc layer

W3 = tf.Variable(tf.random_normal(shape=[8 * 8 * 16, 10]))

B3 = tf.Variable(tf.random_normal(shape=[10]))

logit = tf.matmul(flat, W3) + B3

 

# loss

loss = tf.nn.softmax_cross_entropy_with_logits_v2(

logits=logit, labels=Y)

loss = tf.reduce_mean(loss)

optimizer = tf.train.AdamOptimizer(0.01).minimize(loss)

 

# acc

pred = tf.nn.softmax(logit)

acc = tf.equal(tf.argmax(pred, axis=1), tf.argmax(Y, axis=1))

acc = tf.reduce_mean(tf.cast(acc, tf.float32))



 

sess = tf.Session()

sess.run(tf.global_variables_initializer())

 

epochs = 10

batch = 256

n_batch = len(x_train) // batch

 

for e in range(epochs):

for b in range(n_batch):

x = x_train[b * batch:(b + 1) * batch]

y = y_train[b * batch:(b + 1) * batch]

sess.run(optimizer, feed_dict={X: x, Y: y})

 

if b % 20 == 0:

print(sess.run([loss, acc], feed_dict={X: x_train, Y: y_train}))

 

print("학습평가")

print(sess.run([loss, acc], feed_dict={X: x_test, Y: y_test}))

 

 

 

1 2 3 4 ··· 56 
BLOG main image
solstyle
From the Depth of My Drawer
by solstyle

카테고리

분류 전체보기 (56)
SolStorys (27)
temp (3)
SolBox (25)

태그목록

달력

«   2024/11   »
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
tistory!get rss Tistory Tistory 가입하기!

최근에 올라온 글

최근에 달린 댓글

최근에 받은 트랙백