深度学习----------------------注意力机制

news2024/10/6 14:49:10

目录

  • 心理学
    • 不随意线索
    • 随意线索
  • 注意力机制
  • 非参注意力池化层
  • Nadaraya-Watson核回归
    • 参数化的注意力机制
  • 总结
  • 注意力汇聚:Nadaraya-Watson核回归代码
    • 生成数据集
    • 核回归
    • 非参数注意力汇聚
    • 注意力权重
      • 该部分总代码
    • 带参数的注意力汇聚
      • 将训练数据集转换为键和值
      • 训练带参数的注意力汇聚模型
      • 预测结果绘制
        • 该部分总代码

心理学

动物需要在复杂环境下有效关注值得注意的点。
心理学框架:人类根据随意线索和不随意线索选择注意点

不随意线索

在这里插入图片描述

随意线索

在这里插入图片描述




注意力机制

卷积全连接池化层都只考虑不随意线索
注意力机制则显示的考虑随意线索
    随意线索被称为查询
    每个输入一个值不随意线索的对
    通过注意力池化层来有偏向性的选择某些输入。




非参注意力池化层

给定数据( x i x_i xi, y i y_i yi),i=1,…,n。其中x就是key、y就是value。

最简单的池化是平均池化
这里的x是query
在这里插入图片描述




更好的方案Nadaraya-Watson核回归

在这里插入图片描述




Nadaraya-Watson核回归

在这里插入图片描述

参数化的注意力机制

在之前基础上引入可以学习的w

在这里插入图片描述




总结

心理学认为人通过随意线索不随意线索选择注意点

注意力机制中,通过query(随意线索)和key(不随意线索)来有偏向性的选择输入

在这里插入图片描述




注意力汇聚:Nadaraya-Watson核回归代码

import torch
from torch import nn
from d2l import torch as d2l



生成数据集

import torch

# 训练集样本数量
n_train = 50
# 生成训练集特征x_train,范围为[0, 5),并进行排序
x_train, _ = torch.sort(torch.rand(n_train) * 5)


# 定义函数f,用于生成标签y(真实的函数)
def f(x):
    return 2 * torch.sin(x) + x ** 0.8


# 生成训练集标签y_train,并加上服从正态分布的噪声
y_train = f(x_train) + torch.normal(0.0, 0.5, (n_train,))
# 生成测试集特征x_test,范围为[0, 5),步长为0.1
x_test = torch.arange(0, 5, 0.1)
# 生成测试集的真实标签y_truth
y_truth = f(x_test)
# 计算测试集样本数量
n_test = len(x_test)
print(n_test)

在这里插入图片描述


核回归

在这里插入图片描述

import torch
from torch import nn
from d2l import torch as d2l


# 定义函数f,用于生成标签y(真实的函数)
def f(x):
    return 2 * torch.sin(x) + x ** 0.8


# 绘制核回归结果的图像
def plot_kernel_reg(y_hat):
    # 绘制x_test和对应的真实标签y_truth以及预测标签y_hat的图像
    d2l.plot(x_test, [y_truth, y_hat], 'x', 'y', legend=['Truth', 'Pred'],
             xlim=[0, 5], ylim=[-1, 5])
    # 绘制训练集的散点图,用圆圈表示
    d2l.plt.plot(x_train, y_train, 'o', alpha=0.5)


# 训练集样本数量
n_train = 50
# 生成训练集特征x_train,范围为[0, 5),并进行排序
x_train, _ = torch.sort(torch.rand(n_train) * 5)
# 生成训练集标签y_train,并加上服从正态分布的噪声
y_train = f(x_train) + torch.normal(0.0, 0.5, (n_train,))
# 生成测试集特征x_test,范围为[0, 5),步长为0.1
x_test = torch.arange(0, 5, 0.1)
# 生成测试集的真实标签y_truth
y_truth = f(x_test)
# 计算测试集样本数量
n_test = len(x_test)
# 将y_train的均值重复n_test次作为预测标签y_hat
# 最简单的池化----平均池化,公式:f(x)=1/n * sum(y_i)
y_hat = torch.repeat_interleave(y_train.mean(), n_test)
# 调用plot_kernel_reg函数,绘制核回归结果的图像
plot_kernel_reg(y_hat)
d2l.plt.show()


