Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save crcrpar/d3493b56364df1ddf413e25e0013f68e to your computer and use it in GitHub Desktop.
Save crcrpar/d3493b56364df1ddf413e25e0013f68e to your computer and use it in GitHub Desktop.
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "correct-hebrew",
"metadata": {},
"outputs": [],
"source": [
"# https://discuss.pytorch.org/t/state-dict-for-weight-norm-conv2d-doesnt-give-weight-g-and-weight-v-keys/113876\n",
"%load_ext autoreload\n",
"%autoreload 2"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "general-nashville",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1.8.0\n"
]
}
],
"source": [
"import torch\n",
"import torch.nn as nn\n",
"import torch.nn.functional as F\n",
"\n",
"print(torch.__version__)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "exceptional-chart",
"metadata": {},
"outputs": [],
"source": [
"class MyLinear(nn.Linear):\n",
" \n",
" def load_state_dict(self, *args, **kwargs):\n",
" print(\"calling load_state_dict\")\n",
" ret = super().load_state_dict(*args, **kwargs)\n",
" nn.utils.weight_norm(self)\n",
" \n",
" print(self._forward_pre_hooks)\n",
" \n",
" return _"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "cross-violence",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"calling load_state_dict\n",
"OrderedDict([(0, <torch.nn.utils.weight_norm.WeightNorm object at 0x7f330aee42d0>)])\n",
"my_linear True True True\n"
]
}
],
"source": [
"linear = nn.Linear(16, 4)\n",
"\n",
"my_linear = MyLinear(16, 4)\n",
"my_linear.load_state_dict(linear.state_dict())\n",
"\n",
"print(\"my_linear\", hasattr(my_linear, \"weight\"), hasattr(my_linear, \"weight_g\"), hasattr(my_linear, \"weight_v\"))"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.7.9"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment