当前位置: 代码网 > it编程>前端脚本>Python > 神经网络中遇到的 python 函数(Pytorch)

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

2024年07月31日 Python 我要评论
学习笔记

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()

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数据;

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)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com