在这里插入图片描述




非参数注意力汇聚

在这里插入图片描述

import torch
from torch import nn
from d2l import torch as d2l


# 定义函数f,用于生成标签y(真实的函数)
def f(x):
    return 2 * torch.sin(x) + x ** 0.8


# 绘制核回归结果的图像
def plot_kernel_reg(y_hat):
    # 绘制x_test和对应的真实标签y_truth以及预测标签y_hat的图像
    d2l.plot(x_test, [y_truth, y_hat], 'x', 'y', legend=['Truth', 'Pred'],
             xlim=[0, 5], ylim=[-1, 5])
    # 绘制训练集的散点图,用圆圈表示
    d2l.plt.plot(x_train, y_train, 'o', alpha=0.5)


# 训练集样本数量
n_train = 50
# 生成训练集特征x_train,范围为[0, 5),并进行排序
x_train, _ = torch.sort(torch.rand(n_train) * 5)
# 生成训练集标签y_train,并加上服从正态分布的噪声
y_train = f(x_train) + torch.normal(0.0, 0.5, (n_train,))
# 生成测试集特征x_test,范围为[0, 5),步长为0.1
x_test = torch.arange(0, 5, 0.1)
# 生成测试集的真实标签y_truth
y_truth = f(x_test)

# 将测试集特征x_test重复n_train次并重新reshape为二维矩阵
# X_repeat的形状为torch.Size([50, 50]),
# x_test为
# tensor([0.0000, 0.1000, 0.2000, 0.3000, 0.4000, 0.5000, 0.6000, 0.7000, 0.8000,
#         0.9000, 1.0000, 1.1000, 1.2000, 1.3000, 1.4000, 1.5000, 1.6000, 1.7000,
#         1.8000, 1.9000, 2.0000, 2.1000, 2.2000, 2.3000, 2.4000, 2.5000, 2.6000,
#         2.7000, 2.8000, 2.9000, 3.0000, 3.1000, 3.2000, 3.3000, 3.4000, 3.5000,
#         3.6000, 3.7000, 3.8000, 3.9000, 4.0000, 4.1000, 4.2000, 4.3000, 4.4000,
#         4.5000, 4.6000, 4.7000, 4.8000, 4.9000])
# X_repeat为在列上重复五十次
# tensor([[0.0000, 0.0000, 0.0000,  ..., 0.0000, 0.0000, 0.0000],
#         [0.1000, 0.1000, 0.1000,  ..., 0.1000, 0.1000, 0.1000],
#         [0.2000, 0.2000, 0.2000,  ..., 0.2000, 0.2000, 0.2000],
#         ...,
#         [4.7000, 4.7000, 4.7000,  ..., 4.7000, 4.7000, 4.7000],
#         [4.8000, 4.8000, 4.8000,  ..., 4.8000, 4.8000, 4.8000],
#         [4.9000, 4.9000, 4.9000,  ..., 4.9000, 4.9000, 4.9000]])
X_repeat = x_test.repeat_interleave(n_train).reshape((-1, n_train))
# 计算注意力权重,通过对特征差值的平方取负并除以2,再进行softmax归一化
# dim=0是按行求和(实际上是按照行方向对每一列求和)
# dim=1按列求和(实际上是按照列的方向对每一行求和)每一行的和为1
attention_weights = nn.functional.softmax(-(X_repeat - x_train)**2 / 2, dim=1)
# 注意力权重与训练集标签y_train进行矩阵乘法得到预测标签y_hat
y_hat = torch.matmul(attention_weights, y_train)
# 调用plot_kernel_reg函数,绘制非参数注意力汇聚的核回归结果图像
plot_kernel_reg(y_hat)
d2l.plt.show()

