-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy path2_Linear_Classifier.py
More file actions
31 lines (23 loc) · 897 Bytes
/
2_Linear_Classifier.py
File metadata and controls
31 lines (23 loc) · 897 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
import tensorflow as tf
import numpy as np
input_data=np.array([[0,0,1],[0,1,1],[1,0,1],[1,1,1]],dtype=np.float32)
output_data=np.array([[0,0,1,1]],dtype=np.float32).T
x =tf.placeholder(dtype=np.float32,shape=[4,3])
y_=tf.placeholder(dtype=np.float32,shape=[4,1])
weight=tf.Variable(np.random.random_sample([3,1]),dtype=tf.float32,name='weight')
bias=tf.Variable(np.random.random_sample([1]),dtype=tf.float32,name='bias')
y=tf.matmul(x,weight)+bias
Loss=tf.reduce_mean(tf.square(y-y_))
train=tf.train.GradientDescentOptimizer(learning_rate=1e-2).minimize(Loss)
feed_dict={x:input_data, y_:output_data}
init=tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
for i in range(10000):
_,l=sess.run([train,Loss],feed_dict=feed_dict)
if i%100==0:
print(l)
print(Loss)
print(y)
print(sess.run(Loss,feed_dict=feed_dict))
print(sess.run(y,feed_dict=feed_dict))