Skip to content

Commit 9842501

Browse files
authored
Merge branch 'master' into minor_update
2 parents 6418dbd + f0b2a01 commit 9842501

7 files changed

Lines changed: 145 additions & 6 deletions

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
.idea
33
__pycache__
44
*.pyc
5+
.DS_Store

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
== Lab code (WIP) ==
1+
# Lab code (WIP)
2+
This is work in progress. Please do not use them, since they may have many bugs and trial code. We will let you know when it's done.
23

34
## Naming rule:
45

@@ -14,4 +15,4 @@ python -m unittest discover -s tests;
1415
# http://stackoverflow.com/questions/14328406/
1516
pip install autopep8 # if you haven't install
1617
autopep8 . --recursive --in-place --pep8-passes 2000 --verbose
17-
```
18+
```

klab-12-2-rnn_long_char.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@
5656
# Store model graph in png
5757
plot(model, to_file=os.path.basename(__file__) + '.png', show_shapes=True)
5858

59-
6059
model.compile(loss='categorical_crossentropy',
6160
optimizer='rmsprop', metrics=['accuracy'])
6261
model.fit(dataX, dataY, nb_epoch=1000)
@@ -70,4 +69,4 @@
7069
index = np.argmax(prediction, axis=1)
7170
result = [char_set[j] for j in index]
7271

73-
print(''.join(x_str), ' -> ', ''.join(result))
72+
print(''.join(x_str), ' -> ', ''.join(result))

lab-04-2-multi_variable_linear_regression.py

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

4-
x_data = [[1., 1.], [2., 2.], [3., 3.], [4., 4.], [5., 5.]]
5-
y_data = [1, 2, 3, 4, 5]
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)
68

79
W = tf.Variable(tf.random_uniform(
810
shape=[2, 1], minval=-1.0, maxval=1.0, dtype=tf.float32))
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Lab 5 Logistic Regression Classifier
2+
import tensorflow as tf
3+
import numpy as np
4+
5+
xy = np.loadtxt('data.csv', delimiter=',')
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+
13+
X = tf.placeholder(tf.float32)
14+
Y = tf.placeholder(tf.float32)
15+
16+
W = tf.Variable(tf.random_uniform(
17+
shape=[3, 1], minval=-1.0, maxval=1.0, dtype=tf.float32))
18+
# Hypothesis
19+
h = tf.matmul(X, W)
20+
hypothesis = tf.div(1., 1. + tf.exp(-h))
21+
22+
# cost function
23+
cost = -tf.reduce_mean(Y * tf.log(hypothesis) + (1 - Y)
24+
* tf.log(1 - hypothesis))
25+
26+
# Minimize
27+
optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.0001)
28+
train = optimizer.minimize(cost)
29+
30+
# Initialize variable
31+
init = tf.global_variables_initializer()
32+
33+
# Launch graph
34+
sess = tf.Session()
35+
sess.run(init)
36+
37+
for step in range(2001):
38+
sess.run(train, feed_dict={X: x_data, Y: y_data})
39+
if step % 20 == 0:
40+
print(step, sess.run(cost, feed_dict={
41+
X: x_data, Y: y_data}), sess.run(W))

lab-06-1-softmax_classifier.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Lab 6 Softmax Classifier
2+
import tensorflow as tf
3+
import numpy as np
4+
5+
x_data = np.array([[1, 2, 1], [1, 3, 2], [1, 3, 4], [1, 5, 5],
6+
[1, 7, 5], [1, 2, 5], [1, 6, 6], [1, 7, 7]], dtype=np.float32)
7+
y_data = np.array([[0, 0, 1], [0, 0, 1], [0, 0, 1], [0, 1, 0],
8+
[0, 1, 0], [0, 1, 0], [1, 0, 0], [1, 0, 0]], dtype=np.float32)
9+
10+
X = tf.placeholder("float", [None, 3])
11+
Y = tf.placeholder("float", [None, 3])
12+
13+
W = tf.Variable(tf.zeros([3, 3]))
14+
# Softmax
15+
hypothesis = tf.nn.softmax(tf.matmul(X, W))
16+
# tf.nn.softmax computes softmax activations
17+
# softmax = exp(logits) / reduce_sum(exp(logits), dim)
18+
19+
# Cross entropy cost
20+
cost = tf.reduce_mean(-tf.reduce_sum(Y *
21+
tf.log(hypothesis), axis=1))
22+
23+
optimizer = tf.train.GradientDescentOptimizer(
24+
learning_rate=0.1).minimize(cost)
25+
26+
init = tf.global_variables_initializer()
27+
28+
# Launch graph
29+
with tf.Session() as sess:
30+
sess.run(init)
31+
32+
for step in range(2001):
33+
sess.run(optimizer, feed_dict={X: x_data, Y: y_data})
34+
if step % 200 == 0:
35+
print(step, sess.run(cost, feed_dict={
36+
X: x_data, Y: y_data}), sess.run(W))
37+
38+
print('--------------')
39+
40+
# Testing & One-hot encoding
41+
a = sess.run(hypothesis, feed_dict={X: [[1, 11, 7]]})
42+
print(a, sess.run(tf.arg_max(a, 1)))
43+
44+
print('--------------')
45+
46+
b = sess.run(hypothesis, feed_dict={X: [[1, 3, 4]]})
47+
print(a, sess.run(tf.arg_max(b, 1)))
48+
49+
print('--------------')
50+
51+
c = sess.run(hypothesis, feed_dict={X: [[1, 1, 0]]})
52+
print(a, sess.run(tf.arg_max(c, 1)))
53+
54+
print('--------------')
55+
56+
all = sess.run(hypothesis, feed_dict={
57+
X: [[1, 11, 7], [1, 3, 4], [1, 1, 0]]})
58+
print(all, sess.run(tf.arg_max(all, 1)))
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Lab 7 Learning rate and Evaluation
2+
import tensorflow as tf
3+
import numpy as np
4+
5+
x_data = np.array([[1, 2, 1], [1, 3, 2], [1, 3, 4], [1, 5, 5],
6+
[1, 7, 5], [1, 2, 5], [1, 6, 6], [1, 7, 7]], dtype=np.float32)
7+
y_data = np.array([[0, 0, 1], [0, 0, 1], [0, 0, 1], [0, 1, 0],
8+
[0, 1, 0], [0, 1, 0], [1, 0, 0], [1, 0, 0]], dtype=np.float32)
9+
10+
X = tf.placeholder("float", [None, 3])
11+
Y = tf.placeholder("float", [None, 3])
12+
13+
W = tf.Variable(tf.zeros([3, 3]))
14+
# Softmax
15+
hypothesis = tf.nn.softmax(tf.matmul(X, W))
16+
# tf.nn.softmax computes softmax activations
17+
# softmax = exp(logits) / reduce_sum(exp(logits), dim)
18+
19+
# Cross entropy cost
20+
cost = tf.reduce_mean(-tf.reduce_sum(Y *
21+
tf.log(hypothesis), axis=1))
22+
23+
# Changed learning_rate to 10
24+
optimizer = tf.train.GradientDescentOptimizer(
25+
learning_rate=10.).minimize(cost)
26+
27+
init = tf.global_variables_initializer()
28+
29+
# Launch graph
30+
with tf.Session() as sess:
31+
sess.run(init)
32+
33+
for step in range(2001):
34+
sess.run(optimizer, feed_dict={X: x_data, Y: y_data})
35+
if step % 200 == 0:
36+
print(step, sess.run(cost, feed_dict={
37+
X: x_data, Y: y_data}), sess.run(W))

0 commit comments

Comments
 (0)