Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save crcrpar/71e216135e4c35ee507ec8b15425fcc7 to your computer and use it in GitHub Desktop.
Save crcrpar/71e216135e4c35ee507ec8b15425fcc7 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "entire-measurement",
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"\n",
"import torch\n",
"import torch.nn as nn"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "second-brown",
"metadata": {},
"outputs": [],
"source": [
"l = nn.Linear(10, 5)\n",
"\n",
"x = np.random.uniform(size=(2, 10)).astype(np.float32)\n",
"x_tensor = torch.from_numpy(x)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "conditional-frontier",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"tensor([[-0.3263, 0.2433, -0.0709, 0.0432, 0.1191],\n",
" [-0.6762, 0.3042, 0.2187, 0.2360, 0.0271]])\n"
]
}
],
"source": [
"with torch.no_grad():\n",
" y_tensor = l(x_tensor)\n",
"print(y_tensor)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "published-apparel",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[-0.32633856, 0.24331217, -0.07092977, 0.04322059, 0.1190879 ],\n",
" [-0.67622596, 0.3041517 , 0.21873462, 0.2360289 , 0.02713893]],\n",
" dtype=float32)"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.dot(x, l.weight.data.numpy().T) + l.bias.data.numpy()"
]
}
],
"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