|
3 | 3 | import numpy as np
|
4 | 4 |
|
5 | 5 | tf.set_random_seed(777) # for reproducibility
|
6 |
| -learning_rate = 0.1 |
7 | 6 |
|
8 |
| -x_data = [[0, 0], |
9 |
| - [0, 1], |
10 |
| - [1, 0], |
11 |
| - [1, 1]] |
12 |
| -y_data = [[0], |
13 |
| - [1], |
14 |
| - [1], |
15 |
| - [0]] |
16 |
| -x_data = np.array(x_data, dtype=np.float32) |
17 |
| -y_data = np.array(y_data, dtype=np.float32) |
| 7 | +x_data = np.array([[0, 0], [0, 1], [1, 0], [1, 1]], dtype=np.float32) |
| 8 | +y_data = np.array([[0], [1], [1], [0]], dtype=np.float32) |
18 | 9 |
|
19 | 10 | X = tf.placeholder(tf.float32, [None, 2])
|
20 | 11 | Y = tf.placeholder(tf.float32, [None, 1])
|
21 | 12 |
|
22 |
| -W = tf.Variable(tf.random_normal([2, 1]), name='weight') |
23 |
| -b = tf.Variable(tf.random_normal([1]), name='bias') |
| 13 | +W = tf.Variable(tf.random_normal([2, 1]), name="weight") |
| 14 | +b = tf.Variable(tf.random_normal([1]), name="bias") |
24 | 15 |
|
25 | 16 | # Hypothesis using sigmoid: tf.div(1., 1. + tf.exp(tf.matmul(X, W)))
|
26 | 17 | hypothesis = tf.sigmoid(tf.matmul(X, W) + b)
|
27 | 18 |
|
28 | 19 | # cost/loss function
|
29 |
| -cost = -tf.reduce_mean(Y * tf.log(hypothesis) + (1 - Y) * |
30 |
| - tf.log(1 - hypothesis)) |
31 |
| - |
32 |
| -train = tf.train.GradientDescentOptimizer(learning_rate=learning_rate).minimize(cost) |
| 20 | +cost = -tf.reduce_mean(Y * tf.log(hypothesis) + (1 - Y) * tf.log(1 - hypothesis)) |
| 21 | +train = tf.train.GradientDescentOptimizer(learning_rate=0.1).minimize(cost) |
33 | 22 |
|
34 | 23 | # Accuracy computation
|
35 | 24 | # True if hypothesis>0.5 else False
|
|
42 | 31 | sess.run(tf.global_variables_initializer())
|
43 | 32 |
|
44 | 33 | for step in range(10001):
|
45 |
| - sess.run(train, feed_dict={X: x_data, Y: y_data}) |
| 34 | + _, cost_val, w_val = sess.run( |
| 35 | + [train, cost, W], feed_dict={X: x_data, Y: y_data} |
| 36 | + ) |
46 | 37 | if step % 100 == 0:
|
47 |
| - print(step, sess.run(cost, feed_dict={ |
48 |
| - X: x_data, Y: y_data}), sess.run(W)) |
| 38 | + print(step, cost_val, w_val) |
49 | 39 |
|
50 | 40 | # Accuracy report
|
51 |
| - h, c, a = sess.run([hypothesis, predicted, accuracy], |
52 |
| - feed_dict={X: x_data, Y: y_data}) |
| 41 | + h, c, a = sess.run( |
| 42 | + [hypothesis, predicted, accuracy], feed_dict={X: x_data, Y: y_data} |
| 43 | + ) |
53 | 44 | print("\nHypothesis: ", h, "\nCorrect: ", c, "\nAccuracy: ", a)
|
54 | 45 |
|
55 | 46 | '''
|
|
0 commit comments