Udemy-PyTorch/torch - test.ipynb
2023-01-30 00:30:21 +08:00

160 lines
3.4 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"id": "4a54ab90",
"metadata": {},
"outputs": [],
"source": [
"import torch"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "6533ad3a",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"torch.cuda.is_available()"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "d44a0818",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"tensor([[6.7262e-44, 0.0000e+00, 6.8664e-44],\n",
" [0.0000e+00, 3.4130e+20, 3.0694e-41],\n",
" [3.8088e+20, 3.0694e-41, 1.4013e-45],\n",
" [0.0000e+00, 0.0000e+00, 0.0000e+00],\n",
" [1.3452e-43, 0.0000e+00, 1.3452e-43]])"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x = torch.empty(5, 3)\n",
"x"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "637d9ebe",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"tensor([[0., 0., 0., 0., 0.],\n",
" [0., 0., 0., 0., 0.],\n",
" [0., 0., 0., 0., 0.]])"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x = torch.zeros((3, 5), dtype=torch.float)\n",
"x"
]
},
{
"cell_type": "code",
"execution_count": 28,
"id": "dd60207e",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"tensor([[0.1210, 0.4432, 0.5018, 0.2111, 0.1256],\n",
" [0.6894, 0.3928, 0.0216, 0.1406, 0.5416],\n",
" [0.0044, 0.4856, 0.2907, 0.9889, 0.2060]])\n",
"tensor([[0.7810, 0.4015, 0.9711, 0.1654, 0.3593],\n",
" [0.5972, 0.1060, 0.6192, 0.6601, 0.9575],\n",
" [0.3084, 0.4609, 0.6921, 0.3311, 0.6630]])\n",
"tensor([[0.9020, 0.8447, 1.4729, 0.3764, 0.4850],\n",
" [1.2866, 0.4987, 0.6409, 0.8006, 1.4991],\n",
" [0.3128, 0.9465, 0.9828, 1.3200, 0.8690]])\n"
]
}
],
"source": [
"x.shape\n",
"x = torch.rand_like(x, dtype=torch.float)\n",
"y = torch.rand_like(x, dtype=torch.float)\n",
"print(x)\n",
"print(y)\n",
"z = x+y\n",
"print(z)"
]
},
{
"cell_type": "code",
"execution_count": 35,
"id": "0d2ed6cd",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"tensor([[0.2814, 0.6475, 0.3659, 0.4057],\n",
" [0.9773, 0.5319, 0.7534, 0.8681],\n",
" [0.4856, 0.3504, 0.0362, 0.6139]], requires_grad=True)\n"
]
}
],
"source": [
"x = torch.rand((3, 4), requires_grad=True)\n",
"print(x)"
]
}
],
"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.10.8"
}
},
"nbformat": 4,
"nbformat_minor": 5
}