diff --git a/PythonAI/JupyterLab/DQNCartPole.ipynb b/PythonAI/JupyterLab/DQNCartPole.ipynb new file mode 100644 index 0000000..818191e --- /dev/null +++ b/PythonAI/JupyterLab/DQNCartPole.ipynb @@ -0,0 +1,327 @@ +{ + "cells": [ + { + "cell_type": "code", + "source": [ + "# And for visualization on Colab install\n", + "# !apt-get install x11-utils > /dev/null 2>&1 \n", + "# !pip install pyglet\n", + "# !apt-get install -y xvfb python-opengl > /dev/null 2>&1\n", + "# !pip install gym pyvirtualdisplay > /dev/null 2>&1" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "tOhAux1ubEKG", + "outputId": "727c632b-d755-4ac2-dd1a-9b84c4f1259f" + }, + "execution_count": 1, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Requirement already satisfied: pyglet in /usr/local/lib/python3.7/dist-packages (1.5.0)\n", + "Requirement already satisfied: future in /usr/local/lib/python3.7/dist-packages (from pyglet) (0.16.0)\n" + ] + } + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "id": "JZV-qP-yay8_" + }, + "outputs": [], + "source": [ + "import random\n", + "import gym\n", + "#import math\n", + "import numpy as np\n", + "from collections import deque\n", + "import tensorflow as tf\n", + "from tensorflow.keras.models import Sequential\n", + "from tensorflow.keras.layers import Dense\n", + "from tensorflow.keras.optimizers import Adam\n" + ] + }, + { + "cell_type": "code", + "source": [ + "## Uncomment if working on Colab\n", + "# from pyvirtualdisplay import Display\n", + "# display = Display(visible=0, size=(600, 400))\n", + "# display.start()" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "i63z1vW0c4Sp", + "outputId": "222984a5-6556-4942-a509-c5a1a639c63f" + }, + "execution_count": 3, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "" + ] + }, + "metadata": {}, + "execution_count": 3 + } + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": { + "id": "ikpmIrLyay9B" + }, + "outputs": [], + "source": [ + "EPOCHS = 1000\n", + "THRESHOLD = 45\n", + "MONITOR = True" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": { + "id": "trKmD7d2ay9C" + }, + "outputs": [], + "source": [ + "class DQN():\n", + " def __init__(self, env_string,batch_size=64):\n", + " self.memory = deque(maxlen=100000)\n", + " self.env = gym.make(env_string)\n", + " input_size = self.env.observation_space.shape[0]\n", + " action_size = self.env.action_space.n\n", + " self.batch_size = batch_size\n", + " self.gamma = 1.0\n", + " self.epsilon = 1.0\n", + " self.epsilon_min = 0.01\n", + " self.epsilon_decay = 0.995\n", + " \n", + " alpha=0.01\n", + " alpha_decay=0.01\n", + " if MONITOR: self.env = gym.wrappers.Monitor(self.env, 'data/'+env_string, force=True)\n", + " \n", + " # Init model\n", + " self.model = Sequential()\n", + " self.model.add(Dense(24, input_dim=input_size, activation='tanh'))\n", + " self.model.add(Dense(48, activation='tanh'))\n", + " self.model.add(Dense(action_size, activation='linear'))\n", + " self.model.compile(loss='mse', optimizer=Adam(lr=alpha, decay=alpha_decay))\n", + "\n", + " def remember(self, state, action, reward, next_state, done):\n", + " self.memory.append((state, action, reward, next_state, done))\n", + "\n", + " def choose_action(self, state, epsilon):\n", + " if np.random.random() <= epsilon:\n", + " return self.env.action_space.sample()\n", + " else:\n", + " return np.argmax(self.model.predict(state))\n", + "\n", + " def preprocess_state(self, state):\n", + " return np.reshape(state, [1, 4])\n", + "\n", + " def replay(self, batch_size):\n", + " x_batch, y_batch = [], []\n", + " minibatch = random.sample(self.memory, min(len(self.memory), batch_size))\n", + " for state, action, reward, next_state, done in minibatch:\n", + " y_target = self.model.predict(state)\n", + " y_target[0][action] = reward if done else reward + self.gamma * np.max(self.model.predict(next_state)[0])\n", + " x_batch.append(state[0])\n", + " y_batch.append(y_target[0])\n", + " \n", + " self.model.fit(np.array(x_batch), np.array(y_batch), batch_size=len(x_batch), verbose=0)\n", + " #epsilon = max(epsilon_min, epsilon_decay*epsilon) # decrease epsilon\n", + " \n", + "\n", + " def train(self):\n", + " scores = deque(maxlen=100)\n", + " avg_scores = []\n", + " \n", + "\n", + " for e in range(EPOCHS):\n", + " state = self.env.reset()\n", + " state = self.preprocess_state(state)\n", + " done = False\n", + " i = 0\n", + " while not done:\n", + " action = self.choose_action(state,self.epsilon)\n", + " next_state, reward, done, _ = self.env.step(action)\n", + " next_state = self.preprocess_state(next_state)\n", + " self.remember(state, action, reward, next_state, done)\n", + " state = next_state\n", + " self.epsilon = max(self.epsilon_min, self.epsilon_decay*self.epsilon) # decrease epsilon\n", + " i += 1\n", + "\n", + " scores.append(i)\n", + " mean_score = np.mean(scores)\n", + " avg_scores.append(mean_score)\n", + " if mean_score >= THRESHOLD and e >= 100:\n", + " print('Ran {} episodes. Solved after {} trials βœ”'.format(e, e - 100))\n", + " return avg_scores\n", + " if e % 100 == 0:\n", + " print('[Episode {}] - Mean survival time over last 100 episodes was {} ticks.'.format(e, mean_score))\n", + "\n", + " self.replay(self.batch_size)\n", + " \n", + " print('Did not solve after {} episodes 😞'.format(e))\n", + " return avg_scores\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "4STstW_7ay9E", + "outputId": "b9a26bf3-dd8c-4b4f-c92c-9b6a75333acf" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stderr", + "text": [ + "/usr/local/lib/python3.7/dist-packages/keras/optimizer_v2/adam.py:105: UserWarning: The `lr` argument is deprecated, use `learning_rate` instead.\n", + " super(Adam, self).__init__(name, **kwargs)\n" + ] + }, + { + "output_type": "stream", + "name": "stdout", + "text": [ + "[Episode 0] - Mean survival time over last 100 episodes was 28.0 ticks.\n", + "[Episode 100] - Mean survival time over last 100 episodes was 15.71 ticks.\n", + "[Episode 200] - Mean survival time over last 100 episodes was 27.81 ticks.\n", + "Ran 259 episodes. Solved after 159 trials βœ”\n" + ] + } + ], + "source": [ + "env_string = 'CartPole-v0'\n", + "agent = DQN(env_string)\n", + "scores = agent.train()" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 265 + }, + "id": "28iEbGwzay9F", + "outputId": "e9ab9177-f5eb-472f-dead-64a17be16cf7" + }, + "outputs": [ + { + "output_type": "display_data", + "data": { + "text/plain": [ + "
" + ], + "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXAAAAD4CAYAAAD1jb0+AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4yLjIsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+WH4yJAAAgAElEQVR4nO3deXxU9bn48c+TyUpWsoeELew7hLCIYHGrFje07tWiVemmV9veapd7W9t7e9vaa6WLt1argNaKrejPfa0oBRQI+xoIIYSE7JCdTDIz398fM8EgCQnJLDmT5/165eXMmZlznuMJT77znO8ixhiUUkpZT0igA1BKKdU7msCVUsqiNIErpZRFaQJXSimL0gSulFIWFerPgyUnJ5sRI0b485BKKWV5W7ZsqTbGpHx+u18T+IgRI8jLy/PnIZVSyvJE5Ehn27WEopRSFqUJXCmlLEoTuFJKWZQmcKWUsihN4EopZVGawJVSyqI0gSullEVpAldKKR+qrG/hf9/Np7Cq0ev71gSulFI+VFDVyB/XFFBe1+L1fWsCV0opH2pP3BkJUV7ftyZwpZTyoTJPAk+Pi/T6vnucwEXEJiLbROQNz/MVInJYRLZ7fqZ7PTqllLK4srqTJAwKIyrc5vV9n8tkVvcD+4C4Dtu+b4x5ybshKaVU8Civa/FJ6xt62AIXkSzgCuAvPolCKaWCVFldCxnxAUzgwDLgQcD1ue2/EJGdIvKYiER09kERWSoieSKSV1VV1ZdYlVLKcsrrWnxyAxN6kMBF5Eqg0hiz5XMv/RAYD8wCEoGHOvu8MeZJY0yuMSY3JeWM+ciVUipotbQ5qWlqJSOAJZTzgatFpAhYBVwkIn81xpQZN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907eO9HzexwBjPT8ftsCfQ7ncK4ZQI7ncSxwwHNOQXVtz3Ke/ea6WqEFPhsoMMYUGmNagVXANQGOydeuAVZ6Hq8EFgcwll4zxqwFjn9uc1fndg3wrHH7FEgQkQz/RNp3XZxrV64BVhlj7MaYw0AB7t9zSzDGlBljtnoeNwD7gEyC7Nqe5Ty74vfraoUEngkc7fC8hLP/T7QaA7wnIltEZKlnW5oxpszzuBxIC0xoPtHVuQXrdb7XUzZ4pkMpLGjOVURGADOAjQTxtf3ceUI/ua5WSODBbr4xJgf4EvBtEbmg44vG/d0sKPt6BvO5efwJGAVMB8qARwMbjneJSAywGnjAGFPf8bVguradnGe/ua5WSOClwNAOz7M824KCMabU899K4BXcX7kq2r9iev5bGbgIva6rcwu662yMqTDGOI0xLuApPvs6bflzFZEw3EnteWPMy57NQXdtOzvP/nRdrZDANwNjRGSkiIQDNwOvBTgmrxCRaBGJbX8MfBHYjfv8lnjetgR4NTAR+kRX5/Ya8FVPj4W5QF2Hr+OW9Lk677W4ry24z/VmEYkQkZHAGGCTv+PrLRER4GlgnzHmtx1eCqpr29V59qvrGug7vT28G7wI9x3gQ8CPAx2PF88rG/dd6x3AnvZzA5KAfwIHgQ+AxEDH2svzewH3V8w23PXAu7o6N9w9FB73XONdQG6g4/fCuT7nOZeduP9xZ3R4/48955oPfCnQ8Z/juc7HXR7ZCWz3/CwKtmt7lvPsN9dVh9IrpZRFWaGEopRSqhOawJVSyqI0gSullEVpAldKKYvSBK6UUhalCVwppSxKE7hSSlnU/wcjnjSTXwT+2gAAAABJRU5ErkJggg==\n" + }, + "metadata": { + "needs_background": "light" + } + } + ], + "source": [ + "import matplotlib.pyplot as plt\n", + "plt.plot(scores)\n", + "plt.show()" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "178ufOPzay9F", + "outputId": "3836158e-b35a-471d-c59f-a2c0460712b8" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Model: \"sequential\"\n", + "_________________________________________________________________\n", + " Layer (type) Output Shape Param # \n", + "=================================================================\n", + " dense (Dense) (None, 24) 120 \n", + " \n", + " dense_1 (Dense) (None, 48) 1200 \n", + " \n", + " dense_2 (Dense) (None, 2) 98 \n", + " \n", + "=================================================================\n", + "Total params: 1,418\n", + "Trainable params: 1,418\n", + "Non-trainable params: 0\n", + "_________________________________________________________________\n" + ] + } + ], + "source": [ + "agent.model.summary()" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": { + "id": "5E_2klZ3ay9G" + }, + "outputs": [], + "source": [ + "agent.env.close()" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": { + "id": "b0TrCnMbay9H" + }, + "outputs": [], + "source": [ + "" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "name": "python3" + }, + "language_info": { + "name": "python" + }, + "colab": { + "name": "DQNCartPole.ipynb", + "provenance": [], + "collapsed_sections": [] + }, + "accelerator": "GPU" + }, + "nbformat": 4, + "nbformat_minor": 0 +} \ No newline at end of file diff --git a/PythonAI/JupyterLab/Pipfile b/PythonAI/JupyterLab/Pipfile index e267826..c17f83e 100644 --- a/PythonAI/JupyterLab/Pipfile +++ b/PythonAI/JupyterLab/Pipfile @@ -17,6 +17,9 @@ torch = "*" torchvision = "*" ultralytics = "*" statsmodels = "*" +accelerate = "*" +datasets = "*" +h2o = "*" [dev-packages] diff --git a/PythonAI/JupyterLab/PythonAI_7.ipynb b/PythonAI/JupyterLab/PythonAI_7.ipynb index e4902cd..1572da3 100644 --- a/PythonAI/JupyterLab/PythonAI_7.ipynb +++ b/PythonAI/JupyterLab/PythonAI_7.ipynb @@ -462,7 +462,1529 @@ "id": "f4989fd8-9578-4d77-92f0-1fd26e206359", "metadata": {}, "source": [ - "### 123" + "# Biblioteka H2O" + ] + }, + { + "cell_type": "markdown", + "id": "89675f04-66e9-4430-8b6c-338214129a81", + "metadata": {}, + "source": [ + "## AutoML" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "aa49b185-819d-453a-b19d-d9f353590a94", + "metadata": {}, + "outputs": [], + "source": [ + "import h2o\n", + "from h2o.automl import H2OAutoML\n", + "from h2o.frame import H2OFrame" + ] + }, + { + "cell_type": "markdown", + "id": "71c96630-ae51-4f8a-9991-8111918c635d", + "metadata": {}, + "source": [ + "### Wczytaj dane" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "28642ede-36a4-4c89-b4e8-0b7ac9ef432b", + "metadata": { + "collapsed": true, + "jupyter": { + "outputs_hidden": true + } + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
sepal_lengthsepal_widthpetal_lengthpetal_widthspecies
05.13.51.40.2setosa
14.93.01.40.2setosa
24.73.21.30.2setosa
34.63.11.50.2setosa
45.03.61.40.2setosa
\n", + "
" + ], + "text/plain": [ + " sepal_length sepal_width petal_length petal_width species\n", + "0 5.1 3.5 1.4 0.2 setosa\n", + "1 4.9 3.0 1.4 0.2 setosa\n", + "2 4.7 3.2 1.3 0.2 setosa\n", + "3 4.6 3.1 1.5 0.2 setosa\n", + "4 5.0 3.6 1.4 0.2 setosa" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "import pandas as pd\n", + "data = pd.read_csv('https://raw.githubusercontent.com/mwaskom/seaborn-data/master/iris.csv')\n", + "data.head()" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "de1467ab-a6a1-432c-b581-7f0943de5114", + "metadata": { + "collapsed": true, + "jupyter": { + "outputs_hidden": true + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Checking whether there is an H2O instance running at http://localhost:54321..... not found.\n", + "Attempting to start a local H2O server...\n", + " Java Version: openjdk version \"24.0.1\" 2025-04-15; OpenJDK Runtime Environment (build 24.0.1); OpenJDK 64-Bit Server VM (build 24.0.1, mixed mode, sharing)\n", + " Starting server from /home/sasza/.local/share/virtualenvs/JupyterLab-9JRWupKp/lib/python3.12/site-packages/h2o/backend/bin/h2o.jar\n", + " Ice root: /tmp/tmpa7tg0tyr\n", + " JVM stdout: /tmp/tmpa7tg0tyr/h2o_sasza_started_from_python.out\n", + " JVM stderr: /tmp/tmpa7tg0tyr/h2o_sasza_started_from_python.err\n", + " Server is running at http://127.0.0.1:54321\n", + "Connecting to H2O server at http://127.0.0.1:54321 ... successful.\n" + ] + }, + { + "data": { + "text/html": [ + "\n", + " \n", + "
\n", + " \n", + " \n", + " \n", + " \n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "
H2O_cluster_uptime:03 secs
H2O_cluster_timezone:Europe/Warsaw
H2O_data_parsing_timezone:UTC
H2O_cluster_version:3.46.0.7
H2O_cluster_version_age:1 month and 23 days
H2O_cluster_name:H2O_from_python_sasza_cnf7d2
H2O_cluster_total_nodes:1
H2O_cluster_free_memory:3.859 Gb
H2O_cluster_total_cores:4
H2O_cluster_allowed_cores:4
H2O_cluster_status:locked, healthy
H2O_connection_url:http://127.0.0.1:54321
H2O_connection_proxy:{\"http\": null, \"https\": null}
H2O_internal_security:False
Python_version:3.12.10 final
\n", + "
\n" + ], + "text/plain": [ + "-------------------------- -----------------------------\n", + "H2O_cluster_uptime: 03 secs\n", + "H2O_cluster_timezone: Europe/Warsaw\n", + "H2O_data_parsing_timezone: UTC\n", + "H2O_cluster_version: 3.46.0.7\n", + "H2O_cluster_version_age: 1 month and 23 days\n", + "H2O_cluster_name: H2O_from_python_sasza_cnf7d2\n", + "H2O_cluster_total_nodes: 1\n", + "H2O_cluster_free_memory: 3.859 Gb\n", + "H2O_cluster_total_cores: 4\n", + "H2O_cluster_allowed_cores: 4\n", + "H2O_cluster_status: locked, healthy\n", + "H2O_connection_url: http://127.0.0.1:54321\n", + "H2O_connection_proxy: {\"http\": null, \"https\": null}\n", + "H2O_internal_security: False\n", + "Python_version: 3.12.10 final\n", + "-------------------------- -----------------------------" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Parse progress: |β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| (done) 100%\n" + ] + } + ], + "source": [ + "h2o.init()\n", + "hf = h2o.H2OFrame(data)" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "523b4cd3-4174-4207-b437-ee714930cbac", + "metadata": {}, + "outputs": [], + "source": [ + "x = hf.columns[:-1]\n", + "y = 'species'\n", + "hf[y] = hf[y].asfactor() # klasyfikator" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "57187f1c-7341-428c-b5a9-232d605ffcdf", + "metadata": {}, + "outputs": [], + "source": [ + "train, test = hf.split_frame(ratios=[0.8])" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "4705361d-045f-49b2-ae62-96b61e62edeb", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "AutoML progress: |β–ˆβ–ˆ\n", + "14:25:09.382: _min_rows param, The dataset size is too small to split for min_rows=100.0: must have at least 200.0 (weighted) rows, but have only 120.0.\n", + "\n", + "β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| (done) 100%\n" + ] + }, + { + "data": { + "text/html": [ + "
Model Details\n",
+       "=============\n",
+       "H2OGeneralizedLinearEstimator : Generalized Linear Modeling\n",
+       "Model Key: GLM_1_AutoML_1_20250521_142458\n",
+       "
\n", + "
\n", + " \n", + "
\n", + " \n", + " \n", + " \n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + " \n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "
GLM Model: summary
familylinkregularizationlambda_searchnumber_of_predictors_totalnumber_of_active_predictorsnumber_of_iterationstraining_frame
multinomialmultinomialRidge ( lambda = 4.397E-5 )nlambda = 30, lambda.max = 43.968, lambda.min = 4.397E-5, lambda.1se = 4.76E-41512189AutoML_1_20250521_142458_training_py_3_sid_bd54
\n", + "
\n", + "
\n", + "
ModelMetricsMultinomialGLM: glm\n",
+       "** Reported on train data. **\n",
+       "\n",
+       "MSE: 0.0064962546799750085\n",
+       "RMSE: 0.08059934664732096\n",
+       "LogLoss: 0.02583526104275212\n",
+       "Null degrees of freedom: 119\n",
+       "Residual degrees of freedom: 105\n",
+       "Null deviance: 260.9916640757084\n",
+       "Residual deviance: 6.2004626502605085\n",
+       "AUC table was not computed: it is either disabled (model parameter 'auc_type' was set to AUTO or NONE) or the domain size exceeds the limit (maximum is 50 domains).\n",
+       "AUCPR table was not computed: it is either disabled (model parameter 'auc_type' was set to AUTO or NONE) or the domain size exceeds the limit (maximum is 50 domains).
\n", + "
\n", + " \n", + "
\n", + " \n", + " \n", + " \n", + "\n", + "\n", + "\n", + "\n", + " \n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "
Confusion Matrix: Row labels: Actual class; Column labels: Predicted class
setosaversicolorvirginicaErrorRate
42.00.00.00.00 / 42
0.045.01.00.02173911 / 46
0.00.032.00.00 / 32
42.045.033.00.00833331 / 120
\n", + "
\n", + "
\n", + "
\n", + " \n", + "
\n", + " \n", + " \n", + " \n", + "\n", + " \n", + "\n", + "\n", + "\n", + "\n", + "\n", + "
Top-3 Hit Ratios:
khit_ratio
10.9916667
21.0
31.0
\n", + "
\n", + "
\n", + "
ModelMetricsMultinomialGLM: glm\n",
+       "** Reported on cross-validation data. **\n",
+       "\n",
+       "MSE: 0.0166173490471575\n",
+       "RMSE: 0.12890829704544818\n",
+       "LogLoss: 0.05335955355419502\n",
+       "Null degrees of freedom: 119\n",
+       "Residual degrees of freedom: 105\n",
+       "Null deviance: 261.2275216133223\n",
+       "Residual deviance: 12.806292853006806\n",
+       "AUC table was not computed: it is either disabled (model parameter 'auc_type' was set to AUTO or NONE) or the domain size exceeds the limit (maximum is 50 domains).\n",
+       "AUCPR table was not computed: it is either disabled (model parameter 'auc_type' was set to AUTO or NONE) or the domain size exceeds the limit (maximum is 50 domains).
\n", + "
\n", + " \n", + "
\n", + " \n", + " \n", + " \n", + "\n", + "\n", + "\n", + "\n", + " \n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "
Confusion Matrix: Row labels: Actual class; Column labels: Predicted class
setosaversicolorvirginicaErrorRate
42.00.00.00.00 / 42
0.044.02.00.04347832 / 46
0.01.031.00.031251 / 32
42.045.033.00.0253 / 120
\n", + "
\n", + "
\n", + "
\n", + " \n", + "
\n", + " \n", + " \n", + " \n", + "\n", + " \n", + "\n", + "\n", + "\n", + "\n", + "\n", + "
Top-3 Hit Ratios:
khit_ratio
10.975
21.0
31.0
\n", + "
\n", + "
\n", + "
\n", + " \n", + "
\n", + " \n", + " \n", + " \n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + " \n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "
Cross-Validation Metrics Summary:
meansdcv_1_validcv_2_validcv_3_validcv_4_validcv_5_valid
accuracy0.9750.02282180.95833331.00.95833331.00.9583333
aicnan0.0nannannannannan
aucnan0.0nannannannannan
err0.0250.02282180.04166670.00.04166670.00.0416667
err_count0.60.54772261.00.01.00.01.0
loglikelihood0.00.00.00.00.00.00.0
logloss0.05238350.04099970.06493020.00975270.10880050.01452400.0639103
max_per_class_error0.07079360.06651150.11111110.00.10.00.1428571
mean_per_class_accuracy0.97640210.02217050.9629631.00.96666661.00.9523810
mean_per_class_error0.02359790.02217050.03703700.00.03333330.00.0476191
mse0.01609400.01446510.02088080.00153020.03636770.00260480.0190864
null_deviance52.2455020.335530552.05789652.05789651.89940652.6061652.60616
pr_aucnan0.0nannannannannan
r20.97306490.02494640.9657340.99748890.93690420.99582060.9693767
residual_deviance2.51440981.96798343.1166520.46813165.2224220.69715063.067692
rmse0.11270280.06511590.14450210.03911800.19070320.05103750.1381534
\n", + "
\n", + "
\n", + "
\n", + " \n", + "
\n", + " \n", + " \n", + " \n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + " \n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "
Scoring History:
timestampdurationiterationlambdapredictorsdeviance_traindeviance_xvaldeviance_sealphaiterationstraining_rmsetraining_loglosstraining_r2training_classification_errortraining_auctraining_pr_auc
2025-05-21 14:25:08 0.000 sec2,44E2152.13373092.14379190.00612750.0None
2025-05-21 14:25:08 0.009 sec4,27E2152.10982352.12444710.00601920.0None
2025-05-21 14:25:08 0.019 sec6,17E2152.07316022.09445530.00594300.0None
2025-05-21 14:25:08 0.029 sec8,11E2152.01847932.04905580.00592700.0None
2025-05-21 14:25:08 0.037 sec10,65E1151.94021841.98265800.00611720.0None
2025-05-21 14:25:08 0.066 sec12,41E1151.83456461.89037270.00679350.0None
2025-05-21 14:25:08 0.078 sec15,25E1151.70188761.77041980.00825970.0None
2025-05-21 14:25:08 0.093 sec18,16E1151.54959401.62636060.01067210.0None
2025-05-21 14:25:08 0.104 sec21,97E0151.38871911.46856600.01376540.0None
2025-05-21 14:25:08 0.116 sec24,6E0151.23067181.30855000.01731560.0None
---------------------------------------------------
2025-05-21 14:25:09 0.286 sec69,32E-2150.18946010.22448980.03229160.0None
2025-05-21 14:25:09 0.304 sec76,2E-2150.15863330.19286950.03195250.0None
2025-05-21 14:25:09 0.324 sec84,12E-2150.13464660.16892680.03170950.0None
2025-05-21 14:25:09 0.348 sec92,77E-3150.11576030.15160530.03163400.0None
2025-05-21 14:25:09 0.373 sec102,48E-3150.10054150.13839440.03201290.0None
2025-05-21 14:25:09 0.407 sec114,3E-3150.08800680.12877290.03280760.0None
2025-05-21 14:25:09 0.439 sec128,18E-3150.07725140.12149700.03397980.0None
2025-05-21 14:25:09 0.482 sec146,11E-3150.06761100.11547110.03522660.0None
2025-05-21 14:25:09 0.525 sec166,71E-4150.05903180.11044850.03657800.0None
2025-05-21 14:25:09 0.586 sec189,44E-4150.05167050.10671910.03778780.01890.08059930.02583530.98934560.0083333nannan
\n", + "
\n", + "
[30 rows x 17 columns]
\n", + "
\n", + " \n", + "
\n", + " \n", + " \n", + " \n", + "\n", + "\n", + "\n", + " \n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "
Variable Importances:
variablerelative_importancescaled_importancepercentage
petal_width24.05773161.00.4077040
petal_length22.17227550.92162790.3757513
sepal_width7.53101630.31303930.1276274
sepal_length5.24681950.21809290.0889173
\n", + "
\n", + "
\n",
+       "\n",
+       "[tips]\n",
+       "Use `model.explain()` to inspect the model.\n",
+       "--\n",
+       "Use `h2o.display.toggle_user_tips()` to switch on/off this section.
" + ], + "text/plain": [ + "Model Details\n", + "=============\n", + "H2OGeneralizedLinearEstimator : Generalized Linear Modeling\n", + "Model Key: GLM_1_AutoML_1_20250521_142458\n", + "\n", + "\n", + "GLM Model: summary\n", + " family link regularization lambda_search number_of_predictors_total number_of_active_predictors number_of_iterations training_frame\n", + "-- ----------- ----------- --------------------------- ------------------------------------------------------------------------------ ---------------------------- ----------------------------- ---------------------- -----------------------------------------------\n", + " multinomial multinomial Ridge ( lambda = 4.397E-5 ) nlambda = 30, lambda.max = 43.968, lambda.min = 4.397E-5, lambda.1se = 4.76E-4 15 12 189 AutoML_1_20250521_142458_training_py_3_sid_bd54\n", + "\n", + "ModelMetricsMultinomialGLM: glm\n", + "** Reported on train data. **\n", + "\n", + "MSE: 0.0064962546799750085\n", + "RMSE: 0.08059934664732096\n", + "LogLoss: 0.02583526104275212\n", + "Null degrees of freedom: 119\n", + "Residual degrees of freedom: 105\n", + "Null deviance: 260.9916640757084\n", + "Residual deviance: 6.2004626502605085\n", + "AUC table was not computed: it is either disabled (model parameter 'auc_type' was set to AUTO or NONE) or the domain size exceeds the limit (maximum is 50 domains).\n", + "AUCPR table was not computed: it is either disabled (model parameter 'auc_type' was set to AUTO or NONE) or the domain size exceeds the limit (maximum is 50 domains).\n", + "\n", + "Confusion Matrix: Row labels: Actual class; Column labels: Predicted class\n", + "setosa versicolor virginica Error Rate\n", + "-------- ------------ ----------- ---------- -------\n", + "42 0 0 0 0 / 42\n", + "0 45 1 0.0217391 1 / 46\n", + "0 0 32 0 0 / 32\n", + "42 45 33 0.00833333 1 / 120\n", + "\n", + "Top-3 Hit Ratios: \n", + "k hit_ratio\n", + "--- -----------\n", + "1 0.991667\n", + "2 1\n", + "3 1\n", + "\n", + "ModelMetricsMultinomialGLM: glm\n", + "** Reported on cross-validation data. **\n", + "\n", + "MSE: 0.0166173490471575\n", + "RMSE: 0.12890829704544818\n", + "LogLoss: 0.05335955355419502\n", + "Null degrees of freedom: 119\n", + "Residual degrees of freedom: 105\n", + "Null deviance: 261.2275216133223\n", + "Residual deviance: 12.806292853006806\n", + "AUC table was not computed: it is either disabled (model parameter 'auc_type' was set to AUTO or NONE) or the domain size exceeds the limit (maximum is 50 domains).\n", + "AUCPR table was not computed: it is either disabled (model parameter 'auc_type' was set to AUTO or NONE) or the domain size exceeds the limit (maximum is 50 domains).\n", + "\n", + "Confusion Matrix: Row labels: Actual class; Column labels: Predicted class\n", + "setosa versicolor virginica Error Rate\n", + "-------- ------------ ----------- --------- -------\n", + "42 0 0 0 0 / 42\n", + "0 44 2 0.0434783 2 / 46\n", + "0 1 31 0.03125 1 / 32\n", + "42 45 33 0.025 3 / 120\n", + "\n", + "Top-3 Hit Ratios: \n", + "k hit_ratio\n", + "--- -----------\n", + "1 0.975\n", + "2 1\n", + "3 1\n", + "\n", + "Cross-Validation Metrics Summary: \n", + " mean sd cv_1_valid cv_2_valid cv_3_valid cv_4_valid cv_5_valid\n", + "----------------------- --------- --------- ------------ ------------ ------------ ------------ ------------\n", + "accuracy 0.975 0.0228218 0.958333 1 0.958333 1 0.958333\n", + "aic nan 0 nan nan nan nan nan\n", + "auc nan 0 nan nan nan nan nan\n", + "err 0.025 0.0228218 0.0416667 0 0.0416667 0 0.0416667\n", + "err_count 0.6 0.547723 1 0 1 0 1\n", + "loglikelihood 0 0 0 0 0 0 0\n", + "logloss 0.0523835 0.0409997 0.0649302 0.00975274 0.1088 0.014524 0.0639103\n", + "max_per_class_error 0.0707936 0.0665115 0.111111 0 0.1 0 0.142857\n", + "mean_per_class_accuracy 0.976402 0.0221705 0.962963 1 0.966667 1 0.952381\n", + "mean_per_class_error 0.0235979 0.0221705 0.037037 0 0.0333333 0 0.0476191\n", + "mse 0.016094 0.0144651 0.0208808 0.00153022 0.0363677 0.00260483 0.0190864\n", + "null_deviance 52.2455 0.33553 52.0579 52.0579 51.8994 52.6062 52.6062\n", + "pr_auc nan 0 nan nan nan nan nan\n", + "r2 0.973065 0.0249464 0.965734 0.997489 0.936904 0.995821 0.969377\n", + "residual_deviance 2.51441 1.96798 3.11665 0.468132 5.22242 0.697151 3.06769\n", + "rmse 0.112703 0.0651159 0.144502 0.039118 0.190703 0.0510375 0.138153\n", + "\n", + "Scoring History: \n", + " timestamp duration iteration lambda predictors deviance_train deviance_xval deviance_se alpha iterations training_rmse training_logloss training_r2 training_classification_error training_auc training_pr_auc\n", + "--- ------------------- ---------- ----------- -------- ------------ ------------------- ------------------- -------------------- ------- ------------ ------------------- ------------------- ------------------ ------------------------------- -------------- -----------------\n", + " 2025-05-21 14:25:08 0.000 sec 2 ,44E2 15 2.133730863902864 2.143791870671952 0.006127485167832629 0.0\n", + " 2025-05-21 14:25:08 0.009 sec 4 ,27E2 15 2.109823497004997 2.124447052133733 0.006019190334986377 0.0\n", + " 2025-05-21 14:25:08 0.019 sec 6 ,17E2 15 2.073160199909241 2.094455325248478 0.005943024319550815 0.0\n", + " 2025-05-21 14:25:08 0.029 sec 8 ,11E2 15 2.018479322918903 2.049055765143296 0.005927012538951306 0.0\n", + " 2025-05-21 14:25:08 0.037 sec 10 ,65E1 15 1.9402184105915015 1.9826579902297001 0.006117181207132829 0.0\n", + " 2025-05-21 14:25:08 0.066 sec 12 ,41E1 15 1.8345645682634668 1.8903726945119605 0.006793505863131489 0.0\n", + " 2025-05-21 14:25:08 0.078 sec 15 ,25E1 15 1.7018876431332406 1.770419793586073 0.00825974339616678 0.0\n", + " 2025-05-21 14:25:08 0.093 sec 18 ,16E1 15 1.549594019755228 1.626360624800752 0.010672125419873993 0.0\n", + " 2025-05-21 14:25:08 0.104 sec 21 ,97E0 15 1.3887191064195916 1.4685659565224465 0.013765387525882756 0.0\n", + " 2025-05-21 14:25:08 0.116 sec 24 ,6E0 15 1.230671777708392 1.3085499773065608 0.01731558636398283 0.0\n", + "--- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---\n", + " 2025-05-21 14:25:09 0.286 sec 69 ,32E-2 15 0.18946005379115832 0.22448978146133225 0.03229160249507911 0.0\n", + " 2025-05-21 14:25:09 0.304 sec 76 ,2E-2 15 0.1586333130483982 0.19286946059058724 0.0319524676158399 0.0\n", + " 2025-05-21 14:25:09 0.324 sec 84 ,12E-2 15 0.13464664873996973 0.16892678250168502 0.03170950741963931 0.0\n", + " 2025-05-21 14:25:09 0.348 sec 92 ,77E-3 15 0.11576029587087913 0.15160526037916444 0.031634046547799576 0.0\n", + " 2025-05-21 14:25:09 0.373 sec 102 ,48E-3 15 0.10054152835462461 0.13839439593696545 0.03201294777566429 0.0\n", + " 2025-05-21 14:25:09 0.407 sec 114 ,3E-3 15 0.08800680617924246 0.12877291886068756 0.03280757994614725 0.0\n", + " 2025-05-21 14:25:09 0.439 sec 128 ,18E-3 15 0.07725142038556079 0.12149697003092079 0.03397976185390619 0.0\n", + " 2025-05-21 14:25:09 0.482 sec 146 ,11E-3 15 0.06761100929528671 0.11547114534552119 0.03522659584217499 0.0\n", + " 2025-05-21 14:25:09 0.525 sec 166 ,71E-4 15 0.05903181216772587 0.11044846010401099 0.03657803672695835 0.0\n", + " 2025-05-21 14:25:09 0.586 sec 189 ,44E-4 15 0.05167052208550425 0.10671910710838992 0.03778780546070541 0.0 189 0.08059934664732096 0.02583526104275212 0.9893455504109749 0.008333333333333333 nan nan\n", + "[30 rows x 17 columns]\n", + "\n", + "\n", + "Variable Importances: \n", + "variable relative_importance scaled_importance percentage\n", + "------------ --------------------- ------------------- ------------\n", + "petal_width 24.0577 1 0.407704\n", + "petal_length 22.1723 0.921628 0.375751\n", + "sepal_width 7.53102 0.313039 0.127627\n", + "sepal_length 5.24682 0.218093 0.0889173\n", + "\n", + "[tips]\n", + "Use `model.explain()` to inspect the model.\n", + "--\n", + "Use `h2o.display.toggle_user_tips()` to switch on/off this section." + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "aml = H2OAutoML(max_models=10, seed=1)\n", + "aml.train(x=x, y=y, training_frame=train)" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "75c5a4bc-f018-42f2-a6a1-b435c12c3029", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "glm prediction progress: |β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| (done) 100%\n" + ] + }, + { + "data": { + "text/html": [ + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "
predict setosa versicolor virginica
setosa 0.999928 7.21647e-058.5428e-34
setosa 0.99983 0.0001697553.19529e-38
setosa 0.999808 0.0001917568.41246e-34
setosa 0.997104 0.00289629 1.14583e-28
setosa 0.996121 0.00387855 1.09209e-30
setosa 0.99941 0.00059029 1.03092e-31
setosa 0.996053 0.00394737 5.61059e-31
setosa 0.999942 5.80918e-053.96212e-33
versicolor6.90548e-08 0.99984 0.000159472
versicolor7.55608e-05 0.999924 6.65662e-09
[10 rows x 4 columns]
" + ], + "text/plain": [ + "predict setosa versicolor virginica\n", + "---------- ----------- ------------ -----------\n", + "setosa 0.999928 7.21647e-05 8.5428e-34\n", + "setosa 0.99983 0.000169755 3.19529e-38\n", + "setosa 0.999808 0.000191756 8.41246e-34\n", + "setosa 0.997104 0.00289629 1.14583e-28\n", + "setosa 0.996121 0.00387855 1.09209e-30\n", + "setosa 0.99941 0.00059029 1.03092e-31\n", + "setosa 0.996053 0.00394737 5.61059e-31\n", + "setosa 0.999942 5.80918e-05 3.96212e-33\n", + "versicolor 6.90548e-08 0.99984 0.000159472\n", + "versicolor 7.55608e-05 0.999924 6.65662e-09\n", + "[10 rows x 4 columns]\n" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "preds = aml.leader.predict(test)\n", + "preds.head()" + ] + }, + { + "cell_type": "markdown", + "id": "d594a3e6-19d3-4c18-8ce1-b3adc73736a9", + "metadata": {}, + "source": [ + "# Sieci DQN\n", + "## Gymnasium" ] }, { diff --git a/PythonAI/JupyterLab/llm_test.ipynb b/PythonAI/JupyterLab/llm_test.ipynb new file mode 100644 index 0000000..0a9a204 --- /dev/null +++ b/PythonAI/JupyterLab/llm_test.ipynb @@ -0,0 +1,413 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 21, + "id": "7018890b-b220-48a3-a1b8-d8f6c5483f6c", + "metadata": { + "editable": true, + "slideshow": { + "slide_type": "" + }, + "tags": [] + }, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "huggingface/tokenizers: The current process just got forked, after parallelism has already been used. Disabling parallelism to avoid deadlocks...\n", + "To disable this warning, you can either:\n", + "\t- Avoid using `tokenizers` before the fork if possible\n", + "\t- Explicitly set the environment variable TOKENIZERS_PARALLELISM=(true | false)\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Cloning into 'gpt-neo-125M'...\n", + "remote: Enumerating objects: 65, done.\u001b[K\n", + "remote: Counting objects: 100% (5/5), done.\u001b[K\n", + "remote: Compressing objects: 100% (5/5), done.\u001b[K\n", + "remote: Total 65 (delta 1), reused 0 (delta 0), pack-reused 60 (from 1)\u001b[K\n", + "Unpacking objects: 100% (65/65), 1.11 MiB | 9.13 MiB/s, done.\n", + "Filtering content: 100% (4/4), 1.93 GiB | 54.88 MiB/s, done.\n" + ] + } + ], + "source": [ + "!git clone https://huggingface.co/EleutherAI/gpt-neo-125M" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "36078c20-4185-4f45-84f2-d3e95e1dcac9", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\u001b[33mDEPRECATION: Loading egg at /usr/local/lib/python3.12/dist-packages/nvfuser-0.2.13a0+0d33366-py3.12-linux-x86_64.egg is deprecated. pip 25.1 will enforce this behaviour change. A possible replacement is to use pip for package installation. Discussion can be found at https://github.com/pypa/pip/issues/12330\u001b[0m\u001b[33m\n", + "\u001b[0m\u001b[33mDEPRECATION: Loading egg at /usr/local/lib/python3.12/dist-packages/dill-0.3.9-py3.12.egg is deprecated. pip 25.1 will enforce this behaviour change. A possible replacement is to use pip for package installation. Discussion can be found at https://github.com/pypa/pip/issues/12330\u001b[0m\u001b[33m\n", + "\u001b[0m\u001b[33mDEPRECATION: Loading egg at /usr/local/lib/python3.12/dist-packages/lightning_utilities-0.11.8-py3.12.egg is deprecated. pip 25.1 will enforce this behaviour change. A possible replacement is to use pip for package installation. Discussion can be found at https://github.com/pypa/pip/issues/12330\u001b[0m\u001b[33m\n", + "\u001b[0m\u001b[33mDEPRECATION: Loading egg at /usr/local/lib/python3.12/dist-packages/opt_einsum-3.4.0-py3.12.egg is deprecated. pip 25.1 will enforce this behaviour change. A possible replacement is to use pip for package installation. Discussion can be found at https://github.com/pypa/pip/issues/12330\u001b[0m\u001b[33m\n", + "\u001b[0m\u001b[33mDEPRECATION: Loading egg at /usr/local/lib/python3.12/dist-packages/igraph-0.11.8-py3.12-linux-x86_64.egg is deprecated. pip 25.1 will enforce this behaviour change. A possible replacement is to use pip for package installation. Discussion can be found at https://github.com/pypa/pip/issues/12330\u001b[0m\u001b[33m\n", + "\u001b[0m\u001b[33mDEPRECATION: Loading egg at /usr/local/lib/python3.12/dist-packages/lightning_thunder-0.2.0.dev0-py3.12.egg is deprecated. pip 25.1 will enforce this behaviour change. A possible replacement is to use pip for package installation. Discussion can be found at https://github.com/pypa/pip/issues/12330\u001b[0m\u001b[33m\n", + "\u001b[0m\u001b[33mDEPRECATION: Loading egg at /usr/local/lib/python3.12/dist-packages/texttable-1.7.0-py3.12.egg is deprecated. pip 25.1 will enforce this behaviour change. A possible replacement is to use pip for package installation. Discussion can be found at https://github.com/pypa/pip/issues/12330\u001b[0m\u001b[33m\n", + "\u001b[0m\u001b[33mDEPRECATION: Loading egg at /usr/local/lib/python3.12/dist-packages/looseversion-1.3.0-py3.12.egg is deprecated. pip 25.1 will enforce this behaviour change. A possible replacement is to use pip for package installation. Discussion can be found at https://github.com/pypa/pip/issues/12330\u001b[0m\u001b[33m\n", + "\u001b[0m\n", + "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip is available: \u001b[0m\u001b[31;49m24.3.1\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m25.1.1\u001b[0m\n", + "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49mpython -m pip install --upgrade pip\u001b[0m\n" + ] + } + ], + "source": [ + "!pip install accelerate>=0.26.0" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "11d0a83e-391e-4590-9db6-6a2a3c140ae2", + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "huggingface/tokenizers: The current process just got forked, after parallelism has already been used. Disabling parallelism to avoid deadlocks...\n", + "To disable this warning, you can either:\n", + "\t- Avoid using `tokenizers` before the fork if possible\n", + "\t- Explicitly set the environment variable TOKENIZERS_PARALLELISM=(true | false)\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\u001b[33mDEPRECATION: Loading egg at /usr/local/lib/python3.12/dist-packages/nvfuser-0.2.13a0+0d33366-py3.12-linux-x86_64.egg is deprecated. pip 25.1 will enforce this behaviour change. A possible replacement is to use pip for package installation. Discussion can be found at https://github.com/pypa/pip/issues/12330\u001b[0m\u001b[33m\n", + "\u001b[0m\u001b[33mDEPRECATION: Loading egg at /usr/local/lib/python3.12/dist-packages/dill-0.3.9-py3.12.egg is deprecated. pip 25.1 will enforce this behaviour change. A possible replacement is to use pip for package installation. Discussion can be found at https://github.com/pypa/pip/issues/12330\u001b[0m\u001b[33m\n", + "\u001b[0m\u001b[33mDEPRECATION: Loading egg at /usr/local/lib/python3.12/dist-packages/lightning_utilities-0.11.8-py3.12.egg is deprecated. pip 25.1 will enforce this behaviour change. A possible replacement is to use pip for package installation. Discussion can be found at https://github.com/pypa/pip/issues/12330\u001b[0m\u001b[33m\n", + "\u001b[0m\u001b[33mDEPRECATION: Loading egg at /usr/local/lib/python3.12/dist-packages/opt_einsum-3.4.0-py3.12.egg is deprecated. pip 25.1 will enforce this behaviour change. A possible replacement is to use pip for package installation. Discussion can be found at https://github.com/pypa/pip/issues/12330\u001b[0m\u001b[33m\n", + "\u001b[0m\u001b[33mDEPRECATION: Loading egg at /usr/local/lib/python3.12/dist-packages/igraph-0.11.8-py3.12-linux-x86_64.egg is deprecated. pip 25.1 will enforce this behaviour change. A possible replacement is to use pip for package installation. Discussion can be found at https://github.com/pypa/pip/issues/12330\u001b[0m\u001b[33m\n", + "\u001b[0m\u001b[33mDEPRECATION: Loading egg at /usr/local/lib/python3.12/dist-packages/lightning_thunder-0.2.0.dev0-py3.12.egg is deprecated. pip 25.1 will enforce this behaviour change. A possible replacement is to use pip for package installation. Discussion can be found at https://github.com/pypa/pip/issues/12330\u001b[0m\u001b[33m\n", + "\u001b[0m\u001b[33mDEPRECATION: Loading egg at /usr/local/lib/python3.12/dist-packages/texttable-1.7.0-py3.12.egg is deprecated. pip 25.1 will enforce this behaviour change. A possible replacement is to use pip for package installation. Discussion can be found at https://github.com/pypa/pip/issues/12330\u001b[0m\u001b[33m\n", + "\u001b[0m\u001b[33mDEPRECATION: Loading egg at /usr/local/lib/python3.12/dist-packages/looseversion-1.3.0-py3.12.egg is deprecated. pip 25.1 will enforce this behaviour change. A possible replacement is to use pip for package installation. Discussion can be found at https://github.com/pypa/pip/issues/12330\u001b[0m\u001b[33m\n", + "\u001b[0m\n", + "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip is available: \u001b[0m\u001b[31;49m24.3.1\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m25.1.1\u001b[0m\n", + "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49mpython -m pip install --upgrade pip\u001b[0m\n" + ] + } + ], + "source": [ + "!pip install datasets transformers torch gradio --quiet" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "7057ed8b-c98c-446e-b40b-2de8a800cb7c", + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "/usr/local/lib/python3.12/dist-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n", + " from .autonotebook import tqdm as notebook_tqdm\n" + ] + } + ], + "source": [ + "from transformers import AutoTokenizer, AutoModelForCausalLM\n", + "import torch\n", + "\n", + "# ŚcieΕΌka do lokalnego modelu\n", + "local_model_path = \"./gpt-neo-125M\"\n", + "\n", + "# ZaΕ‚aduj tokenizer i model w trybie offline\n", + "tokenizer = AutoTokenizer.from_pretrained(local_model_path, local_files_only=True)\n", + "tokenizer.pad_token = tokenizer.eos_token\n", + "model = AutoModelForCausalLM.from_pretrained(local_model_path, local_files_only=True)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "id": "3575956e-aa32-4173-8678-9a34b838ce3c", + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Map: 100%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 20/20 [00:00<00:00, 1442.80 examples/s]" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Dataset({\n", + " features: ['input_ids', 'attention_mask'],\n", + " num_rows: 20\n", + "})\n", + "Liczba prΓ³bek po tokenizacji: 20\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "\n" + ] + } + ], + "source": [ + "from datasets import Dataset\n", + "from transformers import AutoTokenizer, AutoModelForCausalLM, TrainingArguments, Trainer, DataCollatorForLanguageModeling\n", + "\n", + "# ŚcieΕΌka do lokalnego modelu\n", + "local_model_path = \"./gpt-neo-125M\"\n", + "\n", + "# Tokenizer\n", + "tokenizer = AutoTokenizer.from_pretrained(local_model_path, local_files_only=True)\n", + "tokenizer.pad_token = tokenizer.eos_token # <-- wymagane do paddingu\n", + "\n", + "# Wczytanie danych z pliku\n", + "with open(\"data.txt\", encoding=\"utf-8\") as f:\n", + " lines = [line.strip() for line in f if line.strip()] # Usuwamy puste linie\n", + "\n", + "# Budujemy Dataset z listy sΕ‚ownikΓ³w\n", + "data = [{\"text\": line} for line in lines]\n", + "raw_dataset = Dataset.from_list(data)\n", + "\n", + "# Funkcja tokenizujΔ…ca\n", + "def tokenize_function(example):\n", + " return tokenizer(\n", + " example[\"text\"],\n", + " truncation=True,\n", + " max_length=128,\n", + " padding=\"max_length\"\n", + " )\n", + "\n", + "# Tokenizacja datasetu\n", + "tokenized_dataset = raw_dataset.map(tokenize_function, batched=True, remove_columns=[\"text\"])\n", + "\n", + "# SprawdΕΊ czy dane sΔ… OK\n", + "print(tokenized_dataset)\n", + "print(f\"Liczba prΓ³bek po tokenizacji: {len(tokenized_dataset)}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "id": "c5eedf83-e003-434e-af7c-a57d6efdd120", + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "/tmp/ipykernel_6455/2971221750.py:23: FutureWarning: `tokenizer` is deprecated and will be removed in version 5.0.0 for `Trainer.__init__`. Use `processing_class` instead.\n", + " trainer = Trainer(\n" + ] + }, + { + "data": { + "text/html": [ + "\n", + "
\n", + " \n", + " \n", + " [60/60 00:12, Epoch 3/3]\n", + "
\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
StepTraining Loss
502.103000

" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/plain": [ + "('./gpt-neo-finetuned/tokenizer_config.json',\n", + " './gpt-neo-finetuned/special_tokens_map.json',\n", + " './gpt-neo-finetuned/vocab.json',\n", + " './gpt-neo-finetuned/merges.txt',\n", + " './gpt-neo-finetuned/added_tokens.json',\n", + " './gpt-neo-finetuned/tokenizer.json')" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Wczytanie modelu\n", + "model = AutoModelForCausalLM.from_pretrained(local_model_path, local_files_only=True)\n", + "\n", + "# Collator (bez maskowania)\n", + "data_collator = DataCollatorForLanguageModeling(\n", + " tokenizer=tokenizer,\n", + " mlm=False\n", + ")\n", + "\n", + "# Argumenty treningowe\n", + "training_args = TrainingArguments(\n", + " output_dir=\"./gpt-neo-finetuned\",\n", + " overwrite_output_dir=True,\n", + " per_device_train_batch_size=1,\n", + " num_train_epochs=3,\n", + " save_steps=500,\n", + " logging_steps=50,\n", + " prediction_loss_only=True,\n", + " fp16=True\n", + ")\n", + "\n", + "# Tworzymy Trainer\n", + "trainer = Trainer(\n", + " model=model,\n", + " args=training_args,\n", + " train_dataset=tokenized_dataset, # <-- to byΕ‚a literΓ³wka, nie \"train_dataset\"\n", + " tokenizer=tokenizer,\n", + " data_collator=data_collator\n", + ")\n", + "\n", + "# Start treningu\n", + "trainer.train()\n", + "\n", + "# Zapis modelu i tokenizer\n", + "trainer.save_model(\"./gpt-neo-finetuned\")\n", + "tokenizer.save_pretrained(\"./gpt-neo-finetuned\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "id": "0d5c46d3-e115-4a68-9337-1f20b62a21a4", + "metadata": {}, + "outputs": [], + "source": [ + "model_path = \"./gpt-neo-finetuned\"\n", + "\n", + "tokenizer = AutoTokenizer.from_pretrained(model_path)\n", + "model = AutoModelForCausalLM.from_pretrained(model_path)" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "id": "e656cc27-3748-4530-9ae5-05b2f8e9e105", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "* Running on local URL: http://127.0.0.1:7863\n", + "* Running on public URL: https://596aa820b4401f3637.gradio.live\n", + "\n", + "This share link expires in 1 week. For free permanent hosting and GPU upgrades, run `gradio deploy` from the terminal in the working directory to deploy to Hugging Face Spaces (https://huggingface.co/spaces)\n" + ] + }, + { + "data": { + "text/html": [ + "

" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/plain": [] + }, + "execution_count": 20, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "import gradio as gr\n", + "\n", + "def chat(message, chat_history):\n", + " if chat_history:\n", + " prompt = chat_history + f\"\\nUser: {message}\\nAI:\"\n", + " else:\n", + " prompt = f\"User: {message}\\nAI:\"\n", + " \n", + " inputs = tokenizer(prompt, return_tensors=\"pt\")\n", + " outputs = model.generate(\n", + " **inputs,\n", + " max_length=len(inputs[\"input_ids\"][0]) + 200,\n", + " temperature=0.7,\n", + " pad_token_id=tokenizer.eos_token_id,\n", + " do_sample=True,\n", + " top_p=0.9\n", + " )\n", + " full_response = tokenizer.decode(outputs[0], skip_special_tokens=True)\n", + " response = full_response.split(\"AI:\")[-1].strip()\n", + " chat_history += f\"\\nUser: {message}\\nAI: {response}\"\n", + " return chat_history, chat_history\n", + "\n", + "# Gradio UI w trybie notebookowym\n", + "with gr.Blocks() as demo:\n", + " gr.Markdown(\"### Lokalny Czat z GPT\")\n", + " chatbot_output = gr.Textbox(label=\"Historia rozmowy\", lines=20, interactive=False)\n", + " user_input = gr.Textbox(label=\"Twoje pytanie\", placeholder=\"Zadaj pytanie i naciΕ›nij Enter\")\n", + " state = gr.State(\"\")\n", + "\n", + " user_input.submit(chat, [user_input, state], [chatbot_output, state])\n", + " user_input.submit(lambda: \"\", None, user_input) # CzyΕ›ci input po wysΕ‚aniu\n", + "\n", + "demo.launch(inline=True,share=True)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "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.12.3" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}