cross_validation.ipynb
5.62 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"deletable": true,
"editable": true
},
"outputs": [],
"source": [
"from keras.models import Model\n",
"from keras.layers import Input, Dense, Dropout, Activation, BatchNormalization\n",
"from keras.optimizers import SGD, Adam\n",
"import numpy as np\n",
"from sklearn.model_selection import StratifiedKFold"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": true,
"editable": true
},
"source": [
"# Preparing data"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true,
"deletable": true,
"editable": true
},
"outputs": [],
"source": [
"filename = 'input_data.csv'\n",
"raw_data = open(filename, 'rt')\n",
"data = np.loadtxt(raw_data, delimiter= '\\t')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"deletable": true,
"editable": true
},
"outputs": [],
"source": [
"print data.shape"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": true,
"editable": true
},
"source": [
"Our dataset consists of ~466K examples (pairs of mentions), each example described by 1126 features. Labels say whether a pair belongs to the same cluster (1) or not (0)."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true,
"deletable": true,
"editable": true
},
"outputs": [],
"source": [
"size_of_dataset = 466852\n",
"number_of_features = 1126\n",
"\n",
"X = data[:,0:1126]\n",
"Y = data[:,1126] #last column consists of labels\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": true,
"editable": true
},
"source": [
"# 10-fold cross validation of the neural network model"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"deletable": true,
"editable": true
},
"outputs": [],
"source": [
"seed = 1\n",
"kfold = StratifiedKFold(n_splits=10, shuffle=True, random_state=seed)\n",
"cvscores = []\n",
"precision_scores = []\n",
"recall_scores = []\n",
"f1_scores = []\n",
"\n",
"for train, test in kfold.split(X, Y):\n",
"\n",
" inputs = Input(shape=(number_of_features,))\n",
" output_from_1st_layer = Dense(1000, activation='relu')(inputs)\n",
" output_from_1st_layer = Dropout(0.5)(output_from_1st_layer)\n",
" output_from_1st_layer = BatchNormalization()(output_from_1st_layer)\n",
" output_from_2nd_layer = Dense(500, activation='relu')(output_from_1st_layer)\n",
" output_from_2nd_layer = Dropout(0.5)(output_from_2nd_layer)\n",
" output_from_2nd_layer = BatchNormalization()(output_from_2nd_layer)\n",
" output = Dense(1, activation='sigmoid')(output_from_2nd_layer)\n",
"\n",
" model = Model(inputs, output)\n",
" model.compile(optimizer='Adam',loss='binary_crossentropy',metrics=['accuracy'])\n",
" model.fit(X[train], Y[train], batch_size=256, nb_epoch=25)\n",
" \n",
" # evaluate the model\n",
" scores = model.evaluate(X[test], Y[test])\n",
" print(\"%s: %.2f%%\" % (model.metrics_names[1], scores[1]*100))\n",
" cvscores.append(scores[1] * 100)\n",
"\n",
" #calculate other metrics: precision, recall, f1\n",
" predictions = model.predict(X[test])\n",
" true_positives = 0.0\n",
" false_positives = 0.0\n",
" true_negatives = 0.0\n",
" false_negatives = 0.0\n",
"\n",
" for i in range(len(X[test])):\n",
" if (predictions[i]<0.5 and Y[test][i]==0): true_negatives += 1 \n",
" if (predictions[i]<0.5 and Y[test][i]==1): false_negatives += 1\n",
" if (predictions[i]>=0.5 and Y[test][i]==1): true_positives += 1\n",
" if (predictions[i]>=0.5 and Y[test][i]==0): false_positives += 1 \n",
" \n",
" precision = true_positives/(true_positives+false_positives)\n",
" recall = true_positives/(true_positives+false_negatives)\n",
" f1 = 2*(precision*recall)/(precision+recall)\n",
"\n",
" precision_scores.append(precision)\n",
" recall_scores.append(recall)\n",
" f1_scores.append(f1)\n",
"\n",
" print ('Precision: ' + repr(precision))\n",
" print ('Recall: ' + repr(recall))\n",
" print ('F1: ' + repr(f1))"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false,
"deletable": true,
"editable": true
},
"source": [
"# Summary"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true,
"deletable": true,
"editable": true
},
"outputs": [],
"source": [
"print(\"%.2f%% (+/- %.2f%%)\" % (np.mean(cvscores), np.std(cvscores)))\n",
"print(\"%.2f%% (+/- %.2f%%)\" % (np.mean(precision_scores), np.std(precision_scores)))\n",
"print(\"%.2f%% (+/- %.2f%%)\" % (np.mean(recall_scores), np.std(recall_scores)))\n",
"print(\"%.2f%% (+/- %.2f%%)\" % (np.mean(f1_scores), np.std(f1_scores)))"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.10"
}
},
"nbformat": 4,
"nbformat_minor": 2
}