Skip to content

adding three jupyter notebook for 7.1.1, 7.1.2 and 7.1.3 #167

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 117 additions & 0 deletions 7.1.1-introduction-to-funcational-API.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Model: \"sequential\"\n",
"_________________________________________________________________\n",
"Layer (type) Output Shape Param # \n",
"=================================================================\n",
"dense (Dense) (None, 32) 2080 \n",
"_________________________________________________________________\n",
"dense_1 (Dense) (None, 32) 1056 \n",
"_________________________________________________________________\n",
"dense_2 (Dense) (None, 10) 330 \n",
"=================================================================\n",
"Total params: 3,466\n",
"Trainable params: 3,466\n",
"Non-trainable params: 0\n",
"_________________________________________________________________\n",
"Model: \"model\"\n",
"_________________________________________________________________\n",
"Layer (type) Output Shape Param # \n",
"=================================================================\n",
"input_1 (InputLayer) [(None, 64)] 0 \n",
"_________________________________________________________________\n",
"dense_3 (Dense) (None, 32) 2080 \n",
"_________________________________________________________________\n",
"dense_4 (Dense) (None, 32) 1056 \n",
"_________________________________________________________________\n",
"dense_5 (Dense) (None, 10) 330 \n",
"=================================================================\n",
"Total params: 3,466\n",
"Trainable params: 3,466\n",
"Non-trainable params: 0\n",
"_________________________________________________________________\n"
]
}
],
"source": [
"from keras.models import Sequential, Model \n",
"from keras import layers\n",
"from keras import Input\n",
"seq_model = Sequential()\n",
"seq_model.add(layers.Dense(32, activation='relu', input_shape=(64,))) \n",
"seq_model.add(layers.Dense(32, activation='relu')) \n",
"seq_model.add(layers.Dense(10, activation='softmax'))\n",
"seq_model.summary()\n",
"\n",
"input_tensor = Input(shape=(64,))\n",
"x = layers.Dense(32, activation='relu')(input_tensor)\n",
"x = layers.Dense(32, activation='relu')(x)\n",
"output_tensor = layers.Dense(10, activation='softmax')(x)\n",
"model = Model(input_tensor, output_tensor) \n",
"model.summary() "
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"ename": "SyntaxError",
"evalue": "invalid syntax (<ipython-input-3-233ca56a8921>, line 6)",
"output_type": "error",
"traceback": [
"\u001b[0;36m File \u001b[0;32m\"<ipython-input-3-233ca56a8921>\"\u001b[0;36m, line \u001b[0;32m6\u001b[0m\n\u001b[0;31m Generates dummy Numpy data to train on\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n"
]
}
],
"source": [
"model.compile(optimizer='rmsprop', loss='categorical_crossentropy')\n",
"\n",
"import numpy as np\n",
"x_train = np.random.random((1000, 64)) \n",
"y_train = np.random.random((1000, 10))\n",
"\n",
"model.fit(x_train, y_train, epochs=10, batch_size=128)\n",
"score = model.evaluate(x_train, y_train)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.5"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
181 changes: 181 additions & 0 deletions 7.1.2-build-a-concatenate-network.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# two inputs, one output model"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Model: \"model_9\"\n",
"__________________________________________________________________________________________________\n",
"Layer (type) Output Shape Param # Connected to \n",
"==================================================================================================\n",
"text (InputLayer) [(None, None)] 0 \n",
"__________________________________________________________________________________________________\n",
"question (InputLayer) [(None, None)] 0 \n",
"__________________________________________________________________________________________________\n",
"embedding_20 (Embedding) (None, None, 64) 640000 text[0][0] \n",
"__________________________________________________________________________________________________\n",
"embedding_21 (Embedding) (None, None, 32) 320000 question[0][0] \n",
"__________________________________________________________________________________________________\n",
"lstm_20 (LSTM) (None, 32) 12416 embedding_20[0][0] \n",
"__________________________________________________________________________________________________\n",
"lstm_21 (LSTM) (None, 16) 3136 embedding_21[0][0] \n",
"__________________________________________________________________________________________________\n",
"concatenate_8 (Concatenate) (None, 48) 0 lstm_20[0][0] \n",
" lstm_21[0][0] \n",
"__________________________________________________________________________________________________\n",
"dense_8 (Dense) (None, 500) 24500 concatenate_8[0][0] \n",
"==================================================================================================\n",
"Total params: 1,000,052\n",
"Trainable params: 1,000,052\n",
"Non-trainable params: 0\n",
"__________________________________________________________________________________________________\n",
"None\n"
]
}
],
"source": [
"from keras.models import Model\n",
"from keras import layers\n",
"from keras import Input\n",
"\n",
"text_vocabulary_size = 10000\n",
"question_vocabulary_size = 10000\n",
"answer_vocabulary_size = 500\n",
"\n",
"text_input = Input(shape=(None,), dtype = 'int32', name ='text')\n",
"embedded_text = layers.Embedding(text_vocabulary_size,64)(text_input)\n",
"encoded_text = layers.LSTM(32)(embedded_text)\n",
"\n",
"question_input = Input(shape=(None,), dtype = 'int32', name = 'question')\n",
"embedded_question = layers.Embedding(question_vocabulary_size,32)(question_input)\n",
"encoded_question = layers.LSTM(16)(embedded_question)\n",
"\n",
"# concatenate two inputs and give output\n",
"concatenated = layers.concatenate([encoded_text, encoded_question], axis=-1)\n",
"answer = layers.Dense(answer_vocabulary_size, activation='softmax')(concatenated)\n",
"\n",
"model = Model([text_input, question_input], answer)\n",
"\n",
"model.compile(optimizer = 'rmsprop', loss='categorical_crossentropy', metrics=['acc'])\n",
"print(model.summary())"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Epoch 1/10\n",
"8/8 [==============================] - 2s 37ms/step - loss: 1553.1508 - acc: 0.0012\n",
"Epoch 2/10\n",
"8/8 [==============================] - 0s 39ms/step - loss: 1556.4614 - acc: 0.0000e+00\n",
"Epoch 3/10\n",
"8/8 [==============================] - 0s 38ms/step - loss: 1562.3912 - acc: 0.0000e+00\n",
"Epoch 4/10\n",
"8/8 [==============================] - 0s 36ms/step - loss: 1568.0669 - acc: 0.0000e+00\n",
"Epoch 5/10\n",
"8/8 [==============================] - 0s 37ms/step - loss: 1570.9047 - acc: 0.0000e+00\n",
"Epoch 6/10\n",
"8/8 [==============================] - 0s 37ms/step - loss: 1569.9131 - acc: 0.0000e+00\n",
"Epoch 7/10\n",
"8/8 [==============================] - 0s 40ms/step - loss: 1575.4770 - acc: 0.0000e+00\n",
"Epoch 8/10\n",
"8/8 [==============================] - 0s 43ms/step - loss: 1572.6059 - acc: 0.0000e+00\n",
"Epoch 9/10\n",
"8/8 [==============================] - 0s 35ms/step - loss: 1572.1957 - acc: 0.0000e+00\n",
"Epoch 10/10\n",
"8/8 [==============================] - 0s 36ms/step - loss: 1572.1025 - acc: 0.0000e+00\n"
]
},
{
"data": {
"text/plain": [
"<tensorflow.python.keras.callbacks.History at 0x7fc58901a430>"
]
},
"execution_count": 35,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import numpy as np\n",
"\n",
"num_samples = 1000\n",
"max_length = 100\n",
"\n",
"text = np.random.randint(1, text_vocabulary_size, size = (num_samples, max_length))\n",
"\n",
"question = np.random.randint(1, question_vocabulary_size, size = (num_samples, max_length))\n",
"\n",
"answers = np.random.randint(0,2, size = (num_samples, answer_vocabulary_size))\n",
"\n",
"\n",
"model.fit([text, question], answers, epochs = 10, batch_size = 128)\n",
"#model.fit({\"text\":text, \"question\":question}, answers, epochs=10, batch_size=128)\n"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<KerasTensor: shape=(None, 1000, 100) dtype=int32 (created by layer 'text')>"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.5"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Loading