神经网络中遇到的 python 函数(Pytorch)

1.getattr()

函数用于返回一个对象属性值。

def getattr(object, name, default=None): # known special case of getattr
    """
    getattr(object, name[, default]) -> value
    
    Get a named attribute from an object; getattr(x, 'y') is equivalent to x.y.
    When a default argument is given, it is returned when the attribute doesn't
    exist; without it, an exception is raised in that case.
    """
    pass
getattr(object, name[, default])
object -- 对象。
name -- 字符串,对象属性。
default -- 默认返回值,如果不提供该参数,在没有对应属性时,将触发 AttributeError。

2.assert

用于判断一个表达式,在表达式条件为 false 的时候触发异常。
语法格式:

`assert expression`

assert 后面也可以紧跟参数:

assert expression [, arguments]

等价于:

if not expression:
    raise AssertionError(arguments)

3.mkdir()

创建目录。

    def mkdir(self, mode=0o777, parents=False, exist_ok=False):
        """
        Create a new directory at this given path.
        """
        if self._closed:
            self._raise_closed()
        try:
            self._accessor.mkdir(self, mode)
        except FileNotFoundError:
            if not parents or self.parent == self:
                raise
            self.parent.mkdir(parents=True, exist_ok=True)
            self.mkdir(mode, parents=False, exist_ok=exist_ok)
        except OSError:
            # Cannot rely on checking for EEXIST, since the operating system
            # could give priority to other errors like EACCES or EROFS
            if not exist_ok or not self.is_dir():
                raise
mkdir(parents=True, exist_ok=True)
parents:如果父目录不存在,是否创建父目录。
exist_ok:只有在目录不存在时创建目录,目录已存在时不会抛出异常。

4.datetime.datetime.now()

寻找当前时间。

datetime.datetime.now(time_zone)
datetime.datetime.now()
time_zone 	可选的 	默认情况下,这个参数被设置为None 。我们输入我们希望转换的当前时间的时区。
time = datetime.datetime.now()
print("The time in this instance is:", time)
# The time in this instance is: 2023-07-17 12:55:35.257722

5.strftime()

格式化时间,返回当地时间(显示的格式以字符串显示,主要通过format决定)。

datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S,%A,%B')
年月日分时秒的符号如下:
%y	年份两位数
%Y	年份四位数
%m	月份两位数
%d	月份
%H	24小时制
%I	12小时制
%M	分
%S	秒
%a	星期简化
%A	星期完整
%b	月份简化
%B	月份完整

6.create_logger()

创建日志。

class Logger(object):
    def __init__(self):
        pass

    @staticmethod
    def create_logger(path="file.log", log_level=logging.DEBUG, encoding='utf-8'):
        """
        创建日志函数
        :param path: 日志文件路径
        :param log_level: 日志等级:logging.DEBUG/logging.INFO/ logging.WARNING/logging.ERROR/logging.CRITICAL
        :param encoding: 文件编码
        :return: logger
        """
        logger = logging.getLogger(__name__)
        logger.setLevel(log_level)
        handler = logging.FileHandler(path, mode='a', encoding=encoding)
        handler.setLevel(logging.INFO)
        ch = logging.StreamHandler()
        ch.setLevel(logging.DEBUG)
        formatter = logging.Formatter("%(asctime)s:%(filename)s:%(funcName)s:%(lineno)d:%(levelname)s:%(message)s")
        formatter = logging.Formatter("%(message)s")
        handler.setFormatter(formatter)
        ch.setFormatter(formatter)
        logger.addHandler(handler)
        logger.addHandler(ch)
        return logger

7.Logger.info

通过 msg和不定参数args来进行日志的格式化

    def info(self, msg, *args, **kwargs):
        """
        Log 'msg % args' with severity 'INFO'.

        To pass exception information, use the keyword argument exc_info with
        a true value, e.g.

        logger.info("Houston, we have a %s", "interesting problem", exc_info=1)
        """
        if self.isEnabledFor(INFO):
            self._log(INFO, msg, args, **kwargs)

