Skip to content

Commit ae789c6

Browse files
authored
Minor update (hunkim#5)
* Fixed char * basic s2s done * fixed condition * Added .gitignore * Let's use np when we need * Let's use np when we need * Added file reading
1 parent a75a82d commit ae789c6

6 files changed

Lines changed: 261 additions & 21 deletions

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
__pycache__
44
*.pyc
55
.DS_Store
6-
MNIST_data
6+
klab*.png
7+
MNIST_data

klab-12-4-seq2seq.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
# Build training date set
2020
dataX = []
2121
dataY = []
22-
for i in range(100):
22+
23+
for i in range(1000):
2324
rand_pick = np.random.choice(10, 7)
2425
x = [char_dic[digit[c]] for c in rand_pick]
2526
y = [char_dic[alpha[c]] for c in rand_pick]
@@ -62,10 +63,23 @@
6263
# Store model graph in png
6364
plot(model, to_file=os.path.basename(__file__) + '.png', show_shapes=True)
6465

65-
predictions = model.predict(dataX, verbose=0)
66+
# Create test dataset for fun
67+
testX = []
68+
for i in range(10):
69+
rand_pick = np.random.choice(10, 7)
70+
x = [char_dic[digit[c]] for c in rand_pick]
71+
testX.append(x)
72+
73+
# One-hot encoding
74+
testX = np_utils.to_categorical(testX, nb_classes=nb_classes)
75+
# reshape X to be [samples, time steps, features]
76+
testX = np.reshape(testX, (-1, seq_length, data_dim))
77+
78+
79+
predictions = model.predict(testX, verbose=0)
6680
for i, prediction in enumerate(predictions):
6781
# print(prediction)
68-
x_index = np.argmax(dataX[i], axis=1)
82+
x_index = np.argmax(testX[i], axis=1)
6983
x_str = [char_set[j] for j in x_index]
7084

7185
index = np.argmax(prediction, axis=1)

lab-01-basics.ipynb

Lines changed: 197 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,225 @@
11
{
22
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {
6+
"slideshow": {
7+
"slide_type": "slide"
8+
}
9+
},
10+
"source": [
11+
"# Getting Started With TensorFlow\n",
12+
"https://www.tensorflow.org/get_started/get_started"
13+
]
14+
},
315
{
416
"cell_type": "code",
5-
"execution_count": null,
17+
"execution_count": 1,
618
"metadata": {
7-
"collapsed": true
19+
"collapsed": false,
20+
"slideshow": {
21+
"slide_type": "slide"
22+
}
823
},
9-
"outputs": [],
24+
"outputs": [
25+
{
26+
"data": {
27+
"text/plain": [
28+
"'1.0.0'"
29+
]
30+
},
31+
"execution_count": 1,
32+
"metadata": {},
33+
"output_type": "execute_result"
34+
}
35+
],
1036
"source": [
1137
"import tensorflow as tf\n",
1238
"tf.__version__"
1339
]
40+
},
41+
{
42+
"cell_type": "markdown",
43+
"metadata": {
44+
"slideshow": {
45+
"slide_type": "slide"
46+
}
47+
},
48+
"source": [
49+
"## Tensors"
50+
]
51+
},
52+
{
53+
"cell_type": "code",
54+
"execution_count": 3,
55+
"metadata": {
56+
"collapsed": false,
57+
"slideshow": {
58+
"slide_type": "slide"
59+
}
60+
},
61+
"outputs": [
62+
{
63+
"data": {
64+
"text/plain": [
65+
"[[[1.0, 2.0, 3.0]], [[7.0, 8.0, 9.0]]]"
66+
]
67+
},
68+
"execution_count": 3,
69+
"metadata": {},
70+
"output_type": "execute_result"
71+
}
72+
],
73+
"source": [
74+
"3 # a rank 0 tensor; this is a scalar with shape []\n",
75+
"[1. ,2., 3.] # a rank 1 tensor; this is a vector with shape [3]\n",
76+
"[[1., 2., 3.], [4., 5., 6.]] # a rank 2 tensor; a matrix with shape [2, 3]\n",
77+
"[[[1., 2., 3.]], [[7., 8., 9.]]] # a rank 3 tensor with shape [2, 1, 3]"
78+
]
79+
},
80+
{
81+
"cell_type": "markdown",
82+
"metadata": {},
83+
"source": [
84+
"## Computational Graph"
85+
]
86+
},
87+
{
88+
"cell_type": "code",
89+
"execution_count": 4,
90+
"metadata": {
91+
"collapsed": false
92+
},
93+
"outputs": [
94+
{
95+
"name": "stdout",
96+
"output_type": "stream",
97+
"text": [
98+
"Tensor(\"Const:0\", shape=(), dtype=float32) Tensor(\"Const_1:0\", shape=(), dtype=float32)\n"
99+
]
100+
}
101+
],
102+
"source": [
103+
"node1 = tf.constant(3.0, tf.float32)\n",
104+
"node2 = tf.constant(4.0) # also tf.float32 implicitly\n",
105+
"print(node1, node2)"
106+
]
107+
},
108+
{
109+
"cell_type": "code",
110+
"execution_count": 5,
111+
"metadata": {
112+
"collapsed": false
113+
},
114+
"outputs": [
115+
{
116+
"name": "stdout",
117+
"output_type": "stream",
118+
"text": [
119+
"[3.0, 4.0]\n"
120+
]
121+
}
122+
],
123+
"source": [
124+
"sess = tf.Session()\n",
125+
"print(sess.run([node1, node2]))"
126+
]
127+
},
128+
{
129+
"cell_type": "code",
130+
"execution_count": 6,
131+
"metadata": {
132+
"collapsed": false,
133+
"scrolled": true
134+
},
135+
"outputs": [
136+
{
137+
"name": "stdout",
138+
"output_type": "stream",
139+
"text": [
140+
"node3: Tensor(\"Add:0\", shape=(), dtype=float32)\n",
141+
"sess.run(node3): 7.0\n"
142+
]
143+
}
144+
],
145+
"source": [
146+
"node3 = tf.add(node1, node2)\n",
147+
"print(\"node3: \", node3)\n",
148+
"print(\"sess.run(node3): \",sess.run(node3))"
149+
]
150+
},
151+
{
152+
"cell_type": "markdown",
153+
"metadata": {},
154+
"source": [
155+
"![Graph](https://www.tensorflow.org/images/getting_started_adder.png)"
156+
]
157+
},
158+
{
159+
"cell_type": "code",
160+
"execution_count": 13,
161+
"metadata": {
162+
"collapsed": false
163+
},
164+
"outputs": [
165+
{
166+
"name": "stdout",
167+
"output_type": "stream",
168+
"text": [
169+
"7.5\n",
170+
"[ 3. 7.]\n"
171+
]
172+
}
173+
],
174+
"source": [
175+
"a = tf.placeholder(tf.float32)\n",
176+
"b = tf.placeholder(tf.float32)\n",
177+
"adder_node = a + b # + provides a shortcut for tf.add(a, b)\n",
178+
"\n",
179+
"print(sess.run(adder_node, feed_dict={a: 3, b:4.5}))\n",
180+
"print(sess.run(adder_node, feed_dict={a: [1,3], b: [2, 4]}))"
181+
]
182+
},
183+
{
184+
"cell_type": "code",
185+
"execution_count": 14,
186+
"metadata": {
187+
"collapsed": false
188+
},
189+
"outputs": [
190+
{
191+
"name": "stdout",
192+
"output_type": "stream",
193+
"text": [
194+
"22.5\n"
195+
]
196+
}
197+
],
198+
"source": [
199+
"add_and_triple = adder_node * 3.\n",
200+
"print(sess.run(add_and_triple, feed_dict={a: 3, b:4.5}))"
201+
]
14202
}
15203
],
16204
"metadata": {
17205
"kernelspec": {
18-
"display_name": "Python 2",
206+
"display_name": "Python 3",
19207
"language": "python",
20-
"name": "python2"
208+
"name": "python3"
21209
},
22210
"language_info": {
23211
"codemirror_mode": {
24212
"name": "ipython",
25-
"version": 2.0
213+
"version": 3
26214
},
27215
"file_extension": ".py",
28216
"mimetype": "text/x-python",
29217
"name": "python",
30218
"nbconvert_exporter": "python",
31-
"pygments_lexer": "ipython2",
32-
"version": "2.7.6"
219+
"pygments_lexer": "ipython3",
220+
"version": "3.6.0"
33221
}
34222
},
35223
"nbformat": 4,
36224
"nbformat_minor": 0
37-
}
225+
}

