-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathkeras_example.py
31 lines (30 loc) · 967 Bytes
/
keras_example.py
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
from keras.models import Sequential
from keras.layers import Dense
import numpy
# fix random seed for reproducibility
numpy.random.seed(7)
# load pima indians dataset
dataset = numpy.loadtxt("pima-indians-diabetes.data", delimiter=",")
# split into input (X) and output (Y) variables
initial_split=0.7
X = dataset[:,0:8]
Y = dataset[:,8]
num_rows=len(Y)
split_index=int(initial_split*num_rows)
Xtr=X[:split_index,:]
Xte=X[split_index:,:]
ytr=Y[:split_index]
yte=Y[split_index:]
# create model
model = Sequential()
model.add(Dense(8, input_dim=8, activation='relu'))
model.add(Dense(8, activation='relu'))
model.add(Dense(8, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
# Compile model
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
# Fit the model
model.fit(Xtr, ytr, epochs=150, batch_size=10)
# evaluate the model
scores = model.evaluate(Xte, yte)
print("\n%s: %.2f%%" % (model.metrics_names[1], scores[1]*100))