Pytorch note
1.对array数组进行shuffle
- 简单实现——利用np.array型的index下标对np.array进行排序
idx = np.array([0,2,1])
x = np.array([[5],[6],[7]])
x = x[idx]
[[5], [7], [6]]
array可以直接求均值与标准差(.mean, .std)
X.mean()
X.std()
label与plt.legend()一起用
plt.scatter(X_train[y_train==0, 0], X_train[y_train==0, 1], label="class 0", marker='o')
plt.scatter(X_train[y_train==1, 0], X_train[y_train==1, 1], label="class 1", marker='s')
plt.xlabel("feature 1")
plt.ylabel("feature 2")
plt.legend()
plt.show()
2. 使用np.meshgrid()输出正方点阵并使用cdist()计算点与点之间的距离
- 获取28*28个点
# col = [[0 1 2 3 ... n], [0 1 2 3 ... n], ... [0 1 2 3 ... n]]# row = [[0 0 0 0 ... 0], [1 1 1 ... n], [n n n ... n]]col, row = np.meshgrid(np.arange(n), np.arange(img_size))# 28*28*2coord = np.stack((col, row), axis=2).reshape(-1, 2) / img_size
- 计算28*28个点两两之间的距离
# 784*784
dist = cdist(coord, coord, metric="euclidean")
3. device的相关使用
# 新建torch.device的类device = torch.device(args.device)# torch.tensor()将numpy 转成torch.tensor类# torch.to()的参数是torch.device类x = torch.tensor(x).to(device)
4. with torch.no_grad()
- torch.no_grad() 是一个上下文管理器,被该语句 wrap 起来的部分将不会track 梯度。
a = torch.tensor([1.1], requires_grad=True)
b = a * 2
- 输出:
b
Out[63]: tensor([2.2000], grad_fn=<MulBackward0>)
b.add_(2)
Out[64]: tensor([4.2000], grad_fn=<AddBackward0>)
- 可以看到不被wrap的情况下,b.grad_fn 为 addbackward 表示这个add 操作被track了
with torch.no_grad():b.mul_(2)
- 在被包裹的情况下可以看到 b.grad_fn 还是为 add,mul 操作没有被 track. 但是注意,乘法操作是被执行了的。(4.2 -> 8.4)
b
Out[66]: tensor([8.4000], grad_fn=<AddBackward0>)
- 所以如果有不想被track的计算部分可以通过这么一个上下文管理器包裹起来。这样可以执行计算,但该计算不会在反向传播中被记录。
5. 对数据进行shuffle
- np.random.permutation(int)
- 按照一定顺序取数组x = x[2,3,0,1,5,6,4] => x[permutation]
def shuffle(self):permutation = np.random.permutation(self.size)xs, ys = self.xs[permutation], self.ys[permutation]self.xs = xsself.ys = ys
6. return 带yield的函数
- yield-生成器
def func():def go():indx = 0while indx < 5:s = indxindx = indx + 1yield sreturn go()for iter, item in enumerate(func()):print(item)
0
1
2
3
4
7. torch.nn.utils.clip_grad_norm_()
这个函数进行的是梯度剪切,规定了最大不能超过的max_norm.
if self.clip is not None:# 梯度剪切,规定了最大不能超过的max_norm.torch.nn.utils.clip_grad_norm_(self.model.parameters(), self.clip)
8. local()函数/ 批量生成变量
locals()函数
(1)返回当前位置的全部局部变量
(2)Return a dictionary containing the current scope’s local variables.
x_train, y_train = x[:num_train], y[:num_train]x_val, y_val = (x[num_train: num_train + num_val],y[num_train: num_train + num_val],)x_test, y_test = x[-num_test:], y[-num_test:]for cat in ["train", "val", "test"]:_x, _y = locals()["x_" + cat], locals()["y_" + cat]print(cat, "x: ", _x.shape, "y:", _y.shape)np.savez_compressed(os.path.join(args.output_dir, f"{cat}.npz"),x=_x,y=_y,x_offsets=x_offsets.reshape(list(x_offsets.shape) + [1]),y_offsets=y_offsets.reshape(list(y_offsets.shape) + [1]),)
9. 如果要输出到屏幕的数据太多,最好在print函数里使用Flush=True,确保缓存的数据全部输出
if iter % args.print_every == 0:log = 'Iter: {:03d}, Train Loss: {:.4f}, Train MAPE: {:.4f}, Train RMSE: {:.4f}'# flush=True 保证内存的内容及时刷新到屏幕print(log.format(iter, train_loss[-1], train_mape[-1], train_mape[-1], train_rmse[-1]),flush=True)
10.对list进行切片时,…代表省略所有的:
# 切片时使用,省略所有的:yhat = yhat[:realy.size(0), ...]
11. StandardScaler.transform与StandardScaler.inverse_transform
在准备DataLoader进行批处理化的时候,使用transform,在预测之后使用inverse_transform进行还原
12. Torch.tensor.contiguous
在transpose操作数据的时候,常常使得数据在逻辑上是连续的,但是内存并不连续。若仍然要使得内存连续,则使用.contiguous()
def forward(self, x, A):x = torch.einsum('ncvl,vw->ncwl', (x, A))# 将x的元素在内存上也变得连续return x.contiguous()
12.torch.nn.function.softmax() dim=0与dim=1
dim=?:沿着这个维度进行softmax
input = torch.tensor([[4.,4.,4.],[5.,5.,5.]])
# dim=1
output = F.softmax(input, dim=1)
print(output)
## 输出
tensor([[0.3333, 0.3333, 0.3333],[0.3333, 0.3333, 0.3333]])
# dim=0
output = F.softmax(input, dim=0)
print(output)
## 输出
tensor([[0.2689, 0.2689, 0.2689],[0.7311, 0.7311, 0.7311]])
Pytorch note相关推荐
- Pytorch Note 快乐星球
Pytorch Note 什么是快乐星球,让我用简单易懂的代码带你进入pytorch快乐星球 这是我的Pytoch学习笔记,下面会慢慢的更新我的学习笔记 part1: 深度学习基础 PyTorch介绍 ...
- 机器学习环境配置(Tesla K80安装PyTorch的全过程)
"""Waring:多图预警""" 本以为update&upgrade要花上大半天,ali云真的快 先查看一下Ubuntu默认的Py ...
- Pytorch Note40 词嵌入(word embedding)
Pytorch Note40 词嵌入(word embedding) 文章目录 Pytorch Note40 词嵌入(word embedding) 词嵌入 PyTorch 实现 Skip-Gram ...
- Pytorch Note46 生成对抗网络的数学原理
Pytorch Note46 生成对抗网络的数学原理 文章目录 Pytorch Note46 生成对抗网络的数学原理 全部笔记的汇总贴: Pytorch Note 快乐星球 之前介绍了什么是生成对抗, ...
- Pytorch Note5 动态图和静态图 dynamic-graph
Pytorch Note5 动态图和静态图 动态图和静态图 TensorFlow PyTorch 全部笔记的汇总贴:Pytorch Note 快乐星球 动态图和静态图 目前神经网络框架分为静态图框架和 ...
- pytorch学习笔记之dataload
pytorch的数据加载:torch.utils.data.DataLoader 源码解析 python中iterator和iterable的区别:前者表示迭代器,后者表示是否可迭代:迭代器不用知道一 ...
- Pytorch Note52 灵活的数据读取介绍
Pytorch Note52 灵活的数据读取介绍 文章目录 Pytorch Note52 灵活的数据读取介绍 灵活的数据读取 读入数据 传入数据预处理方式 Dataset DataLoader 例子 ...
- Pytorch Note32 稠密连接的卷积网络 DenseNet
Pytorch Note32 稠密连接的卷积网络 DenseNet 文章目录 Pytorch Note32 稠密连接的卷积网络 DenseNet DenseNet Dense Block DenseN ...
- Pytorch Note1 Pytorch介绍
Pytorch Note1 Pytorch介绍 PyTorch的诞生 常见的深度学习框架简介 为什么选择PyTorch 总结一下Pytorch的特点 Python 优先 命令式体验 快速精益 安装 全 ...
最新文章
- JUC 常用 4 大并发工具类
- JSR303(Bean Validation 1.0)
- iOS_CNBlog项目开发 (基于博客园api开发) 上篇
- iOS开发 plist字段列表,很全
- 【Android Developers Training】 58. 缓存位图
- 附加一个:为什么要用简单工厂模式
- 还不理解“分布式事务”?这篇给你讲清楚!
- iOS内存分配五大区:
- Android日志输出管理
- Java基础0309
- Hololens UnityChatSDK配置教程 Unity跨平台音视频通讯SDK汇总
- 程序员浪费生命的几种方式
- python 通过逗号分割字符串_「Python 秘籍」使用多个界定符分割字符串
- 吉米多维奇数学分析习题集每日一题--泰勒公式习题1376
- 企业邮箱是什么?企业邮箱有什么好处?企业邮箱域名怎么设置?
- java有理数类的封装_【Java笔记】有理数的类封装
- 量化城市︱计算机视觉技术在街道品质量化评估中的应用
- 如何自己开发一个Android APP(3)——XML和Android
- 如何使用手机将图片改成JPG格式?教你一招解决
- ROS Navigation Stack安装