aboutsummaryrefslogtreecommitdiff
path: root/old_notebooks/TensorflowToPyTorch.ipynb
blob: 76d588c9c3d0f619c1432f9a8a1ebaff1752c4a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "0dfb6ada-ed98-48c9-8610-cadb7493e138",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sunlab.environment.base.cpu import *\n",
    "from sunlab.environment.base.extras import *\n",
    "from sunlab.globals import FILES\n",
    "from sunlab.sunflow import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "4136a260-bb40-47ec-8aad-d2a6ac31f1f2",
   "metadata": {},
   "outputs": [],
   "source": [
    "model, dataset = load_aae_and_dataset(FILES['TRAINING_DATASET'], FILES['PRETRAINED_MODEL_DIR'], MaxAbsScaler)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "00694ce2",
   "metadata": {},
   "source": [
    "# Save for PyTorch!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "23ae3a97",
   "metadata": {},
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "Already Saved the model Weights!",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[22], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mAlready Saved the model Weights!\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m      2\u001b[0m names_tup \u001b[38;5;241m=\u001b[39m [(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mLAYER_\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mi\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m_WEIGHTS\u001b[39m\u001b[38;5;124m\"\u001b[39m,\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mLAYER_\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mi\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m_BIAS\u001b[39m\u001b[38;5;124m\"\u001b[39m,) \u001b[38;5;28;01mfor\u001b[39;00m i \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mrange\u001b[39m(\u001b[38;5;241m1\u001b[39m,\u001b[38;5;241m3\u001b[39m\u001b[38;5;241m+\u001b[39m\u001b[38;5;241m1\u001b[39m)]\n\u001b[1;32m      3\u001b[0m names \u001b[38;5;241m=\u001b[39m []\n",
      "\u001b[0;31mValueError\u001b[0m: Already Saved the model Weights!"
     ]
    }
   ],
   "source": [
    "raise ValueError(\"Already Saved the model Weights!\")\n",
    "names_tup = [(f\"LAYER_{i}_WEIGHTS\",f\"LAYER_{i}_BIAS\",) for i in range(1,3+1)]\n",
    "names = []\n",
    "for name_tup in names_tup:\n",
    "    names.extend(name_tup)\n",
    "ENCODER_DICT = {}\n",
    "for idx, name in enumerate(names):\n",
    "    trainable_variable = model.encoder.model.trainable_variables[idx].numpy()\n",
    "    ENCODER_DICT[name] = trainable_variable\n",
    "\n",
    "names_tup = [(f\"LAYER_{i}_WEIGHTS\",f\"LAYER_{i}_BIAS\",) for i in range(1,3+1)]\n",
    "names = []\n",
    "for name_tup in names_tup:\n",
    "    names.extend(name_tup)\n",
    "DECODER_DICT = {}\n",
    "for idx, name in enumerate(names):\n",
    "    trainable_variable = model.decoder.model.trainable_variables[idx].numpy()\n",
    "    DECODER_DICT[name] = trainable_variable\n",
    "\n",
    "names_tup = [(f\"LAYER_{i}_WEIGHTS\",f\"LAYER_{i}_BIAS\",) for i in range(1,3+1)]\n",
    "names = []\n",
    "for name_tup in names_tup:\n",
    "    names.extend(name_tup)\n",
    "DISCRIMINATOR_DICT = {}\n",
    "for idx, name in enumerate(names):\n",
    "    trainable_variable = model.discriminator.model.trainable_variables[idx].numpy()\n",
    "    DISCRIMINATOR_DICT[name] = trainable_variable\n",
    "\n",
    "AAE_DICT = {\n",
    "    \"ENCODER\": ENCODER_DICT,\n",
    "    \"DECODER\": DECODER_DICT,\n",
    "    \"DISCRIMINATOR\": DISCRIMINATOR_DICT,\n",
    "}\n",
    "\n",
    "np.save(DIR_ROOT + \"models/current_model/portable/trainable_variables.npy\", AAE_DICT)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3332ec53",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "tfnb",
   "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.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}