Skip to content

Commit dd29441

Browse files
committed
origin source code
1 parent 661f3e0 commit dd29441

6 files changed

+494
-0
lines changed

origin_source/lab-12-1-hello-rnn.py

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# Lab 12 RNN
2+
import tensorflow as tf
3+
import numpy as np
4+
tf.set_random_seed(777) # reproducibility
5+
6+
idx2char = ['h', 'i', 'e', 'l', 'o']
7+
# Teach hello: hihell -> ihello
8+
x_data = [[0, 1, 0, 2, 3, 3]] # hihell
9+
x_one_hot = [[[1, 0, 0, 0, 0], # h 0
10+
[0, 1, 0, 0, 0], # i 1
11+
[1, 0, 0, 0, 0], # h 0
12+
[0, 0, 1, 0, 0], # e 2
13+
[0, 0, 0, 1, 0], # l 3
14+
[0, 0, 0, 1, 0]]] # l 3
15+
16+
y_data = [[1, 0, 2, 3, 3, 4]] # ihello
17+
18+
num_classes = 5
19+
input_dim = 5 # one-hot size
20+
hidden_size = 5 # output from the LSTM. 5 to directly predict one-hot
21+
batch_size = 1 # one sentence
22+
sequence_length = 6 # |ihello| == 6
23+
learning_rate = 0.1
24+
25+
X = tf.placeholder(
26+
tf.float32, [None, sequence_length, input_dim]) # X one-hot
27+
Y = tf.placeholder(tf.int32, [None, sequence_length]) # Y label
28+
29+
cell = tf.contrib.rnn.BasicLSTMCell(num_units=hidden_size, state_is_tuple=True)
30+
initial_state = cell.zero_state(batch_size, tf.float32)
31+
outputs, _states = tf.nn.dynamic_rnn(
32+
cell, X, initial_state=initial_state, dtype=tf.float32)
33+
34+
# FC layer
35+
X_for_fc = tf.reshape(outputs, [-1, hidden_size])
36+
# fc_w = tf.get_variable("fc_w", [hidden_size, num_classes])
37+
# fc_b = tf.get_variable("fc_b", [num_classes])
38+
# outputs = tf.matmul(X_for_fc, fc_w) + fc_b
39+
outputs = tf.contrib.layers.fully_connected(
40+
inputs=X_for_fc, num_outputs=num_classes, activation_fn=None)
41+
42+
# reshape out for sequence_loss
43+
outputs = tf.reshape(outputs, [batch_size, sequence_length, num_classes])
44+
45+
weights = tf.ones([batch_size, sequence_length])
46+
sequence_loss = tf.contrib.seq2seq.sequence_loss(
47+
logits=outputs, targets=Y, weights=weights)
48+
loss = tf.reduce_mean(sequence_loss)
49+
train = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(loss)
50+
51+
prediction = tf.argmax(outputs, axis=2)
52+
53+
with tf.Session() as sess:
54+
sess.run(tf.global_variables_initializer())
55+
for i in range(50):
56+
l, _ = sess.run([loss, train], feed_dict={X: x_one_hot, Y: y_data})
57+
result = sess.run(prediction, feed_dict={X: x_one_hot})
58+
print(i, "loss:", l, "prediction: ", result, "true Y: ", y_data)
59+
60+
# print char using dic
61+
result_str = [idx2char[c] for c in np.squeeze(result)]
62+
print("\tPrediction str: ", ''.join(result_str))
63+
64+
'''
65+
0 loss: 1.71584 prediction: [[2 2 2 3 3 2]] true Y: [[1, 0, 2, 3, 3, 4]]
66+
Prediction str: eeelle
67+
1 loss: 1.56447 prediction: [[3 3 3 3 3 3]] true Y: [[1, 0, 2, 3, 3, 4]]
68+
Prediction str: llllll
69+
2 loss: 1.46284 prediction: [[3 3 3 3 3 3]] true Y: [[1, 0, 2, 3, 3, 4]]
70+
Prediction str: llllll
71+
3 loss: 1.38073 prediction: [[3 3 3 3 3 3]] true Y: [[1, 0, 2, 3, 3, 4]]
72+
Prediction str: llllll
73+
4 loss: 1.30603 prediction: [[3 3 3 3 3 3]] true Y: [[1, 0, 2, 3, 3, 4]]
74+
Prediction str: llllll
75+
5 loss: 1.21498 prediction: [[3 3 3 3 3 3]] true Y: [[1, 0, 2, 3, 3, 4]]
76+
Prediction str: llllll
77+
6 loss: 1.1029 prediction: [[3 0 3 3 3 4]] true Y: [[1, 0, 2, 3, 3, 4]]
78+
Prediction str: lhlllo
79+
7 loss: 0.982386 prediction: [[1 0 3 3 3 4]] true Y: [[1, 0, 2, 3, 3, 4]]
80+
Prediction str: ihlllo
81+
8 loss: 0.871259 prediction: [[1 0 3 3 3 4]] true Y: [[1, 0, 2, 3, 3, 4]]
82+
Prediction str: ihlllo
83+
9 loss: 0.774338 prediction: [[1 0 2 3 3 4]] true Y: [[1, 0, 2, 3, 3, 4]]
84+
Prediction str: ihello
85+
10 loss: 0.676005 prediction: [[1 0 2 3 3 4]] true Y: [[1, 0, 2, 3, 3, 4]]
86+
Prediction str: ihello
87+
88+
...
89+
90+
'''
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"### Lab 12-2 char seq rnn"
8+
]
9+
},
10+
{
11+
"cell_type": "code",
12+
"execution_count": null,
13+
"metadata": {},
14+
"outputs": [],
15+
"source": []
16+
}
17+
],
18+
"metadata": {
19+
"kernelspec": {
20+
"display_name": "Python 3",
21+
"language": "python",
22+
"name": "python3"
23+
},
24+
"language_info": {
25+
"codemirror_mode": {
26+
"name": "ipython",
27+
"version": 3
28+
},
29+
"file_extension": ".py",
30+
"mimetype": "text/x-python",
31+
"name": "python",
32+
"nbconvert_exporter": "python",
33+
"pygments_lexer": "ipython3",
34+
"version": "3.6.6"
35+
}
36+
},
37+
"nbformat": 4,
38+
"nbformat_minor": 2
39+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# Lab 12 Character Sequence RNN
2+
import tensorflow as tf
3+
import numpy as np
4+
tf.set_random_seed(777) # reproducibility
5+
6+
sample = " if you want you"
7+
idx2char = list(set(sample)) # index -> char
8+
char2idx = {c: i for i, c in enumerate(idx2char)} # char -> idex
9+
10+
# hyper parameters
11+
dic_size = len(char2idx) # RNN input size (one hot size)
12+
hidden_size = len(char2idx) # RNN output size
13+
num_classes = len(char2idx) # final output size (RNN or softmax, etc.)
14+
batch_size = 1 # one sample data, one batch
15+
sequence_length = len(sample) - 1 # number of lstm rollings (unit #)
16+
learning_rate = 0.1
17+
18+
sample_idx = [char2idx[c] for c in sample] # char to index
19+
x_data = [sample_idx[:-1]] # X data sample (0 ~ n-1) hello: hell
20+
y_data = [sample_idx[1:]] # Y label sample (1 ~ n) hello: ello
21+
22+
X = tf.placeholder(tf.int32, [None, sequence_length]) # X data
23+
Y = tf.placeholder(tf.int32, [None, sequence_length]) # Y label
24+
25+
x_one_hot = tf.one_hot(X, num_classes) # one hot: 1 -> 0 1 0 0 0 0 0 0 0 0
26+
cell = tf.contrib.rnn.BasicLSTMCell(
27+
num_units=hidden_size, state_is_tuple=True)
28+
initial_state = cell.zero_state(batch_size, tf.float32)
29+
outputs, _states = tf.nn.dynamic_rnn(
30+
cell, x_one_hot, initial_state=initial_state, dtype=tf.float32)
31+
32+
# FC layer
33+
X_for_fc = tf.reshape(outputs, [-1, hidden_size])
34+
outputs = tf.contrib.layers.fully_connected(X_for_fc, num_classes, activation_fn=None)
35+
36+
# reshape out for sequence_loss
37+
outputs = tf.reshape(outputs, [batch_size, sequence_length, num_classes])
38+
39+
weights = tf.ones([batch_size, sequence_length])
40+
sequence_loss = tf.contrib.seq2seq.sequence_loss(
41+
logits=outputs, targets=Y, weights=weights)
42+
loss = tf.reduce_mean(sequence_loss)
43+
train = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(loss)
44+
45+
prediction = tf.argmax(outputs, axis=2)
46+
47+
with tf.Session() as sess:
48+
sess.run(tf.global_variables_initializer())
49+
for i in range(50):
50+
l, _ = sess.run([loss, train], feed_dict={X: x_data, Y: y_data})
51+
result = sess.run(prediction, feed_dict={X: x_data})
52+
53+
# print char using dic
54+
result_str = [idx2char[c] for c in np.squeeze(result)]
55+
56+
print(i, "loss:", l, "Prediction:", ''.join(result_str))
57+
58+
59+
'''
60+
0 loss: 2.35377 Prediction: uuuuuuuuuuuuuuu
61+
1 loss: 2.21383 Prediction: yy you y you
62+
2 loss: 2.04317 Prediction: yy yoo ou
63+
3 loss: 1.85869 Prediction: yy ou uou
64+
4 loss: 1.65096 Prediction: yy you a you
65+
5 loss: 1.40243 Prediction: yy you yan you
66+
6 loss: 1.12986 Prediction: yy you wann you
67+
7 loss: 0.907699 Prediction: yy you want you
68+
8 loss: 0.687401 Prediction: yf you want you
69+
9 loss: 0.508868 Prediction: yf you want you
70+
10 loss: 0.379423 Prediction: yf you want you
71+
11 loss: 0.282956 Prediction: if you want you
72+
12 loss: 0.208561 Prediction: if you want you
73+
74+
...
75+
76+
'''
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# Lab 12 Character Sequence Softmax only
2+
import tensorflow as tf
3+
import numpy as np
4+
tf.set_random_seed(777) # reproducibility
5+
6+
sample = " if you want you"
7+
idx2char = list(set(sample)) # index -> char
8+
char2idx = {c: i for i, c in enumerate(idx2char)} # char -> idex
9+
10+
# hyper parameters
11+
dic_size = len(char2idx) # RNN input size (one hot size)
12+
rnn_hidden_size = len(char2idx) # RNN output size
13+
num_classes = len(char2idx) # final output size (RNN or softmax, etc.)
14+
batch_size = 1 # one sample data, one batch
15+
sequence_length = len(sample) - 1 # number of lstm rollings (unit #)
16+
learning_rate = 0.1
17+
18+
sample_idx = [char2idx[c] for c in sample] # char to index
19+
x_data = [sample_idx[:-1]] # X data sample (0 ~ n-1) hello: hell
20+
y_data = [sample_idx[1:]] # Y label sample (1 ~ n) hello: ello
21+
22+
X = tf.placeholder(tf.int32, [None, sequence_length]) # X data
23+
Y = tf.placeholder(tf.int32, [None, sequence_length]) # Y label
24+
25+
# flatten the data (ignore batches for now). No effect if the batch size is 1
26+
X_one_hot = tf.one_hot(X, num_classes) # one hot: 1 -> 0 1 0 0 0 0 0 0 0 0
27+
X_for_softmax = tf.reshape(X_one_hot, [-1, rnn_hidden_size])
28+
29+
# softmax layer (rnn_hidden_size -> num_classes)
30+
softmax_w = tf.get_variable("softmax_w", [rnn_hidden_size, num_classes])
31+
softmax_b = tf.get_variable("softmax_b", [num_classes])
32+
outputs = tf.matmul(X_for_softmax, softmax_w) + softmax_b
33+
34+
# expend the data (revive the batches)
35+
outputs = tf.reshape(outputs, [batch_size, sequence_length, num_classes])
36+
weights = tf.ones([batch_size, sequence_length])
37+
38+
# Compute sequence cost/loss
39+
sequence_loss = tf.contrib.seq2seq.sequence_loss(
40+
logits=outputs, targets=Y, weights=weights)
41+
loss = tf.reduce_mean(sequence_loss) # mean all sequence loss
42+
train = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(loss)
43+
44+
prediction = tf.argmax(outputs, axis=2)
45+
46+
with tf.Session() as sess:
47+
sess.run(tf.global_variables_initializer())
48+
for i in range(3000):
49+
l, _ = sess.run([loss, train], feed_dict={X: x_data, Y: y_data})
50+
result = sess.run(prediction, feed_dict={X: x_data})
51+
52+
# print char using dic
53+
result_str = [idx2char[c] for c in np.squeeze(result)]
54+
print(i, "loss:", l, "Prediction:", ''.join(result_str))
55+
56+
'''
57+
0 loss: 2.29513 Prediction: yu yny y y oyny
58+
1 loss: 2.10156 Prediction: yu ynu y y oynu
59+
2 loss: 1.92344 Prediction: yu you y u you
60+
61+
..
62+
63+
2997 loss: 0.277323 Prediction: yf you yant you
64+
2998 loss: 0.277323 Prediction: yf you yant you
65+
2999 loss: 0.277323 Prediction: yf you yant you
66+
'''
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
from __future__ import print_function
2+
3+
import tensorflow as tf
4+
import numpy as np
5+
from tensorflow.contrib import rnn
6+
7+
tf.set_random_seed(777) # reproducibility
8+
9+
sentence = ("if you want to build a ship, don't drum up people together to "
10+
"collect wood and don't assign them tasks and work, but rather "
11+
"teach them to long for the endless immensity of the sea.")
12+
13+
char_set = list(set(sentence))
14+
char_dic = {w: i for i, w in enumerate(char_set)}
15+
16+
data_dim = len(char_set)
17+
hidden_size = len(char_set)
18+
num_classes = len(char_set)
19+
sequence_length = 10 # Any arbitrary number
20+
learning_rate = 0.1
21+
22+
dataX = []
23+
dataY = []
24+
for i in range(0, len(sentence) - sequence_length):
25+
x_str = sentence[i:i + sequence_length]
26+
y_str = sentence[i + 1: i + sequence_length + 1]
27+
print(i, x_str, '->', y_str)
28+
29+
x = [char_dic[c] for c in x_str] # x str to index
30+
y = [char_dic[c] for c in y_str] # y str to index
31+
32+
dataX.append(x)
33+
dataY.append(y)
34+
35+
batch_size = len(dataX)
36+
37+
X = tf.placeholder(tf.int32, [None, sequence_length])
38+
Y = tf.placeholder(tf.int32, [None, sequence_length])
39+
40+
# One-hot encoding
41+
X_one_hot = tf.one_hot(X, num_classes)
42+
print(X_one_hot) # check out the shape
43+
44+
45+
# Make a lstm cell with hidden_size (each unit output vector size)
46+
def lstm_cell():
47+
cell = rnn.BasicLSTMCell(hidden_size, state_is_tuple=True)
48+
return cell
49+
50+
multi_cells = rnn.MultiRNNCell([lstm_cell() for _ in range(2)], state_is_tuple=True)
51+
52+
# outputs: unfolding size x hidden size, state = hidden size
53+
outputs, _states = tf.nn.dynamic_rnn(multi_cells, X_one_hot, dtype=tf.float32)
54+
55+
# FC layer
56+
X_for_fc = tf.reshape(outputs, [-1, hidden_size])
57+
outputs = tf.contrib.layers.fully_connected(X_for_fc, num_classes, activation_fn=None)
58+
59+
# reshape out for sequence_loss
60+
outputs = tf.reshape(outputs, [batch_size, sequence_length, num_classes])
61+
62+
# All weights are 1 (equal weights)
63+
weights = tf.ones([batch_size, sequence_length])
64+
65+
sequence_loss = tf.contrib.seq2seq.sequence_loss(
66+
logits=outputs, targets=Y, weights=weights)
67+
mean_loss = tf.reduce_mean(sequence_loss)
68+
train_op = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(mean_loss)
69+
70+
sess = tf.Session()
71+
sess.run(tf.global_variables_initializer())
72+
73+
for i in range(500):
74+
_, l, results = sess.run(
75+
[train_op, mean_loss, outputs], feed_dict={X: dataX, Y: dataY})
76+
for j, result in enumerate(results):
77+
index = np.argmax(result, axis=1)
78+
print(i, j, ''.join([char_set[t] for t in index]), l)
79+
80+
# Let's print the last char of each result to check it works
81+
results = sess.run(outputs, feed_dict={X: dataX})
82+
for j, result in enumerate(results):
83+
index = np.argmax(result, axis=1)
84+
if j is 0: # print all for the first result to make a sentence
85+
print(''.join([char_set[t] for t in index]), end='')
86+
else:
87+
print(char_set[index[-1]], end='')
88+
89+
'''
90+
0 167 tttttttttt 3.23111
91+
0 168 tttttttttt 3.23111
92+
0 169 tttttttttt 3.23111
93+
94+
499 167 of the se 0.229616
95+
499 168 tf the sea 0.229616
96+
499 169 the sea. 0.229616
97+
98+
g you want to build a ship, don't drum up people together to collect wood and don't assign them tasks and work, but rather teach them to long for the endless immensity of the sea.
99+
100+
'''

0 commit comments

Comments
 (0)