{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "a93bbf66", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Parameter containing:\n", "tensor([[-0.1953, 0.0349, 0.2066, 0.3050, 0.0570, -0.1622, -0.1848, -0.1546,\n", " -0.1174, 0.1902],\n", " [-0.2510, 0.1219, -0.1402, -0.0613, -0.0890, 0.0510, -0.0082, -0.2142,\n", " 0.1112, 0.0488],\n", " [-0.1486, -0.2754, -0.0817, 0.2407, -0.2888, 0.3057, 0.1441, 0.1292,\n", " 0.0201, -0.1883],\n", " [-0.1317, -0.2687, -0.2205, 0.1629, 0.0945, -0.0216, -0.0291, -0.2245,\n", " 0.2112, 0.2221],\n", " [-0.1276, 0.2041, 0.2358, -0.0377, -0.2722, -0.0177, -0.0213, -0.0972,\n", " -0.0971, 0.0606]], requires_grad=True)\n", "Parameter containing:\n", "tensor([ 0.2709, 0.0198, -0.1025, -0.0345, 0.1773], requires_grad=True)\n" ] } ], "source": [ "import torch.nn as nn\n", "class MyModel(nn.Module):\n", " def __init__(self):\n", " super().__init__()\n", " self.fc = nn.Linear(10, 5)\n", "\n", " def forward(self, x):\n", " return self.fc(x)\n", "\n", "# 实例化模型时,fc 就已经被初始化了\n", "model = MyModel()\n", "print(model.fc.weight)\n", "print(model.fc.bias)" ] }, { "cell_type": "code", "execution_count": 2, "id": "b11a6f0e", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Parameter containing:\n", "tensor([[ 0.4859, 0.4045, -0.3092, 0.0537, -0.3359, -0.1761, 0.4106, -0.6187,\n", " -0.2196, -0.0027],\n", " [ 0.3794, 0.5629, -0.4486, -0.1120, -0.3996, 0.0114, -0.4191, -0.1235,\n", " 0.2704, -0.4082],\n", " [-0.6163, 0.3071, -0.2106, -0.4641, -0.2065, -0.5145, 0.0373, -0.2939,\n", " 0.5346, 0.2736],\n", " [ 0.5658, -0.4418, -0.3231, 0.2414, -0.1687, -0.1383, 0.0326, 0.1102,\n", " 0.4646, -0.4835],\n", " [-0.3686, 0.5512, -0.0530, 0.4921, -0.5184, -0.4362, 0.1822, 0.4276,\n", " -0.4707, -0.2698]], requires_grad=True)\n", "Parameter containing:\n", "tensor([0., 0., 0., 0., 0.], requires_grad=True)\n" ] } ], "source": [ "import torch.nn as nn\n", "\n", "# 创建一个线性层\n", "layer = nn.Linear(10, 5)\n", "\n", "# 最简单的用法:直接用 init 覆盖默认初始化\n", "nn.init.xavier_uniform_(layer.weight) # 均匀分布初始化权重\n", "nn.init.zeros_(layer.bias) # 偏置设为0\n", "\n", "print(layer.weight)\n", "print(layer.bias)\n" ] }, { "cell_type": "code", "execution_count": 3, "id": "74cdfef7", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "False\n", "True\n" ] } ], "source": [ "import torch\n", "\n", "# 1. 普通张量:默认不记录梯度\n", "x = torch.tensor([2.0, 3.0])\n", "print(x.requires_grad) # False\n", "\n", "#PyTorch最核心的优势就是Autograd自动微分引擎,不用手动求导,代码一行开启自动计算梯度。\n", "# 2. 开启梯度记录(模型参数专用)只有设置requires_grad=True的张量,才会记录运算流程、支持求梯度。\n", "w = torch.tensor([1.5], requires_grad=True)\n", "print(w.requires_grad) # True\n" ] }, { "cell_type": "code", "execution_count": 4, "id": "b0cd775e", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "w的梯度: tensor([-18.])\n" ] } ], "source": [ "import torch\n", "# 权重参数,开启梯度\n", "w = torch.tensor([2.0], requires_grad=True)\n", "# 正向运算\n", "y = w * 3\n", "# 模拟损失函数\n", "loss = (y - 9) ** 2\n", "# 反向传播,自动求导\n", "loss.backward()\n", "# 打印w的梯度\n", "print(\"w的梯度:\", w.grad)\n" ] }, { "cell_type": "code", "execution_count": 5, "id": "7ac1ab87", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[0.27272727 0.40677966]\n", " [1. 0. ]\n", " [0. 1. ]]\n" ] } ], "source": [ "import numpy as np\n", "\n", "# 假设这是\"视频时长\"和\"粉丝数\"两个特征,数量级差异巨大\n", "data = np.array([[60, 500000], # 60秒,50万粉丝\n", " [180, 20000], # 3分钟,2万粉丝\n", " [15, 1200000]]) # 15秒,120万粉丝\n", "\n", "# Min-Max 归一化:每个特征独立压缩到 [0, 1]\n", "data_norm = (data - data.min(axis=0)) / (data.max(axis=0) - data.min(axis=0))\n", "print(data_norm)\n", "# [[0.273, 0.407],\n", "# [1.000, 0.000],\n", "# [0.000, 1.000]]\n", "# 现在两个特征在同一个尺度上了\n" ] }, { "cell_type": "code", "execution_count": null, "id": "5c538338", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "tensor([[1, 0, 0, 0],\n", " [0, 1, 0, 0],\n", " [0, 0, 1, 0],\n", " [0, 0, 0, 1]])\n" ] } ], "source": [ "import torch.nn.functional as F\n", "import torch\n", "# 4个类别 \n", "labels = torch.tensor([0, 1,2,3])\n", "one_hot = F.one_hot(labels, num_classes=4)\n", "\n", "print(one_hot)\n", "# tensor([[1, 0, 0, 0],\n", "# [0, 1, 0, 0],\n", "# [0, 0, 1, 0],\n", "# [0, 0, 0, 1]])\n" ] } ], "metadata": { "kernelspec": { "display_name": "shuhe_xwdev (3.11.14)", "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.11.14" } }, "nbformat": 4, "nbformat_minor": 5 }