移动零 【in_place的理解】

给一个数组 nums 写一个函数将 0 移动到数组的最后面,非零元素保持原数组的顺序
注意事项

1.必须在原数组上操作
2.最小化操作数

样例

给出 nums = [0, 1, 0, 3, 12], 调用函数之后, nums = [1, 3, 12, 0, 0].

解答

对于这道题我最开始用的方法很快速。。就是用Python先筛一圈不为零的,然后再筛一圈为零的【个数】,添加在后头。不过报错了。后来想明白报错的原因是因为要求“inplace”操作。不用返回值。

inplace的方法只能是就地转换。我想到的办法是两个指针,第一个指针找“0”,第二个指针找0之后出现的第一个“不为零”的数,然后他们俩调换。
代码如下

class Solution:
    # @param {int[]} nums an integer array
    # @return nothing, do this in-place
    def moveZeroes(self, nums):
        # Write your code here
        i, j = 0, 0
        while i < len(nums):
            if nums[i] == 0:
                j = i + 1
                while j < len(nums):
                    if nums[j] != 0:
                        temp = nums[i]
                        nums[i] = nums[j]
                        nums[j] = temp
                        break
                    j += 1
                if j >= len(nums):
                    break
            i += 1
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这段代码看起来已经很完整了,但有一些改进可以提出。 首先,由于我们只需要使用 VGG16 的特征提取部分作为 FCN 的 backbone,而不需要其完整的网络结构,因此可以使用 `vgg16.features` 而不是 `vgg16` 来加载预训练模型。这样可以减少模型的参数量。 其次,为了更好地利用 GPU 的性能,可以将输入数据和模型移动到 GPU 上进行计算。可以使用 `to()` 方法将数据和模型移动到 GPU 上,如 `model.to(device)` 和 `a.to(device)`,其中 `device` 是你希望在其上运行代码的设备(如 `'cuda'`)。 最后,你可以考虑添加一些必要的注释来解释代码的功能和每个部分的作用。这有助于其他人更好地理解你的代码。 下面是改进后的代码: ```python import torch import torch.nn as nn import torchvision.models as models class FCNTransformer(nn.Module): def __init__(self, num_classes): super(FCNTransformer, self).__init__() # Load pre-trained V16 model as FCN backbone vgg16 = models.vgg16(pretrained=True) features = list(vgg16.features.children()) self.backbone = nn.Sequential(*features) # FCN layers self.fcn_layers = nn.Sequential( nn.Conv2d(512, 4096, 7), nn.ReLU(inplace=True), nn.Dropout(), nn.Conv2d(4096, 4096, 1), nn.ReLU(inplace=True), nn.Dropout(), nn.Conv2d(4096, num_classes, 1) ) # Transformer layers self.transformer = nn.Transformer( d_model=512, nhead=8, num_encoder_layers=6, num_decoder_layers=6, dim_feedforward=2048, dropout=0.1 ) def forward(self, x): # Backbone feature extraction features = self.backbone(x) # FCN layers fcn_out = self.fcn_layers(features) # Reshaping output for transformer input b, c, h, w = fcn_out.size() fcn_out = fcn_out.squeeze().view(c, b, -1).permute(2, 0, 1) # Reshaping for transformer input # Transformer encoding transformer_out = self.transformer.encode(fcn_out) # Reshaping output for segmentation prediction transformer_out = transformer_out.permute(1, 2, 0).view(b, c, h, w) return transformer_out if __name__ == '__main__': device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') a = torch.randn(1, 3, 512, 512).to(device) model = FCNTransformer(num_classes=2).to(device) print(model(a).shape) ``` 这样,你的代码就更完善了,同时也考虑了在 GPU 上进行计算的情况。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值