Skip to content

Commit b0265b5

Browse files
committed
Added
1 parent b2067ca commit b0265b5

5 files changed

Lines changed: 307 additions & 6 deletions

klab-12-1-softmax_hello_char.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import numpy as np
2+
from keras.models import Sequential
3+
from keras.layers import Dense, TimeDistributed, Activation, LSTM
4+
from keras.utils import np_utils
5+
6+
import os
7+
8+
# brew install graphviz
9+
# pip3 install graphviz
10+
# pip3 install pydot
11+
from keras.utils.visualize_util import plot
12+
13+
# sample text
14+
sample = "hihello"
15+
16+
char_set = list(set(sample)) # id -> char ['i', 'l', 'e', 'o', 'h']
17+
char_dic = {w: i for i, w in enumerate(char_set)}
18+
19+
x_str = sample[:-1]
20+
y_str = sample[1:]
21+
22+
data_dim = len(char_set)
23+
timesteps = len(y_str)
24+
nb_classes = len(char_set)
25+
26+
print(x_str, y_str)
27+
28+
x = [char_dic[c] for c in x_str] # char to index
29+
y = [char_dic[c] for c in y_str] # char to index
30+
31+
# One-hot encoding
32+
x = np_utils.to_categorical(x, nb_classes=nb_classes)
33+
# reshape X to be [samples, time steps, features]
34+
x = np.reshape(x, (-1, len(x), data_dim))
35+
print(x.shape)
36+
37+
# One-hot encoding
38+
y = np_utils.to_categorical(y, nb_classes=nb_classes)
39+
# time steps
40+
y = np.reshape(y, (-1, len(y), data_dim))
41+
print(y.shape)
42+
43+
model = Sequential()
44+
model.add(Dense(nb_classes, input_shape=(
45+
timesteps, data_dim)))
46+
model.add(Activation('softmax'))
47+
model.summary()
48+
# Store model graph in png
49+
plot(model, to_file=os.path.basename(__file__) + '.png', show_shapes=True)
50+
51+
model.compile(loss='categorical_crossentropy',
52+
optimizer='rmsprop', metrics=['accuracy'])
53+
model.fit(x, y, nb_epoch=100)
54+
55+
predictions = model.predict(x, verbose=0)
56+
for i, prediction in enumerate(predictions):
57+
x_index = np.argmax(x[i], axis=1)
58+
x_str = [char_set[j] for j in x_index]
59+
print(x_index, ''.join(x_str))
60+
61+
index = np.argmax(prediction, axis=1)
62+
result = [char_set[j] for j in index]
63+
print(index, ''.join(result))

klab-12-2-rnn_long_char.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
from keras.utils.visualize_util import plot
1212

1313
# sample sentence
14-
sentence = "If 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."
14+
sentence = "if 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."
1515

16-
char_set = list(set(sentence)) # id -> char ['i', 'l', 'e', 'o', 'h']
16+
char_set = list(set(sentence)) # id -> char ['i', 'l', 'e', 'o', 'h', ...]
1717
char_dic = {w: i for i, w in enumerate(char_set)}
1818

1919
data_dim = len(char_set)

klab-12-4-seq2seq.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from keras.layers import Activation, TimeDistributed, Dense, RepeatVector, LSTM
55
from keras.utils import np_utils
66
from keras.utils.visualize_util import plot
7+
from keras.callbacks import TensorBoard
78
import os
89

910
digit = "0123456789"
@@ -38,6 +39,8 @@
3839

3940

4041
print('Build model...')
42+
TensorBoard(log_dir='./logs', histogram_freq=1, write_graph=True, write_images=False)
43+
4144
model = Sequential()
4245
# "Encode" the input sequence using an RNN, producing an output of HIDDEN_SIZE
4346
# note: in a situation where your input sequences have a variable length,
@@ -62,12 +65,15 @@
6265
# Store model graph in png
6366
plot(model, to_file=os.path.basename(__file__) + '.png', show_shapes=True)
6467

65-
# Create test dataset for fun
68+
# Create test data set for fun
6669
testX = []
70+
testY = []
6771
for i in range(10):
6872
rand_pick = np.random.choice(10, 7)
6973
x = [char_dic[digit[c]] for c in rand_pick]
74+
y = [alpha[c] for c in rand_pick]
7075
testX.append(x)
76+
testY.append(y)
7177

