0%

Dockerfile

1
2
3
4
5
6
7
8
9
10
11
FROM ubuntu:latest

WORKDIR /root
COPY v2ray.sh /root

RUN mkdir -p /etc/v2ray /usr/local/share/v2ray /var/log/v2ray \
&& chmod +x /root/v2ray.sh \
&& /root/v2ray.sh

VOLUME /etc/v2ray
CMD [ "v2ray", "-config", "/etc/v2ray/config.json" ]

sh

1
2
3
4
5
6
7
8
9
10
apt update \
&& apt install wget zip \
&& wget https://github.com/v2fly/v2ray-core/releases/download/${TAG}/${V2RAY_FILE} \
&& unzip v2ray.zip \
&& chmod +x v2ray v2ctl \
&& mv v2ray v2ctl /usr/bin \
&& mv geosite.dat geoip.dat /usr/local/share/v2ray \
&& mv config.json /etc/v2ray/config.json

echo 'Done'

遇到的坑

1. np.prod()

对传入的list计算乘积,可以指定dim。

2. nn.ConvTranspose2d()

反卷积操作。

3. argument after * must be an iterable

如果在函数内部使用了*, 对传入的参数进行unpack, 需要注意需要传入一个可以迭代的对象。

1
2
def Reshape(input, shape):
return input.view(input.shape[0], *shape)

如果传的是一个标量,可以将它变为一个tuple传入。

1
2
shape = np.prod([1, 2, 3])
Reshape(input, (shape,))

在传入的shape后加一个,就相当于是一个tuple了。

4. reduce failed to synchronize: device-side assert triggered.

这是神经网络最后一层没有normalize导致的,可以加一层Sigmoid()解决。

5. d_fake的batch_size一直是torch.Size([256, 1])

在计算bce_loss(d_fake, real_label)时,生成的d_fake没有与最后一个batch_size匹配。原因在于Net中d_fake是由gen_imgs输入D生成的,而一开始生成的噪声的batch_size决定了gen_imgs的第一维度的大小。

问题出在一开始生成噪声的时候输入的是self.batch_size而不是imgs_batch.

6. std evaluated to zero after conversion to torch.int64, leading to division by zero.

dataset在返回数据时通过transform.Normalize()时出现的问题。

dataset在读取自制的数据集时,类型转换的问题。需要将文本的str格式转成float,而不是int.

7. output with shape [1, 28, 28] doesn’t match the broadcast shape [3, 28, 28].

由于MNIST数据集通道的问题。

1
2
3
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
# 改为
transforms.Normalize((0.5,), (0.5,))

8. _pickle.UnpicklingError: A load persistent id instruction was encountered, but no persistent_load…

由于pytorch版本所致,torch.save(), torch.load()里面保存了版本信息,不一样的话会报错。

9. Only one class present in y_true. ROC AUC score is not defined in that case.

在计算ROC AUC时,如果一个batch中的类别都是同样的,就会报错。

解决方案

  • 整理0,1标签数据较为均衡
  • 使用try except然后pass

10. one of the variables needed for gradient computation has been modified by an inplace operation 由于Pytorch版本的大坑

由于pytorch版本不一样,因此报错!!!

都是Python3.7

Pytorch = 1.7.1 (报错)

Pytorch = 1.0.0 (不报错)

11. Pytorch自带的mnist数据集读取库 由于Pytorch版本的坑

Pytorch = 1.7.1 (指定到有mnist的目录)

Pytorch = 1.0.0 (指定到有raw的目录)

init.py文件的主要作用

用来管理当前目录下的.py文件。

  1. __init__.py是package的标识。
  2. 其中__all__可以用来进行模糊导入(import *)。

使用import导入包的时候,就相当于导入了这个包的__init__.py

import的理解

可以导入文件变量

from的理解

只能文件


Reference

https://www.cnblogs.com/BlueSkyyj/p/9415087.html

数据类型

旧版:

Tensor是基本的数据类型,Variable是对Tensor的封装。只有Variable引入了计算图,实现自动求导backward().

定义是否需要梯度: Variable(requires_grad=True).

新版:

Variable与Tensor进行合并了,torch.Tensor现在一样可以实现自动求导。

定义是否需要梯度: Tensor.requires_grad_().

初始化:

Tensor([0, 1, 2])

Tensor(2, 3).fill_(9)

torch.ones(4, 5)

对Tensor内部属性的操作:

Tensor是一个对象,如果对其操作需要通过.requires_grad_()等方法对Field进行赋值。

torch.ones(2, 3)等,是一个方法,其中可以通过传requires_grad=True等字典对Field进行赋值,由于返回的仍是一个Tensor类型的对象,也可以使用.requires_grad_()等方法进行赋值。

浮点型

可以通过tensor.to(torch.float64)Tensor类型的数据转换成FloatTensor类型的数据。

梯度

Pytorch计算图中有两种元素:数据(Tensor)和运算(可求导运算)。

Tensor分为两类:叶子结点,非叶子结点。

计算Tensor梯度(即grad属性被赋值)的条件:

  • 叶子结点
  • requires_grad=True
  • 依赖该Tensor的所有Tensorrequires_grad=True

进行反向传播时,需要是一个标量

梯度下降

就是在求得了grad的叶子结点,使用Tensor.data + Tensor.grad.data实现梯度下降。

Tensor在cuda上的转移

所有需要使用GPU训练的参数以及关联的参数都需要放到cuda上。

1
2
3
4
5
cuda = True if torch.cuda.is_available() else False
device_id = 7
if cuda:
torch.cuda.set_device(device_id)
Tensor = torch.cuda.FloatTensor if cuda else torch.FloatTensor

如果已经是在torch.FloatTensor中的参数,可以通过.type(Tensor)来转移参数。


Reference

https://pytorch.org/docs/stable/tensors.html

https://zhuanlan.zhihu.com/p/85506092