{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(201, 150)\n",
      "(201, 75) (201, 75)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 234792 characters]",
      "text/plain": [
       "<Figure size 800x800 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "%matplotlib inline\n",
    "\n",
    "data = pd.read_csv(\"./result/mnist/06-25-01-20_75_200_0.35_0.8_0.7_acc.csv\", header=None)\n",
    "print(data.shape)\n",
    "\n",
    "loss = pd.DataFrame()\n",
    "acc = pd.DataFrame()\n",
    "\n",
    "for i in range(len(data.iloc[0])):\n",
    "    if i % 2 == 0:\n",
    "        loss[i] = data[i]\n",
    "        # loss.append(iris[i])\n",
    "    else:\n",
    "        acc[i] = data[i]\n",
    "        # acc.append(iris[i])\n",
    "\n",
    "print(loss.shape, acc.shape)\n",
    "# print(loss)\n",
    "\n",
    "fig, axes = plt.subplots(nrows=2, sharey=False, sharex=True, figsize=(8,8))\n",
    "\n",
    "loss.replace('nan' ,np.inf ,inplace=True)\n",
    "loss = loss.fillna(np.inf)\n",
    "\n",
    "loss.plot(kind=\"line\", ax=axes[0], legend=False, alpha=0.5, ylabel=\"loss\", grid=True, title=f\"best loss {loss.min(numeric_only=True).min()}\")\n",
    "\n",
    "acc.plot(kind=\"line\", ax=axes[1], legend=False, alpha=0.5, ylabel=\"acc\", xlabel=\"epoch\", grid=True, title=f\"best acc {acc.max(numeric_only=True).max()}\")\n",
    "\n",
    "plt.show()\n",
    "plt.clf()\n",
    "plt.clf()\n",
    "plt.close()\n",
    "plt.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "import tensorflow as tf\n",
    "\n",
    "gpus = tf.config.experimental.list_physical_devices(\"GPU\")\n",
    "if gpus:\n",
    "    try:\n",
    "        # tf.config.experimental.set_visible_devices(gpus[0], \"GPU\")\n",
    "        tf.config.experimental.set_memory_growth(gpus[0], True)\n",
    "    except RuntimeError as e:\n",
    "        print(e)\n",
    "\n",
    "from tensorflow import keras\n",
    "from tensorflow.keras.layers import (Conv2D, Dense, Dropout, Flatten,\n",
    "                                     MaxPooling2D)\n",
    "from tensorflow.keras.models import Sequential"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Model: \"sequential_26\"\n",
      "_________________________________________________________________\n",
      " Layer (type)                Output Shape              Param #   \n",
      "=================================================================\n",
      " conv2d_52 (Conv2D)          (None, 24, 24, 32)        832       \n",
      "                                                                 \n",
      " max_pooling2d_52 (MaxPoolin  (None, 8, 8, 32)         0         \n",
      " g2D)                                                            \n",
      "                                                                 \n",
      " conv2d_53 (Conv2D)          (None, 6, 6, 64)          18496     \n",
      "                                                                 \n",
      " max_pooling2d_53 (MaxPoolin  (None, 3, 3, 64)         0         \n",
      " g2D)                                                            \n",
      "                                                                 \n",
      " dropout_26 (Dropout)        (None, 3, 3, 64)          0         \n",
      "                                                                 \n",
      " flatten_26 (Flatten)        (None, 576)               0         \n",
      "                                                                 \n",
      " dense_52 (Dense)            (None, 128)               73856     \n",
      "                                                                 \n",
      " dense_53 (Dense)            (None, 10)                1290      \n",
      "                                                                 \n",
      "=================================================================\n",
      "Total params: 94,474\n",
      "Trainable params: 94,474\n",
      "Non-trainable params: 0\n",
      "_________________________________________________________________\n",
      "None\n",
      "(5, 5, 1, 32)\n",
      "-0.08480508 0.08527838\n",
      "(32,)\n",
      "0.0 0.0\n",
      "(3, 3, 32, 64)\n",
      "-0.08332829 0.08333322\n",
      "(64,)\n",
      "0.0 0.0\n",
      "(576, 128)\n",
      "-0.09231657 0.09231355\n",
      "(128,)\n",
      "0.0 0.0\n",
      "(128, 10)\n",
      "-0.20795095 0.20850043\n",
      "(10,)\n",
      "0.0 0.0\n"
     ]
    }
   ],
   "source": [
    "def make_model():\n",
    "    model = Sequential()\n",
    "    model.add(Conv2D(32, kernel_size=(5, 5), activation='relu', input_shape=(28,28,1)))\n",
    "    model.add(MaxPooling2D(pool_size=(3, 3)))\n",
    "    model.add(Conv2D(64, kernel_size=(3, 3), activation='relu'))\n",
    "    model.add(MaxPooling2D(pool_size=(2, 2)))\n",
    "    model.add(Dropout(0.25))\n",
    "    model.add(Flatten())\n",
    "    model.add(Dense(128, activation='relu'))\n",
    "    model.add(Dense(10, activation='softmax'))\n",
    "\n",
    "    # model.summary()\n",
    "\n",
    "    return model\n",
    "\n",
    "model = make_model()\n",
    "weights = model.get_weights()\n",
    "\n",
    "print(model.summary())\n",
    "\n",
    "for i in range(len(weights)):\n",
    "    print(weights[i].shape)\n",
    "    print(weights[i].min(), weights[i].max())\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2023-05-26 03:53:33.924839: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:980] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero\n",
      "2023-05-26 03:53:33.928891: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:980] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero\n",
      "2023-05-26 03:53:33.929032: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:980] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero\n",
      "2023-05-26 03:53:33.929450: I tensorflow/core/platform/cpu_feature_guard.cc:193] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations:  AVX2 FMA\n",
      "To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.\n",
      "2023-05-26 03:53:33.929902: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:980] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero\n",
      "2023-05-26 03:53:33.930018: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:980] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero\n",
      "2023-05-26 03:53:33.930117: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:980] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero\n",
      "2023-05-26 03:53:34.287172: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:980] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero\n",
      "2023-05-26 03:53:34.287322: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:980] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero\n",
      "2023-05-26 03:53:34.287430: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:980] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero\n",
      "2023-05-26 03:53:34.287524: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1616] Created device /job:localhost/replica:0/task:0/device:GPU:0 with 10109 MB memory:  -> device: 0, name: NVIDIA GeForce RTX 3060, pci bus id: 0000:09:00.0, compute capability: 8.6\n"
     ]
    }
   ],
   "source": [
    "model = make_model()\n",
    "# json_ = model.to_json()\n",
    "# print(json_)\n",
    "# for layer in model.get_weights():\n",
    "    # print(layer.shape)\n",
    "weight = model.get_weights()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 ~ 800\n",
      "(5, 5, 1, 32)\n",
      "800 ~ 832\n",
      "(32,)\n",
      "832 ~ 19264\n",
      "(3, 3, 32, 64)\n",
      "19264 ~ 19328\n",
      "(64,)\n",
      "19328 ~ 93056\n",
      "(576, 128)\n",
      "93056 ~ 93184\n",
      "(128,)\n",
      "93184 ~ 94464\n",
      "(128, 10)\n",
      "94464 ~ 94474\n",
      "(10,)\n",
      "[800, 32, 18432, 64, 73728, 128, 1280, 10]\n",
      "[(5, 5, 1, 32), (32,), (3, 3, 32, 64), (64,), (576, 128), (128,), (128, 10), (10,)]\n"
     ]
    }
   ],
   "source": [
    "from time import time\n",
    "\n",
    "import cupy as cp\n",
    "\n",
    "\n",
    "def encode(weights):\n",
    "    w_gpu = cp.array([])\n",
    "    lenght = []\n",
    "    shape = []\n",
    "    for layer in weights:\n",
    "        shape.append(layer.shape)\n",
    "        w_ = layer.reshape(-1)\n",
    "        lenght.append(len(w_))\n",
    "        w_gpu = cp.append(w_gpu, w_)\n",
    "    \n",
    "    return w_gpu, shape, lenght\n",
    "\n",
    "def decode(weight, shape, lenght):\n",
    "    weights = []\n",
    "    start = 0\n",
    "    for i in range(len(shape)):\n",
    "        end = start + lenght[i]\n",
    "        print(f\"{start} ~ {end}\")\n",
    "        print(f\"{shape[i]}\")\n",
    "        w_ = weight[start:end]\n",
    "        w_ = w_.reshape(shape[i])\n",
    "        weights.append(w_)\n",
    "        start = end\n",
    "\n",
    "    return weights\n",
    "\n",
    "w = 0.8\n",
    "v,_,_ = encode(weight)\n",
    "c0 = 0.5\n",
    "c1 = 1.5\n",
    "r0 = 0.2\n",
    "r1 = 0.8\n",
    "p_best,_,_ = encode(weight)\n",
    "g_best,_,_ = encode(weight)\n",
    "layer,shape,leng = encode(weight)\n",
    "\n",
    "# new_v = w*v[i]\n",
    "# new_v = new_v + c0*r0*(p_best[i] - layer)\n",
    "# new_v = new_v + c1*r1*(self.g_best[i] - layer)\n",
    "\n",
    "start = time()\n",
    "new_velocity = w * v + c0 * r0 * (p_best - layer) + c1 * r1 * (g_best - layer)\n",
    "\n",
    "# print(new_velocity)\n",
    "\n",
    "we2 = decode(new_velocity, shape, leng)\n",
    "# print(we2)\n",
    "\n",
    "\n",
    "# # s= [1,2]\n",
    "# print(w)\n",
    "print(leng)\n",
    "print(shape)\n",
    "\n",
    "# w2 = w\n",
    "# c1 = c\n",
    "\n",
    "# tf_start = time()\n",
    "# w3 = tf.multiply(w2, w)\n",
    "# tf_end = time()\n",
    "# mul_start = time()\n",
    "# w4 = w2 * w\n",
    "# mul_end = time()\n",
    "# cuda_start = time()\n",
    "# w5 = c1 * c\n",
    "# cuda_end = time()\n",
    "\n",
    "# print(f\"tf 연산 > {tf_end-tf_start} | {w3}\")\n",
    "# print(f\"곱셈 연산 > {mul_end-mul_start} | {w4}\")\n",
    "# print(f\"cuda 연산 > {cuda_end-cuda_start} | {w5}\")\n",
    "\n",
    "# for i in range(len(w)):\n",
    "#     if w[i] != w2[i]:\n",
    "#         print(\"not same\")\n",
    "#         break\n",
    "#     else:\n",
    "#         print(\"same\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1/1 [==============================] - 0s 452ms/step\n",
      "[[0.0000000e+00 1.0000000e+00 8.5117706e-28]\n",
      " [0.0000000e+00 1.0000000e+00 0.0000000e+00]\n",
      " [1.0000000e+00 3.3700031e-35 0.0000000e+00]\n",
      " [1.0000000e+00 1.3158974e-19 0.0000000e+00]\n",
      " [0.0000000e+00 1.0000000e+00 0.0000000e+00]\n",
      " [0.0000000e+00 0.0000000e+00 1.0000000e+00]\n",
      " [1.0000000e+00 1.4602315e-27 0.0000000e+00]\n",
      " [0.0000000e+00 0.0000000e+00 1.0000000e+00]\n",
      " [1.0000000e+00 2.4845295e-16 0.0000000e+00]\n",
      " [0.0000000e+00 1.0000000e+00 1.6942224e-33]\n",
      " [1.0000000e+00 0.0000000e+00 0.0000000e+00]\n",
      " [0.0000000e+00 1.0000000e+00 0.0000000e+00]\n",
      " [1.0000000e+00 9.0455008e-36 0.0000000e+00]\n",
      " [1.0000000e+00 0.0000000e+00 0.0000000e+00]\n",
      " [0.0000000e+00 1.8117375e-33 1.0000000e+00]\n",
      " [0.0000000e+00 1.0000000e+00 6.7984806e-36]\n",
      " [0.0000000e+00 1.7472901e-25 1.0000000e+00]\n",
      " [0.0000000e+00 6.2991115e-37 1.0000000e+00]\n",
      " [0.0000000e+00 0.0000000e+00 1.0000000e+00]\n",
      " [0.0000000e+00 1.0598510e-30 1.0000000e+00]\n",
      " [1.0000000e+00 1.7519910e-30 0.0000000e+00]\n",
      " [0.0000000e+00 1.0000000e+00 0.0000000e+00]\n",
      " [0.0000000e+00 1.0000000e+00 0.0000000e+00]\n",
      " [1.0000000e+00 7.4562871e-27 0.0000000e+00]\n",
      " [0.0000000e+00 0.0000000e+00 1.0000000e+00]\n",
      " [0.0000000e+00 0.0000000e+00 1.0000000e+00]\n",
      " [0.0000000e+00 0.0000000e+00 1.0000000e+00]\n",
      " [0.0000000e+00 1.0000000e+00 0.0000000e+00]\n",
      " [0.0000000e+00 1.0000000e+00 0.0000000e+00]\n",
      " [1.0000000e+00 0.0000000e+00 0.0000000e+00]]\n",
      "[[0. 1. 0.]\n",
      " [0. 1. 0.]\n",
      " [1. 0. 0.]\n",
      " [1. 0. 0.]\n",
      " [0. 1. 0.]\n",
      " [0. 0. 1.]\n",
      " [1. 0. 0.]\n",
      " [0. 0. 1.]\n",
      " [1. 0. 0.]\n",
      " [0. 1. 0.]\n",
      " [1. 0. 0.]\n",
      " [0. 1. 0.]\n",
      " [1. 0. 0.]\n",
      " [1. 0. 0.]\n",
      " [0. 0. 1.]\n",
      " [0. 1. 0.]\n",
      " [0. 0. 1.]\n",
      " [0. 0. 1.]\n",
      " [0. 0. 1.]\n",
      " [0. 0. 1.]\n",
      " [1. 0. 0.]\n",
      " [0. 1. 0.]\n",
      " [0. 1. 0.]\n",
      " [1. 0. 0.]\n",
      " [0. 0. 1.]\n",
      " [0. 0. 1.]\n",
      " [0. 0. 1.]\n",
      " [0. 1. 0.]\n",
      " [0. 1. 0.]\n",
      " [1. 0. 0.]]\n",
      "1/1 [==============================] - 0s 88ms/step - loss: 0.0000e+00 - accuracy: 1.0000\n",
      "[0.0, 1.0]\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2023-06-02 14:34:49.851147: I tensorflow/stream_executor/cuda/cuda_blas.cc:1614] TensorFloat-32 will be used for the matrix multiplication. This will only be logged once.\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import tensorflow as tf\n",
    "from sklearn.datasets import load_iris\n",
    "from sklearn.model_selection import train_test_split\n",
    "from tensorflow import keras\n",
    "from tensorflow.keras import layers\n",
    "from tensorflow.keras.models import Sequential\n",
    "\n",
    "\n",
    "def get_xor():\n",
    "    x = np.array([[0,0],[0,1],[1,0],[1,1]])\n",
    "    y = np.array([[0],[1],[1],[0]])\n",
    "\n",
    "    return x,y\n",
    "\n",
    "def get_iris():\n",
    "    iris = load_iris()\n",
    "    x = iris.data\n",
    "    y = iris.target\n",
    "\n",
    "    y = keras.utils.to_categorical(y, 3)\n",
    "\n",
    "    x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, shuffle=True, stratify=y)\n",
    "\n",
    "    return x_train, x_test, y_train, y_test\n",
    "\n",
    "# model = keras.models.load_model(\"./result/xor/06-02-13-31/75_0.35_0.8_0.6.h5\")\n",
    "model = keras.models.load_model(\"./result/iris/06-02-13-48/50_0.4_0.8_0.7.h5\")\n",
    "# x,y = get_xor()\n",
    "x_train, x_test, y_train, y_test = get_iris()\n",
    "\n",
    "print(model.predict(x_test))\n",
    "print(y_test)\n",
    "print(model.evaluate(x_test,y_test))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2023-06-11 14:01:31.378413: I tensorflow/core/platform/cpu_feature_guard.cc:182] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.\n",
      "To enable the following instructions: AVX2 FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "TensorFlow version: 2.12.0\n",
      "Linked TRT ver: (8, 4, 3)\n"
     ]
    }
   ],
   "source": [
    "import tensorflow as tf\n",
    "\n",
    "print(\"TensorFlow version:\", tf.__version__)\n",
    "import tensorflow.compiler as tf_cc\n",
    "import tensorrt as trt\n",
    "\n",
    "linked_trt_ver=tf_cc.tf2tensorrt._pywrap_py_utils.get_linked_tensorrt_version()\n",
    "print(f\"Linked TRT ver: {linked_trt_ver}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "pso",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.16"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