在这里插入图片描述

非参的好处是不需要学习参数,然后有理论证明只要给你足够多的数据是能够把原始的模型弄出来的。




注意力权重

# 可视化注意力权重
d2l.show_heatmaps(attention_weights.unsqueeze(0).unsqueeze(0),
                xlabel='Sorted training inputs', ylabel='Sorted test inputs')



该部分总代码

import torch
from torch import nn
from d2l import torch as d2l


# 定义函数f,用于生成标签y(真实的函数)
def f(x):
    return 2 * torch.sin(x) + x ** 0.8

# 训练集样本数量
n_train = 50
# 生成训练集特征x_train,范围为[0, 5),并进行排序
x_train, _ = torch.sort(torch.rand(n_train) * 5)
# 生成训练集标签y_train,并加上服从正态分布的噪声
y_train = f(x_train) + torch.normal(0.0, 0.5, (n_train,))
# 生成测试集特征x_test,范围为[0, 5),步长为0.1
x_test = torch.arange(0, 5, 0.1)
# 生成测试集的真实标签y_truth
y_truth = f(x_test)

# 将测试集特征x_test重复n_train次并重新reshape为二维矩阵
# X_repeat的形状为torch.Size([50, 50]),
# x_test为
# tensor([0.0000, 0.1000, 0.2000, 0.3000, 0.4000, 0.5000, 0.6000, 0.7000, 0.8000,
#         0.9000, 1.0000, 1.1000, 1.2000, 1.3000, 1.4000, 1.5000, 1.6000, 1.7000,
#         1.8000, 1.9000, 2.0000, 2.1000, 2.2000, 2.3000, 2.4000, 2.5000, 2.6000,
#         2.7000, 2.8000, 2.9000, 3.0000, 3.1000, 3.2000, 3.3000, 3.4000, 3.5000,
#         3.6000, 3.7000, 3.8000, 3.9000, 4.0000, 4.1000, 4.2000, 4.3000, 4.4000,
#         4.5000, 4.6000, 4.7000, 4.8000, 4.9000])
# X_repeat为在列上重复五十次
# tensor([[0.0000, 0.0000, 0.0000,  ..., 0.0000, 0.0000, 0.0000],
#         [0.1000, 0.1000, 0.1000,  ..., 0.1000, 0.1000, 0.1000],
#         [0.2000, 0.2000, 0.2000,  ..., 0.2000, 0.2000, 0.2000],
#         ...,
#         [4.7000, 4.7000, 4.7000,  ..., 4.7000, 4.7000, 4.7000],
#         [4.8000, 4.8000, 4.8000,  ..., 4.8000, 4.8000, 4.8000],
#         [4.9000, 4.9000, 4.9000,  ..., 4.9000, 4.9000, 4.9000]])
X_repeat = x_test.repeat_interleave(n_train).reshape((-1, n_train))
# 计算注意力权重,通过对特征差值的平方取负并除以2,再进行softmax归一化
# dim=0是按行求和(实际上是按照行方向对每一列求和)
# dim=1按列求和(实际上是按照列的方向对每一行求和)每一行的和为1
attention_weights = nn.functional.softmax(-(X_repeat - x_train)**2 / 2, dim=1)
# 可视化注意力权重
d2l.show_heatmaps(attention_weights.unsqueeze(0).unsqueeze(0),
                xlabel='Sorted training inputs', ylabel='Sorted test inputs')
d2l.plt.show()

在这里插入图片描述




带参数注意力汇聚 假定两个张量的形状分别是(n,a,b)和(n,b,c),他们的批量矩阵乘法输出的形状为(n,a,c)

import torch

X = torch.ones((2, 1, 4))
Y = torch.ones((2, 4, 6))
print(torch.bmm(X, Y).shape)

在这里插入图片描述


使用小批量矩阵乘法来计算小批量数据中的加权平均值

