Skip to content

Commit 159e89d

Browse files
authored
Add files via upload
1 parent 24a1318 commit 159e89d

14 files changed

+3380
-0
lines changed

Bidirectioanl GRU.ipynb

+126
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"#Bidirectional GRU"
10+
]
11+
},
12+
{
13+
"cell_type": "code",
14+
"execution_count": null,
15+
"metadata": {},
16+
"outputs": [],
17+
"source": [
18+
"from __future__ import print_function\n",
19+
"from sklearn.cross_validation import train_test_split\n",
20+
"import pandas as pd\n",
21+
"import numpy as np\n",
22+
"np.random.seed(1337) # for reproducibility\n",
23+
"from keras.preprocessing import sequence\n",
24+
"from keras.utils import np_utils\n",
25+
"from keras.models import Sequential\n",
26+
"from keras.layers import Dense, Dropout, Activation, Embedding\n",
27+
"from keras.layers import Dense, Dropout, Embedding, LSTM, Input, Bidirectional\n",
28+
"from keras.datasets import imdb\n",
29+
"from keras.utils.np_utils import to_categorical\n",
30+
"from sklearn.metrics import (precision_score, recall_score,\n",
31+
" f1_score, accuracy_score,mean_squared_error,mean_absolute_error)\n",
32+
"from sklearn import metrics\n",
33+
"from sklearn.preprocessing import Normalizer\n",
34+
"import h5py\n",
35+
"from keras import callbacks"
36+
]
37+
},
38+
{
39+
"cell_type": "code",
40+
"execution_count": null,
41+
"metadata": {},
42+
"outputs": [],
43+
"source": [
44+
"dataset = np.loadtxt(\"pima-indians-diabetes.csv\", delimiter=\",\")\n",
45+
"# split into input (X) and output (Y) variables\n",
46+
"X = dataset[:,0:8]\n",
47+
"Y = dataset[:,8]\n",
48+
"\n",
49+
"#normalize the data\n",
50+
"scaler = Normalizer().fit(X)\n",
51+
"X = scaler.transform(X)"
52+
]
53+
},
54+
{
55+
"cell_type": "code",
56+
"execution_count": null,
57+
"metadata": {},
58+
"outputs": [],
59+
"source": [
60+
"X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.33, random_state=42)"
61+
]
62+
},
63+
{
64+
"cell_type": "code",
65+
"execution_count": null,
66+
"metadata": {},
67+
"outputs": [],
68+
"source": [
69+
"# reshape input to be [samples, time steps, features]\n",
70+
"X_train = np.reshape(trainX, (trainX.shape[0], 1, trainX.shape[1]))\n",
71+
"X_test = np.reshape(testT, (testT.shape[0], 1, testT.shape[1]))"
72+
]
73+
},
74+
{
75+
"cell_type": "code",
76+
"execution_count": null,
77+
"metadata": {},
78+
"outputs": [],
79+
"source": [
80+
"# 1. define the network\n",
81+
"model = Sequential()\n",
82+
"model.add(Bidirectional(GRU(4),input_shape=(1, 8)))\n",
83+
"model.add(Dropout(0.1))\n",
84+
"model.add(Dense(1))\n",
85+
"model.add(Activation('sigmoid'))"
86+
]
87+
},
88+
{
89+
"cell_type": "code",
90+
"execution_count": null,
91+
"metadata": {},
92+
"outputs": [],
93+
"source": [
94+
"# try using different optimizers and different optimizer configs\n",
95+
"model.compile(loss='binary_crossentropy',optimizer='adam',metrics=['accuracy'])\n",
96+
"checkpointer = callbacks.ModelCheckpoint(filepath=\"logs/bidirectioanl-gru/checkpoint-{epoch:02d}.hdf5\", verbose=1, save_best_only=True, monitor='val_acc',mode='max')\n",
97+
"model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=1000, validation_data=(X_test, y_test),callbacks=[checkpointer])\n",
98+
"model.save(\"logs/bidirectioanl-gru/lstm1layer_model.hdf5\")\n",
99+
"\n",
100+
"loss, accuracy = model.evaluate(X_test, y_test)\n",
101+
"print(\"\\nLoss: %.2f, Accuracy: %.2f%%\" % (loss, accuracy*100))"
102+
]
103+
}
104+
],
105+
"metadata": {
106+
"kernelspec": {
107+
"display_name": "securetensor",
108+
"language": "python",
109+
"name": "securetensor"
110+
},
111+
"language_info": {
112+
"codemirror_mode": {
113+
"name": "ipython",
114+
"version": 2
115+
},
116+
"file_extension": ".py",
117+
"mimetype": "text/x-python",
118+
"name": "python",
119+
"nbconvert_exporter": "python",
120+
"pygments_lexer": "ipython2",
121+
"version": "2.7.12"
122+
}
123+
},
124+
"nbformat": 4,
125+
"nbformat_minor": 2
126+
}

