-
Notifications
You must be signed in to change notification settings - Fork 8
/
keras_first_example.py
84 lines (39 loc) · 1.03 KB
/
keras_first_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
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
81
82
83
84
# coding: utf-8
# In[1]:
from sklearn import datasets
from sklearn.metrics import log_loss
from keras.models import Sequential
from keras.layers import Dense, Activation
from keras.optimizers import SGD
from keras.utils.np_utils import to_categorical
# ## Data
# In[2]:
data = datasets.load_iris()
# In[3]:
X_train = data.data
# In[4]:
y_train = data.target
# In[5]:
X_train.shape
# In[6]:
y_train.shape
# In[7]:
y_train_binary = to_categorical(y_train)
# ## Model
# In[8]:
mdl = Sequential()
mdl.add(Dense(10, input_dim=4, activation='relu'))
mdl.add(Dense(output_dim=3, activation='softmax'))
# In[9]:
mdl.compile(loss='binary_crossentropy', optimizer=SGD(lr=0.01))
# In[10]:
mdl.fit(X_train, y_train_binary, nb_epoch=50, batch_size=32)
# In[11]:
pred_classes = mdl.predict_classes(X_train, batch_size=32)
pred_prob = mdl.predict_proba(X_train, batch_size=32)
# In[12]:
act = y_train_binary
pred = pred_prob
# In[13]:
log_loss(y_train_binary.reshape(-1,1), pred_prob.reshape(-1, 1))
# - https://keras.io/