import torch

# weights 的形状为[2,10]
weights = torch.ones((2, 10)) * 0.1
values = torch.arange(20.0).reshape((2, 10))
# 执行小批量矩阵乘法,计算加权平均值
# weights.unsqueeze(1)的形状为[2,1,10],在第一维度上加了一个维度
# values.unsqueeze(-1)的形状为[2,10,1],在最后一维度上加了一个维度
torch.bmm(weights.unsqueeze(1), values.unsqueeze(-1))



带参数的注意力汇聚

在这里插入图片描述

# 带参数的注意力汇聚
class NWKernelRegression(nn.Module):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        # 创建形状为(1,)的参数张量w,用于调整注意力权重
        self.w = nn.Parameter(torch.rand((1,), requires_grad=True))

    def forward(self, queries, keys, values):
        # 重复queries并调整形状,使其与keys具有相同的列数
        queries = queries.repeat_interleave(keys.shape[1]).reshape(-1, keys.shape[1])
        # 计算注意力权重,通过调整参数w对注意力进行调节
        self.attention_weights = nn.functional.softmax(-((queries - keys) * self.w) ** 2 / 2, dim=1)
        # 执行带参数的注意力汇聚,并返回最终结果的形状调整
        return torch.bmm(self.attention_weights.unsqueeze(1), values.unsqueeze(-1)).reshape(-1)



将训练数据集转换为键和值

import torch
from torch import nn


# 带参数的注意力汇聚
class NWKernelRegression(nn.Module):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        # 创建形状为(1,)的参数张量w,用于调整注意力权重
        self.w = nn.Parameter(torch.rand((1,), requires_grad=True))

    def forward(self, queries, keys, values):
        # 重复queries并调整形状,使其与keys具有相同的列数
        queries = queries.repeat_interleave(keys.shape[1]).reshape(-1, keys.shape[1])
        # 计算注意力权重,通过调整参数w对注意力进行调节
        self.attention_weights = nn.functional.softmax(-((queries - keys) * self.w) ** 2 / 2, dim=1)
        # 执行带参数的注意力汇聚,并返回最终结果的形状调整
        return torch.bmm(self.attention_weights.unsqueeze(1), values.unsqueeze(-1)).reshape(-1)


def f(x):
    return 2 * torch.sin(x) + x ** 0.8


n_train = 50
x_train, _ = torch.sort(torch.rand(n_train) * 5)
y_train = f(x_train) + torch.normal(0.0, 0.5, (n_train,))
# X_tile的形状:(50, 50)
X_tile = x_train.repeat((n_train, 1))
# 将y_train在行维度上重复n_train次,形成一个矩阵Y_tile,形状为(n_train, n_train)
Y_tile = y_train.repeat((n_train, 1))
# 通过掩码操作,从X_tile中排除对角线元素,得到键矩阵keys
# torch.eye(n_train)使对角线全为1其余为0
# tensor([[1., 0., 0.,  ..., 0., 0., 0.],
#         [0., 1., 0.,  ..., 0., 0., 0.],
#         [0., 0., 1.,  ..., 0., 0., 0.],
#         ...,
#         [0., 0., 0.,  ..., 1., 0., 0.],
#         [0., 0., 0.,  ..., 0., 1., 0.],
#         [0., 0., 0.,  ..., 0., 0., 1.]])
# 然后使1 - torch.eye(n_train)将对角线的元素变为0其余为1,然后将结果转为布尔型
# keys的形状为[50, 49]
keys = X_tile[(1 - torch.eye(n_train)).type(torch.bool)].reshape((n_train, -1))
# 通过掩码操作,从Y_tile中排除对角线元素,得到值矩阵values
values = Y_tile[(1 - torch.eye(n_train)).type(torch.bool)].reshape(n_train, -1)

往前缩进一列,所以变成了49列
在这里插入图片描述




训练带参数的注意力汇聚模型

import torch
from torch import nn
from d2l import torch as d2l