Bidirectional-LSTM.ipynb

+126
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"#Bidirectional LSTM"
10+
]
11+
},
12+
{
13+
"cell_type": "code",
14+
"execution_count": null,
15+
"metadata": {},
16+
"outputs": [],
17+
"source": [
18+
"from __future__ import print_function\n",
19+
"from sklearn.cross_validation import train_test_split\n",
20+
"import pandas as pd\n",
21+
"import numpy as np\n",
22+
"np.random.seed(1337) # for reproducibility\n",
23+
"from keras.preprocessing import sequence\n",
24+
"from keras.utils import np_utils\n",
25+
"from keras.models import Sequential\n",
26+
"from keras.layers import Dense, Dropout, Activation, Embedding\n",
27+
"from keras.layers import Dense, Dropout, Embedding, LSTM, Input, Bidirectional\n",
28+
"from keras.datasets import imdb\n",
29+
"from keras.utils.np_utils import to_categorical\n",
30+
"from sklearn.metrics import (precision_score, recall_score,\n",
31+
" f1_score, accuracy_score,mean_squared_error,mean_absolute_error)\n",
32+
"from sklearn import metrics\n",
33+
"from sklearn.preprocessing import Normalizer\n",
34+
"import h5py\n",
35+
"from keras import callbacks"
36+
]
37+
},
38+
{
39+
"cell_type": "code",
40+
"execution_count": null,
41+
"metadata": {},
42+
"outputs": [],
43+
"source": [
44+
"dataset = np.loadtxt(\"pima-indians-diabetes.csv\", delimiter=\",\")\n",
45+
"# split into input (X) and output (Y) variables\n",
46+
"X = dataset[:,0:8]\n",
47+
"Y = dataset[:,8]\n",
48+
"\n",
49+
"#normalize the data\n",
50+
"scaler = Normalizer().fit(X)\n",
51+
"X = scaler.transform(X)"
52+
]
53+
},
54+
{
55+
"cell_type": "code",
56+
"execution_count": null,
57+
"metadata": {},
58+
"outputs": [],
59+
"source": [
60+
"X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.33, random_state=42)"
61+
]
62+
},
63+
{
64+
"cell_type": "code",
65+
"execution_count": null,
66+
"metadata": {},
67+
"outputs": [],
68+
"source": [
69+
"# reshape input to be [samples, time steps, features]\n",
70+
"X_train = np.reshape(trainX, (trainX.shape[0], 1, trainX.shape[1]))\n",
71+
"X_test = np.reshape(testT, (testT.shape[0], 1, testT.shape[1]))"
72+
]
73+
},
74+
{
75+
"cell_type": "code",
76+
"execution_count": null,
77+
"metadata": {},
78+
"outputs": [],
79+
"source": [
80+
"# 1. define the network\n",
81+
"model = Sequential()\n",
82+
"model.add(Bidirectional(LSTM(4),input_shape=(1, 8)))\n",
83+
"model.add(Dropout(0.1))\n",
84+
"model.add(Dense(1))\n",
85+
"model.add(Activation('sigmoid'))"
86+
]
87+
},
88+
{
89+
"cell_type": "code",
90+
"execution_count": null,
91+
"metadata": {},
92+
"outputs": [],
93+
"source": [
94+
"# try using different optimizers and different optimizer configs\n",
95+
"model.compile(loss='binary_crossentropy',optimizer='adam',metrics=['accuracy'])\n",
96+
"checkpointer = callbacks.ModelCheckpoint(filepath=\"logs/bidirectioanl-lstm/checkpoint-{epoch:02d}.hdf5\", verbose=1, save_best_only=True, monitor='val_acc',mode='max')\n",
97+
"model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=1000, validation_data=(X_test, y_test),callbacks=[checkpointer])\n",
98+
"model.save(\"logs/bidirectioanl-lstm/lstm1layer_model.hdf5\")\n",
99+
"\n",
100+
"loss, accuracy = model.evaluate(X_test, y_test)\n",
101+
"print(\"\\nLoss: %.2f, Accuracy: %.2f%%\" % (loss, accuracy*100))"
102+
]
103+
}
104+
],
105+
"metadata": {
106+
"kernelspec": {
107+
"display_name": "securetensor",
108+
"language": "python",
109+
"name": "securetensor"
110+
},
111+
"language_info": {
112+
"codemirror_mode": {
113+
"name": "ipython",
114+
"version": 2
115+
},
116+
"file_extension": ".py",
117+
"mimetype": "text/x-python",
118+
"name": "python",
119+
"nbconvert_exporter": "python",
120+
"pygments_lexer": "ipython2",
121+
"version": "2.7.12"
122+
}
123+
},
124+
"nbformat": 4,
125+
"nbformat_minor": 2
126+
}

