-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtensorflow_practice.py
More file actions
40 lines (27 loc) · 979 Bytes
/
tensorflow_practice.py
File metadata and controls
40 lines (27 loc) · 979 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# -*- coding: utf-8 -*-
"""
Created on Sun Sep 3 02:21:52 2017
@author: zhaoweiheng
"""
import numpy as np
import tensorflow as tf
tf.set_random_seed(0)
w = tf.Variable(tf.zeros([2, 1]))
b = tf.Variable(tf.zeros([1]))
x = tf.placeholder(tf.float32, shape=[None, 2])
t = tf.placeholder(tf.float32, shape=[None, 1])
y = tf.nn.sigmoid(tf.matmul(x, w) + b)
cross_entropy = - tf.reduce_sum(t * tf.log(y) + (1 - t) * tf.log(1 - y))
train_step = tf.train.GradientDescentOptimizer(0.1).minimize(cross_entropy)
correct_prediction = tf.equal(tf.to_float(tf.greater(y, 0.5)), t)
X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
Y = np.array([[0], [1], [1], [1]])
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)
for epoch in range(200):
sess.run(train_step, feed_dict={x: X, t: Y})
classified = correct_prediction.eval(session=sess, feed_dict={x: X, t: Y})
prob = y.eval(session=sess, feed_dict={x: X, t: Y})
print(sess.run(w))
print(sess.run(b))