# 带参数的注意力汇聚
class NWKernelRegression(nn.Module):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.w = nn.Parameter(torch.rand((1,), requires_grad=True))

    def forward(self, queries, keys, values):
        queries = queries.repeat_interleave(keys.shape[1]).reshape(-1, keys.shape[1])
        self.attention_weights = nn.functional.softmax(-((queries - keys) * self.w) ** 2 / 2, dim=1)
        return torch.bmm(self.attention_weights.unsqueeze(1), values.unsqueeze(-1)).reshape(-1)


def f(x):
    return 2 * torch.sin(x) + x ** 0.8


n_train = 50
x_train, _ = torch.sort(torch.rand(n_train) * 5)
y_train = f(x_train) + torch.normal(0.0, 0.5, (n_train,))
X_tile = x_train.repeat((n_train, 1))
Y_tile = y_train.repeat((n_train, 1))
keys = X_tile[(1 - torch.eye(n_train)).type(torch.bool)].reshape((n_train, -1))
values = Y_tile[(1 - torch.eye(n_train)).type(torch.bool)].reshape(n_train, -1)
# 创建带参数的注意力汇聚模型
net = NWKernelRegression()
loss = nn.MSELoss(reduction='none')
# 创建随机梯度下降优化器,用于参数更新
trainer = torch.optim.SGD(net.parameters(), lr=0.5)
# 创建动画绘制器,用于绘制损失曲线
animator = d2l.Animator(xlabel='epoch', ylabel='loss', xlim=[1, 5])

# 遍历5次
for epoch in range(5):
    trainer.zero_grad()
    l = loss(net(x_train, keys, values), y_train) / 2
    # 反向传播,计算梯度
    l.sum().backward()
    # 更新参数
    trainer.step()
    # 打印当前的损失
    print(f'epoch {epoch + 1}, loss {float(l.sum()):.6f}')
    # 绘制损失曲线
    animator.add(epoch + 1, float(l.sum()))
d2l.plt.show()

在这里插入图片描述




预测结果绘制

import torch
from torch import nn
from d2l import torch as d2l


# 带参数的注意力汇聚
class NWKernelRegression(nn.Module):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.w = nn.Parameter(torch.rand((1,), requires_grad=True))

    def forward(self, queries, keys, values):
        queries = queries.repeat_interleave(keys.shape[1]).reshape(-1, keys.shape[1])
        self.attention_weights = nn.functional.softmax(-((queries - keys) * self.w) ** 2 / 2, dim=1)
        return torch.bmm(self.attention_weights.unsqueeze(1), values.unsqueeze(-1)).reshape(-1)

# 绘制核回归结果的图像
def plot_kernel_reg(y_hat):
    # 绘制x_test和对应的真实标签y_truth以及预测标签y_hat的图像
    d2l.plot(x_test, [y_truth, y_hat], 'x', 'y', legend=['Truth', 'Pred'],
             xlim=[0, 5], ylim=[-1, 5])
    # 绘制训练集的散点图,用圆圈表示
    d2l.plt.plot(x_train, y_train, 'o', alpha=0.5)

def f(x):
    return 2 * torch.sin(x) + x ** 0.8


n_train = 50
x_train, _ = torch.sort(torch.rand(n_train) * 5)
y_train = f(x_train) + torch.normal(0.0, 0.5, (n_train,))
x_test = torch.arange(0, 5, 0.1)
y_truth = f(x_test)
n_test = len(x_test)

X_tile = x_train.repeat((n_train, 1))
Y_tile = y_train.repeat((n_train, 1))
keys = X_tile[(1 - torch.eye(n_train)).type(torch.bool)].reshape((n_train, -1))
values = Y_tile[(1 - torch.eye(n_train)).type(torch.bool)].reshape(n_train, -1)

# 创建带参数的注意力汇聚模型
net = NWKernelRegression()
loss = nn.MSELoss(reduction='none')
# 创建随机梯度下降优化器,用于参数更新
trainer = torch.optim.SGD(net.parameters(), lr=0.5)

