import tensorflow as tf
import numpy as np
def rnn_1():
vocab = np.array(['e', 'n', 'o', 'r', 's', 't'])
x = [[0., 0., 0., 0., 0., 1.], # t
[1., 0., 0., 0., 0., 0.], # e
[0., 1., 0., 0., 0., 0.], # n
[0., 0., 0., 0., 1., 0.], # s
[0., 0., 1., 0., 0., 0.]] # o
y = [[1, 0, 0, 0, 0, 0], # e
[0, 1, 0, 0, 0, 0], # n
[0, 0, 0, 0, 1, 0], # s
[0, 0, 1, 0, 0, 0], # o
[0, 0, 0, 1, 0, 0]] # r
w = tf.Variable(tf.random_uniform([6,6]))
b = tf.Variable(tf.random_uniform([6]))
# (5,6) = (5,6) @ (6, 6)
z = tf.matmul(x, w) + b
h = tf.nn.softmax(z)
loss = tf.nn.softmax_cross_entropy_with_logits_v2(logits=z, labels=y)
optimizer = tf.train.GradientDescentOptimizer(0.1)
train = optimizer.minimize(loss=loss)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for i in range(1000):
sess.run(train)
print(sess.run(loss))
print(sess.run(h))
print(np.argmax(sess.run(h), axis=1 ))
print(vocab[np.argmax(sess.run(h), axis=1)])
rnn_1()
def rnn_2_2():
vocab = np.array(['e', 'n', 'o', 'r', 's', 't'])
# tenso
x = [[0., 0., 0., 0., 0, 1.],
[1., 0., 0., 0., 0, 0.],
[0., 1., 0., 0., 0, 0.],
[0., 0., 0., 0., 1, 0.],
[0., 0., 1., 0., 0, 0.]]
# ensor
y = [[1., 0., 0., 0., 0., 0.], # 클래스 6개. Dense 형태
[0., 1., 0., 0., 0., 0.],
[0., 0., 0., 0., 1., 0.],
[0., 0., 1., 0., 0., 0.],
[0., 0., 0., 1., 0., 0.]]
hidden_size = 2 # 전개량
x = np.float32([x]) # dynamic_rnn은 3차원 데이터를 입력으로 요구 함. x: 2차원 -> 3차원
# RNN layer
cells = tf.nn.rnn_cell.BasicRNNCell(num_units=hidden_size)
outputs, _states = tf.nn.dynamic_rnn(cells, x, dtype=tf.float32)
print(outputs.shape)
# Softmax layer
w = tf.Variable(tf.random_uniform([hidden_size, 6]))
b = tf.Variable(tf.random_uniform([6]))
# y(5, 6) = x(5, hidden_size) @ w(hidden_size, 6) # w에서 x와 만나는 것은 피처의 개수. 뒤는 class의 개수
z = tf.matmul(outputs[0], w) + b
h = tf.nn.softmax(z)
loss_i = tf.nn.softmax_cross_entropy_with_logits_v2(logits=z, labels=y)
loss = tf.reduce_mean(loss_i)
optimizer = tf.train.GradientDescentOptimizer(0.1)
train = optimizer.minimize(loss)
sess = tf.Session()
sess.run(tf.global_variables_initializer())
for i in range(1000):
sess.run(train)
pred = sess.run(z)
pred_arg = np.argmax(pred, axis=1)
print(i, sess.run(loss), pred_arg, ''.join(vocab[pred_arg])) #
print('-' * 50)
sess.close()
# one-hot encoding 유지한 상태에서 길이가 같은 여러 단어의 다음 글자 prediction
# 개인적으로 y(target, true value)도 one-hot encoding 유지한 것이 더 직관적이고 좋다.
# 아래 나오는 sparse_softmax_cross_entropy_with_logits와 sequence_loss는 과도하게 복잡하고, 최종 결과도 one-hot encoding된 결과이기 때문에, 불필요한 혼란 제거를 위해서는 그냥 y도 one-hot encoding 유지하는게 좋지 않을까.
# 강사님은 왜 굳이 sparse 형태를 사용하려 하는 것일까...
import tensorflow as tf
import numpy as np
from sklearn import preprocessing
def make_onehot(words):
data = list(''.join(words))
lb = preprocessing.LabelBinarizer().fit(data)
xx, yy = [], []
for text in words:
onehot = lb.transform(list(text))
xx.append(onehot[:-1])
yy.append([onehot[i] for i in range(1,6)])
return np.float32(xx), np.int32(yy), lb.classes_
def rnn_words(words):
hidden_size = 5
x, y, vocab = make_onehot(words)
batch_size, seq_length, n_classes = x.shape
cells = tf.nn.rnn_cell.BasicRNNCell(num_units=hidden_size)
outputs, states_ = tf.nn.dynamic_rnn(cells, x, dtype=tf.float32)
z = tf.contrib.layers.fully_connected(inputs=outputs,
num_outputs=n_classes,
activation_fn=None)
h = tf.nn.softmax(z)
loss = tf.nn.softmax_cross_entropy_with_logits_v2(logits=z, labels=y)
optimizer = tf.train.AdamOptimizer(0.1)
train = optimizer.minimize(loss)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for i in range(100):
sess.run(train)
print(sess.run(loss))
result = vocab[np.argmax(sess.run(z), axis=2)]
print([''.join(x) for x in result])
rnn_words(['tensor', 'houses', 'yellow'])
# 가변길이 단어들 RNN 처리. y에 대해 one-hot encoding 유지.
import tensorflow as tf
import numpy as np
from sklearn import preprocessing
def make_onehot(words):
same_length_words = []
m = max([len(w) for w in words])
for w in words:
w += ' '*(m-len(w))
same_length_words.append(w)
data = list(''.join(words))
lb = preprocessing.LabelBinarizer().fit(data)
print(lb.classes_)
xx, yy = [], []
for w in same_length_words:
onehot = lb.transform(list(w))
print("onehot", onehot.shape)
# (10 = 최대 단어 길이 seq_length, 12개 알파벳 n_classes)
xx.append(onehot[:-1])
yy.append(onehot[1:])
return np.float32(xx), np.int32(yy), lb.classes_
def rnn_multi_words_different_length(words):
word_length = [len(w) for w in words]
x, y, vocab = make_onehot(words)
print("x.shape", x.shape, "y.shape", y.shape, "\n", vocab)
# (4, 10, 12) (4, 10, 12)
# ['a' 'c' 'e' 'f' 'h' 'l' 'n' 'o' 'r' 's' 't' 'w']
hidden_size = 8
batch_size, seq_length, n_classes = x.shape
cells = tf.nn.rnn_cell.BasicRNNCell(num_units=hidden_size)
outputs, state_ = tf.nn.dynamic_rnn(cells, x, dtype=tf.float32,
sequence_length=word_length)
z = tf.contrib.layers.fully_connected(inputs=outputs,
num_outputs=n_classes,
activation_fn=None)
print("z.shape", z.shape)
loss = tf.nn.softmax_cross_entropy_with_logits_v2(logits=z, labels=y)
optimizer = tf.train.AdamOptimizer(0.1)
train = optimizer.minimize(loss=loss)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for i in range(1000):
sess.run(train)
print(sess.run(loss))
result = ([''.join(x) for x in vocab[np.argmax(sess.run(z) , axis=2)]])
for w, l in zip(result, word_length):
print("result ", w[:l-1])
rnn_multi_words_different_length (['sea', 'what', 'corw', 'tensorflow'])
# one-hot label 제거
def rnn_2_3():
vocab = np.array(['e', 'n', 'o', 'r', 's', 't'])
# tenso
x = [[0., 0., 0., 0., 0, 1.],
[1., 0., 0., 0., 0, 0.],
[0., 1., 0., 0., 0, 0.],
[0., 0., 0., 0., 1, 0.],
[0., 0., 1., 0., 0, 0.]]
# ensor
y = [0, 1, 4, 2, 3] # Sparse 형태
hidden_size = 5 # 전개량
x = np.float32([x])
# RNN layer
cells = tf.nn.rnn_cell.BasicRNNCell(num_units=hidden_size)
outputs, _states = tf.nn.dynamic_rnn(cells, x, dtype=tf.float32)
print(outputs.shape)
# Softmax layer. Fully Connected Layer
w = tf.Variable(tf.random_uniform([hidden_size, 6]))
b = tf.Variable(tf.random_uniform([6]))
z = tf.matmul(outputs[0], w) + b
h = tf.nn.softmax(z)
# y(5, 6) = x(5, hidden_size) @ w(hidden_size, 6) # w에서 x와 만나는 것은 피처의 개수. 뒤는 class의 개수
loss_i = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=z, labels=y)
loss = tf.reduce_mean(loss_i)
optimizer = tf.train.GradientDescentOptimizer(0.1)
train = optimizer.minimize(loss)
sess = tf.Session()
sess.run(tf.global_variables_initializer())
for i in range(1000):
sess.run(train)
pred = sess.run(z)
pred_arg = np.argmax(pred, axis=1)
print(i, sess.run(loss), pred_arg, ''.join(vocab[pred_arg])) #
print('-' * 50)
sess.close()
# Day_03_07_rnn_5_different.py
import numpy as np
import tensorflow as tf
from sklearn import preprocessing
def make_onehot_different(text_array):
data = list(''.join(text_array))
lb = preprocessing.LabelBinarizer().fit(data)
max_len = max([len(t) for t in text_array])
xx, yy = [], []
for text in text_array:
if len(text) < max_len:
text += '*' * (max_len - len(text))
onehot = lb.transform(list(text))
x = onehot[:-1]
x = np.float32(x)
y = onehot[1:]
y = np.argmax(y, axis=1)
xx.append(x)
yy.append(list(y))
return np.float32(xx), tf.constant(yy), lb.classes_
def rnn_5_different(text_array, iteration=100):
x, y, vocab = make_onehot_different(text_array)
batch_size, seq_length, n_classes = x.shape
hidden_size = 2
seq_len_array = [len(t) for t in text_array]
cells = tf.nn.rnn_cell.BasicRNNCell(num_units=hidden_size)
outputs, _states = tf.nn.dynamic_rnn(cells, x, dtype=tf.float32,
sequence_length=seq_len_array)
# xavier 초기화 사용
z = tf.contrib.layers.fully_connected(inputs=outputs,
num_outputs=n_classes,
activation_fn=None)
w = tf.ones([batch_size, seq_length])
loss = tf.contrib.seq2seq.sequence_loss(logits=z, targets=y, weights=w)
optimizer = tf.train.AdamOptimizer(0.1)
train = optimizer.minimize(loss)
sess = tf.Session()
sess.run(tf.global_variables_initializer())
for i in range(iteration):
sess.run(train)
pred = sess.run(z)
pred_arg = np.argmax(pred, axis=2)
if i % 10 == 0:
print(i, end=' ')
for arg, length in zip(pred_arg, seq_len_array):
valid = vocab[arg]
print(''.join(valid[:length - 1]), end=' ')
print()
print('-' * 50)
print('ans :', text_array)
sess.close()
rnn_5_different(['tensor', 'sea', 'white'])
# Day_03_08_rnn_final.py
import numpy as np
import tensorflow as tf
from sklearn import preprocessing
def make_onehot_sentence(sentence, seq_length):
data = list(sentence)
lb = preprocessing.LabelBinarizer().fit(data)
print(lb.classes_)
onehot = lb.transform(data)
x = np.float32(onehot[:-1])
y = np.argmax(onehot[1:], axis=1)
# print(len(sentence))
# for i in range(len(sentence) - seq_length):
# print(i, i + seq_length, sentence[i:i+seq_length])
idx = [(i, i + seq_length) for i in range(len(sentence) - seq_length)]
xx = [x[s:e] for s, e in idx]
yy = [y[s:e] for s, e in idx]
print(*yy[:5], sep='\n')
return np.float32(xx), tf.constant(np.int32(yy)), lb.classes_
def rnn_final(sentence, iteration=100):
x, y, vocab = make_onehot_sentence(sentence, 20)
batch_size, seq_length, n_classes = x.shape
hidden_size = 7
cells = [tf.nn.rnn_cell.BasicRNNCell(num_units=hidden_size) for _ in range(2)]
multi = tf.nn.rnn_cell.MultiRNNCell(cells)
outputs, _states = tf.nn.dynamic_rnn(multi, x, dtype=tf.float32)
# xavier 초기화 사용
z = tf.contrib.layers.fully_connected(inputs=outputs,
num_outputs=n_classes,
activation_fn=None)
w = tf.ones([batch_size, seq_length])
loss = tf.contrib.seq2seq.sequence_loss(logits=z, targets=y, weights=w)
optimizer = tf.train.AdamOptimizer(0.1)
train = optimizer.minimize(loss)
sess = tf.Session()
sess.run(tf.global_variables_initializer())
for i in range(iteration):
sess.run(train)
print(i, sess.run(loss))
print('-' * 50)
pred = sess.run(z)
print(pred.shape) # (151, 20, 25)
pred_arg = np.argmax(pred, axis=2)
print(pred_arg.shape) # (151, 20)
total = '*' + ''.join(vocab[pred_arg[0]])
for arg in pred_arg[1:]:
curr = ''.join(vocab[arg])
# print(curr)
total += curr[-1]
print('[{}]'.format(total))
print('[{}]'.format(sentence))
sess.close()
sentence = ("if you want to build a ship, "
"don't drum up people to collect wood and "
"don't assign them tasks and work, "
"but rather teach them to long for the endless immensity of the sea.")
rnn_final(sentence, 1000)
# if you want to build a ship
# ---------------
# 0 : if you want
# 1 : f you want
# 2 : you want t
# 3 : you want to
#
# 0 : if you want
# 1 : t to build
# Day_03_10_stock.py
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
from sklearn import preprocessing, model_selection
np.set_printoptions(linewidth=1000)
def minmax_scale(data):
mx = np.max(data, axis=0)
mn = np.min(data, axis=0)
return (data - mn) / (mx - mn + 1e-7)
def rnn_stock_1():
stock = np.loadtxt('Data/stock_daily.csv',
delimiter=',', dtype=np.float32)
stock = stock[::-1]
# stock = minmax_scale(stock)
# stock = preprocessing.minmax_scale(stock)
scaler = preprocessing.MinMaxScaler().fit(stock)
stock = scaler.transform(stock)
print(stock[:3])
print(stock.shape, stock.dtype) # (732, 5) float32
x = stock
y = stock[:, -1:]
print(y.shape) # (732, 1)
seq_length = 7
n_features = 5 # n_classes
output_dim = 1
hidden_size = 10
x_data, y_data = [], []
for i in range(len(y) - seq_length):
x_data.append(x[i:i+seq_length]) # 0~6, 1~7
y_data.append(y[i+seq_length]) # 7 , 8
# print(x_data[-1], '->', y_data[-1])
x_data = np.float32(x_data)
y_data = np.float32(y_data)
print("SHAPE:", x_data.shape, y_data.shape)
cells = tf.nn.rnn_cell.BasicRNNCell(num_units=hidden_size, activation=tf.tanh)
outputs, _states = tf.nn.dynamic_rnn(cells, x_data, dtype=tf.float32)
print(outputs.shape, outputs[:, -1].shape)
z = tf.contrib.layers.fully_connected(inputs=outputs[:, -1],
num_outputs=output_dim,
activation_fn=None)
loss = tf.reduce_mean((z - y_data) ** 2)
optimizer = tf.train.AdamOptimizer(0.1)
train = optimizer.minimize(loss)
sess = tf.Session()
sess.run(tf.global_variables_initializer())
for i in range(100):
sess.run(train)
print(i, sess.run(loss))
def rnn_stock_2():
stock = np.loadtxt('Data/stock_daily.csv',
delimiter=',', dtype=np.float32)
stock = stock[::-1]
# stock = minmax_scale(stock)
# stock = preprocessing.minmax_scale(stock)
scaler = preprocessing.MinMaxScaler().fit(stock)
stock = scaler.transform(stock)
print(stock[:3])
print(stock.shape, stock.dtype) # (732, 5) float32
x = stock
y = stock[:, -1:]
print(y.shape) # (732, 1)
seq_length = 7
n_features = 5 # n_classes
output_dim = 1
hidden_size = 10
x_data, y_data = [], []
for i in range(len(y) - seq_length):
x_data.append(x[i:i + seq_length]) # 0~6, 1~7
y_data.append(y[i + seq_length]) # 7 , 8
# print(x_data[-1], '->', y_data[-1])
x_data = np.float32(x_data)
y_data = np.float32(y_data)
# 75:25
data = model_selection.train_test_split(x_data, y_data, shuffle=False)
x_train, x_test, y_train, y_test = data
x_holder = tf.placeholder(tf.float32, [None, seq_length, n_features])
cells = tf.nn.rnn_cell.BasicRNNCell(num_units=hidden_size, activation=tf.tanh)
outputs, _states = tf.nn.dynamic_rnn(cells, x_holder, dtype=tf.float32)
z = tf.contrib.layers.fully_connected(inputs=outputs[:, -1],
num_outputs=output_dim,
activation_fn=None)
loss = tf.reduce_mean((z - y_train) ** 2)
optimizer = tf.train.AdamOptimizer(0.1)
train = optimizer.minimize(loss)
sess = tf.Session()
sess.run(tf.global_variables_initializer())
for i in range(100):
sess.run(train, {x_holder: x_train})
print(i, sess.run(loss, {x_holder: x_train}))
pred = sess.run(z, {x_holder: x_test})
plt.plot(y_test, 'r')
plt.plot(pred, 'g')
plt.show()
# rnn_stock_1()
rnn_stock_1()
print('\n\n\n\n\n\n\n')
import numpy as np
def rnn_1():
vocab = np.array(['e', 'n', 'o', 'r', 's', 't'])
x = [[0., 0., 0., 0., 0., 1.], # t
[1., 0., 0., 0., 0., 0.], # e
[0., 1., 0., 0., 0., 0.], # n
[0., 0., 0., 0., 1., 0.], # s
[0., 0., 1., 0., 0., 0.]] # o
y = [[1, 0, 0, 0, 0, 0], # e
[0, 1, 0, 0, 0, 0], # n
[0, 0, 0, 0, 1, 0], # s
[0, 0, 1, 0, 0, 0], # o
[0, 0, 0, 1, 0, 0]] # r
w = tf.Variable(tf.random_uniform([6,6]))
b = tf.Variable(tf.random_uniform([6]))
# (5,6) = (5,6) @ (6, 6)
z = tf.matmul(x, w) + b
h = tf.nn.softmax(z)
loss = tf.nn.softmax_cross_entropy_with_logits_v2(logits=z, labels=y)
optimizer = tf.train.GradientDescentOptimizer(0.1)
train = optimizer.minimize(loss=loss)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for i in range(1000):
sess.run(train)
print(sess.run(loss))
print(sess.run(h))
print(np.argmax(sess.run(h), axis=1 ))
print(vocab[np.argmax(sess.run(h), axis=1)])
rnn_1()
def rnn_2_2():
vocab = np.array(['e', 'n', 'o', 'r', 's', 't'])
# tenso
x = [[0., 0., 0., 0., 0, 1.],
[1., 0., 0., 0., 0, 0.],
[0., 1., 0., 0., 0, 0.],
[0., 0., 0., 0., 1, 0.],
[0., 0., 1., 0., 0, 0.]]
# ensor
y = [[1., 0., 0., 0., 0., 0.], # 클래스 6개. Dense 형태
[0., 1., 0., 0., 0., 0.],
[0., 0., 0., 0., 1., 0.],
[0., 0., 1., 0., 0., 0.],
[0., 0., 0., 1., 0., 0.]]
hidden_size = 2 # 전개량
x = np.float32([x]) # dynamic_rnn은 3차원 데이터를 입력으로 요구 함. x: 2차원 -> 3차원
# RNN layer
cells = tf.nn.rnn_cell.BasicRNNCell(num_units=hidden_size)
outputs, _states = tf.nn.dynamic_rnn(cells, x, dtype=tf.float32)
print(outputs.shape)
# Softmax layer
w = tf.Variable(tf.random_uniform([hidden_size, 6]))
b = tf.Variable(tf.random_uniform([6]))
# y(5, 6) = x(5, hidden_size) @ w(hidden_size, 6) # w에서 x와 만나는 것은 피처의 개수. 뒤는 class의 개수
z = tf.matmul(outputs[0], w) + b
h = tf.nn.softmax(z)
loss_i = tf.nn.softmax_cross_entropy_with_logits_v2(logits=z, labels=y)
loss = tf.reduce_mean(loss_i)
optimizer = tf.train.GradientDescentOptimizer(0.1)
train = optimizer.minimize(loss)
sess = tf.Session()
sess.run(tf.global_variables_initializer())
for i in range(1000):
sess.run(train)
pred = sess.run(z)
pred_arg = np.argmax(pred, axis=1)
print(i, sess.run(loss), pred_arg, ''.join(vocab[pred_arg])) #
print('-' * 50)
sess.close()
# one-hot encoding 유지한 상태에서 길이가 같은 여러 단어의 다음 글자 prediction
# 개인적으로 y(target, true value)도 one-hot encoding 유지한 것이 더 직관적이고 좋다.
# 아래 나오는 sparse_softmax_cross_entropy_with_logits와 sequence_loss는 과도하게 복잡하고, 최종 결과도 one-hot encoding된 결과이기 때문에, 불필요한 혼란 제거를 위해서는 그냥 y도 one-hot encoding 유지하는게 좋지 않을까.
# 강사님은 왜 굳이 sparse 형태를 사용하려 하는 것일까...
import tensorflow as tf
import numpy as np
from sklearn import preprocessing
def make_onehot(words):
data = list(''.join(words))
lb = preprocessing.LabelBinarizer().fit(data)
xx, yy = [], []
for text in words:
onehot = lb.transform(list(text))
xx.append(onehot[:-1])
yy.append([onehot[i] for i in range(1,6)])
return np.float32(xx), np.int32(yy), lb.classes_
def rnn_words(words):
hidden_size = 5
x, y, vocab = make_onehot(words)
batch_size, seq_length, n_classes = x.shape
cells = tf.nn.rnn_cell.BasicRNNCell(num_units=hidden_size)
outputs, states_ = tf.nn.dynamic_rnn(cells, x, dtype=tf.float32)
z = tf.contrib.layers.fully_connected(inputs=outputs,
num_outputs=n_classes,
activation_fn=None)
h = tf.nn.softmax(z)
loss = tf.nn.softmax_cross_entropy_with_logits_v2(logits=z, labels=y)
optimizer = tf.train.AdamOptimizer(0.1)
train = optimizer.minimize(loss)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for i in range(100):
sess.run(train)
print(sess.run(loss))
result = vocab[np.argmax(sess.run(z), axis=2)]
print([''.join(x) for x in result])
rnn_words(['tensor', 'houses', 'yellow'])
# 가변길이 단어들 RNN 처리. y에 대해 one-hot encoding 유지.
import tensorflow as tf
import numpy as np
from sklearn import preprocessing
def make_onehot(words):
same_length_words = []
m = max([len(w) for w in words])
for w in words:
w += ' '*(m-len(w))
same_length_words.append(w)
data = list(''.join(words))
lb = preprocessing.LabelBinarizer().fit(data)
print(lb.classes_)
xx, yy = [], []
for w in same_length_words:
onehot = lb.transform(list(w))
print("onehot", onehot.shape)
# (10 = 최대 단어 길이 seq_length, 12개 알파벳 n_classes)
xx.append(onehot[:-1])
yy.append(onehot[1:])
return np.float32(xx), np.int32(yy), lb.classes_
def rnn_multi_words_different_length(words):
word_length = [len(w) for w in words]
x, y, vocab = make_onehot(words)
print("x.shape", x.shape, "y.shape", y.shape, "\n", vocab)
# (4, 10, 12) (4, 10, 12)
# ['a' 'c' 'e' 'f' 'h' 'l' 'n' 'o' 'r' 's' 't' 'w']
hidden_size = 8
batch_size, seq_length, n_classes = x.shape
cells = tf.nn.rnn_cell.BasicRNNCell(num_units=hidden_size)
outputs, state_ = tf.nn.dynamic_rnn(cells, x, dtype=tf.float32,
sequence_length=word_length)
z = tf.contrib.layers.fully_connected(inputs=outputs,
num_outputs=n_classes,
activation_fn=None)
print("z.shape", z.shape)
loss = tf.nn.softmax_cross_entropy_with_logits_v2(logits=z, labels=y)
optimizer = tf.train.AdamOptimizer(0.1)
train = optimizer.minimize(loss=loss)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for i in range(1000):
sess.run(train)
print(sess.run(loss))
result = ([''.join(x) for x in vocab[np.argmax(sess.run(z) , axis=2)]])
for w, l in zip(result, word_length):
print("result ", w[:l-1])
rnn_multi_words_different_length (['sea', 'what', 'corw', 'tensorflow'])
# one-hot label 제거
def rnn_2_3():
vocab = np.array(['e', 'n', 'o', 'r', 's', 't'])
# tenso
x = [[0., 0., 0., 0., 0, 1.],
[1., 0., 0., 0., 0, 0.],
[0., 1., 0., 0., 0, 0.],
[0., 0., 0., 0., 1, 0.],
[0., 0., 1., 0., 0, 0.]]
# ensor
y = [0, 1, 4, 2, 3] # Sparse 형태
hidden_size = 5 # 전개량
x = np.float32([x])
# RNN layer
cells = tf.nn.rnn_cell.BasicRNNCell(num_units=hidden_size)
outputs, _states = tf.nn.dynamic_rnn(cells, x, dtype=tf.float32)
print(outputs.shape)
# Softmax layer. Fully Connected Layer
w = tf.Variable(tf.random_uniform([hidden_size, 6]))
b = tf.Variable(tf.random_uniform([6]))
z = tf.matmul(outputs[0], w) + b
h = tf.nn.softmax(z)
# y(5, 6) = x(5, hidden_size) @ w(hidden_size, 6) # w에서 x와 만나는 것은 피처의 개수. 뒤는 class의 개수
loss_i = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=z, labels=y)
loss = tf.reduce_mean(loss_i)
optimizer = tf.train.GradientDescentOptimizer(0.1)
train = optimizer.minimize(loss)
sess = tf.Session()
sess.run(tf.global_variables_initializer())
for i in range(1000):
sess.run(train)
pred = sess.run(z)
pred_arg = np.argmax(pred, axis=1)
print(i, sess.run(loss), pred_arg, ''.join(vocab[pred_arg])) #
print('-' * 50)
sess.close()
# Day_03_07_rnn_5_different.py
import numpy as np
import tensorflow as tf
from sklearn import preprocessing
def make_onehot_different(text_array):
data = list(''.join(text_array))
lb = preprocessing.LabelBinarizer().fit(data)
max_len = max([len(t) for t in text_array])
xx, yy = [], []
for text in text_array:
if len(text) < max_len:
text += '*' * (max_len - len(text))
onehot = lb.transform(list(text))
x = onehot[:-1]
x = np.float32(x)
y = onehot[1:]
y = np.argmax(y, axis=1)
xx.append(x)
yy.append(list(y))
return np.float32(xx), tf.constant(yy), lb.classes_
def rnn_5_different(text_array, iteration=100):
x, y, vocab = make_onehot_different(text_array)
batch_size, seq_length, n_classes = x.shape
hidden_size = 2
seq_len_array = [len(t) for t in text_array]
cells = tf.nn.rnn_cell.BasicRNNCell(num_units=hidden_size)
outputs, _states = tf.nn.dynamic_rnn(cells, x, dtype=tf.float32,
sequence_length=seq_len_array)
# xavier 초기화 사용
z = tf.contrib.layers.fully_connected(inputs=outputs,
num_outputs=n_classes,
activation_fn=None)
w = tf.ones([batch_size, seq_length])
loss = tf.contrib.seq2seq.sequence_loss(logits=z, targets=y, weights=w)
optimizer = tf.train.AdamOptimizer(0.1)
train = optimizer.minimize(loss)
sess = tf.Session()
sess.run(tf.global_variables_initializer())
for i in range(iteration):
sess.run(train)
pred = sess.run(z)
pred_arg = np.argmax(pred, axis=2)
if i % 10 == 0:
print(i, end=' ')
for arg, length in zip(pred_arg, seq_len_array):
valid = vocab[arg]
print(''.join(valid[:length - 1]), end=' ')
print()
print('-' * 50)
print('ans :', text_array)
sess.close()
rnn_5_different(['tensor', 'sea', 'white'])
# Day_03_08_rnn_final.py
import numpy as np
import tensorflow as tf
from sklearn import preprocessing
def make_onehot_sentence(sentence, seq_length):
data = list(sentence)
lb = preprocessing.LabelBinarizer().fit(data)
print(lb.classes_)
onehot = lb.transform(data)
x = np.float32(onehot[:-1])
y = np.argmax(onehot[1:], axis=1)
# print(len(sentence))
# for i in range(len(sentence) - seq_length):
# print(i, i + seq_length, sentence[i:i+seq_length])
idx = [(i, i + seq_length) for i in range(len(sentence) - seq_length)]
xx = [x[s:e] for s, e in idx]
yy = [y[s:e] for s, e in idx]
print(*yy[:5], sep='\n')
return np.float32(xx), tf.constant(np.int32(yy)), lb.classes_
def rnn_final(sentence, iteration=100):
x, y, vocab = make_onehot_sentence(sentence, 20)
batch_size, seq_length, n_classes = x.shape
hidden_size = 7
cells = [tf.nn.rnn_cell.BasicRNNCell(num_units=hidden_size) for _ in range(2)]
multi = tf.nn.rnn_cell.MultiRNNCell(cells)
outputs, _states = tf.nn.dynamic_rnn(multi, x, dtype=tf.float32)
# xavier 초기화 사용
z = tf.contrib.layers.fully_connected(inputs=outputs,
num_outputs=n_classes,
activation_fn=None)
w = tf.ones([batch_size, seq_length])
loss = tf.contrib.seq2seq.sequence_loss(logits=z, targets=y, weights=w)
optimizer = tf.train.AdamOptimizer(0.1)
train = optimizer.minimize(loss)
sess = tf.Session()
sess.run(tf.global_variables_initializer())
for i in range(iteration):
sess.run(train)
print(i, sess.run(loss))
print('-' * 50)
pred = sess.run(z)
print(pred.shape) # (151, 20, 25)
pred_arg = np.argmax(pred, axis=2)
print(pred_arg.shape) # (151, 20)
total = '*' + ''.join(vocab[pred_arg[0]])
for arg in pred_arg[1:]:
curr = ''.join(vocab[arg])
# print(curr)
total += curr[-1]
print('[{}]'.format(total))
print('[{}]'.format(sentence))
sess.close()
sentence = ("if you want to build a ship, "
"don't drum up people to collect wood and "
"don't assign them tasks and work, "
"but rather teach them to long for the endless immensity of the sea.")
rnn_final(sentence, 1000)
# if you want to build a ship
# ---------------
# 0 : if you want
# 1 : f you want
# 2 : you want t
# 3 : you want to
#
# 0 : if you want
# 1 : t to build
# Day_03_10_stock.py
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
from sklearn import preprocessing, model_selection
np.set_printoptions(linewidth=1000)
def minmax_scale(data):
mx = np.max(data, axis=0)
mn = np.min(data, axis=0)
return (data - mn) / (mx - mn + 1e-7)
def rnn_stock_1():
stock = np.loadtxt('Data/stock_daily.csv',
delimiter=',', dtype=np.float32)
stock = stock[::-1]
# stock = minmax_scale(stock)
# stock = preprocessing.minmax_scale(stock)
scaler = preprocessing.MinMaxScaler().fit(stock)
stock = scaler.transform(stock)
print(stock[:3])
print(stock.shape, stock.dtype) # (732, 5) float32
x = stock
y = stock[:, -1:]
print(y.shape) # (732, 1)
seq_length = 7
n_features = 5 # n_classes
output_dim = 1
hidden_size = 10
x_data, y_data = [], []
for i in range(len(y) - seq_length):
x_data.append(x[i:i+seq_length]) # 0~6, 1~7
y_data.append(y[i+seq_length]) # 7 , 8
# print(x_data[-1], '->', y_data[-1])
x_data = np.float32(x_data)
y_data = np.float32(y_data)
print("SHAPE:", x_data.shape, y_data.shape)
cells = tf.nn.rnn_cell.BasicRNNCell(num_units=hidden_size, activation=tf.tanh)
outputs, _states = tf.nn.dynamic_rnn(cells, x_data, dtype=tf.float32)
print(outputs.shape, outputs[:, -1].shape)
z = tf.contrib.layers.fully_connected(inputs=outputs[:, -1],
num_outputs=output_dim,
activation_fn=None)
loss = tf.reduce_mean((z - y_data) ** 2)
optimizer = tf.train.AdamOptimizer(0.1)
train = optimizer.minimize(loss)
sess = tf.Session()
sess.run(tf.global_variables_initializer())
for i in range(100):
sess.run(train)
print(i, sess.run(loss))
def rnn_stock_2():
stock = np.loadtxt('Data/stock_daily.csv',
delimiter=',', dtype=np.float32)
stock = stock[::-1]
# stock = minmax_scale(stock)
# stock = preprocessing.minmax_scale(stock)
scaler = preprocessing.MinMaxScaler().fit(stock)
stock = scaler.transform(stock)
print(stock[:3])
print(stock.shape, stock.dtype) # (732, 5) float32
x = stock
y = stock[:, -1:]
print(y.shape) # (732, 1)
seq_length = 7
n_features = 5 # n_classes
output_dim = 1
hidden_size = 10
x_data, y_data = [], []
for i in range(len(y) - seq_length):
x_data.append(x[i:i + seq_length]) # 0~6, 1~7
y_data.append(y[i + seq_length]) # 7 , 8
# print(x_data[-1], '->', y_data[-1])
x_data = np.float32(x_data)
y_data = np.float32(y_data)
# 75:25
data = model_selection.train_test_split(x_data, y_data, shuffle=False)
x_train, x_test, y_train, y_test = data
x_holder = tf.placeholder(tf.float32, [None, seq_length, n_features])
cells = tf.nn.rnn_cell.BasicRNNCell(num_units=hidden_size, activation=tf.tanh)
outputs, _states = tf.nn.dynamic_rnn(cells, x_holder, dtype=tf.float32)
z = tf.contrib.layers.fully_connected(inputs=outputs[:, -1],
num_outputs=output_dim,
activation_fn=None)
loss = tf.reduce_mean((z - y_train) ** 2)
optimizer = tf.train.AdamOptimizer(0.1)
train = optimizer.minimize(loss)
sess = tf.Session()
sess.run(tf.global_variables_initializer())
for i in range(100):
sess.run(train, {x_holder: x_train})
print(i, sess.run(loss, {x_holder: x_train}))
pred = sess.run(z, {x_holder: x_test})
plt.plot(y_test, 'r')
plt.plot(pred, 'g')
plt.show()
# rnn_stock_1()
rnn_stock_1()
print('\n\n\n\n\n\n\n')
댓글 없음:
댓글 쓰기