Python中使用logging调用Logger.info方法的处理过程

8. item() 和 items()

item()的作用是取出单元素张量的元素值并返回该值,保持该元素类型不变。

x= torch.randn(3,3)
x[11].item()

items()的作用是把字典中的每对key和value组成一个元组,并把这些元祖放在列表中返回。

d = {'a' : 1'b': 2,'C' : 3}
d.items()

item()和items()的用法详解与区别

9.glob()

可以查找符合自己目的的文件,类似于Windows下的文件搜索,支持通配符操作*,?,[]这三个通配符,*代表0个或多个字符,?代表一个字符,[]匹配指定范围内的字符,如[0-9]匹配数字。
glob方法返回所有匹配的文件路径列表(list);
方法需要一个参数用来指定匹配的路径字符串(字符串可以为绝对路径也可以为相对路径),其返回的文件名只包括当前目录里的文件名,不包括子文件夹里的文件。

glob.glob(r’c:*.txt’)  # 获得C盘下的所有txt文件
glob.glob(r’E:\pic**.jpg’)  # 获得指定目录下的所有jpg文件

10.tqdm模块

进度条将处理情况进行可视化展示。

tqdm(iterator)

具体使用参考:详细介绍Python进度条tqdm的使用

11.zip()

将可迭代的对象作为参数,将对象中对应的元素打包成一个个元组,然后返回由这些元组组成的列表。

zip([iterable, ...])  # iterabl -- 一个或多个迭代器;

12.with

with 语句用于异常处理,封装了 try…except…finally 编码范式,提高了易用性。
with 语句实现原理建立在上下文管理器之上。
上下文管理器是一个实现 __enter____exit__ 方法的类。使用 with 语句确保在嵌套块的末尾调用 __exit__ 方法。

13.transpose()

按轴交换数据。
两个参数是要互换的轴

import numpy as np
import torch
# 创建
x = np.arange(12).reshape((2,2,3))
x = torch.Tensor(x)
print(x)
# 输出
tensor([[[ 0.,  1.,  2.],
         [ 3.,  4.,  5.]],

        [[ 6.,  7.,  8.],
         [ 9., 10., 11.]]])
# transpose:转换第一维度和第二维度,即二维矩阵的x和y
a = x.transpose(1,2)
print(a)
#输出
tensor([[[ 0.,  3.],
         [ 1.,  4.],
         [ 2.,  5.]],

        [[ 6.,  9.],
         [ 7., 10.],
         [ 8., 11.]]])
# transpose:
b = x.transpose(0,2)
print(b)
# 输出
tensor([[[ 0.,  6.],
         [ 3.,  9.]],

        [[ 1.,  7.],
         [ 4., 10.]],

        [[ 2.,  8.],
         [ 5., 11.]]])

14.nonzero()

nonzero函数是numpy中用于得到数组array中非零元素的位置(数组索引)的函数。

numpy.nonzero(arr)

15.view()

view() 的作用相当于numpy中的reshape,重新定义矩阵的形状。

tensor.view()
a1 = torch.arange(0,16)
print(a1)
#输出
tensor([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15])

a2 = a1.view(8, 2)
a3 = a1.view(2, 8)
a4 = a1.view(4, 4)


#输出
tensor([[ 0,  1],
        [ 2,  3],
        [ 4,  5],
        [ 6,  7],
        [ 8,  9],
        [10, 11],
        [12, 13],
        [14, 15]])
tensor([[ 0,  1,  2,  3,  4,  5,  6,  7],
        [ 8,  9, 10, 11, 12, 13, 14, 15]])
tensor([[ 0,  1,  2,  3],
        [ 4,  5,  6,  7],
        [ 8,  9, 10, 11],
        [12, 13, 14, 15]])

view中一个参数定为-1,代表自动调整这个维度上的元素个数,以保证元素的总数不变。

