In [1]:
import tensorflow as tf
import numpy as np

In [2]:
char_rdic = ['h', 'e', 'l', 'o'] #voca
char_dic = {w:i for i,w in enumerate(char_rdic)}

In [3]:
char_dic


Out[3]:
{'e': 1, 'h': 0, 'l': 2, 'o': 3}

In [4]:
ground_truth = [char_dic[c] for c in 'hello'] #y_data
#ground_truth = [char_dic[c] for c in char_rdic]

In [5]:
ground_truth


Out[5]:
[0, 1, 2, 2, 3]

In [6]:
# word = 'hell'
word = char_rdic[:]

In [7]:
# x_data = np.zeros((len(word), len(char_dic)), dtype='f')

# for i,c in enumerate(word):
#     x_data[i][char_dic[c]] = 1

# x_data

x_data = tf.one_hot(ground_truth[:-1], len(char_dic), 
                    1.0, 0.0, -1)
x_data


Out[7]:
<tf.Tensor 'one_hot:0' shape=(4, 4) dtype=float32>

In [8]:
#Configuration

char_vocab_size = len(char_dic)
rnn_size = char_vocab_size
time_step_size = len(word)
batch_size = 1

In [9]:
# RNN Model

rnn_cell = tf.nn.rnn_cell.BasicRNNCell(rnn_size)
state = tf.zeros([batch_size, rnn_cell.state_size])
X_split = tf.split(0, time_step_size, x_data)
outputs, state = tf.nn.rnn(rnn_cell, X_split, state)

In [10]:
print(outputs)
print(state)


[<tf.Tensor 'RNN/BasicRNNCell/Tanh:0' shape=(1, 4) dtype=float32>, <tf.Tensor 'RNN/BasicRNNCell_1/Tanh:0' shape=(1, 4) dtype=float32>, <tf.Tensor 'RNN/BasicRNNCell_2/Tanh:0' shape=(1, 4) dtype=float32>, <tf.Tensor 'RNN/BasicRNNCell_3/Tanh:0' shape=(1, 4) dtype=float32>]
Tensor("RNN/BasicRNNCell_3/Tanh:0", shape=(1, 4), dtype=float32)

In [11]:
logits = tf.reshape(tf.concat(1, outputs), [-1, rnn_size])
targets = tf.reshape(ground_truth[1:], [-1])
weights = tf.ones(time_step_size*batch_size)

In [14]:
loss = tf.nn.seq2seq.sequence_loss_by_example(
        [logits], [targets], [weights])
cost = tf.reduce_sum(loss) / batch_size
train_op = tf.train.AdamOptimizer(0.01, 0.9).minimize(cost)

In [15]:
#Launch the graph in session

with tf.Session() as sess:
    init = tf.global_variables_initializer()
    sess.run(init)
    for i in range(100):
        sess.run(train_op)
        result = sess.run(tf.argmax(logits, 1))
        print(result, [char_rdic[t] for t in result])