Bidirectional-RNN.ipynb

+126
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"#Bidirectioanl RNN"
10+
]
11+
},
12+
{
13+
"cell_type": "code",
14+
"execution_count": null,
15+
"metadata": {},
16+
"outputs": [],
17+
"source": [
18+
"from __future__ import print_function\n",
19+
"from sklearn.cross_validation import train_test_split\n",
20+
"import pandas as pd\n",
21+
"import numpy as np\n",
22+
"np.random.seed(1337) # for reproducibility\n",
23+
"from keras.preprocessing import sequence\n",
24+
"from keras.utils import np_utils\n",
25+
"from keras.models import Sequential\n",
26+
"from keras.layers import Dense, Dropout, Activation, Embedding\n",
27+
"from keras.layers import Dense, Dropout, Embedding, LSTM, Input, Bidirectional\n",
28+
"from keras.datasets import imdb\n",
29+
"from keras.utils.np_utils import to_categorical\n",
30+
"from sklearn.metrics import (precision_score, recall_score,\n",
31+
" f1_score, accuracy_score,mean_squared_error,mean_absolute_error)\n",
32+
"from sklearn import metrics\n",
33+
"from sklearn.preprocessing import Normalizer\n",
34+
"import h5py\n",
35+
"from keras import callbacks"
36+
]
37+
},
38+
{
39+
"cell_type": "code",
40+
"execution_count": null,
41+
"metadata": {},
42+
"outputs": [],
43+
"source": [
44+
"dataset = np.loadtxt(\"pima-indians-diabetes.csv\", delimiter=\",\")\n",
45+
"# split into input (X) and output (Y) variables\n",
46+
"X = dataset[:,0:8]\n",
47+
"Y = dataset[:,8]\n",
48+
"\n",
49+
"#normalize the data\n",
50+
"scaler = Normalizer().fit(X)\n",
51+
"X = scaler.transform(X)"
52+
]
53+
},
54+
{
55+
"cell_type": "code",
56+
"execution_count": null,
57+
"metadata": {},
58+
"outputs": [],
59+
"source": [
60+
"X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.33, random_state=42)"
61+
]
62+
},
63+
{
64+
"cell_type": "code",
65+
"execution_count": null,
66+
"metadata": {},
67+
"outputs": [],
68+
"source": [
69+
"# reshape input to be [samples, time steps, features]\n",
70+
"X_train = np.reshape(trainX, (trainX.shape[0], 1, trainX.shape[1]))\n",
71+
"X_test = np.reshape(testT, (testT.shape[0], 1, testT.shape[1]))"
72+
]
73+
},
74+
{
75+
"cell_type": "code",
76+
"execution_count": null,
77+
"metadata": {},
78+
"outputs": [],
79+
"source": [
80+
"# 1. define the network\n",
81+
"model = Sequential()\n",
82+
"model.add(Bidirectional(SimpleRNN(4),input_shape=(1, 8)))\n",
83+
"model.add(Dropout(0.1))\n",
84+
"model.add(Dense(1))\n",
85+
"model.add(Activation('sigmoid'))"
86+
]
87+
},
88+
{
89+
"cell_type": "code",
90+
"execution_count": null,
91+
"metadata": {},
92+
"outputs": [],
93+
"source": [
94+
"# try using different optimizers and different optimizer configs\n",
95+
"model.compile(loss='binary_crossentropy',optimizer='adam',metrics=['accuracy'])\n",
96+
"checkpointer = callbacks.ModelCheckpoint(filepath=\"logs/bidirectioanl-rnn/checkpoint-{epoch:02d}.hdf5\", verbose=1, save_best_only=True, monitor='val_acc',mode='max')\n",
97+
"model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=1000, validation_data=(X_test, y_test),callbacks=[checkpointer])\n",
98+
"model.save(\"logs/bidirectioanl-rnn/lstm1layer_model.hdf5\")\n",
99+
"\n",
100+
"loss, accuracy = model.evaluate(X_test, y_test)\n",
101+
"print(\"\\nLoss: %.2f, Accuracy: %.2f%%\" % (loss, accuracy*100))"
102+
]
103+
}
104+
],
105+
"metadata": {
106+
"kernelspec": {
107+
"display_name": "securetensor",
108+
"language": "python",
109+
"name": "securetensor"
110+
},
111+
"language_info": {
112+
"codemirror_mode": {
113+
"name": "ipython",
114+
"version": 2
115+
},
116+
"file_extension": ".py",
117+
"mimetype": "text/x-python",
118+
"name": "python",
119+
"nbconvert_exporter": "python",
120+
"pygments_lexer": "ipython2",
121+
"version": "2.7.12"
122+
}
123+
},
124+
"nbformat": 4,
125+
"nbformat_minor": 2
126+
}

0 commit comments

Comments
 (0)