机器学习 - 线性问题

news/2024/10/25 18:22:59/

矩阵做 transpose

import torch tensor_Matrix_A = torch.tensor([[1,2],[4,5],[7,8]
], dtype=torch.float32)
print(tensor_Matrix_A.T)# 结果
tensor([[1., 4., 7.],[2., 5., 8.]])

torch.nn.Linear() 模块也被称为 “feed-forward layer"或者"fully connected layer”。公式如下:
y = x ⋅ A T + b y = x \cdot A^T + b y=xAT+b

解释上面的公式:

  • X: It is the input to the layer (deep learning is a stack of layers like torch.nn.Linear() and others on top of each other).
  • A: It is the weights matrix created by the layer, this starts out as random numbers that get adjusted as a neural network learns to better represent patterns in the data (notice the “T”, that’s because the weights matrix gets transposed).
  • b: It is the bias term used to slightly offset the weights and inputs
  • y: It is the output (a manipulation of the input in the hopes to discover patterns in it).

使用代码来举例

import torchtensor_Matrix_A = torch.tensor([[1,2],[4,5],[7,8]
], dtype=torch.float32)torch.manual_seed(42)
linear = torch.nn.Linear(in_features=2,out_features=6)
print(linear)
x = tensor_Matrix_B
output = linear(x)
print(f"linear weight: {linear.weight}")  # 得将获得结果进行 transposed, 再做 linear 预算
print(f"Input shape: {x.shape}\n")
print(f"Out:\n{output}\n\nOutput shape: {output.shape}")# 结果如下:
Linear(in_features=2, out_features=6, bias=True)
linear weight: Parameter containing:
tensor([[ 0.5406,  0.5869],[-0.1657,  0.6496],[-0.1549,  0.1427],[-0.3443,  0.4153],[ 0.6233, -0.5188],[ 0.6146,  0.1323]], requires_grad=True)
Input shape: torch.Size([3, 2])Out:
tensor([[ 1.1093,  0.7453,  0.4836,  0.3154,  0.0263,  0.2369],[ 3.9513,  2.3627,  0.6018,  0.8727, -0.2832,  1.8631],[ 5.6657,  3.4961,  0.7323,  1.3590, -0.6974,  2.7424]],grad_fn=<AddmmBackward0>)Output shape: torch.Size([3, 6])

看到这里,点个赞呗~


http://www.ppmy.cn/news/1389206.html

相关文章

谷歌发布Bard AI以与ChatGPT/GPT-4竞争

Google发布Bard AI&#xff0c;与ChatGPT/GPT-4竞争 概述 谷歌近日推出了一款名为Bard的创新型AI聊天机器人&#xff0c;旨在与OpenAI的ChatGPT和微软的Bing Chat竞争。与同类产品不同&#xff0c;Bard能够直接从其模型中生成信息&#xff0c;而不是检索搜索结果。Bard被视为…

stm32之GPIO电路介绍

文章目录 1 GPIO介绍2 GPIO的工作模式2.1 浮空输入2.2 上拉输入2.3 下拉输入2.4 模拟输入2.5 开漏输出2.6 推挽输出2.7 复用开漏输出2.8 复用推挽输出2.9 其他 3 应用方式4 常用库函数 1 GPIO介绍 保护二极管&#xff1a;保护引脚&#xff0c;让引脚的电压位于正常的范围施密特…

【蓝桥杯】蓝桥杯嵌入式——题目总结及文章汇总(内含客观题与主观题,并且都附有详细题解)

介绍 蓝桥杯嵌入式比赛是一项专注于嵌入式开发的全国性比赛&#xff0c;旨在鼓励和促进嵌入式系统的研究和应用&#xff0c;提高嵌入式开发的水平和技能。 比赛分为初赛和复赛两个阶段。初赛难度适中&#xff0c;注重考查参赛选手的嵌入式系统开发能力和实践经验。复赛则采用线…

反射 Reflection

反射 反射的概念 反射机制允许程序在执行期借助于ReflectionAPI取得任何类的内部信息(比如成员变量&#xff0c;构造器&#xff0c;成员方法等等)&#xff0c;并能操作对象的属性及方法。反射在设计模式和框架底层都会用到加载完类之后&#xff0c;在堆中就产生了一个Class类型…

java数据结构与算法刷题-----LeetCode135. 分发糖果

java数据结构与算法刷题目录&#xff08;剑指Offer、LeetCode、ACM&#xff09;-----主目录-----持续更新(进不去说明我没写完)&#xff1a;https://blog.csdn.net/grd_java/article/details/123063846 文章目录 1. 左右遍历2. 进阶&#xff1a;常数空间遍历&#xff0c;升序降…

业务服务:redisson

文章目录 前言一、配置1. 添加依赖2. 配置文件/类3. 注入redission3. 封装工具类 二、应用1. RedisUtils工具类的基本使用 三、队列1. 工具类2. 普通队列3. 有界队列&#xff08;限制数据量&#xff09;4. 延迟队列&#xff08;延迟获取数据&#xff09;5. 优先队列&#xff08…

文章解读与仿真程序复现思路——电网技术EI\CSCD\北大核心《含光储充的配网虚拟电厂二次调频随机模型预测控制策略 》

本专栏栏目提供文章与程序复现思路&#xff0c;具体已有的论文与论文源程序可翻阅本博主免费的专栏栏目《论文与完整程序》 论文与完整源程序_电网论文源程序的博客-CSDN博客https://blog.csdn.net/liang674027206/category_12531414.html 电网论文源程序-CSDN博客电网论文源…

记录C++中,子类同名属性并不能完全覆盖父类属性的问题

问题代码&#xff1a; 首先看一段代码&#xff1a;很简单&#xff0c;就是BBB继承自AAA&#xff0c;然后BBB重写定义了同名属性&#xff0c;然后调用父类AAA的打印函数&#xff1a; #include <iostream> using namespace std;class AAA { public:AAA() {}~AAA() {}void …