# 遍历5次
for epoch in range(5):
    trainer.zero_grad()
    l = loss(net(x_train, keys, values), y_train) / 2
    # 反向传播,计算梯度
    l.sum().backward()
    # 更新参数
    trainer.step()
    # 打印当前的损失
    print(f'epoch {epoch + 1}, loss {float(l.sum()):.6f}')


keys = x_train.repeat((n_test, 1))
values = y_train.repeat((n_test, 1))
# 使用训练好的模型进行预测,得到预测结果y_hat
y_hat = net(x_test, keys, values).unsqueeze(1).detach()
# 绘制预测结果
plot_kernel_reg(y_hat)
d2l.plt.show()

在这里插入图片描述


# 曲线在注意力权重较大的区域变得更不平滑
d2l.show_heatmaps(net.attention_weights.unsqueeze(0).unsqueeze(0),
                 xlabel='Sorted training inputs', ylabel='Sorted testing inputs')

在这里插入图片描述


该部分总代码
import torch
from torch import nn
from d2l import torch as d2l


# 带参数的注意力汇聚
class NWKernelRegression(nn.Module):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.w = nn.Parameter(torch.rand((1,), requires_grad=True))

    def forward(self, queries, keys, values):
        queries = queries.repeat_interleave(keys.shape[1]).reshape(-1, keys.shape[1])
        self.attention_weights = nn.functional.softmax(-((queries - keys) * self.w) ** 2 / 2, dim=1)
        return torch.bmm(self.attention_weights.unsqueeze(1), values.unsqueeze(-1)).reshape(-1)

# 绘制核回归结果的图像
def plot_kernel_reg(y_hat):
    # 绘制x_test和对应的真实标签y_truth以及预测标签y_hat的图像
    d2l.plot(x_test, [y_truth, y_hat], 'x', 'y', legend=['Truth', 'Pred'],
             xlim=[0, 5], ylim=[-1, 5])
    # 绘制训练集的散点图,用圆圈表示
    d2l.plt.plot(x_train, y_train, 'o', alpha=0.5)

def f(x):
    return 2 * torch.sin(x) + x ** 0.8


n_train = 50
x_train, _ = torch.sort(torch.rand(n_train) * 5)
y_train = f(x_train) + torch.normal(0.0, 0.5, (n_train,))
x_test = torch.arange(0, 5, 0.1)
y_truth = f(x_test)
n_test = len(x_test)

X_tile = x_train.repeat((n_train, 1))
Y_tile = y_train.repeat((n_train, 1))
keys = X_tile[(1 - torch.eye(n_train)).type(torch.bool)].reshape((n_train, -1))
values = Y_tile[(1 - torch.eye(n_train)).type(torch.bool)].reshape(n_train, -1)

# 创建带参数的注意力汇聚模型
net = NWKernelRegression()
loss = nn.MSELoss(reduction='none')
# 创建随机梯度下降优化器,用于参数更新
trainer = torch.optim.SGD(net.parameters(), lr=0.5)

# 遍历5次
for epoch in range(5):
    trainer.zero_grad()
    l = loss(net(x_train, keys, values), y_train) / 2
    # 反向传播,计算梯度
    l.sum().backward()
    # 更新参数
    trainer.step()
    # 打印当前的损失
    print(f'epoch {epoch + 1}, loss {float(l.sum()):.6f}')


keys = x_train.repeat((n_test, 1))
values = y_train.repeat((n_test, 1))
# 使用训练好的模型进行预测,得到预测结果y_hat
y_hat = net(x_test, keys, values).unsqueeze(1).detach()
# 绘制预测结果
plot_kernel_reg(y_hat)
# 曲线在注意力权重较大的区域变得更不平滑
d2l.show_heatmaps(net.attention_weights.unsqueeze(0).unsqueeze(0),
                 xlabel='Sorted training inputs', ylabel='Sorted testing inputs')
