2025-05-16
工具使用
00

在当前Word按Alt+F11,然后输入代码:

vba
展开代码
Sub CheckIfDocumentHasPictures() Dim hasPictures As Boolean hasPictures = False ' 检查内联图片(InlineShapes) If ActiveDocument.InlineShapes.Count > 0 Then hasPictures = True End If ' 检查浮动图片(Shapes) If Not hasPictures Then If ActiveDocument.Shapes.Count > 0 Then hasPictures = True End If End If ' 显示结果 If hasPictures Then MsgBox "当前文档包含图片!", vbInformation, "检测结果" Else MsgBox "当前文档没有图片。", vbInformation, "检测结果" End If End Sub
2025-05-15
算法刷题
00
py
展开代码
import numpy as np import random class KMeans: def __init__(self, n_clusters=3, max_iter=300, tol=1e-4): self.n_clusters = n_clusters # 聚类数量 self.max_iter = max_iter # 最大迭代次数 self.tol = tol # 收敛阈值 self.centroids = None # 聚类中心 self.labels = None # 样本标签 def fit(self, X): # 1. 随机初始化聚类中心 n_samples = X.shape[0] random_indices = random.sample(range(n_samples), self.n_clusters) self.centroids = X[random_indices] for _ in range(self.max_iter): # 2. 分配样本到最近的聚类中心 distances = self._compute_distances(X) self.labels = np.argmin(distances, axis=1) # 3. 保存旧中心用于收敛判断 old_centroids = self.centroids.copy() # 4. 更新聚类中心 for i in range(self.n_clusters): # 获取属于当前聚类的所有样本 cluster_samples = X[self.labels == i] if len(cluster_samples) > 0: self.centroids[i] = np.mean(cluster_samples, axis=0) # 5. 检查是否收敛 centroid_shift = np.linalg.norm(old_centroids - self.centroids) if centroid_shift < self.tol: break def predict(self, X): distances = self._compute_distances(X) return np.argmin(distances, axis=1) def _compute_distances(self, X): # 计算每个样本到所有聚类中心的距离 distances = np.zeros((X.shape[0], self.n_clusters)) for i, centroid in enumerate(self.centroids): distances[:, i] = np.linalg.norm(X - centroid, axis=1) return distances # 生成测试数据 np.random.seed(42) X = np.vstack([ np.random.normal(loc=[0, 0], scale=1, size=(100, 2)), np.random.normal(loc=[5, 5], scale=1, size=(100, 2)), np.random.normal(loc=[-5, 5], scale=1, size=(100, 2)) ]) # 训练K-Means kmeans = KMeans(n_clusters=3) kmeans.fit(X) # 预测 labels = kmeans.predict(X) print("聚类中心:\n", kmeans.centroids)
2025-05-15
算法刷题
00

多头:

py
展开代码
import torch import torch.nn as nn import torch.nn.functional as F class SelfAttention(nn.Module): def __init__(self, embed_size, heads): super(SelfAttention, self).__init__() self.embed_size = embed_size self.heads = heads self.head_dim = embed_size // heads assert self.head_dim * heads == embed_size, "Embed size needs to be divisible by heads" # 线性变换得到 Q, K, V self.values = nn.Linear(embed_size, embed_size) self.keys = nn.Linear(embed_size, embed_size) self.queries = nn.Linear(embed_size, embed_size) # 输出线性层 self.fc_out = nn.Linear(embed_size, embed_size) def forward(self, x): # x shape: (N, seq_len, embed_size) N = x.shape[0] seq_len = x.shape[1] # 线性变换得到 Q, K, V values = self.values(x) # (N, seq_len, embed_size) keys = self.keys(x) # (N, seq_len, embed_size) queries = self.queries(x) # (N, seq_len, embed_size) # 分割多头 values = values.reshape(N, seq_len, self.heads, self.head_dim) keys = keys.reshape(N, seq_len, self.heads, self.head_dim) queries = queries.reshape(N, seq_len, self.heads, self.head_dim) # 计算注意力分数 energy = torch.einsum("nqhd,nkhd->nhqk", [queries, keys]) # queries shape: (N, seq_len, heads, head_dim) # keys shape: (N, seq_len, heads, head_dim) # energy shape: (N, heads, seq_len, seq_len) # 缩放点积注意力 attention = F.softmax(energy / (self.embed_size ** (1/2)), dim=3) # 应用注意力到values上 out = torch.einsum("nhql,nlhd->nqhd", [attention, values]) # attention shape: (N, heads, seq_len, seq_len) # values shape: (N, seq_len, heads, head_dim) # out shape: (N, seq_len, heads, head_dim) # 合并多头 out = out.reshape(N, seq_len, self.embed_size) # 输出线性变换 out = self.fc_out(out) return out
2025-05-15
单片机ban
00

该文章已加密,点击 阅读全文 并输入密码后方可查看。

2025-05-15
深度学习ban
00

该文章已加密,点击 阅读全文 并输入密码后方可查看。

2025-05-15
深度学习ban
00

该文章已加密,点击 阅读全文 并输入密码后方可查看。

2025-05-14
算法刷题
00
  1. 两数相加

https://leetcode.cn/problems/add-two-numbers

给你两个 非空 的链表,表示两个非负的整数。它们每位数字都是按照 逆序 的方式存储的,并且每个节点只能存储 一位 数字。

请你将两个数相加,并以相同形式返回一个表示和的链表。

你可以假设除了数字 0 之外,这两个数都不会以 0 开头。

输入:l1 = [2,4,3], l2 = [5,6,4]

输出:[7,0,8]

解释:342 + 465 = 807.

python
展开代码
# Definition for singly-linked list. # class ListNode: # def __init__(self, val=0, next=None): # self.val = val # self.next = next class Solution: def addTwoNumbers( self, l1: Optional[ListNode], l2: Optional[ListNode] ) -> Optional[ListNode]: dammy = ListNode() # 虚拟节点,最终返回这个节点的下一个节点 … head.next = ListNode( v_dig ) # 第一轮 比如7+6=13,那么这里就是3。第二轮,2+5+1=8,链表会是 dammy>>3>>8,从个位开始的 head = head.next if l1: l1 = l1.next if l2: l2 = l2.next return dammy.next
2025-05-13
深度学习
00

【训练】Qwen2.5VL 多机多卡 Grounding Box定位(1):http://101.126.150.28:7878/post/2067

【训练】Qwen2.5VL 多机多卡 Grounding Box定位(2):http://101.126.150.28:7878/post/2094

1. 本次更改了数据集

类似这样:

json
展开代码
{ "messages": [ { "content": "<image>点[56,259]所处位置(也即是图中绿色五角星中心所处位置)的信息是什么?需要优先匹配最近UI元素的box。", "role": "user" }, { "content": "<ref>文本-地址</ref><box>[[33, 241, 66, 264]]</box>", "role": "assistant" } ], "images": [ "/img_datasets/img_small_size_28_prompt/000001.jpg" ] }
2025-05-13
深度学习
00

Transformer中计算最复杂的模块

自注意力机制(Self-Attention) 是Transformer中最耗时的部分。


时间复杂度与哪些因素有关?

  • 序列长度 n
  • 隐藏层维度 d
2025-05-13
深度学习
00

解决训练模型时的内存不足问题

在没有额外显卡的情况下,当你遇到训练模型时出现"out of memory"错误,可以尝试以下几种解决方案: