Skip to content

Commit a75a82d

Browse files
committed
added lab-09-1
1 parent 751661a commit a75a82d

2 files changed

Lines changed: 50 additions & 4 deletions

File tree

lab-07-2-learning_rate_and_evaluation.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
b = tf.Variable(tf.zeros([10]))
1414

1515
# MNIST data image of shape 28 * 28 = 784
16-
X = tf.placeholder("float", [None, 784])
16+
X = tf.placeholder(tf.float32, [None, 784])
1717
# 0 - 9 digits recognition = 10 classes
18-
Y = tf.placeholder("float", [None, 10])
18+
Y = tf.placeholder(tf.float32, [None, 10])
1919

2020
# Hypothesis (using softmax)
2121
hypothesis = tf.nn.softmax(tf.matmul(X, W) + b)
@@ -55,8 +55,8 @@
5555
plt.show()
5656

5757
# Test model
58-
correct_prediction = tf.equal(tf.argmax(hypothesis, 1), tf.argmax(Y, 1))
58+
correct_prediction = tf.equal(tf.arg_max(hypothesis, 1), tf.arg_max(Y, 1))
5959
# Calculate accuracy
60-
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
60+
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
6161
print("Accuracy: ", accuracy.eval(session=sess, feed_dict={
6262
X: mnist.test.images, Y: mnist.test.labels}))

lab-09-1-xor.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Lab 9 XOR
2+
# This example does not work
3+
import tensorflow as tf
4+
import numpy as np
5+
6+
x_data = np.array([[0, 0], [0, 1], [1, 0], [1, 1]], dtype=np.float32)
7+
y_data = np.array([[0], [1], [1], [0]], dtype=np.float32)
8+
9+
X = tf.placeholder(tf.float32)
10+
Y = tf.placeholder(tf.float32)
11+
12+
13+
W = tf.Variable(tf.random_uniform(
14+
shape=[2, 1], minval=-1.0, maxval=1.0, dtype=tf.float32))
15+
16+
# Hypothesis
17+
h = tf.matmul(X, W)
18+
hypothesis = tf.div(1., 1. + tf.exp(-h))
19+
20+
# Cost function
21+
cost = -tf.reduce_mean(Y * tf.log(hypothesis) + (1 - Y)
22+
* tf.log(1 - hypothesis))
23+
24+
train = tf.train.GradientDescentOptimizer(learning_rate=0.01).minimize(cost)
25+
26+
# Initialize variables
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(1001):
34+
sess.run(train, 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))
38+
39+
# Test model
40+
correct_prediction = tf.equal(tf.floor(hypothesis + 0.5), Y)
41+
42+
# Accuracy
43+
accuracy = tf.reduce_mean(tf.cast(correct_prediction, dtype=tf.float32))
44+
print(sess.run([hypothesis, tf.floor(hypothesis + 0.5),
45+
correct_prediction, accuracy], feed_dict={X: x_data, Y: y_data}))
46+
print("Accuracy: ", accuracy.eval({X: x_data, Y: y_data}))

0 commit comments

Comments
 (0)