d2l.plt.show()

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2192153.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

双11买什么东西比较好?买什么?这份双十一好物清单请查收

​双十一购物节是每年最大的购物狂欢节,很多商品都通过一定的优惠活动进行促销!只不过有些朋友不知道这么大的活动力度,有哪些好物值得放心入手的!于是小编根据这些年在双十一的选购经验,整理了一些实用的购物攻略建议…

新手入门大语言模型学习路线

最近有很多同学想要学习大模型,于是我根据多年的学习经验,总结了一些适合你从 0 到 1 的入门经验,分享给大家呀 1、几个学习大模型必备: 教程:动手学大模型Dive into LLMs 《动手学大模型 Dive into LLMs》:内容丰富&#xff0c…

Linux运维02:WM虚拟机安装Centos7操作系统

Centos7镜像文件下载链接:centos-7-isos-x86_64安装包下载_开源镜像站-阿里云 (aliyun.com)https://mirrors.aliyun.com/centos/7/isos/x86_64/ 1.点击“编辑虚拟机设置”、点击“CD/DVD”、点击“预览”选择镜像文件位置,点击“确定”; 2.点…

【STL】list模拟实现(画图万字解析+代码)

list模拟实现 1 模块分析1.1 list的结构1.2 ListNode的结构1.3 迭代器类 2 ListNode节点设计3 迭代器类设计3.1 迭代器类框架3.2 模板设计3.3 operator()前置和后置3.4 operator--()前置--和后置--3.4 operator*()3.5 operator->()3.6 operator!() 和 operator()3.7 迭代器类…

一张照片变换古风写真,Flux如何做到?

前言 解锁图像创作新体验:ComfyUI指南 在AI图像生成领域,ComfyUI 已成为不可忽视的力量。它是基于Stable Diffusion的图像生成工具,提供了一个节点式图形用户界面(GUI),让用户可以通过简单的拖拽与配置来…

睡眠对于生活的重要性

在快节奏的现代生活中,健康养生不再是遥不可及的概念,而是融入日常每一刻的必需。其中,睡眠作为生命不可或缺的环节,其重要性往往被忽视,实则它是身体修复、能量积蓄的黄金时段。今天,让我们深入探讨“健康…

【橙子老哥】.NetCore 管道模型源码深度解读

hello,大家好,今天又是橙子老哥的分享时间,希望大家一起学习,一起进步。 欢迎加入.net意社区,第一时间了解我们的动态,地址:ccnetcore.com 最近遇到很多小伙伴们问我,自己会.netfr…

【电力系统】Matlab|含风电-光伏-光热电站电力系统N-k安全优化调度模型

摘要 本文提出了一种结合风电、光伏与光热电站的电力系统N-k安全优化调度模型。通过在电力系统中集成多种可再生能源发电技术,优化不同类型电源的调度策略,确保在N-k故障情景下系统的稳定运行。基于Matlab仿真,本文分析了可再生能源发电的功…

路由:ReactRouter

概述 一个路径path对应一个组件component 当我们在浏览器中访问一个path的时候,path对应的组件会在页面中进行渲染。 使用 快速开始 安装依赖 npm i react-router-dom基本使用 import { createBrowserRouter, RouterProvider } from react-router-domconst ro…

【JavaEE初阶】多线程案列之定时器的使用和内部原码模拟

前言: 🌈上期博客:【JavaEE初阶】深入理解多线程阻塞队列的原理,如何实现生产者-消费者模型,以及服务器崩掉原因!!!-CSDN博客 🔥感兴趣的小伙伴看一看小编主页&#xff1…

房地产销售|基于springBoot的房地产销售管理系统设计与实现(附项目源码+论文+数据库)

私信或留言即免费送开题报告和任务书(可指定任意题目) 目录 一、摘要 二、相关技术 三、系统设计 四、数据库设计 五、核心代码 六、论文参考 七、源码获取 一、摘要 社会和科技的不断进步带来更便利的生活,计算机技术也越来…

