Skip to content
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
155 changes: 155 additions & 0 deletions Chapter01/keras.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Using TensorFlow backend.\n"
]
}
],
"source": [
"from keras.models import Sequential \n",
"from keras.layers import Dense\n",
"from keras import optimizers\n",
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"WARNING:tensorflow:From c:\\Users\\Crrea\\miniconda3\\envs\\neural-network-projects-python\\lib\\site-packages\\keras\\backend\\tensorflow_backend.py:74: The name tf.get_default_graph is deprecated. Please use tf.compat.v1.get_default_graph instead.\n",
"\n"
]
}
],
"source": [
"model = Sequential()"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"WARNING:tensorflow:From c:\\Users\\Crrea\\miniconda3\\envs\\neural-network-projects-python\\lib\\site-packages\\keras\\backend\\tensorflow_backend.py:517: The name tf.placeholder is deprecated. Please use tf.compat.v1.placeholder instead.\n",
"\n",
"WARNING:tensorflow:From c:\\Users\\Crrea\\miniconda3\\envs\\neural-network-projects-python\\lib\\site-packages\\keras\\backend\\tensorflow_backend.py:4138: The name tf.random_uniform is deprecated. Please use tf.random.uniform instead.\n",
"\n"
]
}
],
"source": [
"# Layer 1\n",
"model.add(Dense(units = 4, activation='sigmoid', input_dim = 3))\n",
"# Output layer\n",
"model.add(Dense(units=1, activation='sigmoid'))"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"_________________________________________________________________\n",
"Layer (type) Output Shape Param # \n",
"=================================================================\n",
"dense_1 (Dense) (None, 4) 16 \n",
"_________________________________________________________________\n",
"dense_2 (Dense) (None, 1) 5 \n",
"=================================================================\n",
"Total params: 21\n",
"Trainable params: 21\n",
"Non-trainable params: 0\n",
"_________________________________________________________________\n",
"None\n"
]
}
],
"source": [
"print(model.summary())"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"# Loss function keras\n",
"sgd = optimizers.SGD(lr=1)\n",
"model.compile(loss = 'mean_squared_error', optimizer=sgd)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[0.03039777]\n",
" [0.9787922 ]\n",
" [0.9621813 ]\n",
" [0.03268841]]\n"
]
}
],
"source": [
"X = np.array([[0,0,1],\n",
" [0,1,1],\n",
" [1,0,1],\n",
" [1,1,1]])\n",
"y = np.array([[0],[1],[1],[0]])\n",
"\n",
"# trainer model with fit\n",
"model.fit(X, y, epochs=1500, verbose=False)\n",
"\n",
"print(model.predict(X))"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "neural-network-projects-python",
"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.6.8"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading