import tensorflow as tf
import numpy as np
char_arr = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
num_dic = {n: i for i, n in enumerate(char_arr)}
dic_len = len(num_dic)
def make_batch(seq_data):
input_batch, target_batch = [], []
for seq in seq_data:
input_num = [num_dic[n] for n in seq[:-1]]
target = num_dic[seq[-1]]
input_batch.append(np.eye(dic_len)[input_num])
target_batch.append(target)
return input_batch, target_batch
tf.reset_default_graph()
learning_rate = 0.01
n_step = 3
n_hidden, total_epoch = 64, 30
n_input = n_class = dic_len
X = tf.placeholder(tf.float32, [None, n_step, n_input])
Y = tf.placeholder(tf.int32, [None])
W = tf.Variable(tf.random_normal([n_hidden, n_class]))
b = tf.Variable(tf.random_normal([n_class]))
cell1 = tf.nn.rnn_cell.BasicLSTMCell(n_hidden)
cell1 = tf.nn.rnn_cell.DropoutWrapper(cell1, output_keep_prob=0.5)
cell2 = tf.nn.rnn_cell.BasicLSTMCell(n_hidden)
multi_cell = tf.nn.rnn_cell.MultiRNNCell([cell1, cell2])
outputs, states = tf.nn.dynamic_rnn(multi_cell, X, dtype=tf.float32)
outputs = tf.transpose(outputs, [1, 0, 2])
outputs = outputs[-1]
model = tf.matmul(outputs, W) + b
cost = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(
logits = model, labels = Y))
optimizer = tf.train.AdamOptimizer(learning_rate).minimize(cost)
%%time
seq_data = ['word', 'wood', 'deep', 'dive', 'cold', 'cool', 'load', 'love', 'kiss', 'kind']
sess = tf.Session()
sess.run(tf.global_variables_initializer())
input_batch, target_batch = make_batch(seq_data)
for epoch in range(total_epoch):
_, loss = sess.run([optimizer, cost],
feed_dict={X: input_batch, Y: target_batch})
if epoch % 4 == 0:
print('Epoch: {:.4f} cost = {:.6f}'.format(epoch + 1, loss))
print('최적화 완료!')
Epoch: 1.0000 cost = 4.107153 Epoch: 5.0000 cost = 1.661128 Epoch: 9.0000 cost = 0.736937 Epoch: 13.0000 cost = 0.514840 Epoch: 17.0000 cost = 0.298752 Epoch: 21.0000 cost = 0.262406 Epoch: 25.0000 cost = 0.156218 Epoch: 29.0000 cost = 0.171951 최적화 완료! CPU times: user 1.56 s, sys: 494 ms, total: 2.06 s Wall time: 3.1 s
%%time
prediction = tf.cast(tf.argmax(model, 1), tf.int32)
prediction_check = tf.equal(prediction, Y)
accuracy = tf.reduce_mean(tf.cast(prediction_check, tf.float32))
input_batch, target_batch = make_batch(seq_data)
predict, accuracy_val = sess.run([prediction, accuracy],
feed_dict={X: input_batch, Y: target_batch})
CPU times: user 80.4 ms, sys: 0 ns, total: 80.4 ms Wall time: 78 ms
predict_words = []
for idx, val in enumerate(seq_data):
last_char = char_arr[predict[idx]]
predict_words.append(val[:3] + last_char)
print('\n=== 예측 결과 ===')
print('입력값:', [w[:-1] + ' ' for w in seq_data])
print('예측값:', predict_words)
print('정확도:', accuracy_val)
sess.close()
=== 예측 결과 === 입력값: ['wor ', 'woo ', 'dee ', 'div ', 'col ', 'coo ', 'loa ', 'lov ', 'kis ', 'kin '] 예측값: ['word', 'wood', 'deep', 'dive', 'cold', 'cood', 'load', 'love', 'kiss', 'kind'] 정확도: 0.9