1. torch.stack((tuple), dim):按照新的维度进行张量的堆叠。
tuple:需要堆叠的张量,是一个tuple,如(t1, t2)
dim:新维度,如dim=0,按照第一个维度作为堆叠的维度
T1, T2都是[3, 3]的张量

如果dim=0, 则第一个维度为新的维度,进行堆叠,堆叠后的shape:[2, 3, 3] ; dim=1, 则第二个维度为新的维度,进行堆叠,堆叠后的shape:[3, 2, 3]。不能超过要堆叠的维度数,如dim=3就会出错。默认为dim=0.



2. x.view(x.size(0), -1): 无论是CNN,还是RNN,LSTM等,卷积或者池化后,在接一个全连接层输出为一个多分类之前,需要将多维tensor展平为一维,然后输入到全连接层中。在pytorch中,tensor.view相当于numpy的reshape。
3. string.encode()和string.decode(): 字符串编码与解码。text.encode('ascii','ignore').decode('ascii'):先将text字符串编码成ascii码,如果有非ascii编码错误,则忽略掉非ascii字符,然后解码ascii字节数组类型成字符串。
如:

结果:

4. str.maketrans('','', string.punctuation):将string.punctuation中的字符全部翻译为None:即字符串包含punctuation =r"""!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~""",不用翻译,直接忽略掉。

结果bottom-left中的‘-’被忽略掉:

5. string.strip().split():将字符串去掉前后空格然后返回字符串中包含单词的列表。
如:tokens: make bottomleft large purple object blue
结果:['make', 'bottomleft', 'large', 'purple', 'object', 'blue']