fiddler抓包18-2_导出jmeter、postman脚本(带请求头)

课程大纲 1. Fiddler导出请求为curl脚本 选中请求,“文件” - “导出会话” - “选中的会话” - “cURL Script”。 2. 导入jmeter ① 复制curl脚本。 ② 打开jmeter,“工具” - “import from cURL”,粘贴脚本,勾选“Add cooki…

二分查找一>寻找峰值

1.题目&#xff1a; 2.解析&#xff1a; 暴力遍历代码&#xff1a;O(N),由于该题数据很少所以可以通过 暴力遍历&#xff1a;O(N),由于该题数据很少所以可以通过int index 0;for(int i 1; i < nums.length-1; i) {//某段区域内一直递增&#xff0c;更新就indexif(nums[i]…

codetop标签树刷题(三)!!暴打面试官!!!!

用于个人复习 1.子结构判断2.寻找重复的子树3.相同的树4.平衡二叉树5.二叉树展开为链表6.将二叉搜索树转化为排序的双向链表7.验证二叉搜索树8.二叉树的完全性检验9.完成二叉树的节点个数10.删除二叉搜索树中的节点11.寻找二叉树中的目标节点 1.子结构判断 给定两棵二叉树 tre…

Libtorch学习之Libtorch-VS2019-图像分割程序

文章目录 环境说明Pytorch 序列化Libtorch 下载VS配置主程序可能遇到的问题参考 环境说明 win10 VS2019 OPENCV4.7.0 Litorch1.13 Pytorch 1.12.1 Pytorch 序列化 import torch from torchvision.models import resnet50 net resnet50(pretrainedTrue) net net.cuda() net…

提升开机速度:有效管理Windows电脑自启动项,打开、关闭自启动项教程分享

日常使用Windows电脑时&#xff0c;总会需要下载各种各样的办公软件。部分软件会默认开机自启功能&#xff0c;开机启动项是指那些在电脑启动时自动运行的程序和服务。电脑开机自启太多的情况下会导致电脑卡顿&#xff0c;开机慢&#xff0c;运行不流畅的情况出现&#xff0c;而…

如何从计算机的硬盘中恢复照片 - 成功

如何从计算机硬盘恢复图片&#xff1f; 与所有电子和机械设备一样&#xff0c;硬盘驱动器也可能由于任何原因而死机。如果您的系统硬盘驱动器已停止工作或在启动系统时听到振动声&#xff0c;则它有可能已死机。如果是这样的话&#xff0c;上面的数据呢&#xff1f; 不要惊慌…

十二、血条UI

一、制作血条UI 注&#xff1a;一般不用Slider制作血条&#xff1b;而是用两个Image制作&#xff0c;选择为填充 使用Slider滑动条制作UI 人物血条&#xff1a;背景深绿色&#xff1b;滑条浅绿色 在场景中的画布选择为OverLay 敌人血条&#xff1a; 在预制体里面制作&#x…

自动驾驶系列—自动驾驶背后的数据通道:通信总线技术详解与应用场景分析

&#x1f31f;&#x1f31f; 欢迎来到我的技术小筑&#xff0c;一个专为技术探索者打造的交流空间。在这里&#xff0c;我们不仅分享代码的智慧&#xff0c;还探讨技术的深度与广度。无论您是资深开发者还是技术新手&#xff0c;这里都有一片属于您的天空。让我们在知识的海洋中…

最具有世界影响力的人颜廷利:全球著名哲学家思想家起名大师

颜廷利教授&#xff0c;这位源自济南唐王镇的杰出人物&#xff0c;不仅是中国当代最杰出的国学大师之一&#xff0c;更是将传统文化与现代科技巧妙结合的先锋。他积极推崇以人工智能技术为辅助的国学研究方法&#xff0c;为这一古老领域注入了新的活力和时代表达。 除了在学术…