77""" 
88Please note, this code is only for python 3+. If you are using python 2+, please modify the code accordingly. 
99""" 
10- import  theano 
10+ import  theano 
11+ from  sklearn .datasets  import  load_boston 
12+ import  theano .tensor  as  T 
13+ import  numpy  as  np 
14+ import  matplotlib .pyplot  as  plt 
15+ 
16+ 
17+ class  Layer (object ):
18+     def  __init__ (self , inputs , in_size , out_size , activation_function = None ):
19+         self .W  =  theano .shared (np .random .normal (0 , 1 , (in_size , out_size )))
20+         self .b  =  theano .shared (np .zeros ((out_size , )) +  0.1 )
21+         self .Wx_plus_b  =  T .dot (inputs , self .W ) +  self .b 
22+         self .activation_function  =  activation_function 
23+         if  activation_function  is  None :
24+             self .outputs  =  self .Wx_plus_b 
25+         else :
26+             self .outputs  =  self .activation_function (self .Wx_plus_b )
27+ 
28+ 
29+ def  minmax_normalization (data ):
30+     xs_max  =  np .max (data , axis = 0 )
31+     xs_min  =  np .min (data , axis = 0 )
32+     xs  =  (1  -  0 ) *  (data  -  xs_min ) /  (xs_max  -  xs_min ) +  0 
33+     return  xs 
34+ 
35+ np .random .seed (100 )
36+ x_data  =  load_boston ().data 
37+ # minmax normalization, rescale the inputs 
38+ x_data  =  minmax_normalization (x_data )
39+ y_data  =  load_boston ().target [:, np .newaxis ]
40+ 
41+ # cross validation, train test data split 
42+ x_train , y_train  =  x_data [:400 ], y_data [:400 ]
43+ x_test , y_test  =  x_data [400 :], y_data [400 :]
44+ 
45+ x  =  T .dmatrix ("x" )
46+ y  =  T .dmatrix ("y" )
47+ 
48+ l1  =  Layer (x , 13 , 50 , T .tanh )
49+ l2  =  Layer (l1 .outputs , 50 , 1 , None )
50+ 
51+ # the way to compute cost 
52+ cost  =  T .mean (T .square (l2 .outputs  -  y ))      # without regularization 
53+ # cost = T.mean(T.square(l2.outputs - y)) + 0.1 * (l1.W ** 2).sum() + (l2.W ** 2).sum()  # with l2 regularization 
54+ # cost = T.mean(T.square(l2.outputs - y)) + 0.1 * abs(l1.W).sum() + abs(l2.W).sum()  # with l1 regularization 
55+ gW1 , gb1 , gW2 , gb2  =  T .grad (cost , [l1 .W , l1 .b , l2 .W , l2 .b ])
56+ 
57+ learning_rate  =  0.01 
58+ train  =  theano .function (
59+     inputs = [x , y ],
60+     updates = [(l1 .W , l1 .W  -  learning_rate  *  gW1 ),
61+              (l1 .b , l1 .b  -  learning_rate  *  gb1 ),
62+              (l2 .W , l2 .W  -  learning_rate  *  gW2 ),
63+              (l2 .b , l2 .b  -  learning_rate  *  gb2 )])
64+ 
65+ compute_cost  =  theano .function (inputs = [x , y ], outputs = cost )
66+ 
67+ # record cost 
68+ train_err_list  =  []
69+ test_err_list  =  []
70+ learning_time  =  []
71+ for  i  in  range (1000 ):
72+     train (x_train , y_train )
73+     if  i  %  10  ==  0 :
74+         # record cost 
75+         train_err_list .append (compute_cost (x_train , y_train ))
76+         test_err_list .append (compute_cost (x_test , y_test ))
77+         learning_time .append (i )
78+ 
79+ # plot cost history 
80+ plt .plot (learning_time , train_err_list , 'r-' )
81+ plt .plot (learning_time , test_err_list , 'b--' )
82+ plt .show ()
0 commit comments