7278
# One-hot encoding
7379
testX = np_utils.to_categorical(testX, nb_classes=nb_classes)
@@ -84,4 +90,5 @@
8490
index = np.argmax(prediction, axis=1)
8591
result = [char_set[j] for j in index]
8692

87-
print(''.join(x_str), ' -> ', ''.join(result))
93+
print(''.join(x_str), ' -> ', ''.join(result),
94+
" true: ", ''.join(testY[i]))

lab-04-4-multi_variable_linear_regression_feed.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@
1414
hypothesis = tf.matmul(x, W) + b
1515

1616
# Simplified cost function
17-
with tf.control_dependencies(y.shape.assert_same_rank(hypothesis)):
18-
cost = tf.reduce_mean(tf.square(hypothesis - y))
17+
cost = tf.reduce_mean(tf.square(hypothesis - y))
1918

2019
# Minimize
2120
optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.01)

rnn_basics.ipynb

Lines changed: 232 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,232 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {
6+
"collapsed": true
7+
},
8+
"source": [
9+
"* http://www.wildml.com/2016/08/rnns-in-tensorflow-a-practical-guide-and-undocumented-features/\n",
10+
"* http://learningtensorflow.com/index.html"
11+
]
12+
},
13+
{
14+
"cell_type": "code",
15+
"execution_count": 1,
16+
"metadata": {
17+
"collapsed": true
18+
},
19+
"outputs": [],
20+
"source": [
21+
"import tensorflow as tf\n",
22+
"import numpy as np\n",
23+
"from tensorflow.contrib import rnn\n",
24+
"import pprint\n",
25+
"pp = pprint.PrettyPrinter(indent=4)"
26+
]
27+
},
28+
{
29+
"cell_type": "markdown",
30+
"metadata": {},
31+
"source": [
32+
"## Static RNN"
33+
]
34+
},
35+
{
36+
"cell_type": "code",
37+
"execution_count": 2,
38+
"metadata": {
39+
"collapsed": true
40+
},
41+
"outputs": [],
42+
"source": [
43+
"tf.reset_default_graph()\n",
44+
"sess = tf.InteractiveSession()"
45+
]
46+
},
47+
{
48+
"cell_type": "code",
49+
"execution_count": 3,
50+
"metadata": {
51+
"collapsed": false
52+
},
53+
"outputs": [
54+
{
55+
"name": "stdout",
56+
"output_type": "stream",
57+
"text": [
58+
"array([[[ 0., 1., 2.],\n",
59+
" [ 3., 4., 5.],\n",
60+
" [ 6., 7., 8.],\n",
61+
" [ 9., 10., 11.]],\n",
62+
"\n",
63+
" [[ 12., 13., 14.],\n",
64+
" [ 15., 16., 17.],\n",
65+
" [ 18., 19., 20.],\n",
66+
" [ 21., 22., 23.]]], dtype=float32)\n"
67+
]
68+
}
69+
],
70+
"source": [
71+
"# Create input data\n",
72+
"x_data = np.arange(24, dtype=np.float32).reshape(2,4,3)\n",
73+
"pp.pprint(x_data)"
74+
]
75+
},
76+
{
77+
"cell_type": "code",
78+
"execution_count": 4,
79+
"metadata": {
80+
"collapsed": false
81+
},
82+
"outputs": [
83+
{
84+
"name": "stdout",
85+
"output_type": "stream",
86+
"text": [
87+
"5 LSTMStateTuple(c=5, h=5)\n",
88+
"5 (LSTMStateTuple(c=5, h=5), LSTMStateTuple(c=5, h=5))\n"
89+
]
90+
}
91+
],
92+
"source": [
93+
"cell = rnn.BasicLSTMCell(num_units=5, state_is_tuple=True)\n",
94+
"print(cell.output_size, cell.state_size)\n",
95+
"cell = rnn.MultiRNNCell([cell] * 2, state_is_tuple=True)\n",
96+
"print(cell.output_size, cell.state_size)"
97+
]
98+
},
99+
{
100+
"cell_type": "code",
101+
"execution_count": 5,
102+
"metadata": {
103+
"collapsed": false
104+
},
105+
"outputs": [
106+
{
107+
"name": "stdout",
108+
"output_type": "stream",
109+
"text": [
110+
"[ array([[ 0., 1., 2.],\n",
111+
" [ 3., 4., 5.],\n",
112+
" [ 6., 7., 8.],\n",
113+
" [ 9., 10., 11.]], dtype=float32),\n",
114+
" array([[ 12., 13., 14.],\n",
115+
" [ 15., 16., 17.],\n",
116+
" [ 18., 19., 20.],\n",
117+
" [ 21., 22., 23.]], dtype=float32)]\n"
118+
]
119+
}
120+
],
121+
"source": [
122+
"x_data = tf.reshape(x_data, [-1, 3])\n",
123+
"x_split = tf.split(value=x_data, num_or_size_splits=2)\n",
124+
"pp.pprint(sess.run(x_split))"
125+
]
126+
},
127+
{
128+
"cell_type": "code",
129+
"execution_count": 6,
130+
"metadata": {
131+
"collapsed": false
132+
},
133+
"outputs": [
134+
{
135+
"ename": "AttributeError",
136+
"evalue": "module 'tensorflow.python.ops.nn' has no attribute 'rnn'",
137+
"output_type": "error",
138+
"traceback": [
139+
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
140+
"\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)",
141+
"\u001b[0;32m<ipython-input-6-9b5a47592263>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0moutputs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0m_states\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mtf\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mnn\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrnn\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mcell\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mx_split\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdtype\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mtf\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfloat32\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0moutputs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0msess\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrun\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtf\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mglobal_variables_initializer\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0mpp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0msess\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrun\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0moutputs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
142+
"\u001b[0;31mAttributeError\u001b[0m: module 'tensorflow.python.ops.nn' has no attribute 'rnn'"
143+
]
144+
}
145+
],
146+
"source": [
147+
"outputs,_states = tf.nn.rnn(cell, x_split, dtype=tf.float32)\n",
148+
"print(outputs)\n",
149+
"sess.run(tf.global_variables_initializer())\n",
150+
"pp.pprint(sess.run(outputs))"
151+
]
152+
},
153+
{
154+
"cell_type": "markdown",
155+
"metadata": {},
156+
"source": [
157+
"## Dynamic Rnn"
158+
]
159+
},
160+
{
161+
"cell_type": "code",
162+
"execution_count": null,
163+
"metadata": {
164+
"collapsed": true
165+
},
166+
"outputs": [],
167+
"source": [
168+
"tf.reset_default_graph()\n",
169+
"sess = tf.InteractiveSession()"
170+
]
171+
},
172+
{
173+
"cell_type": "code",
174+
"execution_count": null,
175+
"metadata": {
176+
"collapsed": false
177+
},
178+
"outputs": [],
179+
"source": [
180+
"tf.reset_default_graph()\n",
181+
"\n",
182+
"# Create input data\n",
183+
"X = np.random.randn(2, 10, 8)\n",
184+
"\n",
185+
"# The second example is of length 6 \n",
186+
"X[1,6:] = 0\n",
187+
"X_lengths = [10, 6]\n",
188+
"\n",
189+
"cell = rnn.LSTMCell(num_units=64, state_is_tuple=True)\n",
190+
"\n",
191+
"outputs, last_states = tf.nn.dynamic_rnn(\n",
192+
" cell=cell,\n",
193+
" dtype=tf.float64,\n",
194+
" sequence_length=X_lengths,\n",
195+
" inputs=X)"
196+
]
197+
},
198+
{
199+
"cell_type": "code",
200+
"execution_count": null,
201+
"metadata": {
202+
"collapsed": false
203+
},
204+
"outputs": [],
205+
"source": [
206+
"sess.run(tf.global_variables_initializer())\n",
207+
"pp.pprint(sess.run(outputs))"
208+
]
209+
}
210+
],
211+
"metadata": {
212+
"kernelspec": {
213+
"display_name": "Python 3",
214+
"language": "python",
215+
"name": "python3"
216+
},
217+
"language_info": {
218+
"codemirror_mode": {
219+
"name": "ipython",
220+
"version": 3
221+
},
222+
"file_extension": ".py",
223+
"mimetype": "text/x-python",
224+
"name": "python",
225+
"nbconvert_exporter": "python",
226+
"pygments_lexer": "ipython3",
227+
"version": "3.6.0"
228+
}
229+
},
230+
"nbformat": 4,
231+
"nbformat_minor": 0
232+
}

0 commit comments

Comments
 (0)