Skip to content

Commit 47ad344

Browse files
authored
Merge pull request #1 from husnainfareed/master
binary classification using neural network
2 parents cf4ac2e + 856f895 commit 47ad344

File tree

1 file changed

+279
-0
lines changed

1 file changed

+279
-0
lines changed
Lines changed: 279 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,279 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 1,
6+
"metadata": {},
7+
"outputs": [
8+
{
9+
"name": "stderr",
10+
"output_type": "stream",
11+
"text": [
12+
"C:\\Users\\Hussnain\\Anaconda3\\envs\\tensorflow\\lib\\site-packages\\h5py\\__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.\n",
13+
" from ._conv import register_converters as _register_converters\n",
14+
"Using TensorFlow backend.\n"
15+
]
16+
}
17+
],
18+
"source": [
19+
"#Imports\n",
20+
"from keras.datasets import imdb\n",
21+
"\n",
22+
"from keras import models\n",
23+
"from keras import layers\n",
24+
"from keras import optimizers\n",
25+
"from keras import losses\n",
26+
"from keras import metrics,activations\n",
27+
"\n",
28+
"import matplotlib.pyplot as plt"
29+
]
30+
},
31+
{
32+
"cell_type": "code",
33+
"execution_count": null,
34+
"metadata": {},
35+
"outputs": [
36+
{
37+
"name": "stdout",
38+
"output_type": "stream",
39+
"text": [
40+
"Downloading data from https://s3.amazonaws.com/text-datasets/imdb.npz\n",
41+
" 1048576/17464789 [>.............................] - ETA: 53:49"
42+
]
43+
}
44+
],
45+
"source": [
46+
"#Downloading data from https://s3.amazonaws.com/text-datasets/imdb.npz\n",
47+
"\n",
48+
"(xtrain,ytrain), (xtest, ytest) = imdb.load_data(num_words=10000)"
49+
]
50+
},
51+
{
52+
"cell_type": "code",
53+
"execution_count": null,
54+
"metadata": {},
55+
"outputs": [],
56+
"source": [
57+
"#Exploring the dataset\n",
58+
"\n",
59+
"print('xtrain shape', xtrain.shape)\n",
60+
"print('ytrain shape', ytrain.shape)\n",
61+
"print()\n",
62+
"print('xtest shape', xtest.shape)\n",
63+
"print('ytest shape', ytest.shape)\n",
64+
"print()\n",
65+
"print('xtrain first review as dictionary index', xtrain[1])\n",
66+
"print()\n",
67+
"print()\n",
68+
"print('ytrain label', ytrain[0])"
69+
]
70+
},
71+
{
72+
"cell_type": "code",
73+
"execution_count": null,
74+
"metadata": {},
75+
"outputs": [],
76+
"source": [
77+
"#index to words mapping\n",
78+
"word_index = imdb.get_word_index()"
79+
]
80+
},
81+
{
82+
"cell_type": "code",
83+
"execution_count": null,
84+
"metadata": {},
85+
"outputs": [],
86+
"source": [
87+
"reverse_word_index = dict([(value, key) for (key, value) in word_index.items()])"
88+
]
89+
},
90+
{
91+
"cell_type": "code",
92+
"execution_count": null,
93+
"metadata": {},
94+
"outputs": [],
95+
"source": [
96+
"decode_review = ' '.join([reverse_word_index.get(i-3, reverse_word_index.get(i)) for i in xtrain[22]])\n",
97+
"decode_review"
98+
]
99+
},
100+
{
101+
"cell_type": "code",
102+
"execution_count": null,
103+
"metadata": {},
104+
"outputs": [],
105+
"source": [
106+
"import numpy as np\n",
107+
"\n",
108+
"def vectorize_sequences(sequences, dimension=10000):\n",
109+
" results = np.zeros((len(sequences), dimension))\n",
110+
" for i, sequence in enumerate(sequences):\n",
111+
" results[i, sequence] = 1. \n",
112+
" return results\n",
113+
"\n",
114+
"x_train = vectorize_sequences(xtrain)\n",
115+
"x_test = vectorize_sequences(xtest)"
116+
]
117+
},
118+
{
119+
"cell_type": "code",
120+
"execution_count": null,
121+
"metadata": {},
122+
"outputs": [],
123+
"source": [
124+
"ytrain = np.asarray(ytrain).astype('float32')\n",
125+
"ytest = np.asarray(ytest).astype('float32')"
126+
]
127+
},
128+
{
129+
"cell_type": "code",
130+
"execution_count": null,
131+
"metadata": {},
132+
"outputs": [],
133+
"source": [
134+
"#model\n",
135+
"model = models.Sequential()\n",
136+
"model.add(layers.Dense(16, activation=activations.relu, input_shape=(10000,)))\n",
137+
"model.add(layers.Dense(16, activation=activations.relu))\n",
138+
"model.add(layers.Dense(1, activation=activations.sigmoid))"
139+
]
140+
},
141+
{
142+
"cell_type": "code",
143+
"execution_count": null,
144+
"metadata": {},
145+
"outputs": [],
146+
"source": [
147+
"model.compile(optimizer=optimizers.RMSprop(lr=0.0001), loss=losses.mse, metrics=['acc'])"
148+
]
149+
},
150+
{
151+
"cell_type": "code",
152+
"execution_count": null,
153+
"metadata": {},
154+
"outputs": [],
155+
"source": [
156+
"x_val = x_train[:10000]\n",
157+
"y_val = ytrain[:10000]\n",
158+
"\n",
159+
"x_train_partial = x_train[10000:]\n",
160+
"y_train_partial = ytrain[10000:]"
161+
]
162+
},
163+
{
164+
"cell_type": "code",
165+
"execution_count": null,
166+
"metadata": {},
167+
"outputs": [],
168+
"source": [
169+
"history = model.fit(x_train_partial, y_train_partial, epochs=4, batch_size=512, validation_data=(x_val,y_val))\n",
170+
"history_dict = history.history\n",
171+
"history_dict.keys()\n",
172+
"print(history.history['acc'][-1])\n",
173+
"print(history.history['val_acc'][-1])"
174+
]
175+
},
176+
{
177+
"cell_type": "code",
178+
"execution_count": null,
179+
"metadata": {},
180+
"outputs": [],
181+
"source": [
182+
"print(model.predict(x_train_partial[22:23]))"
183+
]
184+
},
185+
{
186+
"cell_type": "code",
187+
"execution_count": null,
188+
"metadata": {},
189+
"outputs": [],
190+
"source": [
191+
"loss = history_dict['loss']\n",
192+
"val_loss = history_dict['val_loss']\n",
193+
"epochs = range(0, len(loss)+1)\n",
194+
"epochs"
195+
]
196+
},
197+
{
198+
"cell_type": "code",
199+
"execution_count": null,
200+
"metadata": {},
201+
"outputs": [],
202+
"source": [
203+
"%matplotlib\n",
204+
"acc = history.history['acc']\n",
205+
"val_acc = history.history['val_acc']\n",
206+
"loss = history.history['loss']\n",
207+
"val_loss = history.history['val_loss']\n",
208+
"\n",
209+
"epochs = range(1, len(acc) + 1)\n",
210+
"\n",
211+
"# \"bo\" is for \"blue dot\"\n",
212+
"plt.plot(epochs, loss, 'ro', label='Training loss')\n",
213+
"# b is for \"solid blue line\"\n",
214+
"plt.plot(epochs, val_loss, 'b', label='Validation loss')\n",
215+
"plt.title('Training and validation loss')\n",
216+
"plt.xlabel('Epochs')\n",
217+
"plt.ylabel('Loss')\n",
218+
"plt.legend()\n",
219+
"\n",
220+
"plt.show()"
221+
]
222+
},
223+
{
224+
"cell_type": "code",
225+
"execution_count": null,
226+
"metadata": {},
227+
"outputs": [],
228+
"source": [
229+
"plt.clf() # clear figure# clear \n",
230+
"acc_values = history_dict['acc']\n",
231+
"val_acc_values = history_dict['val_acc']\n",
232+
"\n",
233+
"plt.plot(epochs, acc, 'bo', label='Training acc')\n",
234+
"plt.plot(epochs, val_acc, 'b', label='Validation acc')\n",
235+
"plt.title('Training and validation accuracy')\n",
236+
"plt.xlabel('Epochs')\n",
237+
"plt.ylabel('Loss')\n",
238+
"plt.legend()\n",
239+
"\n",
240+
"plt.show()"
241+
]
242+
},
243+
{
244+
"cell_type": "code",
245+
"execution_count": null,
246+
"metadata": {},
247+
"outputs": [],
248+
"source": []
249+
},
250+
{
251+
"cell_type": "code",
252+
"execution_count": null,
253+
"metadata": {},
254+
"outputs": [],
255+
"source": []
256+
}
257+
],
258+
"metadata": {
259+
"kernelspec": {
260+
"display_name": "Python 3",
261+
"language": "python",
262+
"name": "python3"
263+
},
264+
"language_info": {
265+
"codemirror_mode": {
266+
"name": "ipython",
267+
"version": 3
268+
},
269+
"file_extension": ".py",
270+
"mimetype": "text/x-python",
271+
"name": "python",
272+
"nbconvert_exporter": "python",
273+
"pygments_lexer": "ipython3",
274+
"version": "3.6.5"
275+
}
276+
},
277+
"nbformat": 4,
278+
"nbformat_minor": 2
279+
}

0 commit comments

Comments
 (0)