a2 = a1.view(-1, 16)
a3 = a1.view(-1, 8)
a4 = a1.view(-1, 4)
a5 = a1.view(-1, 2)
a6 = a1.view(4*4, -1)
a7 = a1.view(1*4, -1)
a8 = a1.view(2*4, -1)

#输出
tensor([[ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15]])
tensor([[ 0,  1,  2,  3,  4,  5,  6,  7],
        [ 8,  9, 10, 11, 12, 13, 14, 15]])
tensor([[ 0,  1,  2,  3],
        [ 4,  5,  6,  7],
        [ 8,  9, 10, 11],
        [12, 13, 14, 15]])
tensor([[ 0,  1],
        [ 2,  3],
        [ 4,  5],
        [ 6,  7],
        [ 8,  9],
        [10, 11],
        [12, 13],
        [14, 15]])
tensor([[ 0],
        [ 1],
        [ 2],
        [ 3],
        [ 4],
        [ 5],
        [ 6],
        [ 7],
        [ 8],
        [ 9],
        [10],
        [11],
        [12],
        [13],
        [14],
        [15]])
tensor([[ 0,  1,  2,  3],
        [ 4,  5,  6,  7],
        [ 8,  9, 10, 11],
        [12, 13, 14, 15]])
tensor([[ 0,  1],
        [ 2,  3],
        [ 4,  5],
        [ 6,  7],
        [ 8,  9],
        [10, 11],
        [12, 13],
        [14, 15]])

16.index_select

返回沿着输入张量的指定维度的指定索引号进行索引的张量子集。

torch.index_select(input,dim,index,out=None)
input(Tensor) - 需要进行索引操作的输入张量;
dim(int) - 需要对输入张量进行索引的维度;
index(LongTensor) - 包含索引号的 1D 张量;
out(Tensor, optional) - 指定输出的张量。比如执行 torch.zeros(2, 2, out = tensor_a),相当于执行 tensor_a = torch.zeros(2, 2);

17.nn.ModuleList

是一个存储不同module,并自动将每个module的parameters添加到网络之中的容器。
可以把任意nn.Module的子类(如nn.Conv2d,nn.Linear等)加到这个list里面,方法和python自带的list一样,无非是extend,append等操作,但不同于一般的list,加入到nn.ModuleList里面的module是会自动注册到整个网络上的,同时module的parameters也会自动添加到整个网络中。若使用python的list,则会出问题。

nn.ModuleList并没有定义一个网络,它只是将不同的模块储存在一起,这些模块之间并没有什么先后顺序可言。 网络的执行顺序是根据forward函数来决定的。但是一般设置ModuleList中的顺序和forward中保持一致,增强代码的可读性。

ModuleList可以根据序号来调用,一个模型可以在forward函数中被调用多次。但需要注意的是,被调用多次的模块,是使用同一组parameters的,也就是它们是参数共享的。

18.nn.Sequential

序列容器,用于搭建神经网络的模块被按照被传入构造器的顺序添加到nn.Sequential()容器中.
nn.Sequential() 可以允许将整个容器视为单个模块(即相当于把多个模块封装成一个模块),forward()方法接收输入之后,nn.Sequential()按照内部模块的顺序自动依次计算并输出结果。

不同于nn.ModuleListnn.Sequential已经实现了内部的forward函数,而且里面的模块必须是按照顺序进行排列的,所以我们必须确保前一个模块的输出大小和下一个模块的输入大小是一致的。

19.nn.ReLU()

class ReLU(Module):
    r"""Applies the rectified linear unit function element-wise:

    :math:`\text{ReLU}(x) = (x)^+ = \max(0, x)`

    Args:
        inplace: can optionally do the operation in-place. Default: ``False``
    """
     __constants__ = ['inplace']
    inplace: bool

    def __init__(self, inplace: bool = False):
        super(ReLU, self).__init__()
        self.inplace = inplace

    def forward(self, input: Tensor) -> Tensor:
        return F.relu(input, inplace=self.inplace)

    def extra_repr(self) -> str:
        inplace_str = 'inplace=True' if self.inplace else ''
        return inplace_str

