2
2
import numpy as np
3
3
import dataprep
4
4
5
- sess = tf .InteractiveSession ()
5
+ class Classifier :
6
+
7
+ def __init__ (self ,first_hidden = 4 ):
6
8
7
- inputs = tf . placeholder ( tf . float32 , shape = [ None , 100 ] )
9
+ self . sess = tf . InteractiveSession ( )
8
10
9
- outputs = tf .placeholder (tf .float32 , shape = [None ,1 ])
11
+ self . inputs = tf .placeholder (tf .float32 , shape = [None ,100 ])
10
12
11
- first_hidden = 4
13
+ self . outputs = tf . placeholder ( tf . float32 , shape = [ None , 1 ])
12
14
13
- w1 = tf . Variable ( tf . truncated_normal ([ 100 , first_hidden ]))
15
+ self . first_hidden = first_hidden
14
16
15
- b1 = tf .Variable (tf .zeros ([ first_hidden ]))
17
+ self . w1 = tf .Variable (tf .truncated_normal ([ 100 , self . first_hidden ]))
16
18
17
- layer_1_output = tf .nn . sigmoid (tf .matmul ( inputs , w1 ) + b1 )
19
+ self . b1 = tf .Variable (tf .zeros ([ self . first_hidden ]) )
18
20
19
- w2 = tf .Variable (tf .truncated_normal ([ first_hidden , 100 ]) )
21
+ self . layer_1_output = tf .nn . sigmoid (tf .matmul ( self . inputs , self . w1 ) + self . b1 )
20
22
21
- b2 = tf .Variable (tf .zeros ([ 100 ]))
23
+ self . w2 = tf .Variable (tf .truncated_normal ([ self . first_hidden , 100 ]))
22
24
23
- layer_2_output = tf .nn . sigmoid (tf .matmul ( layer_1_output , w2 ) + b2 )
25
+ self . b2 = tf .Variable (tf .zeros ([ 100 ]) )
24
26
25
- w3 = tf .Variable (tf .truncated_normal ([ 100 , 1 ]) )
27
+ self . layer_2_output = tf .nn . sigmoid (tf .matmul ( self . layer_1_output , self . w2 ) + self . b2 )
26
28
27
- b3 = tf .Variable (tf .zeros ([ 1 ]))
29
+ self . w3 = tf .Variable (tf .truncated_normal ([ 100 , 1 ]))
28
30
29
- result = tf .nn .sigmoid (tf .matmul (layer_2_output ,w3 ) + b3 )
31
+ self .b3 = tf .Variable (tf .zeros ([1 ]))
32
+
33
+ def error_correction (self ):
30
34
31
- error = 0.5 * tf .reduce_sum (tf .subtract ( result , outputs ) * tf . subtract ( result , outputs ) )
35
+ result = tf .nn . sigmoid (tf .matmul ( self . layer_2_output , self . w3 ) + self . b3 )
32
36
33
- train_fixer = tf . train . GradientDescentOptimizer ( 0.05 ). minimize ( error )
37
+ self . error = 0.5 * tf . reduce_sum ( tf . subtract ( result , self . outputs ) * tf . subtract ( result , self . outputs ))
34
38
35
- sess . run ( tf .initialize_all_variables ())
39
+ self . train_fixer = tf .train . GradientDescentOptimizer ( 0.05 ). minimize ( self . error )
36
40
41
+ self .sess .run (tf .initialize_all_variables ())
42
+
43
+ def trainNN (self ,x ,y ):
37
44
38
- #training_inputs = [[0.0, 0.0], [0.0, 1.0], [1.0, 0.0], [1.0, 1.0]]
45
+ for i in range (0 ,3 ):
46
+ _ ,loss = self .sess .run ([self .train_fixer , self .error ],feed_dict =
47
+ {self .inputs :np .array (x ),self .outputs :np .array (y )})
48
+ print (loss )
39
49
40
50
41
- training_inputs = [dataprep .v1 ,dataprep .v2 ]
42
-
43
-
44
-
45
- output1 = [[0.0 ], [1.0 ]]
46
- print ("OUTPUT1" )
47
- print (output1 )
48
-
49
- for i in range (0 ,3 ):
50
- _ ,loss = sess .run ([train_fixer , error ],feed_dict =
51
- {inputs :np .array (training_inputs ),outputs :np .array (output1 )})
52
- print (loss )
51
+
52
+ model = Classifier ()
53
+ model .error_correction ()
54
+
55
+ x = [dataprep .v1 ,dataprep .v2 ]
56
+ y = [[0.0 ], [1.0 ]]
57
+ model .trainNN (x ,y )
0 commit comments