Upload folder using huggingface_hub
Browse files
.ipynb_checkpoints/Chapter1-NeuralNetwork-checkpoint.ipynb
ADDED
|
@@ -0,0 +1,266 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "code",
|
| 5 |
+
"execution_count": 2,
|
| 6 |
+
"id": "2b22b7ad-9329-477a-a8f4-583fb23a8471",
|
| 7 |
+
"metadata": {},
|
| 8 |
+
"outputs": [],
|
| 9 |
+
"source": [
|
| 10 |
+
"import numpy as np\n",
|
| 11 |
+
"import tensorflow as tf\n",
|
| 12 |
+
"from tensorflow import keras\n",
|
| 13 |
+
"import pandas as pd\n",
|
| 14 |
+
"import matplotlib\n",
|
| 15 |
+
"matplotlib.use(\"TkAgg\")\n",
|
| 16 |
+
"import matplotlib.pyplot as plt"
|
| 17 |
+
]
|
| 18 |
+
},
|
| 19 |
+
{
|
| 20 |
+
"cell_type": "code",
|
| 21 |
+
"execution_count": 3,
|
| 22 |
+
"id": "8b9896be-f135-4c8d-9c00-c6fe7e444172",
|
| 23 |
+
"metadata": {},
|
| 24 |
+
"outputs": [
|
| 25 |
+
{
|
| 26 |
+
"name": "stdout",
|
| 27 |
+
"output_type": "stream",
|
| 28 |
+
"text": [
|
| 29 |
+
"(60000, 28, 28)\n",
|
| 30 |
+
"uint8\n"
|
| 31 |
+
]
|
| 32 |
+
}
|
| 33 |
+
],
|
| 34 |
+
"source": [
|
| 35 |
+
"fashion_mnist = keras.datasets.fashion_mnist\n",
|
| 36 |
+
"\n",
|
| 37 |
+
"(x_train_full, y_train_full), (x_test, y_test) = fashion_mnist.load_data()\n",
|
| 38 |
+
"x_test = x_test / 255.0\n",
|
| 39 |
+
"\n",
|
| 40 |
+
"print(x_train_full.shape)\n",
|
| 41 |
+
"print(x_train_full.dtype)\n",
|
| 42 |
+
"\n",
|
| 43 |
+
"x_valid, x_train = x_train_full[:5000] / 255.0, x_train_full[5000:] / 255.0\n",
|
| 44 |
+
"y_valid, y_train = y_train_full[:5000], y_train_full[5000:]\n",
|
| 45 |
+
"\n",
|
| 46 |
+
"class_names = [\n",
|
| 47 |
+
" \"T-shirt/top\", \"Trouser\", \"Pullover\", \"Dress\", \"Coat\",\n",
|
| 48 |
+
" \"Sandal\", \"Shirt\", \"Sneaker\", \"Bag\", \"Ankle boot\"\n",
|
| 49 |
+
"]"
|
| 50 |
+
]
|
| 51 |
+
},
|
| 52 |
+
{
|
| 53 |
+
"cell_type": "code",
|
| 54 |
+
"execution_count": 4,
|
| 55 |
+
"id": "c4514be8-3e16-4f63-bea4-adc8e2576e43",
|
| 56 |
+
"metadata": {},
|
| 57 |
+
"outputs": [
|
| 58 |
+
{
|
| 59 |
+
"name": "stderr",
|
| 60 |
+
"output_type": "stream",
|
| 61 |
+
"text": [
|
| 62 |
+
"C:\\Users\\lacer\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python312\\site-packages\\keras\\src\\layers\\reshaping\\flatten.py:37: UserWarning: Do not pass an `input_shape`/`input_dim` argument to a layer. When using Sequential models, prefer using an `Input(shape)` object as the first layer in the model instead.\n",
|
| 63 |
+
" super().__init__(**kwargs)\n"
|
| 64 |
+
]
|
| 65 |
+
}
|
| 66 |
+
],
|
| 67 |
+
"source": [
|
| 68 |
+
"model = keras.models.Sequential()\n",
|
| 69 |
+
"model.add(keras.layers.Flatten(input_shape=(28, 28)))\n",
|
| 70 |
+
"model.add(keras.layers.Dense(300, activation='relu'))\n",
|
| 71 |
+
"model.add(keras.layers.Dense(100, activation='relu'))\n",
|
| 72 |
+
"model.add(keras.layers.Dense(10, activation='softmax'))"
|
| 73 |
+
]
|
| 74 |
+
},
|
| 75 |
+
{
|
| 76 |
+
"cell_type": "code",
|
| 77 |
+
"execution_count": 6,
|
| 78 |
+
"id": "80db0e8a-cc96-4752-8bec-60315ad34bec",
|
| 79 |
+
"metadata": {},
|
| 80 |
+
"outputs": [
|
| 81 |
+
{
|
| 82 |
+
"name": "stdout",
|
| 83 |
+
"output_type": "stream",
|
| 84 |
+
"text": [
|
| 85 |
+
"WARNING:tensorflow:TensorFlow GPU support is not available on native Windows for TensorFlow >= 2.11. Even if CUDA/cuDNN are installed, GPU will not be used. Please use WSL2 or the TensorFlow-DirectML plugin.\n",
|
| 86 |
+
"Epoch 1/30\n",
|
| 87 |
+
"\u001b[1m1719/1719\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m5s\u001b[0m 3ms/step - accuracy: 0.7659 - loss: 0.7165 - val_accuracy: 0.8362 - val_loss: 0.5032\n",
|
| 88 |
+
"Epoch 2/30\n",
|
| 89 |
+
"\u001b[1m1719/1719\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m5s\u001b[0m 3ms/step - accuracy: 0.8292 - loss: 0.4888 - val_accuracy: 0.8542 - val_loss: 0.4361\n",
|
| 90 |
+
"Epoch 3/30\n",
|
| 91 |
+
"\u001b[1m1719/1719\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m5s\u001b[0m 3ms/step - accuracy: 0.8441 - loss: 0.4461 - val_accuracy: 0.8642 - val_loss: 0.4142\n",
|
| 92 |
+
"Epoch 4/30\n",
|
| 93 |
+
"\u001b[1m1719/1719\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 3ms/step - accuracy: 0.8529 - loss: 0.4180 - val_accuracy: 0.8654 - val_loss: 0.3972\n",
|
| 94 |
+
"Epoch 5/30\n",
|
| 95 |
+
"\u001b[1m1719/1719\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 3ms/step - accuracy: 0.8600 - loss: 0.3981 - val_accuracy: 0.8600 - val_loss: 0.3943\n",
|
| 96 |
+
"Epoch 6/30\n",
|
| 97 |
+
"\u001b[1m1719/1719\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 3ms/step - accuracy: 0.8655 - loss: 0.3811 - val_accuracy: 0.8694 - val_loss: 0.3724\n",
|
| 98 |
+
"Epoch 7/30\n",
|
| 99 |
+
"\u001b[1m1719/1719\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 3ms/step - accuracy: 0.8690 - loss: 0.3680 - val_accuracy: 0.8668 - val_loss: 0.3823\n",
|
| 100 |
+
"Epoch 8/30\n",
|
| 101 |
+
"\u001b[1m1719/1719\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m5s\u001b[0m 3ms/step - accuracy: 0.8726 - loss: 0.3573 - val_accuracy: 0.8724 - val_loss: 0.3629\n",
|
| 102 |
+
"Epoch 9/30\n",
|
| 103 |
+
"\u001b[1m1719/1719\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m5s\u001b[0m 3ms/step - accuracy: 0.8777 - loss: 0.3461 - val_accuracy: 0.8768 - val_loss: 0.3452\n",
|
| 104 |
+
"Epoch 10/30\n",
|
| 105 |
+
"\u001b[1m1719/1719\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 3ms/step - accuracy: 0.8804 - loss: 0.3365 - val_accuracy: 0.8792 - val_loss: 0.3440\n",
|
| 106 |
+
"Epoch 11/30\n",
|
| 107 |
+
"\u001b[1m1719/1719\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 3ms/step - accuracy: 0.8837 - loss: 0.3282 - val_accuracy: 0.8698 - val_loss: 0.3532\n",
|
| 108 |
+
"Epoch 12/30\n",
|
| 109 |
+
"\u001b[1m1719/1719\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 3ms/step - accuracy: 0.8858 - loss: 0.3197 - val_accuracy: 0.8814 - val_loss: 0.3315\n",
|
| 110 |
+
"Epoch 13/30\n",
|
| 111 |
+
"\u001b[1m1719/1719\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 3ms/step - accuracy: 0.8886 - loss: 0.3127 - val_accuracy: 0.8804 - val_loss: 0.3382\n",
|
| 112 |
+
"Epoch 14/30\n",
|
| 113 |
+
"\u001b[1m1719/1719\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 3ms/step - accuracy: 0.8911 - loss: 0.3053 - val_accuracy: 0.8738 - val_loss: 0.3601\n",
|
| 114 |
+
"Epoch 15/30\n",
|
| 115 |
+
"\u001b[1m1719/1719\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 3ms/step - accuracy: 0.8923 - loss: 0.2997 - val_accuracy: 0.8816 - val_loss: 0.3278\n",
|
| 116 |
+
"Epoch 16/30\n",
|
| 117 |
+
"\u001b[1m1719/1719\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 3ms/step - accuracy: 0.8951 - loss: 0.2938 - val_accuracy: 0.8834 - val_loss: 0.3259\n",
|
| 118 |
+
"Epoch 17/30\n",
|
| 119 |
+
"\u001b[1m1719/1719\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 3ms/step - accuracy: 0.8983 - loss: 0.2865 - val_accuracy: 0.8850 - val_loss: 0.3281\n",
|
| 120 |
+
"Epoch 18/30\n",
|
| 121 |
+
"\u001b[1m1719/1719\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 3ms/step - accuracy: 0.9000 - loss: 0.2809 - val_accuracy: 0.8832 - val_loss: 0.3194\n",
|
| 122 |
+
"Epoch 19/30\n",
|
| 123 |
+
"\u001b[1m1719/1719\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 3ms/step - accuracy: 0.9008 - loss: 0.2760 - val_accuracy: 0.8886 - val_loss: 0.3071\n",
|
| 124 |
+
"Epoch 20/30\n",
|
| 125 |
+
"\u001b[1m1719/1719\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 3ms/step - accuracy: 0.9016 - loss: 0.2716 - val_accuracy: 0.8820 - val_loss: 0.3401\n",
|
| 126 |
+
"Epoch 21/30\n",
|
| 127 |
+
"\u001b[1m1719/1719\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 2ms/step - accuracy: 0.9035 - loss: 0.2662 - val_accuracy: 0.8890 - val_loss: 0.3063\n",
|
| 128 |
+
"Epoch 22/30\n",
|
| 129 |
+
"\u001b[1m1719/1719\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 3ms/step - accuracy: 0.9061 - loss: 0.2614 - val_accuracy: 0.8900 - val_loss: 0.3113\n",
|
| 130 |
+
"Epoch 23/30\n",
|
| 131 |
+
"\u001b[1m1719/1719\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m5s\u001b[0m 3ms/step - accuracy: 0.9069 - loss: 0.2565 - val_accuracy: 0.8888 - val_loss: 0.3085\n",
|
| 132 |
+
"Epoch 24/30\n",
|
| 133 |
+
"\u001b[1m1719/1719\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m5s\u001b[0m 3ms/step - accuracy: 0.9091 - loss: 0.2524 - val_accuracy: 0.8842 - val_loss: 0.3263\n",
|
| 134 |
+
"Epoch 25/30\n",
|
| 135 |
+
"\u001b[1m1719/1719\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 3ms/step - accuracy: 0.9102 - loss: 0.2487 - val_accuracy: 0.8894 - val_loss: 0.3044\n",
|
| 136 |
+
"Epoch 26/30\n",
|
| 137 |
+
"\u001b[1m1719/1719\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 3ms/step - accuracy: 0.9108 - loss: 0.2446 - val_accuracy: 0.8914 - val_loss: 0.2958\n",
|
| 138 |
+
"Epoch 27/30\n",
|
| 139 |
+
"\u001b[1m1719/1719\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 3ms/step - accuracy: 0.9133 - loss: 0.2392 - val_accuracy: 0.8788 - val_loss: 0.3307\n",
|
| 140 |
+
"Epoch 28/30\n",
|
| 141 |
+
"\u001b[1m1719/1719\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 3ms/step - accuracy: 0.9149 - loss: 0.2362 - val_accuracy: 0.8914 - val_loss: 0.2978\n",
|
| 142 |
+
"Epoch 29/30\n",
|
| 143 |
+
"\u001b[1m1719/1719\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 3ms/step - accuracy: 0.9171 - loss: 0.2317 - val_accuracy: 0.8886 - val_loss: 0.3139\n",
|
| 144 |
+
"Epoch 30/30\n",
|
| 145 |
+
"\u001b[1m1719/1719\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 3ms/step - accuracy: 0.9177 - loss: 0.2281 - val_accuracy: 0.8922 - val_loss: 0.3038\n"
|
| 146 |
+
]
|
| 147 |
+
}
|
| 148 |
+
],
|
| 149 |
+
"source": [
|
| 150 |
+
"model.compile(\n",
|
| 151 |
+
" loss=\"sparse_categorical_crossentropy\",\n",
|
| 152 |
+
" optimizer=\"sgd\",\n",
|
| 153 |
+
" metrics=[\"accuracy\"]\n",
|
| 154 |
+
")\n",
|
| 155 |
+
"\n",
|
| 156 |
+
"history = model.fit(\n",
|
| 157 |
+
" x_train,\n",
|
| 158 |
+
" y_train,\n",
|
| 159 |
+
" epochs=30 ,\n",
|
| 160 |
+
" validation_data=(x_valid, y_valid)\n",
|
| 161 |
+
")"
|
| 162 |
+
]
|
| 163 |
+
},
|
| 164 |
+
{
|
| 165 |
+
"cell_type": "code",
|
| 166 |
+
"execution_count": 8,
|
| 167 |
+
"id": "2a081c04-338c-40e6-89d1-857ec6ca3232",
|
| 168 |
+
"metadata": {},
|
| 169 |
+
"outputs": [],
|
| 170 |
+
"source": [
|
| 171 |
+
"pd.DataFrame(history.history).plot(figsize=(8, 5))\n",
|
| 172 |
+
"plt.grid(True)\n",
|
| 173 |
+
"plt.gca().set_ylim(0, 1)\n",
|
| 174 |
+
"plt.show()\n"
|
| 175 |
+
]
|
| 176 |
+
},
|
| 177 |
+
{
|
| 178 |
+
"cell_type": "code",
|
| 179 |
+
"execution_count": 9,
|
| 180 |
+
"id": "7cd5d8ac-6a3e-4a92-b4ef-574923bf4b71",
|
| 181 |
+
"metadata": {},
|
| 182 |
+
"outputs": [
|
| 183 |
+
{
|
| 184 |
+
"name": "stdout",
|
| 185 |
+
"output_type": "stream",
|
| 186 |
+
"text": [
|
| 187 |
+
"\u001b[1m313/313\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m1s\u001b[0m 2ms/step - accuracy: 0.8787 - loss: 0.3378\n"
|
| 188 |
+
]
|
| 189 |
+
},
|
| 190 |
+
{
|
| 191 |
+
"data": {
|
| 192 |
+
"text/plain": [
|
| 193 |
+
"[0.33777710795402527, 0.8787000179290771]"
|
| 194 |
+
]
|
| 195 |
+
},
|
| 196 |
+
"execution_count": 9,
|
| 197 |
+
"metadata": {},
|
| 198 |
+
"output_type": "execute_result"
|
| 199 |
+
}
|
| 200 |
+
],
|
| 201 |
+
"source": [
|
| 202 |
+
"model.evaluate(x_test, y_test)\n"
|
| 203 |
+
]
|
| 204 |
+
},
|
| 205 |
+
{
|
| 206 |
+
"cell_type": "code",
|
| 207 |
+
"execution_count": 12,
|
| 208 |
+
"id": "6d401ab5-a415-4c92-98b6-01ac1c15dd47",
|
| 209 |
+
"metadata": {},
|
| 210 |
+
"outputs": [
|
| 211 |
+
{
|
| 212 |
+
"name": "stdout",
|
| 213 |
+
"output_type": "stream",
|
| 214 |
+
"text": [
|
| 215 |
+
"\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 47ms/step\n",
|
| 216 |
+
"[[0. 0. 0. 0. 0. 0.01 0. 0.03 0. 0.95]\n",
|
| 217 |
+
" [0. 0. 1. 0. 0. 0. 0. 0. 0. 0. ]\n",
|
| 218 |
+
" [0. 1. 0. 0. 0. 0. 0. 0. 0. 0. ]]\n",
|
| 219 |
+
"\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 45ms/step\n",
|
| 220 |
+
"[9 2 1]\n",
|
| 221 |
+
"['Ankle boot' 'Pullover' 'Trouser']\n"
|
| 222 |
+
]
|
| 223 |
+
}
|
| 224 |
+
],
|
| 225 |
+
"source": [
|
| 226 |
+
"x_new = x_test[:3]\n",
|
| 227 |
+
"y_proba = model.predict(x_new)\n",
|
| 228 |
+
"print(y_proba.round(2))\n",
|
| 229 |
+
"\n",
|
| 230 |
+
"y_pred = model.predict(x_new).argmax(axis=1)\n",
|
| 231 |
+
"print(y_pred)\n",
|
| 232 |
+
"\n",
|
| 233 |
+
"print(np.array(class_names)[y_pred])\n"
|
| 234 |
+
]
|
| 235 |
+
},
|
| 236 |
+
{
|
| 237 |
+
"cell_type": "code",
|
| 238 |
+
"execution_count": null,
|
| 239 |
+
"id": "5b476174-b4af-43fb-b1f4-8b182c3af14d",
|
| 240 |
+
"metadata": {},
|
| 241 |
+
"outputs": [],
|
| 242 |
+
"source": []
|
| 243 |
+
}
|
| 244 |
+
],
|
| 245 |
+
"metadata": {
|
| 246 |
+
"kernelspec": {
|
| 247 |
+
"display_name": "Python 3",
|
| 248 |
+
"language": "python",
|
| 249 |
+
"name": "python3"
|
| 250 |
+
},
|
| 251 |
+
"language_info": {
|
| 252 |
+
"codemirror_mode": {
|
| 253 |
+
"name": "ipython",
|
| 254 |
+
"version": 3
|
| 255 |
+
},
|
| 256 |
+
"file_extension": ".py",
|
| 257 |
+
"mimetype": "text/x-python",
|
| 258 |
+
"name": "python",
|
| 259 |
+
"nbconvert_exporter": "python",
|
| 260 |
+
"pygments_lexer": "ipython3",
|
| 261 |
+
"version": "3.12.10"
|
| 262 |
+
}
|
| 263 |
+
},
|
| 264 |
+
"nbformat": 4,
|
| 265 |
+
"nbformat_minor": 5
|
| 266 |
+
}
|
.ipynb_checkpoints/Chapter1.2-NeuralNetwork-checkpoint.ipynb
ADDED
|
@@ -0,0 +1,158 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "code",
|
| 5 |
+
"execution_count": null,
|
| 6 |
+
"id": "177faf3e-cec4-4a9f-a613-4c8838af30c1",
|
| 7 |
+
"metadata": {},
|
| 8 |
+
"outputs": [],
|
| 9 |
+
"source": [
|
| 10 |
+
"#No keras existem os modelos de redes neurais estaticos, funcionais e dinamicmos. Neste notebook lidaresmos com dinamico"
|
| 11 |
+
]
|
| 12 |
+
},
|
| 13 |
+
{
|
| 14 |
+
"cell_type": "code",
|
| 15 |
+
"execution_count": null,
|
| 16 |
+
"id": "ef8dbd4c-3443-4a5f-b8e1-99b6f0d958b6",
|
| 17 |
+
"metadata": {},
|
| 18 |
+
"outputs": [],
|
| 19 |
+
"source": [
|
| 20 |
+
"from sklearn.datasets import fetch_california_housing\n",
|
| 21 |
+
"from sklearn.model_selection import train_test_split\n",
|
| 22 |
+
"from sklearn.preprocessing import StandardScaler\n",
|
| 23 |
+
"\n",
|
| 24 |
+
"import numpy as np\n",
|
| 25 |
+
"import tensorflow as tf\n",
|
| 26 |
+
"from tensorflow import keras\n",
|
| 27 |
+
"import pandas as pd\n",
|
| 28 |
+
"import matplotlib\n",
|
| 29 |
+
"matplotlib.use(\"TkAgg\")\n",
|
| 30 |
+
"import matplotlib.pyplot as plt"
|
| 31 |
+
]
|
| 32 |
+
},
|
| 33 |
+
{
|
| 34 |
+
"cell_type": "code",
|
| 35 |
+
"execution_count": null,
|
| 36 |
+
"id": "09438fbc-8705-4712-8111-078a22cf8d1d",
|
| 37 |
+
"metadata": {},
|
| 38 |
+
"outputs": [],
|
| 39 |
+
"source": [
|
| 40 |
+
"housing = fetch_california_housing()\n",
|
| 41 |
+
"x_train_full, x_test, y_train_full, y_test = train_test_split(housing.data,housing.target)\n",
|
| 42 |
+
"x_train, x_valid, y_train, y_valid = train_test_split(x_train_full, y_train_full)\n",
|
| 43 |
+
"\n",
|
| 44 |
+
"scaler=StandardScaler()\n",
|
| 45 |
+
"x_train = scaler.fit_transform(x_train)\n",
|
| 46 |
+
"x_valid = scaler.transform(x_valid)\n",
|
| 47 |
+
"x_test=scaler.transform(x_test)"
|
| 48 |
+
]
|
| 49 |
+
},
|
| 50 |
+
{
|
| 51 |
+
"cell_type": "code",
|
| 52 |
+
"execution_count": null,
|
| 53 |
+
"id": "0e80a374-8f22-40cd-ad12-f15898142f6b",
|
| 54 |
+
"metadata": {},
|
| 55 |
+
"outputs": [],
|
| 56 |
+
"source": [
|
| 57 |
+
"class WideAndDeepModel(keras.Model):\n",
|
| 58 |
+
" def __init__(self, units=30, activation=\"relu\", **kwargs):\n",
|
| 59 |
+
" super().__init__(**kwargs) \n",
|
| 60 |
+
" self.hidden1 = keras.layers.Dense(units, activation=activation)\n",
|
| 61 |
+
" self.hidden2 = keras.layers.Dense(units, activation=activation)\n",
|
| 62 |
+
" self.main_output = keras.layers.Dense(1)\n",
|
| 63 |
+
" self.aux_output = keras.layers.Dense(1)\n",
|
| 64 |
+
" def call(self, inputs):\n",
|
| 65 |
+
" input_A, input_B = inputs\n",
|
| 66 |
+
" hidden1 = self.hidden1(input_B)\n",
|
| 67 |
+
" hidden2 = self.hidden2(hidden1)\n",
|
| 68 |
+
" concat = keras.layers.concatenate([input_A, hidden2])\n",
|
| 69 |
+
" main_output = self.main_output(concat)\n",
|
| 70 |
+
" aux_output = self.aux_output(hidden2)\n",
|
| 71 |
+
" return main_output, aux_output"
|
| 72 |
+
]
|
| 73 |
+
},
|
| 74 |
+
{
|
| 75 |
+
"cell_type": "code",
|
| 76 |
+
"execution_count": null,
|
| 77 |
+
"id": "3a81c2b8-4e95-453a-8d1a-4ba132bd483b",
|
| 78 |
+
"metadata": {},
|
| 79 |
+
"outputs": [],
|
| 80 |
+
"source": [
|
| 81 |
+
"model = WideAndDeepModel()\n",
|
| 82 |
+
"\n",
|
| 83 |
+
"model.compile(\n",
|
| 84 |
+
" loss=[\"mse\", \"mse\"], # uma loss para cada saída\n",
|
| 85 |
+
" optimizer=keras.optimizers.SGD(learning_rate=1e-3),\n",
|
| 86 |
+
" loss_weights=[0.8, 0.2] # opcional: peso de cada saída\n",
|
| 87 |
+
")\n",
|
| 88 |
+
"\n",
|
| 89 |
+
"# Os targets precisam ser tuplas/listas correspondentes às saídas\n",
|
| 90 |
+
"history = model.fit(\n",
|
| 91 |
+
" (x_train_A, x_train_B), # entradas\n",
|
| 92 |
+
" (y_train, y_train), # targets: main_output e aux_output\n",
|
| 93 |
+
" epochs=20,\n",
|
| 94 |
+
" validation_data=((x_valid_A, x_valid_B), (y_valid, y_valid))\n",
|
| 95 |
+
")\n",
|
| 96 |
+
"\n",
|
| 97 |
+
"# Avaliar\n",
|
| 98 |
+
"mse_test = model.evaluate((x_test_A, x_test_B), (y_test, y_test))\n",
|
| 99 |
+
"\n",
|
| 100 |
+
"# Predição\n",
|
| 101 |
+
"y_pred_main, y_pred_aux = model.predict((x_new_A, x_new_B))\n",
|
| 102 |
+
"print(y_pred_main)\n",
|
| 103 |
+
"\n",
|
| 104 |
+
"model.save(\"my_keras_model.h5\") #Salvando modelo, legal isso"
|
| 105 |
+
]
|
| 106 |
+
},
|
| 107 |
+
{
|
| 108 |
+
"cell_type": "code",
|
| 109 |
+
"execution_count": null,
|
| 110 |
+
"id": "044977e1-310c-4f39-9e46-303c86bfbd0f",
|
| 111 |
+
"metadata": {},
|
| 112 |
+
"outputs": [],
|
| 113 |
+
"source": [
|
| 114 |
+
"model = keras.models.load_model(\"my_keras_model.h5\")"
|
| 115 |
+
]
|
| 116 |
+
},
|
| 117 |
+
{
|
| 118 |
+
"cell_type": "code",
|
| 119 |
+
"execution_count": null,
|
| 120 |
+
"id": "079566ef-1c0c-400a-aeba-253ba01d1d5c",
|
| 121 |
+
"metadata": {},
|
| 122 |
+
"outputs": [],
|
| 123 |
+
"source": [
|
| 124 |
+
"checkpoint_cb = keras.callbacs.ModelCheckpoint(\"my_keras_model.h5\")\n",
|
| 125 |
+
"history = model.fit(x_train, y_train, epochs = 10, callbacks = [checkpoint_cb])"
|
| 126 |
+
]
|
| 127 |
+
},
|
| 128 |
+
{
|
| 129 |
+
"cell_type": "code",
|
| 130 |
+
"execution_count": null,
|
| 131 |
+
"id": "8b95335e-9efc-44d9-8b6f-9cf8c6929dc7",
|
| 132 |
+
"metadata": {},
|
| 133 |
+
"outputs": [],
|
| 134 |
+
"source": []
|
| 135 |
+
}
|
| 136 |
+
],
|
| 137 |
+
"metadata": {
|
| 138 |
+
"kernelspec": {
|
| 139 |
+
"display_name": "Python 3",
|
| 140 |
+
"language": "python",
|
| 141 |
+
"name": "python3"
|
| 142 |
+
},
|
| 143 |
+
"language_info": {
|
| 144 |
+
"codemirror_mode": {
|
| 145 |
+
"name": "ipython",
|
| 146 |
+
"version": 3
|
| 147 |
+
},
|
| 148 |
+
"file_extension": ".py",
|
| 149 |
+
"mimetype": "text/x-python",
|
| 150 |
+
"name": "python",
|
| 151 |
+
"nbconvert_exporter": "python",
|
| 152 |
+
"pygments_lexer": "ipython3",
|
| 153 |
+
"version": "3.12.10"
|
| 154 |
+
}
|
| 155 |
+
},
|
| 156 |
+
"nbformat": 4,
|
| 157 |
+
"nbformat_minor": 5
|
| 158 |
+
}
|
Chapter1-NeuralNetwork.ipynb
ADDED
|
@@ -0,0 +1,266 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "code",
|
| 5 |
+
"execution_count": 2,
|
| 6 |
+
"id": "2b22b7ad-9329-477a-a8f4-583fb23a8471",
|
| 7 |
+
"metadata": {},
|
| 8 |
+
"outputs": [],
|
| 9 |
+
"source": [
|
| 10 |
+
"import numpy as np\n",
|
| 11 |
+
"import tensorflow as tf\n",
|
| 12 |
+
"from tensorflow import keras\n",
|
| 13 |
+
"import pandas as pd\n",
|
| 14 |
+
"import matplotlib\n",
|
| 15 |
+
"matplotlib.use(\"TkAgg\")\n",
|
| 16 |
+
"import matplotlib.pyplot as plt"
|
| 17 |
+
]
|
| 18 |
+
},
|
| 19 |
+
{
|
| 20 |
+
"cell_type": "code",
|
| 21 |
+
"execution_count": 3,
|
| 22 |
+
"id": "8b9896be-f135-4c8d-9c00-c6fe7e444172",
|
| 23 |
+
"metadata": {},
|
| 24 |
+
"outputs": [
|
| 25 |
+
{
|
| 26 |
+
"name": "stdout",
|
| 27 |
+
"output_type": "stream",
|
| 28 |
+
"text": [
|
| 29 |
+
"(60000, 28, 28)\n",
|
| 30 |
+
"uint8\n"
|
| 31 |
+
]
|
| 32 |
+
}
|
| 33 |
+
],
|
| 34 |
+
"source": [
|
| 35 |
+
"fashion_mnist = keras.datasets.fashion_mnist\n",
|
| 36 |
+
"\n",
|
| 37 |
+
"(x_train_full, y_train_full), (x_test, y_test) = fashion_mnist.load_data()\n",
|
| 38 |
+
"x_test = x_test / 255.0\n",
|
| 39 |
+
"\n",
|
| 40 |
+
"print(x_train_full.shape)\n",
|
| 41 |
+
"print(x_train_full.dtype)\n",
|
| 42 |
+
"\n",
|
| 43 |
+
"x_valid, x_train = x_train_full[:5000] / 255.0, x_train_full[5000:] / 255.0\n",
|
| 44 |
+
"y_valid, y_train = y_train_full[:5000], y_train_full[5000:]\n",
|
| 45 |
+
"\n",
|
| 46 |
+
"class_names = [\n",
|
| 47 |
+
" \"T-shirt/top\", \"Trouser\", \"Pullover\", \"Dress\", \"Coat\",\n",
|
| 48 |
+
" \"Sandal\", \"Shirt\", \"Sneaker\", \"Bag\", \"Ankle boot\"\n",
|
| 49 |
+
"]"
|
| 50 |
+
]
|
| 51 |
+
},
|
| 52 |
+
{
|
| 53 |
+
"cell_type": "code",
|
| 54 |
+
"execution_count": 4,
|
| 55 |
+
"id": "c4514be8-3e16-4f63-bea4-adc8e2576e43",
|
| 56 |
+
"metadata": {},
|
| 57 |
+
"outputs": [
|
| 58 |
+
{
|
| 59 |
+
"name": "stderr",
|
| 60 |
+
"output_type": "stream",
|
| 61 |
+
"text": [
|
| 62 |
+
"C:\\Users\\lacer\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python312\\site-packages\\keras\\src\\layers\\reshaping\\flatten.py:37: UserWarning: Do not pass an `input_shape`/`input_dim` argument to a layer. When using Sequential models, prefer using an `Input(shape)` object as the first layer in the model instead.\n",
|
| 63 |
+
" super().__init__(**kwargs)\n"
|
| 64 |
+
]
|
| 65 |
+
}
|
| 66 |
+
],
|
| 67 |
+
"source": [
|
| 68 |
+
"model = keras.models.Sequential()\n",
|
| 69 |
+
"model.add(keras.layers.Flatten(input_shape=(28, 28)))\n",
|
| 70 |
+
"model.add(keras.layers.Dense(300, activation='relu'))\n",
|
| 71 |
+
"model.add(keras.layers.Dense(100, activation='relu'))\n",
|
| 72 |
+
"model.add(keras.layers.Dense(10, activation='softmax'))"
|
| 73 |
+
]
|
| 74 |
+
},
|
| 75 |
+
{
|
| 76 |
+
"cell_type": "code",
|
| 77 |
+
"execution_count": 6,
|
| 78 |
+
"id": "80db0e8a-cc96-4752-8bec-60315ad34bec",
|
| 79 |
+
"metadata": {},
|
| 80 |
+
"outputs": [
|
| 81 |
+
{
|
| 82 |
+
"name": "stdout",
|
| 83 |
+
"output_type": "stream",
|
| 84 |
+
"text": [
|
| 85 |
+
"WARNING:tensorflow:TensorFlow GPU support is not available on native Windows for TensorFlow >= 2.11. Even if CUDA/cuDNN are installed, GPU will not be used. Please use WSL2 or the TensorFlow-DirectML plugin.\n",
|
| 86 |
+
"Epoch 1/30\n",
|
| 87 |
+
"\u001b[1m1719/1719\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m5s\u001b[0m 3ms/step - accuracy: 0.7659 - loss: 0.7165 - val_accuracy: 0.8362 - val_loss: 0.5032\n",
|
| 88 |
+
"Epoch 2/30\n",
|
| 89 |
+
"\u001b[1m1719/1719\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m5s\u001b[0m 3ms/step - accuracy: 0.8292 - loss: 0.4888 - val_accuracy: 0.8542 - val_loss: 0.4361\n",
|
| 90 |
+
"Epoch 3/30\n",
|
| 91 |
+
"\u001b[1m1719/1719\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m5s\u001b[0m 3ms/step - accuracy: 0.8441 - loss: 0.4461 - val_accuracy: 0.8642 - val_loss: 0.4142\n",
|
| 92 |
+
"Epoch 4/30\n",
|
| 93 |
+
"\u001b[1m1719/1719\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 3ms/step - accuracy: 0.8529 - loss: 0.4180 - val_accuracy: 0.8654 - val_loss: 0.3972\n",
|
| 94 |
+
"Epoch 5/30\n",
|
| 95 |
+
"\u001b[1m1719/1719\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 3ms/step - accuracy: 0.8600 - loss: 0.3981 - val_accuracy: 0.8600 - val_loss: 0.3943\n",
|
| 96 |
+
"Epoch 6/30\n",
|
| 97 |
+
"\u001b[1m1719/1719\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 3ms/step - accuracy: 0.8655 - loss: 0.3811 - val_accuracy: 0.8694 - val_loss: 0.3724\n",
|
| 98 |
+
"Epoch 7/30\n",
|
| 99 |
+
"\u001b[1m1719/1719\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 3ms/step - accuracy: 0.8690 - loss: 0.3680 - val_accuracy: 0.8668 - val_loss: 0.3823\n",
|
| 100 |
+
"Epoch 8/30\n",
|
| 101 |
+
"\u001b[1m1719/1719\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m5s\u001b[0m 3ms/step - accuracy: 0.8726 - loss: 0.3573 - val_accuracy: 0.8724 - val_loss: 0.3629\n",
|
| 102 |
+
"Epoch 9/30\n",
|
| 103 |
+
"\u001b[1m1719/1719\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m5s\u001b[0m 3ms/step - accuracy: 0.8777 - loss: 0.3461 - val_accuracy: 0.8768 - val_loss: 0.3452\n",
|
| 104 |
+
"Epoch 10/30\n",
|
| 105 |
+
"\u001b[1m1719/1719\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 3ms/step - accuracy: 0.8804 - loss: 0.3365 - val_accuracy: 0.8792 - val_loss: 0.3440\n",
|
| 106 |
+
"Epoch 11/30\n",
|
| 107 |
+
"\u001b[1m1719/1719\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 3ms/step - accuracy: 0.8837 - loss: 0.3282 - val_accuracy: 0.8698 - val_loss: 0.3532\n",
|
| 108 |
+
"Epoch 12/30\n",
|
| 109 |
+
"\u001b[1m1719/1719\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 3ms/step - accuracy: 0.8858 - loss: 0.3197 - val_accuracy: 0.8814 - val_loss: 0.3315\n",
|
| 110 |
+
"Epoch 13/30\n",
|
| 111 |
+
"\u001b[1m1719/1719\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 3ms/step - accuracy: 0.8886 - loss: 0.3127 - val_accuracy: 0.8804 - val_loss: 0.3382\n",
|
| 112 |
+
"Epoch 14/30\n",
|
| 113 |
+
"\u001b[1m1719/1719\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 3ms/step - accuracy: 0.8911 - loss: 0.3053 - val_accuracy: 0.8738 - val_loss: 0.3601\n",
|
| 114 |
+
"Epoch 15/30\n",
|
| 115 |
+
"\u001b[1m1719/1719\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 3ms/step - accuracy: 0.8923 - loss: 0.2997 - val_accuracy: 0.8816 - val_loss: 0.3278\n",
|
| 116 |
+
"Epoch 16/30\n",
|
| 117 |
+
"\u001b[1m1719/1719\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 3ms/step - accuracy: 0.8951 - loss: 0.2938 - val_accuracy: 0.8834 - val_loss: 0.3259\n",
|
| 118 |
+
"Epoch 17/30\n",
|
| 119 |
+
"\u001b[1m1719/1719\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 3ms/step - accuracy: 0.8983 - loss: 0.2865 - val_accuracy: 0.8850 - val_loss: 0.3281\n",
|
| 120 |
+
"Epoch 18/30\n",
|
| 121 |
+
"\u001b[1m1719/1719\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 3ms/step - accuracy: 0.9000 - loss: 0.2809 - val_accuracy: 0.8832 - val_loss: 0.3194\n",
|
| 122 |
+
"Epoch 19/30\n",
|
| 123 |
+
"\u001b[1m1719/1719\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 3ms/step - accuracy: 0.9008 - loss: 0.2760 - val_accuracy: 0.8886 - val_loss: 0.3071\n",
|
| 124 |
+
"Epoch 20/30\n",
|
| 125 |
+
"\u001b[1m1719/1719\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 3ms/step - accuracy: 0.9016 - loss: 0.2716 - val_accuracy: 0.8820 - val_loss: 0.3401\n",
|
| 126 |
+
"Epoch 21/30\n",
|
| 127 |
+
"\u001b[1m1719/1719\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 2ms/step - accuracy: 0.9035 - loss: 0.2662 - val_accuracy: 0.8890 - val_loss: 0.3063\n",
|
| 128 |
+
"Epoch 22/30\n",
|
| 129 |
+
"\u001b[1m1719/1719\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 3ms/step - accuracy: 0.9061 - loss: 0.2614 - val_accuracy: 0.8900 - val_loss: 0.3113\n",
|
| 130 |
+
"Epoch 23/30\n",
|
| 131 |
+
"\u001b[1m1719/1719\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m5s\u001b[0m 3ms/step - accuracy: 0.9069 - loss: 0.2565 - val_accuracy: 0.8888 - val_loss: 0.3085\n",
|
| 132 |
+
"Epoch 24/30\n",
|
| 133 |
+
"\u001b[1m1719/1719\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m5s\u001b[0m 3ms/step - accuracy: 0.9091 - loss: 0.2524 - val_accuracy: 0.8842 - val_loss: 0.3263\n",
|
| 134 |
+
"Epoch 25/30\n",
|
| 135 |
+
"\u001b[1m1719/1719\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 3ms/step - accuracy: 0.9102 - loss: 0.2487 - val_accuracy: 0.8894 - val_loss: 0.3044\n",
|
| 136 |
+
"Epoch 26/30\n",
|
| 137 |
+
"\u001b[1m1719/1719\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 3ms/step - accuracy: 0.9108 - loss: 0.2446 - val_accuracy: 0.8914 - val_loss: 0.2958\n",
|
| 138 |
+
"Epoch 27/30\n",
|
| 139 |
+
"\u001b[1m1719/1719\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 3ms/step - accuracy: 0.9133 - loss: 0.2392 - val_accuracy: 0.8788 - val_loss: 0.3307\n",
|
| 140 |
+
"Epoch 28/30\n",
|
| 141 |
+
"\u001b[1m1719/1719\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 3ms/step - accuracy: 0.9149 - loss: 0.2362 - val_accuracy: 0.8914 - val_loss: 0.2978\n",
|
| 142 |
+
"Epoch 29/30\n",
|
| 143 |
+
"\u001b[1m1719/1719\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 3ms/step - accuracy: 0.9171 - loss: 0.2317 - val_accuracy: 0.8886 - val_loss: 0.3139\n",
|
| 144 |
+
"Epoch 30/30\n",
|
| 145 |
+
"\u001b[1m1719/1719\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m4s\u001b[0m 3ms/step - accuracy: 0.9177 - loss: 0.2281 - val_accuracy: 0.8922 - val_loss: 0.3038\n"
|
| 146 |
+
]
|
| 147 |
+
}
|
| 148 |
+
],
|
| 149 |
+
"source": [
|
| 150 |
+
"model.compile(\n",
|
| 151 |
+
" loss=\"sparse_categorical_crossentropy\",\n",
|
| 152 |
+
" optimizer=\"sgd\",\n",
|
| 153 |
+
" metrics=[\"accuracy\"]\n",
|
| 154 |
+
")\n",
|
| 155 |
+
"\n",
|
| 156 |
+
"history = model.fit(\n",
|
| 157 |
+
" x_train,\n",
|
| 158 |
+
" y_train,\n",
|
| 159 |
+
" epochs=30 ,\n",
|
| 160 |
+
" validation_data=(x_valid, y_valid)\n",
|
| 161 |
+
")"
|
| 162 |
+
]
|
| 163 |
+
},
|
| 164 |
+
{
|
| 165 |
+
"cell_type": "code",
|
| 166 |
+
"execution_count": 8,
|
| 167 |
+
"id": "2a081c04-338c-40e6-89d1-857ec6ca3232",
|
| 168 |
+
"metadata": {},
|
| 169 |
+
"outputs": [],
|
| 170 |
+
"source": [
|
| 171 |
+
"pd.DataFrame(history.history).plot(figsize=(8, 5))\n",
|
| 172 |
+
"plt.grid(True)\n",
|
| 173 |
+
"plt.gca().set_ylim(0, 1)\n",
|
| 174 |
+
"plt.show()\n"
|
| 175 |
+
]
|
| 176 |
+
},
|
| 177 |
+
{
|
| 178 |
+
"cell_type": "code",
|
| 179 |
+
"execution_count": 9,
|
| 180 |
+
"id": "7cd5d8ac-6a3e-4a92-b4ef-574923bf4b71",
|
| 181 |
+
"metadata": {},
|
| 182 |
+
"outputs": [
|
| 183 |
+
{
|
| 184 |
+
"name": "stdout",
|
| 185 |
+
"output_type": "stream",
|
| 186 |
+
"text": [
|
| 187 |
+
"\u001b[1m313/313\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m1s\u001b[0m 2ms/step - accuracy: 0.8787 - loss: 0.3378\n"
|
| 188 |
+
]
|
| 189 |
+
},
|
| 190 |
+
{
|
| 191 |
+
"data": {
|
| 192 |
+
"text/plain": [
|
| 193 |
+
"[0.33777710795402527, 0.8787000179290771]"
|
| 194 |
+
]
|
| 195 |
+
},
|
| 196 |
+
"execution_count": 9,
|
| 197 |
+
"metadata": {},
|
| 198 |
+
"output_type": "execute_result"
|
| 199 |
+
}
|
| 200 |
+
],
|
| 201 |
+
"source": [
|
| 202 |
+
"model.evaluate(x_test, y_test)\n"
|
| 203 |
+
]
|
| 204 |
+
},
|
| 205 |
+
{
|
| 206 |
+
"cell_type": "code",
|
| 207 |
+
"execution_count": 12,
|
| 208 |
+
"id": "6d401ab5-a415-4c92-98b6-01ac1c15dd47",
|
| 209 |
+
"metadata": {},
|
| 210 |
+
"outputs": [
|
| 211 |
+
{
|
| 212 |
+
"name": "stdout",
|
| 213 |
+
"output_type": "stream",
|
| 214 |
+
"text": [
|
| 215 |
+
"\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 47ms/step\n",
|
| 216 |
+
"[[0. 0. 0. 0. 0. 0.01 0. 0.03 0. 0.95]\n",
|
| 217 |
+
" [0. 0. 1. 0. 0. 0. 0. 0. 0. 0. ]\n",
|
| 218 |
+
" [0. 1. 0. 0. 0. 0. 0. 0. 0. 0. ]]\n",
|
| 219 |
+
"\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 45ms/step\n",
|
| 220 |
+
"[9 2 1]\n",
|
| 221 |
+
"['Ankle boot' 'Pullover' 'Trouser']\n"
|
| 222 |
+
]
|
| 223 |
+
}
|
| 224 |
+
],
|
| 225 |
+
"source": [
|
| 226 |
+
"x_new = x_test[:3]\n",
|
| 227 |
+
"y_proba = model.predict(x_new)\n",
|
| 228 |
+
"print(y_proba.round(2))\n",
|
| 229 |
+
"\n",
|
| 230 |
+
"y_pred = model.predict(x_new).argmax(axis=1)\n",
|
| 231 |
+
"print(y_pred)\n",
|
| 232 |
+
"\n",
|
| 233 |
+
"print(np.array(class_names)[y_pred])\n"
|
| 234 |
+
]
|
| 235 |
+
},
|
| 236 |
+
{
|
| 237 |
+
"cell_type": "code",
|
| 238 |
+
"execution_count": null,
|
| 239 |
+
"id": "5b476174-b4af-43fb-b1f4-8b182c3af14d",
|
| 240 |
+
"metadata": {},
|
| 241 |
+
"outputs": [],
|
| 242 |
+
"source": []
|
| 243 |
+
}
|
| 244 |
+
],
|
| 245 |
+
"metadata": {
|
| 246 |
+
"kernelspec": {
|
| 247 |
+
"display_name": "Python 3",
|
| 248 |
+
"language": "python",
|
| 249 |
+
"name": "python3"
|
| 250 |
+
},
|
| 251 |
+
"language_info": {
|
| 252 |
+
"codemirror_mode": {
|
| 253 |
+
"name": "ipython",
|
| 254 |
+
"version": 3
|
| 255 |
+
},
|
| 256 |
+
"file_extension": ".py",
|
| 257 |
+
"mimetype": "text/x-python",
|
| 258 |
+
"name": "python",
|
| 259 |
+
"nbconvert_exporter": "python",
|
| 260 |
+
"pygments_lexer": "ipython3",
|
| 261 |
+
"version": "3.12.10"
|
| 262 |
+
}
|
| 263 |
+
},
|
| 264 |
+
"nbformat": 4,
|
| 265 |
+
"nbformat_minor": 5
|
| 266 |
+
}
|
Chapter1.1-NeuralNetwork.ipynb
ADDED
|
@@ -0,0 +1,208 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "code",
|
| 5 |
+
"execution_count": 4,
|
| 6 |
+
"id": "4d62143b-6a33-4c02-8b1d-3d48d1256737",
|
| 7 |
+
"metadata": {},
|
| 8 |
+
"outputs": [],
|
| 9 |
+
"source": [
|
| 10 |
+
"from sklearn.datasets import fetch_california_housing\n",
|
| 11 |
+
"from sklearn.model_selection import train_test_split\n",
|
| 12 |
+
"from sklearn.preprocessing import StandardScaler\n",
|
| 13 |
+
"\n",
|
| 14 |
+
"import numpy as np\n",
|
| 15 |
+
"import tensorflow as tf\n",
|
| 16 |
+
"from tensorflow import keras\n",
|
| 17 |
+
"import pandas as pd\n",
|
| 18 |
+
"import matplotlib\n",
|
| 19 |
+
"matplotlib.use(\"TkAgg\")\n",
|
| 20 |
+
"import matplotlib.pyplot as plt"
|
| 21 |
+
]
|
| 22 |
+
},
|
| 23 |
+
{
|
| 24 |
+
"cell_type": "code",
|
| 25 |
+
"execution_count": 14,
|
| 26 |
+
"id": "d77530d4-e76b-4958-961e-a08bb19a7977",
|
| 27 |
+
"metadata": {},
|
| 28 |
+
"outputs": [],
|
| 29 |
+
"source": [
|
| 30 |
+
"housing = fetch_california_housing()\n",
|
| 31 |
+
"x_train_full, x_test, y_train_full, y_test = train_test_split(housing.data,housing.target)\n",
|
| 32 |
+
"x_train, x_valid, y_train, y_valid = train_test_split(x_train_full, y_train_full)\n",
|
| 33 |
+
"\n",
|
| 34 |
+
"scaler=StandardScaler()\n",
|
| 35 |
+
"x_train = scaler.fit_transform(x_train)\n",
|
| 36 |
+
"x_valid = scaler.transform(x_valid)\n",
|
| 37 |
+
"x_test=scaler.transform(x_test)"
|
| 38 |
+
]
|
| 39 |
+
},
|
| 40 |
+
{
|
| 41 |
+
"cell_type": "code",
|
| 42 |
+
"execution_count": null,
|
| 43 |
+
"id": "55bd5b4b-8f34-4ade-89b3-35cfcbe61d7d",
|
| 44 |
+
"metadata": {},
|
| 45 |
+
"outputs": [],
|
| 46 |
+
"source": [
|
| 47 |
+
"model = keras.models.Sequential([keras.layers.Dense(30, activation = \"relu\", input_shape=x_train.shape[1:]), keras.layers.Dense(1)])\n"
|
| 48 |
+
]
|
| 49 |
+
},
|
| 50 |
+
{
|
| 51 |
+
"cell_type": "code",
|
| 52 |
+
"execution_count": null,
|
| 53 |
+
"id": "736db746-f0b9-4a4b-b90f-6ae3cda03447",
|
| 54 |
+
"metadata": {
|
| 55 |
+
"scrolled": true
|
| 56 |
+
},
|
| 57 |
+
"outputs": [],
|
| 58 |
+
"source": [
|
| 59 |
+
"input_ = keras.layers.Input(shape=x_train.shape[1:])\n",
|
| 60 |
+
"hidden1 = keras.layers.Dense(30, activation=\"relu\")(input_)\n",
|
| 61 |
+
"hidden2 = keras.layers.Dense(30, activation=\"relu\")(hidden1)\n",
|
| 62 |
+
"concat = keras.layers.Concatenate()([input_,hidden2])\n",
|
| 63 |
+
"output = keras.layers.Dense(1)(concat)\n",
|
| 64 |
+
"model = keras.Model(inputs=[input_], outputs=[output])\n"
|
| 65 |
+
]
|
| 66 |
+
},
|
| 67 |
+
{
|
| 68 |
+
"cell_type": "code",
|
| 69 |
+
"execution_count": 16,
|
| 70 |
+
"id": "af7e711e-d8df-44d6-bf50-70ee09ee2ae6",
|
| 71 |
+
"metadata": {},
|
| 72 |
+
"outputs": [
|
| 73 |
+
{
|
| 74 |
+
"name": "stdout",
|
| 75 |
+
"output_type": "stream",
|
| 76 |
+
"text": [
|
| 77 |
+
"Epoch 1/20\n",
|
| 78 |
+
"\u001b[1m363/363\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m1s\u001b[0m 3ms/step - loss: 1.7751 - val_loss: 1.3058\n",
|
| 79 |
+
"Epoch 2/20\n",
|
| 80 |
+
"\u001b[1m363/363\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m1s\u001b[0m 2ms/step - loss: 0.8318 - val_loss: 0.8696\n",
|
| 81 |
+
"Epoch 3/20\n",
|
| 82 |
+
"\u001b[1m363/363\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m1s\u001b[0m 2ms/step - loss: 0.7257 - val_loss: 0.7567\n",
|
| 83 |
+
"Epoch 4/20\n",
|
| 84 |
+
"\u001b[1m363/363\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m1s\u001b[0m 2ms/step - loss: 0.6701 - val_loss: 0.6548\n",
|
| 85 |
+
"Epoch 5/20\n",
|
| 86 |
+
"\u001b[1m363/363\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m1s\u001b[0m 2ms/step - loss: 0.6205 - val_loss: 0.6165\n",
|
| 87 |
+
"Epoch 6/20\n",
|
| 88 |
+
"\u001b[1m363/363\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m1s\u001b[0m 2ms/step - loss: 0.5843 - val_loss: 0.5791\n",
|
| 89 |
+
"Epoch 7/20\n",
|
| 90 |
+
"\u001b[1m363/363\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m1s\u001b[0m 2ms/step - loss: 0.5507 - val_loss: 0.5629\n",
|
| 91 |
+
"Epoch 8/20\n",
|
| 92 |
+
"\u001b[1m363/363\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m1s\u001b[0m 2ms/step - loss: 0.5270 - val_loss: 0.5499\n",
|
| 93 |
+
"Epoch 9/20\n",
|
| 94 |
+
"\u001b[1m363/363\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m1s\u001b[0m 2ms/step - loss: 0.5061 - val_loss: 0.5127\n",
|
| 95 |
+
"Epoch 10/20\n",
|
| 96 |
+
"\u001b[1m363/363\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m1s\u001b[0m 2ms/step - loss: 0.4899 - val_loss: 0.4930\n",
|
| 97 |
+
"Epoch 11/20\n",
|
| 98 |
+
"\u001b[1m363/363\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m1s\u001b[0m 2ms/step - loss: 0.4746 - val_loss: 0.4816\n",
|
| 99 |
+
"Epoch 12/20\n",
|
| 100 |
+
"\u001b[1m363/363\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m1s\u001b[0m 2ms/step - loss: 0.4634 - val_loss: 0.4685\n",
|
| 101 |
+
"Epoch 13/20\n",
|
| 102 |
+
"\u001b[1m363/363\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m1s\u001b[0m 2ms/step - loss: 0.4539 - val_loss: 0.4632\n",
|
| 103 |
+
"Epoch 14/20\n",
|
| 104 |
+
"\u001b[1m363/363\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m1s\u001b[0m 2ms/step - loss: 0.4478 - val_loss: 0.4546\n",
|
| 105 |
+
"Epoch 15/20\n",
|
| 106 |
+
"\u001b[1m363/363\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m1s\u001b[0m 2ms/step - loss: 0.4418 - val_loss: 0.4485\n",
|
| 107 |
+
"Epoch 16/20\n",
|
| 108 |
+
"\u001b[1m363/363\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m1s\u001b[0m 2ms/step - loss: 0.4374 - val_loss: 0.4442\n",
|
| 109 |
+
"Epoch 17/20\n",
|
| 110 |
+
"\u001b[1m363/363\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m1s\u001b[0m 2ms/step - loss: 0.4337 - val_loss: 0.4400\n",
|
| 111 |
+
"Epoch 18/20\n",
|
| 112 |
+
"\u001b[1m363/363\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m1s\u001b[0m 2ms/step - loss: 0.4304 - val_loss: 0.4377\n",
|
| 113 |
+
"Epoch 19/20\n",
|
| 114 |
+
"\u001b[1m363/363\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m1s\u001b[0m 2ms/step - loss: 0.4274 - val_loss: 0.4378\n",
|
| 115 |
+
"Epoch 20/20\n",
|
| 116 |
+
"\u001b[1m363/363\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m1s\u001b[0m 2ms/step - loss: 0.4257 - val_loss: 0.4346\n",
|
| 117 |
+
"\u001b[1m162/162\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 2ms/step - loss: 0.4405\n",
|
| 118 |
+
"\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 108ms/step\n",
|
| 119 |
+
"[[1.3971645]\n",
|
| 120 |
+
" [3.2480707]\n",
|
| 121 |
+
" [2.94071 ]]\n"
|
| 122 |
+
]
|
| 123 |
+
}
|
| 124 |
+
],
|
| 125 |
+
"source": [
|
| 126 |
+
"x_train_A, x_train_B = x_train[:, :5], x_train[:, 5:]\n",
|
| 127 |
+
"x_valid_A, x_valid_B = x_valid[:, :5], x_valid[:, 5:]\n",
|
| 128 |
+
"x_test_A, x_test_B = x_test[:, :5], x_test[:, 5:]\n",
|
| 129 |
+
"x_new_A, x_new_B = x_test_A[:3], x_test_B[:3]\n",
|
| 130 |
+
"\n",
|
| 131 |
+
"# Construir modelo\n",
|
| 132 |
+
"input_A = keras.layers.Input(shape=[5], name=\"wide_input\")\n",
|
| 133 |
+
"input_B = keras.layers.Input(shape=[3], name=\"deep_input\")\n",
|
| 134 |
+
"\n",
|
| 135 |
+
"hidden1 = keras.layers.Dense(30, activation=\"relu\")(input_B)\n",
|
| 136 |
+
"hidden2 = keras.layers.Dense(30, activation=\"relu\")(hidden1)\n",
|
| 137 |
+
"\n",
|
| 138 |
+
"concat = keras.layers.Concatenate()([input_A, hidden2])\n",
|
| 139 |
+
"output = keras.layers.Dense(1, name=\"output\")(concat)\n",
|
| 140 |
+
"\n",
|
| 141 |
+
"model = keras.Model(inputs=[input_A, input_B], outputs=[output])\n",
|
| 142 |
+
"\n",
|
| 143 |
+
"# Compilar\n",
|
| 144 |
+
"model.compile(loss=\"mse\", optimizer=keras.optimizers.SGD(learning_rate=1e-3))\n",
|
| 145 |
+
"\n",
|
| 146 |
+
"# Treinar\n",
|
| 147 |
+
"history = model.fit(\n",
|
| 148 |
+
" (x_train_A, x_train_B), y_train,\n",
|
| 149 |
+
" epochs=20,\n",
|
| 150 |
+
" validation_data=((x_valid_A, x_valid_B), y_valid)\n",
|
| 151 |
+
")\n",
|
| 152 |
+
"\n",
|
| 153 |
+
"# Avaliar\n",
|
| 154 |
+
"mse_test = model.evaluate((x_test_A, x_test_B), y_test)\n",
|
| 155 |
+
"\n",
|
| 156 |
+
"# Predizer\n",
|
| 157 |
+
"y_pred = model.predict((x_new_A, x_new_B))\n",
|
| 158 |
+
"print(y_pred)"
|
| 159 |
+
]
|
| 160 |
+
},
|
| 161 |
+
{
|
| 162 |
+
"cell_type": "code",
|
| 163 |
+
"execution_count": null,
|
| 164 |
+
"id": "7835c49d-10bf-44cb-a90b-1cb808ce0be7",
|
| 165 |
+
"metadata": {},
|
| 166 |
+
"outputs": [],
|
| 167 |
+
"source": [
|
| 168 |
+
"model.compile(loss=\"mean_squared_error\", optimizer=\"adam\")\n",
|
| 169 |
+
"history = model.fit(x_train, y_train, epochs=20, validation_data=(x_valid, y_valid))\n",
|
| 170 |
+
"mse_test = model.evaluate(x_test, y_test)\n",
|
| 171 |
+
"\n",
|
| 172 |
+
"x_new = x_test[:3]\n",
|
| 173 |
+
"y_pred = model.predict(x_new)\n",
|
| 174 |
+
"\n",
|
| 175 |
+
"print(y_pred)"
|
| 176 |
+
]
|
| 177 |
+
},
|
| 178 |
+
{
|
| 179 |
+
"cell_type": "code",
|
| 180 |
+
"execution_count": null,
|
| 181 |
+
"id": "f052ffc0-841f-4044-a40d-7c57216e0dc6",
|
| 182 |
+
"metadata": {},
|
| 183 |
+
"outputs": [],
|
| 184 |
+
"source": []
|
| 185 |
+
}
|
| 186 |
+
],
|
| 187 |
+
"metadata": {
|
| 188 |
+
"kernelspec": {
|
| 189 |
+
"display_name": "Python 3",
|
| 190 |
+
"language": "python",
|
| 191 |
+
"name": "python3"
|
| 192 |
+
},
|
| 193 |
+
"language_info": {
|
| 194 |
+
"codemirror_mode": {
|
| 195 |
+
"name": "ipython",
|
| 196 |
+
"version": 3
|
| 197 |
+
},
|
| 198 |
+
"file_extension": ".py",
|
| 199 |
+
"mimetype": "text/x-python",
|
| 200 |
+
"name": "python",
|
| 201 |
+
"nbconvert_exporter": "python",
|
| 202 |
+
"pygments_lexer": "ipython3",
|
| 203 |
+
"version": "3.12.10"
|
| 204 |
+
}
|
| 205 |
+
},
|
| 206 |
+
"nbformat": 4,
|
| 207 |
+
"nbformat_minor": 5
|
| 208 |
+
}
|
Chapter1.2-NeuralNetwork.ipynb
ADDED
|
@@ -0,0 +1,216 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "code",
|
| 5 |
+
"execution_count": null,
|
| 6 |
+
"id": "177faf3e-cec4-4a9f-a613-4c8838af30c1",
|
| 7 |
+
"metadata": {},
|
| 8 |
+
"outputs": [],
|
| 9 |
+
"source": [
|
| 10 |
+
"#No keras existem os modelos de redes neurais estaticos, funcionais e dinamicmos. Neste notebook lidaresmos com dinamico"
|
| 11 |
+
]
|
| 12 |
+
},
|
| 13 |
+
{
|
| 14 |
+
"cell_type": "code",
|
| 15 |
+
"execution_count": null,
|
| 16 |
+
"id": "ef8dbd4c-3443-4a5f-b8e1-99b6f0d958b6",
|
| 17 |
+
"metadata": {},
|
| 18 |
+
"outputs": [],
|
| 19 |
+
"source": [
|
| 20 |
+
"from sklearn.datasets import fetch_california_housing\n",
|
| 21 |
+
"from sklearn.model_selection import train_test_split\n",
|
| 22 |
+
"from sklearn.preprocessing import StandardScaler\n",
|
| 23 |
+
"\n",
|
| 24 |
+
"import numpy as np\n",
|
| 25 |
+
"import tensorflow as tf\n",
|
| 26 |
+
"from tensorflow import keras\n",
|
| 27 |
+
"import pandas as pd\n",
|
| 28 |
+
"import matplotlib\n",
|
| 29 |
+
"matplotlib.use(\"TkAgg\")\n",
|
| 30 |
+
"import matplotlib.pyplot as plt"
|
| 31 |
+
]
|
| 32 |
+
},
|
| 33 |
+
{
|
| 34 |
+
"cell_type": "code",
|
| 35 |
+
"execution_count": 3,
|
| 36 |
+
"id": "8b95335e-9efc-44d9-8b6f-9cf8c6929dc7",
|
| 37 |
+
"metadata": {},
|
| 38 |
+
"outputs": [
|
| 39 |
+
{
|
| 40 |
+
"data": {
|
| 41 |
+
"application/vnd.jupyter.widget-view+json": {
|
| 42 |
+
"model_id": "7c30100467c14c088b36a2be435e0014",
|
| 43 |
+
"version_major": 2,
|
| 44 |
+
"version_minor": 0
|
| 45 |
+
},
|
| 46 |
+
"text/plain": [
|
| 47 |
+
"VBox(children=(HTML(value='<center> <img\\nsrc=https://huggingface.co/front/assets/huggingface_logo-noborder.sv…"
|
| 48 |
+
]
|
| 49 |
+
},
|
| 50 |
+
"metadata": {},
|
| 51 |
+
"output_type": "display_data"
|
| 52 |
+
},
|
| 53 |
+
{
|
| 54 |
+
"ename": "RepositoryNotFoundError",
|
| 55 |
+
"evalue": "401 Client Error. (Request ID: Root=1-69d2b972-0d1155714c67789b4cfd9781;b40739f2-40be-43b8-8a63-57d36b83342d)\n\nRepository Not Found for url: https://huggingface.co/api/models/SamuelRx/FirstModel/preupload/main.\nPlease make sure you specified the correct `repo_id` and `repo_type`.\nIf you are trying to access a private or gated repo, make sure you are authenticated and your token has the required permissions.\nFor more details, see https://huggingface.co/docs/huggingface_hub/authentication\nInvalid username or password.\nNote: Creating a commit assumes that the repo already exists on the Huggingface Hub. Please use `create_repo` if it's not the case.",
|
| 56 |
+
"output_type": "error",
|
| 57 |
+
"traceback": [
|
| 58 |
+
"\u001b[31m---------------------------------------------------------------------------\u001b[39m",
|
| 59 |
+
"\u001b[31mHTTPStatusError\u001b[39m Traceback (most recent call last)",
|
| 60 |
+
"\u001b[36mFile \u001b[39m\u001b[32m~\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python312\\site-packages\\huggingface_hub\\utils\\_http.py:761\u001b[39m, in \u001b[36mhf_raise_for_status\u001b[39m\u001b[34m(response, endpoint_name)\u001b[39m\n\u001b[32m 760\u001b[39m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[32m--> \u001b[39m\u001b[32m761\u001b[39m \u001b[43mresponse\u001b[49m\u001b[43m.\u001b[49m\u001b[43mraise_for_status\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n\u001b[32m 762\u001b[39m \u001b[38;5;28;01mexcept\u001b[39;00m httpx.HTTPStatusError \u001b[38;5;28;01mas\u001b[39;00m e:\n",
|
| 61 |
+
"\u001b[36mFile \u001b[39m\u001b[32m~\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python312\\site-packages\\httpx\\_models.py:829\u001b[39m, in \u001b[36mResponse.raise_for_status\u001b[39m\u001b[34m(self)\u001b[39m\n\u001b[32m 828\u001b[39m message = message.format(\u001b[38;5;28mself\u001b[39m, error_type=error_type)\n\u001b[32m--> \u001b[39m\u001b[32m829\u001b[39m \u001b[38;5;28;01mraise\u001b[39;00m HTTPStatusError(message, request=request, response=\u001b[38;5;28mself\u001b[39m)\n",
|
| 62 |
+
"\u001b[31mHTTPStatusError\u001b[39m: Client error '401 Unauthorized' for url 'https://huggingface.co/api/models/SamuelRx/FirstModel/preupload/main'\nFor more information check: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/401",
|
| 63 |
+
"\nThe above exception was the direct cause of the following exception:\n",
|
| 64 |
+
"\u001b[31mRepositoryNotFoundError\u001b[39m Traceback (most recent call last)",
|
| 65 |
+
"\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[3]\u001b[39m\u001b[32m, line 7\u001b[39m\n\u001b[32m 4\u001b[39m login()\n\u001b[32m 6\u001b[39m \u001b[38;5;66;03m# Push your model files\u001b[39;00m\n\u001b[32m----> \u001b[39m\u001b[32m7\u001b[39m \u001b[43mupload_folder\u001b[49m\u001b[43m(\u001b[49m\u001b[43mfolder_path\u001b[49m\u001b[43m=\u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43m.\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mrepo_id\u001b[49m\u001b[43m=\u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mSamuelRx/FirstModel\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mrepo_type\u001b[49m\u001b[43m=\u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mmodel\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\n",
|
| 66 |
+
"\u001b[36mFile \u001b[39m\u001b[32m~\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python312\\site-packages\\huggingface_hub\\utils\\_validators.py:88\u001b[39m, in \u001b[36mvalidate_hf_hub_args.<locals>._inner_fn\u001b[39m\u001b[34m(*args, **kwargs)\u001b[39m\n\u001b[32m 84\u001b[39m validate_repo_id(arg_value)\n\u001b[32m 86\u001b[39m kwargs = smoothly_deprecate_legacy_arguments(fn_name=fn.\u001b[34m__name__\u001b[39m, kwargs=kwargs)\n\u001b[32m---> \u001b[39m\u001b[32m88\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mfn\u001b[49m\u001b[43m(\u001b[49m\u001b[43m*\u001b[49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m*\u001b[49m\u001b[43m*\u001b[49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n",
|
| 67 |
+
"\u001b[36mFile \u001b[39m\u001b[32m~\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python312\\site-packages\\huggingface_hub\\hf_api.py:1932\u001b[39m, in \u001b[36mfuture_compatible.<locals>._inner\u001b[39m\u001b[34m(self, *args, **kwargs)\u001b[39m\n\u001b[32m 1929\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m.run_as_future(fn, \u001b[38;5;28mself\u001b[39m, *args, **kwargs)\n\u001b[32m 1931\u001b[39m \u001b[38;5;66;03m# Otherwise, call the function normally\u001b[39;00m\n\u001b[32m-> \u001b[39m\u001b[32m1932\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mfn\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m*\u001b[49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m*\u001b[49m\u001b[43m*\u001b[49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n",
|
| 68 |
+
"\u001b[36mFile \u001b[39m\u001b[32m~\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python312\\site-packages\\huggingface_hub\\hf_api.py:5376\u001b[39m, in \u001b[36mHfApi.upload_folder\u001b[39m\u001b[34m(self, repo_id, folder_path, path_in_repo, commit_message, commit_description, token, repo_type, revision, create_pr, parent_commit, allow_patterns, ignore_patterns, delete_patterns, run_as_future)\u001b[39m\n\u001b[32m 5372\u001b[39m commit_operations = delete_operations + add_operations\n\u001b[32m 5374\u001b[39m commit_message = commit_message \u001b[38;5;129;01mor\u001b[39;00m \u001b[33m\"\u001b[39m\u001b[33mUpload folder using huggingface_hub\u001b[39m\u001b[33m\"\u001b[39m\n\u001b[32m-> \u001b[39m\u001b[32m5376\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[43m.\u001b[49m\u001b[43mcreate_commit\u001b[49m\u001b[43m(\u001b[49m\n\u001b[32m 5377\u001b[39m \u001b[43m \u001b[49m\u001b[43mrepo_type\u001b[49m\u001b[43m=\u001b[49m\u001b[43mrepo_type\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 5378\u001b[39m \u001b[43m \u001b[49m\u001b[43mrepo_id\u001b[49m\u001b[43m=\u001b[49m\u001b[43mrepo_id\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 5379\u001b[39m \u001b[43m \u001b[49m\u001b[43moperations\u001b[49m\u001b[43m=\u001b[49m\u001b[43mcommit_operations\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 5380\u001b[39m \u001b[43m \u001b[49m\u001b[43mcommit_message\u001b[49m\u001b[43m=\u001b[49m\u001b[43mcommit_message\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 5381\u001b[39m \u001b[43m \u001b[49m\u001b[43mcommit_description\u001b[49m\u001b[43m=\u001b[49m\u001b[43mcommit_description\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 5382\u001b[39m \u001b[43m \u001b[49m\u001b[43mtoken\u001b[49m\u001b[43m=\u001b[49m\u001b[43mtoken\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 5383\u001b[39m \u001b[43m \u001b[49m\u001b[43mrevision\u001b[49m\u001b[43m=\u001b[49m\u001b[43mrevision\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 5384\u001b[39m \u001b[43m \u001b[49m\u001b[43mcreate_pr\u001b[49m\u001b[43m=\u001b[49m\u001b[43mcreate_pr\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 5385\u001b[39m \u001b[43m \u001b[49m\u001b[43mparent_commit\u001b[49m\u001b[43m=\u001b[49m\u001b[43mparent_commit\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 5386\u001b[39m \u001b[43m\u001b[49m\u001b[43m)\u001b[49m\n",
|
| 69 |
+
"\u001b[36mFile \u001b[39m\u001b[32m~\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python312\\site-packages\\huggingface_hub\\utils\\_validators.py:88\u001b[39m, in \u001b[36mvalidate_hf_hub_args.<locals>._inner_fn\u001b[39m\u001b[34m(*args, **kwargs)\u001b[39m\n\u001b[32m 84\u001b[39m validate_repo_id(arg_value)\n\u001b[32m 86\u001b[39m kwargs = smoothly_deprecate_legacy_arguments(fn_name=fn.\u001b[34m__name__\u001b[39m, kwargs=kwargs)\n\u001b[32m---> \u001b[39m\u001b[32m88\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mfn\u001b[49m\u001b[43m(\u001b[49m\u001b[43m*\u001b[49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m*\u001b[49m\u001b[43m*\u001b[49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n",
|
| 70 |
+
"\u001b[36mFile \u001b[39m\u001b[32m~\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python312\\site-packages\\huggingface_hub\\hf_api.py:1932\u001b[39m, in \u001b[36mfuture_compatible.<locals>._inner\u001b[39m\u001b[34m(self, *args, **kwargs)\u001b[39m\n\u001b[32m 1929\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m.run_as_future(fn, \u001b[38;5;28mself\u001b[39m, *args, **kwargs)\n\u001b[32m 1931\u001b[39m \u001b[38;5;66;03m# Otherwise, call the function normally\u001b[39;00m\n\u001b[32m-> \u001b[39m\u001b[32m1932\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mfn\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m*\u001b[49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m*\u001b[49m\u001b[43m*\u001b[49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n",
|
| 71 |
+
"\u001b[36mFile \u001b[39m\u001b[32m~\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python312\\site-packages\\huggingface_hub\\hf_api.py:4688\u001b[39m, in \u001b[36mHfApi.create_commit\u001b[39m\u001b[34m(self, repo_id, operations, commit_message, commit_description, token, repo_type, revision, create_pr, num_threads, parent_commit, run_as_future, _hot_reload)\u001b[39m\n\u001b[32m 4685\u001b[39m \u001b[38;5;66;03m# If updating twice the same file or update then delete a file in a single commit\u001b[39;00m\n\u001b[32m 4686\u001b[39m _warn_on_overwriting_operations(operations)\n\u001b[32m-> \u001b[39m\u001b[32m4688\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[43m.\u001b[49m\u001b[43mpreupload_lfs_files\u001b[49m\u001b[43m(\u001b[49m\n\u001b[32m 4689\u001b[39m \u001b[43m \u001b[49m\u001b[43mrepo_id\u001b[49m\u001b[43m=\u001b[49m\u001b[43mrepo_id\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 4690\u001b[39m \u001b[43m \u001b[49m\u001b[43madditions\u001b[49m\u001b[43m=\u001b[49m\u001b[43madditions\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 4691\u001b[39m \u001b[43m \u001b[49m\u001b[43mtoken\u001b[49m\u001b[43m=\u001b[49m\u001b[43mtoken\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 4692\u001b[39m \u001b[43m \u001b[49m\u001b[43mrepo_type\u001b[49m\u001b[43m=\u001b[49m\u001b[43mrepo_type\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 4693\u001b[39m \u001b[43m \u001b[49m\u001b[43mrevision\u001b[49m\u001b[43m=\u001b[49m\u001b[43munquoted_revision\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;66;43;03m# first-class methods take unquoted revision\u001b[39;49;00m\n\u001b[32m 4694\u001b[39m \u001b[43m \u001b[49m\u001b[43mcreate_pr\u001b[49m\u001b[43m=\u001b[49m\u001b[43mcreate_pr\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 4695\u001b[39m \u001b[43m \u001b[49m\u001b[43mnum_threads\u001b[49m\u001b[43m=\u001b[49m\u001b[43mnum_threads\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 4696\u001b[39m \u001b[43m \u001b[49m\u001b[43mfree_memory\u001b[49m\u001b[43m=\u001b[49m\u001b[38;5;28;43;01mFalse\u001b[39;49;00m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;66;43;03m# do not remove `CommitOperationAdd.path_or_fileobj` on LFS files for \"normal\" users\u001b[39;49;00m\n\u001b[32m 4697\u001b[39m \u001b[43m\u001b[49m\u001b[43m)\u001b[49m\n\u001b[32m 4699\u001b[39m files_to_copy = _fetch_files_to_copy(\n\u001b[32m 4700\u001b[39m copies=copies,\n\u001b[32m 4701\u001b[39m repo_type=repo_type,\n\u001b[32m (...)\u001b[39m\u001b[32m 4705\u001b[39m endpoint=\u001b[38;5;28mself\u001b[39m.endpoint,\n\u001b[32m 4706\u001b[39m )\n\u001b[32m 4707\u001b[39m \u001b[38;5;66;03m# Remove no-op operations (files that have not changed)\u001b[39;00m\n",
|
| 72 |
+
"\u001b[36mFile \u001b[39m\u001b[32m~\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python312\\site-packages\\huggingface_hub\\hf_api.py:4915\u001b[39m, in \u001b[36mHfApi.preupload_lfs_files\u001b[39m\u001b[34m(self, repo_id, additions, token, repo_type, revision, create_pr, num_threads, free_memory, gitignore_content)\u001b[39m\n\u001b[32m 4913\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mlen\u001b[39m(additions_no_upload_mode) > \u001b[32m0\u001b[39m:\n\u001b[32m 4914\u001b[39m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[32m-> \u001b[39m\u001b[32m4915\u001b[39m \u001b[43m_fetch_upload_modes\u001b[49m\u001b[43m(\u001b[49m\n\u001b[32m 4916\u001b[39m \u001b[43m \u001b[49m\u001b[43madditions\u001b[49m\u001b[43m=\u001b[49m\u001b[43madditions_no_upload_mode\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 4917\u001b[39m \u001b[43m \u001b[49m\u001b[43mrepo_type\u001b[49m\u001b[43m=\u001b[49m\u001b[43mrepo_type\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 4918\u001b[39m \u001b[43m \u001b[49m\u001b[43mrepo_id\u001b[49m\u001b[43m=\u001b[49m\u001b[43mrepo_id\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 4919\u001b[39m \u001b[43m \u001b[49m\u001b[43mheaders\u001b[49m\u001b[43m=\u001b[49m\u001b[43mheaders\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 4920\u001b[39m \u001b[43m \u001b[49m\u001b[43mrevision\u001b[49m\u001b[43m=\u001b[49m\u001b[43mrevision\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 4921\u001b[39m \u001b[43m \u001b[49m\u001b[43mendpoint\u001b[49m\u001b[43m=\u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m.\u001b[49m\u001b[43mendpoint\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 4922\u001b[39m \u001b[43m \u001b[49m\u001b[43mcreate_pr\u001b[49m\u001b[43m=\u001b[49m\u001b[43mcreate_pr\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;129;43;01mor\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mFalse\u001b[39;49;00m\u001b[43m,\u001b[49m\n\u001b[32m 4923\u001b[39m \u001b[43m \u001b[49m\u001b[43mgitignore_content\u001b[49m\u001b[43m=\u001b[49m\u001b[43mgitignore_content\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 4924\u001b[39m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n\u001b[32m 4925\u001b[39m \u001b[38;5;28;01mexcept\u001b[39;00m RepositoryNotFoundError \u001b[38;5;28;01mas\u001b[39;00m e:\n\u001b[32m 4926\u001b[39m e.append_to_message(_CREATE_COMMIT_NO_REPO_ERROR_MESSAGE)\n",
|
| 73 |
+
"\u001b[36mFile \u001b[39m\u001b[32m~\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python312\\site-packages\\huggingface_hub\\utils\\_validators.py:88\u001b[39m, in \u001b[36mvalidate_hf_hub_args.<locals>._inner_fn\u001b[39m\u001b[34m(*args, **kwargs)\u001b[39m\n\u001b[32m 84\u001b[39m validate_repo_id(arg_value)\n\u001b[32m 86\u001b[39m kwargs = smoothly_deprecate_legacy_arguments(fn_name=fn.\u001b[34m__name__\u001b[39m, kwargs=kwargs)\n\u001b[32m---> \u001b[39m\u001b[32m88\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mfn\u001b[49m\u001b[43m(\u001b[49m\u001b[43m*\u001b[49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m*\u001b[49m\u001b[43m*\u001b[49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n",
|
| 74 |
+
"\u001b[36mFile \u001b[39m\u001b[32m~\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python312\\site-packages\\huggingface_hub\\_commit_api.py:760\u001b[39m, in \u001b[36m_fetch_upload_modes\u001b[39m\u001b[34m(additions, repo_type, repo_id, headers, revision, endpoint, create_pr, gitignore_content)\u001b[39m\n\u001b[32m 751\u001b[39m payload[\u001b[33m\"\u001b[39m\u001b[33mgitIgnore\u001b[39m\u001b[33m\"\u001b[39m] = gitignore_content\n\u001b[32m 753\u001b[39m resp = http_backoff(\n\u001b[32m 754\u001b[39m \u001b[33m\"\u001b[39m\u001b[33mPOST\u001b[39m\u001b[33m\"\u001b[39m,\n\u001b[32m 755\u001b[39m \u001b[33mf\u001b[39m\u001b[33m\"\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mendpoint\u001b[38;5;132;01m}\u001b[39;00m\u001b[33m/api/\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mrepo_type\u001b[38;5;132;01m}\u001b[39;00m\u001b[33ms/\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mrepo_id\u001b[38;5;132;01m}\u001b[39;00m\u001b[33m/preupload/\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mrevision\u001b[38;5;132;01m}\u001b[39;00m\u001b[33m\"\u001b[39m,\n\u001b[32m (...)\u001b[39m\u001b[32m 758\u001b[39m params={\u001b[33m\"\u001b[39m\u001b[33mcreate_pr\u001b[39m\u001b[33m\"\u001b[39m: \u001b[33m\"\u001b[39m\u001b[33m1\u001b[39m\u001b[33m\"\u001b[39m} \u001b[38;5;28;01mif\u001b[39;00m create_pr \u001b[38;5;28;01melse\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m,\n\u001b[32m 759\u001b[39m )\n\u001b[32m--> \u001b[39m\u001b[32m760\u001b[39m \u001b[43mhf_raise_for_status\u001b[49m\u001b[43m(\u001b[49m\u001b[43mresp\u001b[49m\u001b[43m)\u001b[49m\n\u001b[32m 761\u001b[39m preupload_info = _validate_preupload_info(resp.json())\n\u001b[32m 762\u001b[39m upload_modes.update(**{file[\u001b[33m\"\u001b[39m\u001b[33mpath\u001b[39m\u001b[33m\"\u001b[39m]: file[\u001b[33m\"\u001b[39m\u001b[33muploadMode\u001b[39m\u001b[33m\"\u001b[39m] \u001b[38;5;28;01mfor\u001b[39;00m file \u001b[38;5;129;01min\u001b[39;00m preupload_info[\u001b[33m\"\u001b[39m\u001b[33mfiles\u001b[39m\u001b[33m\"\u001b[39m]})\n",
|
| 75 |
+
"\u001b[36mFile \u001b[39m\u001b[32m~\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python312\\site-packages\\huggingface_hub\\utils\\_http.py:847\u001b[39m, in \u001b[36mhf_raise_for_status\u001b[39m\u001b[34m(response, endpoint_name)\u001b[39m\n\u001b[32m 845\u001b[39m repo_err.repo_type = repo_type\n\u001b[32m 846\u001b[39m repo_err.repo_id = repo_id\n\u001b[32m--> \u001b[39m\u001b[32m847\u001b[39m \u001b[38;5;28;01mraise\u001b[39;00m repo_err \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01me\u001b[39;00m\n\u001b[32m 849\u001b[39m \u001b[38;5;28;01melif\u001b[39;00m response.status_code == \u001b[32m400\u001b[39m:\n\u001b[32m 850\u001b[39m message = (\n\u001b[32m 851\u001b[39m \u001b[33mf\u001b[39m\u001b[33m\"\u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[33mBad request for \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mendpoint_name\u001b[38;5;132;01m}\u001b[39;00m\u001b[33m endpoint:\u001b[39m\u001b[33m\"\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m endpoint_name \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m \u001b[38;5;28;01melse\u001b[39;00m \u001b[33m\"\u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[33mBad request:\u001b[39m\u001b[33m\"\u001b[39m\n\u001b[32m 852\u001b[39m )\n",
|
| 76 |
+
"\u001b[31mRepositoryNotFoundError\u001b[39m: 401 Client Error. (Request ID: Root=1-69d2b972-0d1155714c67789b4cfd9781;b40739f2-40be-43b8-8a63-57d36b83342d)\n\nRepository Not Found for url: https://huggingface.co/api/models/SamuelRx/FirstModel/preupload/main.\nPlease make sure you specified the correct `repo_id` and `repo_type`.\nIf you are trying to access a private or gated repo, make sure you are authenticated and your token has the required permissions.\nFor more details, see https://huggingface.co/docs/huggingface_hub/authentication\nInvalid username or password.\nNote: Creating a commit assumes that the repo already exists on the Huggingface Hub. Please use `create_repo` if it's not the case."
|
| 77 |
+
]
|
| 78 |
+
}
|
| 79 |
+
],
|
| 80 |
+
"source": [
|
| 81 |
+
"from huggingface_hub import login, upload_folder\n",
|
| 82 |
+
"\n",
|
| 83 |
+
"# (optional) Login with your Hugging Face credentials\n",
|
| 84 |
+
"login()\n",
|
| 85 |
+
"\n",
|
| 86 |
+
"# Push your model files\n",
|
| 87 |
+
"upload_folder(folder_path=\".\", repo_id=\"SamuelRx/FirstModel\", repo_type=\"model\")\n",
|
| 88 |
+
"\n"
|
| 89 |
+
]
|
| 90 |
+
},
|
| 91 |
+
{
|
| 92 |
+
"cell_type": "code",
|
| 93 |
+
"execution_count": null,
|
| 94 |
+
"id": "09438fbc-8705-4712-8111-078a22cf8d1d",
|
| 95 |
+
"metadata": {},
|
| 96 |
+
"outputs": [],
|
| 97 |
+
"source": [
|
| 98 |
+
"housing = fetch_california_housing()\n",
|
| 99 |
+
"x_train_full, x_test, y_train_full, y_test = train_test_split(housing.data,housing.target)\n",
|
| 100 |
+
"x_train, x_valid, y_train, y_valid = train_test_split(x_train_full, y_train_full)\n",
|
| 101 |
+
"\n",
|
| 102 |
+
"scaler=StandardScaler()\n",
|
| 103 |
+
"x_train = scaler.fit_transform(x_train)\n",
|
| 104 |
+
"x_valid = scaler.transform(x_valid)\n",
|
| 105 |
+
"x_test=scaler.transform(x_test)"
|
| 106 |
+
]
|
| 107 |
+
},
|
| 108 |
+
{
|
| 109 |
+
"cell_type": "code",
|
| 110 |
+
"execution_count": null,
|
| 111 |
+
"id": "0e80a374-8f22-40cd-ad12-f15898142f6b",
|
| 112 |
+
"metadata": {},
|
| 113 |
+
"outputs": [],
|
| 114 |
+
"source": [
|
| 115 |
+
"class WideAndDeepModel(keras.Model):\n",
|
| 116 |
+
" def __init__(self, units=30, activation=\"relu\", **kwargs):\n",
|
| 117 |
+
" super().__init__(**kwargs) \n",
|
| 118 |
+
" self.hidden1 = keras.layers.Dense(units, activation=activation)\n",
|
| 119 |
+
" self.hidden2 = keras.layers.Dense(units, activation=activation)\n",
|
| 120 |
+
" self.main_output = keras.layers.Dense(1)\n",
|
| 121 |
+
" self.aux_output = keras.layers.Dense(1)\n",
|
| 122 |
+
" def call(self, inputs):\n",
|
| 123 |
+
" input_A, input_B = inputs\n",
|
| 124 |
+
" hidden1 = self.hidden1(input_B)\n",
|
| 125 |
+
" hidden2 = self.hidden2(hidden1)\n",
|
| 126 |
+
" concat = keras.layers.concatenate([input_A, hidden2])\n",
|
| 127 |
+
" main_output = self.main_output(concat)\n",
|
| 128 |
+
" aux_output = self.aux_output(hidden2)\n",
|
| 129 |
+
" return main_output, aux_output"
|
| 130 |
+
]
|
| 131 |
+
},
|
| 132 |
+
{
|
| 133 |
+
"cell_type": "code",
|
| 134 |
+
"execution_count": null,
|
| 135 |
+
"id": "3a81c2b8-4e95-453a-8d1a-4ba132bd483b",
|
| 136 |
+
"metadata": {},
|
| 137 |
+
"outputs": [],
|
| 138 |
+
"source": [
|
| 139 |
+
"model = WideAndDeepModel()\n",
|
| 140 |
+
"\n",
|
| 141 |
+
"model.compile(\n",
|
| 142 |
+
" loss=[\"mse\", \"mse\"], # uma loss para cada saída\n",
|
| 143 |
+
" optimizer=keras.optimizers.SGD(learning_rate=1e-3),\n",
|
| 144 |
+
" loss_weights=[0.8, 0.2] # opcional: peso de cada saída\n",
|
| 145 |
+
")\n",
|
| 146 |
+
"\n",
|
| 147 |
+
"# Os targets precisam ser tuplas/listas correspondentes às saídas\n",
|
| 148 |
+
"history = model.fit(\n",
|
| 149 |
+
" (x_train_A, x_train_B), # entradas\n",
|
| 150 |
+
" (y_train, y_train), # targets: main_output e aux_output\n",
|
| 151 |
+
" epochs=20,\n",
|
| 152 |
+
" validation_data=((x_valid_A, x_valid_B), (y_valid, y_valid))\n",
|
| 153 |
+
")\n",
|
| 154 |
+
"\n",
|
| 155 |
+
"# Avaliar\n",
|
| 156 |
+
"mse_test = model.evaluate((x_test_A, x_test_B), (y_test, y_test))\n",
|
| 157 |
+
"\n",
|
| 158 |
+
"# Predição\n",
|
| 159 |
+
"y_pred_main, y_pred_aux = model.predict((x_new_A, x_new_B))\n",
|
| 160 |
+
"print(y_pred_main)\n",
|
| 161 |
+
"\n",
|
| 162 |
+
"model.save(\"my_keras_model.h5\") #Salvando modelo, legal isso"
|
| 163 |
+
]
|
| 164 |
+
},
|
| 165 |
+
{
|
| 166 |
+
"cell_type": "code",
|
| 167 |
+
"execution_count": null,
|
| 168 |
+
"id": "044977e1-310c-4f39-9e46-303c86bfbd0f",
|
| 169 |
+
"metadata": {},
|
| 170 |
+
"outputs": [],
|
| 171 |
+
"source": [
|
| 172 |
+
"model = keras.models.load_model(\"my_keras_model.h5\")"
|
| 173 |
+
]
|
| 174 |
+
},
|
| 175 |
+
{
|
| 176 |
+
"cell_type": "code",
|
| 177 |
+
"execution_count": null,
|
| 178 |
+
"id": "079566ef-1c0c-400a-aeba-253ba01d1d5c",
|
| 179 |
+
"metadata": {},
|
| 180 |
+
"outputs": [],
|
| 181 |
+
"source": [
|
| 182 |
+
"checkpoint_cb = keras.callbacs.ModelCheckpoint(\"my_keras_model.h5\")\n",
|
| 183 |
+
"history = model.fit(x_train, y_train, epochs = 10, callbacks = [checkpoint_cb])"
|
| 184 |
+
]
|
| 185 |
+
},
|
| 186 |
+
{
|
| 187 |
+
"cell_type": "code",
|
| 188 |
+
"execution_count": null,
|
| 189 |
+
"id": "cda6a722-d453-4544-a92f-42f3f2b6d131",
|
| 190 |
+
"metadata": {},
|
| 191 |
+
"outputs": [],
|
| 192 |
+
"source": []
|
| 193 |
+
}
|
| 194 |
+
],
|
| 195 |
+
"metadata": {
|
| 196 |
+
"kernelspec": {
|
| 197 |
+
"display_name": "Python 3",
|
| 198 |
+
"language": "python",
|
| 199 |
+
"name": "python3"
|
| 200 |
+
},
|
| 201 |
+
"language_info": {
|
| 202 |
+
"codemirror_mode": {
|
| 203 |
+
"name": "ipython",
|
| 204 |
+
"version": 3
|
| 205 |
+
},
|
| 206 |
+
"file_extension": ".py",
|
| 207 |
+
"mimetype": "text/x-python",
|
| 208 |
+
"name": "python",
|
| 209 |
+
"nbconvert_exporter": "python",
|
| 210 |
+
"pygments_lexer": "ipython3",
|
| 211 |
+
"version": "3.12.10"
|
| 212 |
+
}
|
| 213 |
+
},
|
| 214 |
+
"nbformat": 4,
|
| 215 |
+
"nbformat_minor": 5
|
| 216 |
+
}
|