9주차-딥러닝(2)

Chan·2021년 6월 8일

hancom

목록 보기
21/45

텐서플로우 가이드북

https://tensorflowkorea.gitbooks.io/tensorflow-kr/content/g3doc/

tf 모델 저장/복원

ckpt(체크포인트) : 가중치 저장
pb(포토버프) : 네트워크 그래프
tfrecords : 데이터

import tensorflow as tf
 
state1 = tf.Variable(0, name = 'state1')
state2 = tf.Variable(10, name = 'state2')

init_op = tf.global_variables_initializer()
saver = tf.train.Saver({"mys2": state2})

one = tf.constant(1)
new_value = tf.add(state2, one)
update = tf.assign(state2, new_value)

with tf.Session() as sess:
    sess.run(init_op)
    sess.run(update)
    save_path = saver.save(sess, '/tmp/model1.ckpt')
    print(" Model saved in file: ", save_path)
    print(sess.run(state2))

#----------------------

import tensorflow as tf

state1 = tf.Variable(0, name = 'state1')
state2 = tf.Variable(20, name = 'state2')

# one = tf.constant(1)
# new_value = tf.add(state2, one)
# update = tf.assign(state2, new_value)
 
init_op = tf.global_variables_initializer()
saver = tf.train.Saver({"mys2": state2})
 
with tf.Session() as sess:
    #sess.run(init_op)
    saver.restore(sess, '/tmp/model1.ckpt')
    #sess.run(init_op)
    print(" Model restored from file ")
    print(sess.run(state2))

tf2에서 tf1 함수 섞어쓰기

import tensorflow as tf
import tensorflow.compat.v1 as tf1
tf1.disable_eager_execution()

state = tf.Variable(0, name= 'counter')
one = tf.constant(1)
new_value = tf.add(state, one)
update = tf1.assign(state, new_value)
init_op = tf1.global_variables_initializer()
writer = tf.summary.create_file_writer('/tmp/log')

with tf1.Session() as sess:
    sess.run(init_op)
    print(sess.run(state))
    for _ in range(3):
        sess.run(update)
        print(sess.run(update))

tensorboard에서 flow graph 확인

/tmp/log 이런식으로 디렉토리 안에 그래프 저장 후, 터미널에서
tensorboard --logdir=/tmp/log 치고,
ctrl 눌러서 링크 클릭하면 flow graph 확인 가능

tf1.reset_default_graph()
with tf1.name_scope("DefineVariable"):
    x_data = np.random.rand(100).astype('f')
    y_data = x_data * .1 + .3
    W = tf.Variable(tf.random.uniform([1], -1., 1.))
    b = tf.Variable(tf.zeros([1]))
    y = W * x_data + b
with tf1.name_scope("Logit"):
    loss = tf.reduce_mean(tf.square(y - y_data))
    optimizer = tf1.train.GradientDescentOptimizer(.5)
    train = optimizer.minimize(loss)
init = tf1.global_variables_initializer()
sess = tf1.Session()
sess.run(init)
for step in range(201):
    sess.run(train)
    writer = tf1.summary.FileWriter("/tmp/log2", graph = sess.graph)
    if step % 20 == 0:
        print(step, 'W =', sess.run(W),
             'b =', sess.run(b))
sess.close()

Computation Graph (Using tf2.x)

import tensorflow as tf
import numpy as np
import tensorflow.compat.v1 as tf1
tf1.enable_eager_execution()

x_data = np.random.rand(100).astype('f')
y_data = x_data * .1 + .3
init = lambda : (tf.Variable(tf.random.uniform([1], -1, 1),
                            name = 'Weight'), tf.Variable(tf.zeros([1])))
W, b = init()
yp = lambda x : W * x + b
mse = lambda yp, yt : tf.reduce_mean(tf.square(yp-yt))

way = 1
if way == 1:
    for step in range(2001):
        with tf.GradientTape() as tape:
            y = yp(x_data)
            loss = mse(y, y_data)
        grads = tape.gradient(loss, [W, b])
        if step % 200 == 0:
            print('\n\rW = ', W.numpy(), '\n\rb =', b.numpy(),
                 '\n\rg[0] =', grads[0].numpy(), '\n\rg[1] =', grads[1].numpy())
        W.assign(W - 0.2 * grads[0].numpy())
        b.assign(b - 0.2 * grads[1].numpy())

elif way == 2:
    optimizer = tf.optimizers.SGD(learning_rate = 0.2)
    for step in range(201):
        with tf.GradientTape() as tape:
            y = yp(x_data)
            loss = mse(y, y_data)
        grads = tape.gradient(loss, [W, b])
        process_grads = [g for g in grads]
        grads_and_vars = zip(process_grads, [W, b])
        if step % 20 == 0:
            print('\n\rW = ', W.numpy(), '\n\rb =', b.numpy(),
                 '\n\rg[0] =', grads[0].numpy(), '\n\rg[1] =', grads[1].numpy())
        optimizer.apply_gradients(grads_and_vars)

