(valuedict, name, data)
return v
Converts `valuedict[name]` to be a tensor with the same dtype, device,
    and dimension count as `data`, and caches the converted tensor.
Converts `valuedict[name]` to be a tensor with the same dtype, device,
    and dimension count as `data`, and caches the converted tensor.
Converts valuedict [ name ] to be a tensor with the same dtype device and dimension count as data and caches the converted tensor .
def make_matching_tensor(valuedict, name, data):
    '''
    Converts `valuedict[name]` to be a tensor with the same dtype, device,
    and dimension count as `data`, and caches the converted tensor.
    '''
    v = valuedict.get(name, None)
    if v is None:
        return None
    if not isinstance(v, torch.Tensor):
        # Accept non-torch data.
        v = torch.from_numpy(numpy.array(v))
        valuedict[name] = v
    if not v.device == data.device or not v.dtype == data.dtype:
        # Ensure device and type matches.
        assert not v.requires_grad, '%s wrong device or type' % (name)
        v = v.to(device=data.device, dtype=data.dtype)
        valuedict[name] = v
    if len(v.shape) < len(data.shape):
        # Ensure dimensions are unsqueezed as needed.
        assert not v.requires_grad, '%s wrong dimensions' % (name)
        v = v.view((1,) + tuple(v.shape) +
                (1,) * (len(data.shape) - len(v.shape) - 1))
        valuedict[name] = v
    return v
def make_matching_tensor ( valuedict , name , data ) : v = valuedict . get ( name , None ) if v is None : return None if not isinstance ( v , torch . Tensor ) : # Accept non-torch data. v = torch . from_numpy ( numpy . array ( v ) ) valuedict [ name ] = v if not v . device == data . device or not v . dtype == data . dtype : # Ensure device and type matches. assert not v . requires_grad , '%s wrong device or type' % ( name ) v = v . to ( device = data . device , dtype = data . dtype ) valuedict [ name ] = v if len ( v . shape ) < len ( data . shape ) : # Ensure dimensions are unsqueezed as needed. assert not v . requires_grad , '%s wrong dimensions' % ( name ) v = v . view ( ( 1 , ) + tuple ( v . shape ) + ( 1 , ) * ( len ( data . shape ) - len ( v . shape ) - 1 ) ) valuedict [ name ] = v return v