1
1
import numpy as np
2
+ import scipy .special as sp
2
3
import matplotlib .pyplot as plt
3
4
4
- # import scipy.special as sp
5
- # scipy is broken in Windows PyCharm environment. Use np.exp instead.
6
- def sigmoid (x ):
7
- return 1 / (1 + np .exp (- x ))
8
-
9
5
class NeuralNetwork :
10
6
def __init__ (self , input_nodes , hidden_nodes , output_nodes , learning_rate ):
11
7
# three layers
@@ -21,7 +17,8 @@ def __init__(self, input_nodes, hidden_nodes, output_nodes, learning_rate):
21
17
self .who = np .random .normal (0.0 , pow (self .onodes , - 0.5 ), (self .onodes , self .hnodes ))
22
18
23
19
# activation function - sigmoid
24
- self .activation_function = lambda x : sigmoid (x )
20
+ # scipy.special.expit()
21
+ self .activation_function = lambda x : sp .expit (x )
25
22
26
23
def train (self , inputs_list , targets_list ):
27
24
# same as query()
@@ -83,10 +80,13 @@ def query(self, inputs_list):
83
80
query_input = test_data_list [np .random .randint (len (test_data_list ))].split (',' )
84
81
scaled_query_input = (np .asfarray (query_input [1 :])/ 255.0 * 0.99 ) + 0.01
85
82
83
+ digit = int (query_input [0 ])
86
84
# show the digit
87
- print ('Target digit: %s ' % (query_input [ 0 ] ))
85
+ print ('Target digit: %d ' % (digit ))
88
86
# show the output
89
- print n .query (scaled_query_input )
87
+ result = n .query (scaled_query_input )
88
+ print result
89
+ print result [digit ][0 ] / np .sum (result )
90
90
91
91
# image_array = np.asfarray(scaled_query_input).reshape((28, 28))
92
92
# plt.imshow(image_array, cmap='binary', interpolation='None')
0 commit comments