[3 0 2 1] ['o', 'h', 'l', 'e']
[3 0 2 1] ['o', 'h', 'l', 'e']
[3 0 2 1] ['o', 'h', 'l', 'e']
[3 0 2 1] ['o', 'h', 'l', 'e']
[3 0 2 1] ['o', 'h', 'l', 'e']
[3 0 2 1] ['o', 'h', 'l', 'e']
[3 0 2 1] ['o', 'h', 'l', 'e']
[3 0 2 1] ['o', 'h', 'l', 'e']
[3 0 2 1] ['o', 'h', 'l', 'e']
[3 0 2 1] ['o', 'h', 'l', 'e']
[3 0 2 1] ['o', 'h', 'l', 'e']
[3 0 2 1] ['o', 'h', 'l', 'e']
[3 0 2 1] ['o', 'h', 'l', 'e']
[3 0 2 1] ['o', 'h', 'l', 'e']
[3 0 2 1] ['o', 'h', 'l', 'e']
[3 0 2 1] ['o', 'h', 'l', 'e']
[3 0 2 3] ['o', 'h', 'l', 'o']
[3 2 2 3] ['o', 'l', 'l', 'o']
[1 2 2 3] ['e', 'l', 'l', 'o']
[1 2 2 3] ['e', 'l', 'l', 'o']
[1 2 2 3] ['e', 'l', 'l', 'o']
[1 2 2 3] ['e', 'l', 'l', 'o']
[1 2 2 3] ['e', 'l', 'l', 'o']
[1 2 2 3] ['e', 'l', 'l', 'o']
[1 2 2 3] ['e', 'l', 'l', 'o']
[1 2 2 3] ['e', 'l', 'l', 'o']
[1 2 2 3] ['e', 'l', 'l', 'o']
[1 2 2 3] ['e', 'l', 'l', 'o']
[1 2 2 3] ['e', 'l', 'l', 'o']
[1 2 2 3] ['e', 'l', 'l', 'o']
[1 2 2 3] ['e', 'l', 'l', 'o']
[1 2 2 3] ['e', 'l', 'l', 'o']
[1 2 2 3] ['e', 'l', 'l', 'o']
[1 2 2 3] ['e', 'l', 'l', 'o']
[1 2 2 3] ['e', 'l', 'l', 'o']
[1 2 2 3] ['e', 'l', 'l', 'o']
[1 2 2 3] ['e', 'l', 'l', 'o']
[1 2 2 3] ['e', 'l', 'l', 'o']
[1 2 2 3] ['e', 'l', 'l', 'o']
[1 2 2 3] ['e', 'l', 'l', 'o']
[1 2 2 3] ['e', 'l', 'l', 'o']
[1 2 2 3] ['e', 'l', 'l', 'o']
[1 2 2 3] ['e', 'l', 'l', 'o']
[1 2 2 3] ['e', 'l', 'l', 'o']
[1 2 2 3] ['e', 'l', 'l', 'o']
[1 2 2 3] ['e', 'l', 'l', 'o']
[1 2 2 3] ['e', 'l', 'l', 'o']
[1 2 2 3] ['e', 'l', 'l', 'o']
[1 2 2 3] ['e', 'l', 'l', 'o']
[1 2 2 3] ['e', 'l', 'l', 'o']
[1 2 2 3] ['e', 'l', 'l', 'o']
[1 2 2 3] ['e', 'l', 'l', 'o']
[1 2 2 3] ['e', 'l', 'l', 'o']
[1 2 2 3] ['e', 'l', 'l', 'o']
[1 2 2 3] ['e', 'l', 'l', 'o']
[1 2 2 3] ['e', 'l', 'l', 'o']
[1 2 2 3] ['e', 'l', 'l', 'o']
[1 2 2 3] ['e', 'l', 'l', 'o']
[1 2 2 3] ['e', 'l', 'l', 'o']
[1 2 2 3] ['e', 'l', 'l', 'o']
[1 2 2 3] ['e', 'l', 'l', 'o']
[1 2 2 3] ['e', 'l', 'l', 'o']
[1 2 2 3] ['e', 'l', 'l', 'o']
[1 2 2 3] ['e', 'l', 'l', 'o']
[1 2 2 3] ['e', 'l', 'l', 'o']
[1 2 2 3] ['e', 'l', 'l', 'o']
[1 2 2 3] ['e', 'l', 'l', 'o']
[1 2 2 3] ['e', 'l', 'l', 'o']
[1 2 2 3] ['e', 'l', 'l', 'o']
[1 2 2 3] ['e', 'l', 'l', 'o']
[1 2 2 3] ['e', 'l', 'l', 'o']
[1 2 2 3] ['e', 'l', 'l', 'o']
[1 2 2 3] ['e', 'l', 'l', 'o']
[1 2 2 3] ['e', 'l', 'l', 'o']
[1 2 2 3] ['e', 'l', 'l', 'o']
[1 2 2 3] ['e', 'l', 'l', 'o']
[1 2 2 3] ['e', 'l', 'l', 'o']
[1 2 2 3] ['e', 'l', 'l', 'o']
[1 2 2 3] ['e', 'l', 'l', 'o']
[1 2 2 3] ['e', 'l', 'l', 'o']
[1 2 2 3] ['e', 'l', 'l', 'o']
[1 2 2 3] ['e', 'l', 'l', 'o']
[1 2 2 3] ['e', 'l', 'l', 'o']
[1 2 2 3] ['e', 'l', 'l', 'o']
[1 2 2 3] ['e', 'l', 'l', 'o']
[1 2 2 3] ['e', 'l', 'l', 'o']
[1 2 2 3] ['e', 'l', 'l', 'o']
[1 2 2 3] ['e', 'l', 'l', 'o']
[1 2 2 3] ['e', 'l', 'l', 'o']
[1 2 2 3] ['e', 'l', 'l', 'o']
[1 2 2 3] ['e', 'l', 'l', 'o']
[1 2 2 3] ['e', 'l', 'l', 'o']
[1 2 2 3] ['e', 'l', 'l', 'o']
[1 2 2 3] ['e', 'l', 'l', 'o']
[1 2 2 3] ['e', 'l', 'l', 'o']
[1 2 2 3] ['e', 'l', 'l', 'o']
[1 2 2 3] ['e', 'l', 'l', 'o']
[1 2 2 3] ['e', 'l', 'l', 'o']
[1 2 2 3] ['e', 'l', 'l', 'o']
[1 2 2 3] ['e', 'l', 'l', 'o']

In [ ]: