{ "cells": [ { "cell_type": "markdown", "id": "1587aa08", "metadata": {}, "source": [ "# Lesson 28: TensorFlow/Keras classification activity\n", "\n", "## Notebook set up\n", "\n", "### Vocareum compatibility\n", "\n", "Check the notebook path to determine if we are running in Vocareum, if so, install TensorFlow 2.17 and Keras 3.2.0 via pip." ] }, { "cell_type": "code", "execution_count": 1, "id": "96b7c4f9", "metadata": {}, "outputs": [], "source": [ "from pathlib import Path\n", "\n", "# Get the parts of the current directory path as a tuple\n", "path_parts = Path.cwd().parts\n", "\n", "# Check if 'voc' is the first directory level\n", "is_voc_root = len(path_parts) > 1 and path_parts[1] == \"voc\"\n", "\n", "# If we are in a Vocareum instance, upgrade TensorFlow and Keras\n", "if is_voc_root:\n", " print(\"Running in Vocareum environment\")\n", "\n", " get_ipython().run_line_magic(\n", " \"pip\", \"install -qq --no-warn-script-location tensorflow==2.17.0 keras==3.2.0\"\n", " )" ] }, { "cell_type": "markdown", "id": "790c0e7d", "metadata": {}, "source": [ "### Imports" ] }, { "cell_type": "code", "execution_count": 2, "id": "39b9bf45", "metadata": {}, "outputs": [], "source": [ "# Suppress noisy TensorFlow logs before importing\n", "import os\n", "os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'\n", "\n", "# Standard library imports\n", "import itertools\n", "\n", "from pathlib import Path\n", "from shutil import rmtree\n", "from typing import Tuple, Optional\n", "\n", "# Third party imports\n", "import matplotlib.pyplot as plt\n", "import numpy as np\n", "import pandas as pd\n", "import tensorflow as tf\n", "\n", "from sklearn.linear_model import LogisticRegression\n", "from sklearn.metrics import accuracy_score, ConfusionMatrixDisplay\n", "from sklearn.model_selection import train_test_split\n", "from sklearn.preprocessing import StandardScaler\n", "from sklearn.utils.class_weight import compute_class_weight\n", "from tensorflow import keras\n", "from tensorflow.keras import layers\n", "from tensorflow.keras.callbacks import EarlyStopping, TensorBoard" ] }, { "cell_type": "markdown", "id": "1a6707ac", "metadata": {}, "source": [ "### GPU configuration" ] }, { "cell_type": "code", "execution_count": 3, "id": "8982d6f8", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Using GPU: NVIDIA GeForce GTX 1070, compute capability 6.1\n", "Memory growth enabled\n" ] } ], "source": [ "# Configure GPU settings\n", "gpus = tf.config.list_physical_devices('GPU')\n", "\n", "if gpus:\n", " try:\n", " # Use only GPU 0\n", " tf.config.set_visible_devices(gpus[0], 'GPU')\n", "\n", " # Enable memory growth\n", " tf.config.experimental.set_memory_growth(gpus[0], True)\n", "\n", " # Get GPU\n", " details = tf.config.experimental.get_device_details(gpus[0])\n", " cc = details.get('compute_capability', (0, 0))\n", " device = details.get('device_name', '')\n", "\n", " print(f'Using GPU: {device}, compute capability {cc[0]}.{cc[1]}')\n", " print('Memory growth enabled')\n", "\n", " except RuntimeError as e:\n", " print(e)\n", "\n", "else:\n", " print('No GPU found')" ] }, { "cell_type": "markdown", "id": "13ba26d9", "metadata": {}, "source": [ "### Global TensorFlow training configuration" ] }, { "cell_type": "code", "execution_count": 4, "id": "73b5ddcb", "metadata": {}, "outputs": [], "source": [ "# Remove and recreate logs directory to clear any old TensorBoard logs\n", "log_dir = ('../logs')\n", "rmtree(log_dir, ignore_errors=True)\n", "Path(log_dir).mkdir(parents=True, exist_ok=True)\n", "\n", "# Set verbosity for wherever it's accepted\n", "verbose = 0\n", "\n", "# Fix TensorFlow random state for reproducibility\n", "tf.random.set_seed(315)" ] }, { "cell_type": "markdown", "id": "98f5eab8", "metadata": {}, "source": [ "## 1. Data preparation\n", "\n", "### 1.1. Load occupancy data" ] }, { "cell_type": "code", "execution_count": 5, "id": "e94bf1e5", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
| \n", " | date | \n", "Temperature | \n", "Humidity | \n", "Light | \n", "CO2 | \n", "HumidityRatio | \n", "Occupancy | \n", "
|---|---|---|---|---|---|---|---|
| 0 | \n", "2015-02-04 17:51:00 | \n", "23.18 | \n", "27.2720 | \n", "426.0 | \n", "721.25 | \n", "0.004793 | \n", "1 | \n", "
| 1 | \n", "2015-02-04 17:51:59 | \n", "23.15 | \n", "27.2675 | \n", "429.5 | \n", "714.00 | \n", "0.004783 | \n", "1 | \n", "
| 2 | \n", "2015-02-04 17:53:00 | \n", "23.15 | \n", "27.2450 | \n", "426.0 | \n", "713.50 | \n", "0.004779 | \n", "1 | \n", "
| 3 | \n", "2015-02-04 17:54:00 | \n", "23.15 | \n", "27.2000 | \n", "426.0 | \n", "708.25 | \n", "0.004772 | \n", "1 | \n", "
| 4 | \n", "2015-02-04 17:55:00 | \n", "23.10 | \n", "27.2000 | \n", "426.0 | \n", "704.50 | \n", "0.004757 | \n", "1 | \n", "