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*2
coord = 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 = xs
self.ys = ys
6. return 带yield的函数
- yield-生成器
def func():
def go():
indx = 0
while indx < 5:
s = indx
indx = indx + 1
yield s
return 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]])