inplace = False:不会修改输入对象的值,而是返回一个新创建的对象,所以打印出对象存储地址不同,类似于C语言的值传递;
inplace = True :会修改输入对象的值,所以打印出对象存储地址相同,类似于C语言的址传递;会改变输入数据的值,节省反复申请与释放内存的空间与时间,只是将原来的地址传递,效率更好。

20.raise

手动抛出异常。

raise [exceptionName [(reason)]]
#其中,用 [] 括起来的为可选参数,其作用是指定抛出的异常名称,以及异常信息的相关描述。
#如果可选参数全部省略,则 raise 会把当前错误原样抛出;如果仅省略 (reason),则在抛出异常时,将不附带任何的异常描述信息。
raise:单独一个 raise。该语句引发当前上下文中捕获的异常(比如在 except 块中),或默认引发 RuntimeError 异常。
raise 异常类名称:raise 后带一个异常类名称,表示引发执行类型的异常。
raise 异常类名称(描述信息):在引发指定类型的异常的同时,附带异常的描述信息。

21.torch.clamp()

clamp()函数的功能: 将输入input张量每个元素的值压缩到区间 [min,max],并返回结果到一个新张量

torch.clamp(input, min, max, out=None) → Tensor
input:输入张量;
min:限制范围下限;
max:限制范围上限;
out:输出张量。
a=torch.randint(low=0,high=10,size=(10,1))
print(a)
b=torch.clamp(a,3,9)
print(b)

#输出:
tensor([[7],
        [5],
        [5],
        [4],
        [4],
        [9],
        [0],
        [1],
        [4],
        [1]])
tensor([[7],
        [5],
        [5],
        [4],
        [4],
        [9],
        [3],
        [3],
        [4],
        [3]])

clamp_() 与clamp() 的区别:
pytorch 中,一般来说如果对tensor的一个函数后加上了下划线,则表明这是一个in-place类型。
in-place类型是指,当在一个tensor上操作了之后,是直接修改了这个tensor,而不是返回一个新的tensor并不修改旧的tensor。

22. contiguous()

contiguous()函数的作用: 把tensor变成在内存中连续分布的形式。
contiguous一般与 transpose,permute,view搭配使用:使用transpose或permute进行维度变换后,调用contiguous,然后方可使用view对维度进行变形(如:tensor_var.contiguous().view() ),示例如下:

x = torch.Tensor(2,3)
y = x.permute(1,0)         # permute:二维tensor的维度变换,此处功能相当于转置transpose
y.view(-1)                 # 报错,view使用前需调用contiguous()函数
y = x.permute(1,0).contiguous()
y.view(-1)                 # OK

具体原因有两种说法:

1 transpose、permute等维度变换操作后,tensor在内存中不再是连续存储的,而view操作要求tensor的内存连续存储,所以需要contiguous来返回一个contiguous copy;

2 维度变换后的变量是之前变量的浅拷贝,指向同一区域,即view操作会连带原来的变量一同变形,这是不合法的,所以也会报错;即contiguous返回了tensor的深拷贝contiguous copy数据;

pytorch中的contiguous()函数的浅浅解释

23.range()

返回一个 [ e n d − s t a r t s t e p − 1 ] [\frac{end-start}{step}-1] [stependstart1] 的一维向量,其值从 start 到 end,步骤为 step。步长是张量中两个值之间的差距。 o u t i + 1 = o u t i + s t e p out_{i+1}=out_i+step outi+1=outi+step

torch.range(start=0, end, step=1, *, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False)
# → Tensor
  • start(float) -点集的起始值。默认值:0。
  • end(float) -点集的结束值。 step(float)
  • 每对相邻点之间的间隙。默认值:1。

