def kaiming_uniform_(tensor, a=0, mode='fan_in', nonlinearity='leaky_relu'): fan = _calculate_correct_fan(tensor, mode) gain = calculate_gain(nonlinearity, a) std = gain / math.sqrt(fan) bound = math.sqrt(3.0) * std # Calculate uniform bounds from standard deviation with no_grad(): return tensor.uniform_(-bound, bound)
def _type_to(input, dtype='float32', inplace=False): if dtype == input._dtype: return input ctx = MakeContext(inputs=[input]) key = 'torch/ops/astype/{}:{}/dtype:{}/inplace:{}'.format( ctx[0].lower(), ctx[1], dtype, 'true' if inplace else 'false') module = get_module(AsType, key, ctx, dtype=dtype, inplace=inplace) with no_grad(): return module.forward(input)
def dirac_(tensor): dimensions = tensor.ndimension() if dimensions not in [3, 4, 5]: raise ValueError("Only tensors with 3, 4, or 5 dimensions are supported") sizes = tensor.size() min_dim = min(sizes[0], sizes[1]) with no_grad(): tensor.zero_() for d in range(min_dim): if dimensions == 3: # Temporal convolution tensor[d, d, tensor.size(2) // 2] = 1 elif dimensions == 4: # Spatial convolution tensor[d, d, tensor.size(2) // 2, tensor.size(3) // 2] = 1 else: # Volumetric convolution tensor[d, d, tensor.size(2) // 2, tensor.size(3) // 2, tensor.size(4) // 2] = 1 return tensor
def constant_(tensor, val): with no_grad(): return tensor.fill_(val)
def normal_(tensor, mean=0, std=1): with no_grad(): return tensor.normal_(mean, std)
def uniform_(tensor, a=0, b=1): with no_grad(): return tensor.uniform_(a, b)
def kaiming_normal_(tensor, a=0, mode='fan_in', nonlinearity='leaky_relu'): fan = _calculate_correct_fan(tensor, mode) gain = calculate_gain(nonlinearity, a) std = gain / math.sqrt(fan) with no_grad(): return tensor.normal_(0, std)
def xavier_normal_(tensor, gain=1): fan_in, fan_out = _calculate_fan_in_and_fan_out(tensor) std = gain * math.sqrt(2.0 / (fan_in + fan_out)) with no_grad(): return tensor.normal_(0, std)
def xavier_uniform_(tensor, gain=1): fan_in, fan_out = _calculate_fan_in_and_fan_out(tensor) std = gain * math.sqrt(2.0 / (fan_in + fan_out)) a = math.sqrt(3.0) * std # Calculate uniform bounds from standard deviation with no_grad(): return tensor.uniform_(-a, a)