-
Notifications
You must be signed in to change notification settings - Fork 8
/
keras_classification.py
80 lines (44 loc) · 1.57 KB
/
keras_classification.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
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
69
70
71
72
73
74
75
76
77
78
79
80
# coding: utf-8
# Keras Classification example using
# - Sequential model
# - Functional API
# In[2]:
from ds_utils.imports import *
# In[3]:
iris = sklearn.datasets.load_iris()
X = iris.data
y = iris.target
# ## Benchmark
# In[4]:
sklearn.model_selection.cross_val_score(sklearn.dummy.DummyClassifier(), X, y, scoring='neg_log_loss')
# In[5]:
sklearn.model_selection.cross_val_score(sklearn.ensemble.RandomForestClassifier(), X, y, scoring='neg_log_loss')
# ## Sequential model
# In[6]:
y_cat = keras.utils.to_categorical(y)
# In[16]:
model = keras.models.Sequential()
model.add(keras.layers.Dense(32, activation='relu', input_dim=X.shape[1]))
model.add(keras.layers.Dense(16, activation='relu'))
model.add(keras.layers.Dense(y_cat.shape[1], activation='softmax'))
model.compile(optimizer='rmsprop',
loss='categorical_crossentropy',
metrics=['accuracy'])
model.fit(X, y_cat, epochs=100, batch_size=1)
# In[33]:
y_pred = model.predict(X)
# In[34]:
sklearn.metrics.log_loss(y_cat, y_pred)
# ## Functional API
# In[11]:
inputs = keras.layers.Input(shape=(X.shape[1],))
x = keras.layers.Dense(32, activation='relu')(inputs)
x = keras.layers.Dense(16, activation='relu')(x)
predictions = keras.layers.Dense(y_cat.shape[1], activation='softmax')(x)
model = keras.models.Model(inputs=inputs, outputs=predictions)
model.compile(optimizer='rmsprop',
loss='categorical_crossentropy',
metrics=['accuracy'])
model.fit(X, y_cat, epochs=10, batch_size=1)
# ## References
# - https://keras.io/getting-started/functional-api-guide/