应该是变量的 梯度没有 关闭,导致在运行网络的时候,梯度也被储存在了 CUDA 里面。
解决方法是: 在被调用的网络 函数 前面加一个装饰器,(这个函数可以是专门用来eval 的时候用的),
@torch.no_grad() ## 在运行这个函数的时候,不会计算梯度
def get_pos_density(self, positions):
"""Computes and returns the densities."""
assert self.spatial_distortion is not None
positions = self.spatial_distortion(positions)
positions = (positions + 2.0) / 4.0
self._sample_locations = positions
if not self._sample_locations.requires_grad:
self._sample_locations.requires_grad = True
positions_flat = positions.view(-1, 3)
h = self.mlp_base(positions_flat)
density_before_activation, base_mlp_out = torch.split(h, [1, self.geo_feat_dim], dim=-1)
self._density_before_activation = density_before_activation
# Rectifying the density with an exponential is much more stable than a ReLU or
# softplus, because it enables high post-activation (float32) density outputs
# from smaller internal (float16) parameters.
density = trunc_exp(density_before_activation.to(positions))[:, 0]
# print(base_mlp_out)
return density, base_mlp_out