elif way == 3:
    yp, yt = yp(x_data), y_data
    optimizer = tf.optimizers.SGD(learning_rate = 0.1)
    for step in range(201):
        if step % 20 == 0:
            print('\n\rW = ', W.numpy(), '\n\rb =', b.numpy())
        mse_min = lambda : tf.reduce_mean(tf.square((W * x_data + b) - yt))
        optimizer.minimize(mse_min, var_list = [W, b])

Cost and Gradient

import tensorflow as tf
import matplotlib.pyplot as plt
 
X = [1., 2., 3.]
Y = [1., 2., 3.]
 
# W = tf.placeholder(tf.float32)
hypothesis = lambda W: tf.multiply(W, X)
cost = lambda hx: tf.reduce_sum(tf.pow(hx-Y, 2)) / len(X)
W_val, cost_val = [], []

for i in range(-30, 51):
    xPos = i * 0.1
    #xPos = i
    yPos = cost(hypothesis(xPos))
    print('\n\rW =', xPos, '\n\rCost =', yPos)    
    W_val.append(xPos)
    cost_val.append(yPos)
# sess.close()
 
plt.plot(W_val, cost_val, 'ro')
plt.ylabel('Cost')
plt.xlabel('W')
plt.show()

Gradient Descent Optimization (1)

import tensorflow as tf
 
x_data = [1., 2., 3.]
y_data = [1., 2., 3.]
 
W = tf.Variable(tf.random.uniform([1], -1.0, 1.0))
b = tf.Variable(tf.random.uniform([1], -1.0, 1.0))
 
hx = lambda : W * x_data + b
cost = lambda : tf.reduce_mean(tf.square(hx() - y_data))
 
rate = tf.Variable(0.1)
opt = tf.optimizers.SGD(rate)

for step in range(2001):
    if step % 20 == 0:
        print('{:4} {} {} {}'.format(step, cost(), W.numpy(), b.numpy()))
    opt.minimize(cost, var_list = [W, b])

Gradient Descent Optimization (2)

import tensorflow as tf

x_data = [1., 2., 3., 4.]
y_data = [2., 4., 6., 8.]

W = tf.Variable(tf.random.uniform([1], -100.0, 100.0))
b = tf.Variable(tf.random.uniform([1], -100.0, 100.0))

hx = lambda x_data : W * x_data + b
cost = lambda y_data : tf.reduce_mean(tf.square(hx(x_data) - y_data))

rate = tf.Variable(0.1)
opt = tf.optimizers.SGD(rate)

for step in range(601):
    cost_min = lambda : cost(y_data)
    opt.minimize(cost_min, var_list = [W, b])
    if step % 20 == 0:
        print('{:4} {} {} {}'.format(step, cost(y_data), W.numpy(), b.numpy()))
print('\n\rx = 2.5:', hx(2.5).numpy(), '\n\rx = 10:', hx(10).numpy(), '\n\rx = 2.5, 10', hx([2.5, 10]).numpy())

Logistic Regression

# import tensorflow as tf
# import numpy as np
# tf.get_logger().setLevel('ERROR')

xy = np.loadtxt('train.csv', delimiter = ',', unpack = True, dtype = 'f')
x_data = xy[:-1]
y_data = xy[-1]
W = tf.Variable(tf.random.uniform([1, len(x_data)], -1., 1.))
h = lambda X : tf.matmul(W, X)
hx = lambda X : tf.math.divide(1., 1. + tf.math.exp(-h(X)))
cost = lambda Y : -tf.reduce_mean(Y * tf.math.log(hx(x_data)) + (1-Y) * tf.math.log(1 - hx(x_data)))
rate = tf.Variable(.1)
opt = tf.optimizers.SGD(rate)

for step in range(2001):
    with tf.GradientTape() as tape:
        y = hx(x_data)
        loss = cost(y_data)
    grads = tape.gradient(loss, [W])
    process_grads = [g for g in grads]
    grads_and_vars = zip(process_grads, [W])
    opt.apply_gradients(grads_and_vars)    
    if step % 200 == 0:
        print(step, grads[0].numpy(), '\n\rW = ', W.numpy())

print('-'*60) 
print('[1, 2, 2] :', hx([[1.], [2.], [2.]]))
print('[1, 5, 5] :', hx([[1.], [5.], [5.]]))
print('[1, 2, 2], [1, 5, 5] :', hx([[1., 1.], [2., 5.], [2., 5.]]) >.5)
profile
Backend Web Developer

0개의 댓글