pytorch中的插值算法函数模块--interpolate

官方函数说明
torch.nn.functional.interpolate(input, size=None, scale_factor=None, mode='nearest', align_corners=None, recompute_scale_factor=None)
  1. 根据给定的size或者scale_factor(放缩因子)下采样/上采样 输入
  2. mode指定使用的插值算法
  3. 支持输入为时序、空间或者三维立体等输入,输入应该是3-D、4-D、5-D
  4. 输入维度格式应该是: mini-batch x channels x [optional depth] x [optional height] x width.
  5. mode可以使用的算法包括:nearest, linear (3D-only), bilinear, bicubic (4D-only), trilinear (5D-only), area
参数说明
  • input (Tensor) – the input tensor

  • size (int or Tuple[int] or Tuple[int, int] or Tuple[int, int, int]) – output spatial size.
    选择输出的size大小与scale_factor参数功能类似只能选择其中一个使用

  • scale_factor (float or Tuple[float]) – multiplier for spatial size. If scale_factor is a tuple, its length has to match input.dim().
    放缩因子,比如 设置为2,输入维度为2X2,则输出为4X4

  • mode (str) – algorithm used for upsampling: ‘nearest’ | ‘linear’ | ‘bilinear’ | ‘bicubic’ | ‘trilinear’ | ‘area’. Default: ‘nearest’
    选择插值算法

  • align_corners (bool, optional) – Geometrically, we consider the pixels of the input and output as squares rather than points. If set to True, the input and output tensors are aligned by the center points of their corner pixels, preserving the values at the corner pixels. If set to False, the input and output tensors are aligned by the corner points of their corner pixels, and the interpolation uses edge value padding for out-of-boundary values, making this operation independent of input size when scale_factor is kept the same. This only has an effect when mode is ‘linear’, ‘bilinear’, ‘bicubic’ or ‘trilinear’. Default: False
    对齐角–在几何上,我们考虑输入和输出的像素是正方形而不是点。如果设置为True,输入和输出张量将按其角像素的中心点对齐,从而保留角像素处的值。如果设置为False,则输入和输出张量将通过其角像素的角点对齐,插值使用边界外值的边值填充,当比例因子保持不变时,此操作与输入大小无关。这仅在mode为“线性”、“双线性”、“双三次”或“三线性”时有效。默认值:False

  • recompute_scale_factor (bool, optional) – recompute the scale_factor for use in the interpolation calculation. If recompute_scale_factor is True, then scale_factor must be passed in and scale_factor is used to compute the output size. The computed output size will be used to infer new scales for the interpolation. Note that when scale_factor is floating-point, it may differ from the recomputed scale_factor due to rounding and precision issues. If recomputed_scale_factor is False, then size or scale_factor will be used directly for interpolation.
    如果“重新计算比例因子”为True,则必须传入放缩因子,并使用放缩因子计算输出大小。计算出的输出大小将用于推断插值的新比例。请注意,当scale_factor为浮点时,由于舍入和精度问题,它可能与重新计算的scale_factor不同。如果重新计算的比例因子为假,则大小或比例因子将直接用于插值。

example

code 1


import torch 
from torch.nn import functional as F 
input = torch.arange(1,5,dtype=torch.float32).view(1,1,2,2)
print(input)
out1=F.interpolate(input ,scale_factor=3,mode="nearest")
print(out1)

输出:
在这里插入图片描述

code2:

out3 = F.interpolate(input,scale_factor=2,mode="bilinear",align_corners=False)
out4=F.interpolate(input,size=[4,4],mode="bilinear",align_corners=False)
print(out3,"\n",out3==out4)

out:
可以发现scale因子与size都是用来指定输出大小
在这里插入图片描述

  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
pytorch-cnn-股票预测是一个使用PyTorch框架实现的卷积神经网络模型,用于预测股票价格走势的源代码。 这个项目的目的是通过训练一个卷积神经网络模型,来预测股票价格的未来走势。卷积神经网络是一种深度学习模型,通过自动提取特征并学习数据之间的非线性关系来进行预测。 在这个源码,首先进行数据的预处理。通常会使用历史股票价格的时间序列作为输入数据,以及对应的目标值作为标签。数据预处理的操作可能包括数据标准化、缺失值填充等等。 接下来,我们构建卷积神经网络模型。在源码,可能会使用PyTorch提供的卷积层(Convolutional Layer)、池化层(Pooling Layer)和全连接层(Fully Connected Layer)等组件来构建模型,以及使用ReLU、Dropout等激活函数和正则化方法来提高模型的性能。 然后,我们定义损失函数,通常选择均方误差(Mean Squared Error)或交叉熵损失(Cross Entropy Loss)作为模型训练的目标函数,以便优化模型的预测结果。 最后,我们使用训练数据来训练模型,并使用测试数据来评估模型的性能。在源码,我们会定义训练函数和测试函数,通过迭代训练,不断调整模型参数,以使得模型能够更准确地预测未知数据的输出结果。 总结来说,pytorch-cnn-股票预测-源码是一个基于PyTorch框架实现的卷积神经网络模型,用于预测股票价格走势的源代码。通过数据预处理、构建网络模型、定义损失函数和训练评估等步骤,可以使用这个代码来进行股票预测模型的训练和测试。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值