-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathL9 Sonar Different Network Sizes.py
More file actions
68 lines (58 loc) · 2.63 KB
/
L9 Sonar Different Network Sizes.py
File metadata and controls
68 lines (58 loc) · 2.63 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#import libraries
import numpy
import pandas
from keras.models import Sequential
from keras.layers import Dense
from keras.wrappers.scikit_learn import KerasClassifier
from sklearn.cross_validation import cross_val_score
from sklearn.preprocessing import LabelEncoder
from sklearn.cross_validation import StratifiedKFold
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import Pipeline
#load data
dataframe = pandas.read_csv("sonar.csv", header = None)
dataset = dataframe.values
# split into input (X) and output (Y)
X = dataset[:,0:60].astype(float)
Y = dataset[:,60]
# one hot encoding
encoder = LabelEncoder()
encoder.fit(Y)
encoded_Y = encoder.transform(Y)
def create_smaller():
#create a network with less neurons in first hidden layer
# 60i - 30 - 1o
model = Sequential()
model.add(Dense(30, input_dim = 60, init = 'normal', activation = 'relu'))
model.add(Dense(1, init = 'normal', activation = 'sigmoid'))
#compile model
model.compile(loss = 'binary_crossentropy', optimizer = 'adam', metrics = ['accuracy'])
return model
def create_larger():
#create a network with more hidden layers
# 60i - 60 - 30 - 1o
model = Sequential()
model.add(Dense(60, input_dim = 60, init = 'normal', activation = 'relu'))
model.add(Dense(30, init = 'normal', activation = 'relu'))
model.add(Dense(1, init = 'normal', activation = 'sigmoid'))
model.compile(loss = 'binary_crossentropy', optimizer = 'adam', metrics = ['accuracy'])
return model
seed = 7
numpy.random.seed(seed)
#evaluating the smaller network
estimatorsSmall = []
estimatorsSmall.append(('standardize', StandardScaler()))
estimatorsSmall.append(('mlp', KerasClassifier(build_fn = create_smaller, nb_epoch = 100, batch_size = 5, verbose = 0)))
pipelineSmall = Pipeline(estimatorsSmall)
kfold = StratifiedKFold(y = encoded_Y, n_folds = 10, shuffle = True, random_state = seed)
resultsSmall = cross_val_score(pipelineSmall, X, encoded_Y, cv = kfold)
#evaluating the larger network
estimatorsLarge = []
estimatorsLarge.append(('standardize', StandardScaler()))
estimatorsLarge.append(('mlp', KerasClassifier(build_fn = create_larger, nb_epoch = 100, batch_size = 5, verbose = 0)))
pipelineLarge = Pipeline(estimatorsLarge)
kfold = StratifiedKFold(y = encoded_Y, n_folds = 10, shuffle = True, random_state = seed)
resultsLarge = cross_val_score(pipelineLarge, X, encoded_Y, cv = kfold)
# results
print("Smaller: %.2f%% (%.2f%%)" % (resultsSmall.mean() * 100, resultsSmall.std() * 100))
print("Larger: %.2f%% (%.2f%%)" % (resultsLarge.mean() * 100, resultsLarge.std() * 100))