关键字参数:

    out(Tensor,可选的) -输出张量。
    dtype(torch.dtype, 可选的) -返回张量的所需数据类型。默认值:如果 None ,使用全局默认值(参见 torch.set_default_tensor_type() )。如果未给出 dtype,则从其他输入参数推断数据类型。如果 start 、 end 或 stop 中的任何一个是浮点数,则 dtype 被推断为默认 dtype,请参阅 get_default_dtype() 。否则, dtype 被推断为 torch.int64 。
    layout(torch.layout, 可选的) -返回张量的所需布局。默认值:torch.strided。
    device(torch.device, 可选的) -返回张量的所需设备。默认值:如果 None ,使用当前设备作为默认张量类型(参见 torch.set_default_tensor_type() )。 device 将是 CPU 张量类型的 CPU 和 CUDA 张量类型的当前 CUDA 设备。
    requires_grad(bool,可选的) -如果 autograd 应该在返回的张量上记录操作。默认值:False。
>>> torch.range(1, 4)
tensor([ 1.,  2.,  3.,  4.])
>>> torch.range(1, 4, 0.5)
tensor([ 1.0000,  1.5000,  2.0000,  2.5000,  3.0000,  3.5000,  4.0000])

torch.arange 与 torch.range一样:Torch.arange函数详解

24.new_tensor()

返回一个以 data 作为张量数据的新张量。默认情况下,返回的张量与该张量具有相同的torch.dtype 和torch.device。

Tensor.new_tensor(data, dtype=None, device=None, requires_grad=False) → Tensor

参数:

    data(array_like) -返回的张量副本 data 。
    dtype(torch.dtype, 可选的) -返回张量的所需类型。默认值:如果没有,则与此张量相同 torch.dtype。
    device(torch.device, 可选的) -返回张量的所需设备。默认值:如果没有,则与此张量相同 torch.device。
    requires_grad(bool,可选的) -如果 autograd 应该在返回的张量上记录操作。默认值:False。

25.meshgrid()

如果输入具有大小为 S 0 , . . . , S N − 1 S_0,...,S_{N-1} S0,...,SN1 N N N 张量,则输出也将具有 N N N 张量,其中每个张量的形状为 ( S 0 , . . . , S N − 1 ) (S_0,...,S_{N-1}) (S0,...,SN1)

torch.meshgrid(*tensors, indexing=None)

参数:

    tensors(张量列表) -标量或一维张量列表。标量将自动被视为大小为 

的张量

indexing-

(str,可选):索引模式,“xy” 或“ij”,默认为“ij”。请参阅警告以了解未来的变化。

如果选择“xy”,则第一个维度对应于第二个输入的基数,第二个维度对应于第一个输入的基数。

如果选择“ij”,则维度的顺序与输入的基数相同。
>>> x = torch.tensor([1, 2, 3])
>>> y = torch.tensor([4, 5, 6])
>>> grid_x, grid_y = torch.meshgrid(x, y, indexing='ij')
>>> grid_x
tensor([[1, 1, 1],
        [2, 2, 2],
        [3, 3, 3]])
>>> grid_y
tensor([[4, 5, 6],
        [4, 5, 6],
        [4, 5, 6]])

26.int()

int( )函数能将str类型、float类型的数据转换成整数类型。
int( )函数的本质是将数据转换为整数。
对于浮点数,int( )函数会做取整处理。取整就是直接抹零,直接输出整数部分。

27.Conv1d()

class torch.nn.Conv1d(in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True)`
in_channels(int) – 输入信号的通道。在文本分类中,即为词向量的维度
out_channels(int) – 卷积产生的通道。有多少个out_channels,就需要多少个1维卷积
kernel_size(int or tuple) - 卷积核的尺寸,卷积核的大小为(k,),第二个维度是由in_channels来决定的,所以实际上卷积大小为kernel_size*in_channels
stride(int or tuple, optional) - 卷积步长
padding (int or tuple, optional)- 输入的每一条边补充0的层数
dilation(int or tuple, `optional``) – 卷积核元素之间的间距
groups(int, optional) – 从输入通道到输出通道的阻塞连接数
bias(bool, optional) - 如果bias=True,添加偏置

Conv1D 详解

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

飞大圣

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值