lab-04-2-multi_variable_linear_regression.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
# Lab 4 Multi-variable linear regression
22
import tensorflow as tf
3-
import numpy as np
43

5-
x_data = np.array([[1., 1.], [2., 2.], [3., 3.], [
6-
4., 4.], [5., 5.]], dtype=np.float32)
7-
y_data = np.array([1, 2, 3, 4, 5], dtype=np.float32).reshape(-1, 1)
4+
x_data = [[1., 1.], [2., 2.], [3., 3.],
5+
[4., 4.], [5., 5.]]
6+
y_data = [[1], [2], [3], [4], [5]]
87

98
W = tf.Variable(tf.random_uniform(
109
shape=[2, 1], minval=-1.0, maxval=1.0, dtype=tf.float32))
@@ -21,7 +20,7 @@
2120
optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.01)
2221
train = optimizer.minimize(cost)
2322

24-
# Initialize variable
23+
# Initialize variables
2524
init = tf.global_variables_initializer()
2625

2726
# Launch graph
@@ -31,5 +30,5 @@
3130
for step in range(2001):
3231
sess.run(train)
3332
if step % 20 == 0:
34-
print(step, sess.run(cost), sess.run(
35-
hypothesis), sess.run(W), sess.run(b))
33+
print(step, sess.run(cost),
34+
sess.run(hypothesis), sess.run(W), sess.run(b))
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Lab 4 Multi-variable linear regression
2+
import tensorflow as tf
3+
import numpy as np
4+
5+
xy = np.loadtxt('data.csv', delimiter=',', dtype=np.float32)
6+
x_data = xy[:, 0:-1]
7+
y_data = xy[:, [-1]]
8+
9+
print(x_data.shape, x_data, len(x_data))
10+
print(y_data.shape, y_data)
11+
12+
W = tf.Variable(tf.random_uniform(
13+
shape=[3, 1], minval=-1.0, maxval=1.0, dtype=tf.float32))
14+
b = tf.Variable(tf.random_uniform(
15+
shape=[1], minval=-1.0, maxval=1.0, dtype=tf.float32))
16+
17+
# Hypothesis
18+
hypothesis = tf.matmul(x_data, W) + b
19+
20+
# Simplified cost function
21+
cost = tf.reduce_mean(tf.square(hypothesis - y_data))
22+
23+
# Minimize
24+
optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.01)
25+
train = optimizer.minimize(cost)
26+
27+
# Initialize variables
28+
init = tf.global_variables_initializer()
29+
30+
# Launch graph
31+
sess = tf.Session()
32+
sess.run(init)
33+
34+
for step in range(2001):
35+
sess.run(train)
36+
if step % 20 == 0:
37+
print(step, sess.run(cost),
38+
sess.run(hypothesis), sess.run(W), sess.run(b))

lab-04-3-multi_variable_linear_regression_feed.py renamed to lab-04-4-multi_variable_linear_regression_feed.py

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

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

2020
# Minimize

0 commit comments

Comments
 (0)