我们从Python开源项目中,提取了以下14个代码示例,用于说明如何使用torch.HalfTensor()。
def test_print(self): for t in torch._tensor_classes: if t == torch.HalfTensor: continue # HalfTensor does not support fill if t in torch.sparse._sparse_tensor_classes: continue if t.is_cuda and not torch.cuda.is_available(): continue obj = t(100, 100).fill_(1) obj.__repr__() str(obj) for t in torch._storage_classes: if t.is_cuda and not torch.cuda.is_available(): continue obj = t(100).fill_(1) obj.__repr__() str(obj) x = torch.Tensor([4, float('inf'), 1.5, float('-inf'), 0, float('nan'), 1]) x.__repr__() str(x)
def is_tensor(object_): missed_tensor_classes = {torch.HalfTensor} return torch.is_tensor(object_) or type(object_) in missed_tensor_classes
def test_numpy_scalars(self): import numpy as np class ScalarDataset(torch.utils.data.Dataset): def __init__(self, dtype): self.dtype = dtype def __getitem__(self, i): return self.dtype() def __len__(self): return 4 dtypes = { np.float64: torch.DoubleTensor, np.float32: torch.FloatTensor, np.float16: torch.HalfTensor, np.int64: torch.LongTensor, np.int32: torch.IntTensor, np.int16: torch.ShortTensor, np.int8: torch.CharTensor, np.uint8: torch.ByteTensor, } for dt, tt in dtypes.items(): dset = ScalarDataset(dt) loader = DataLoader(dset, batch_size=2) batch = next(iter(loader)) self.assertIsInstance(batch, tt)
def _graph_constant(g, value, dims, type, *args, **kwargs): assert isinstance(value, numbers.Number) assert type is not None isscalar = False if dims is None or dims == 0 or set(dims) == set([0]): dims = [1] isscalar = True type = type.lower() if type == "char": tensor = torch.CharTensor(*dims) elif type == "short": tensor = torch.ShortTensor(*dims) elif type == "int": tensor = torch.IntTensor(*dims) elif type == "long": tensor = torch.LongTensor(*dims) elif type == "half": tensor = torch.HalfTensor(*dims) elif type == "float": tensor = torch.FloatTensor(*dims) elif type == "double": tensor = torch.DoubleTensor(*dims) else: raise ValueError("Unknown type, type should be one of the following strings: " "char, short, int, long, half, float, double") tensor.fill_(value) if isscalar: return g.op("Constant", *args, value_z=tensor, **kwargs) return g.op("Constant", *args, value_t=tensor, **kwargs)
def compare_cpu_gpu(tensor_constructor, arg_constructor, fn, t, precision=1e-5, force_gpu_half=False): def tmp(self): cpu_tensor = tensor_constructor(t) type_map = {} if force_gpu_half: type_map = { 'torch.FloatTensor': 'torch.cuda.HalfTensor', 'torch.DoubleTensor': 'torch.cuda.HalfTensor', } gpu_tensor = to_gpu(cpu_tensor, type_map) cpu_args = arg_constructor(t) gpu_args = [to_gpu(arg, type_map) for arg in cpu_args] cpu_result = getattr(cpu_tensor, fn)(*cpu_args) try: gpu_result = getattr(gpu_tensor, fn)(*gpu_args) except RuntimeError as e: reason = e.args[0] if 'only supports floating-point types' in reason or 'unimplemented data type' in reason: raise unittest.SkipTest('unimplemented data type') raise except AttributeError as e: reason = e.args[0] if 'object has no attribute' in reason: raise unittest.SkipTest('unimplemented data type') raise # If one changes, another should change as well self.assertEqual(cpu_tensor, gpu_tensor, precision) self.assertEqual(cpu_args, gpu_args, precision) # Compare results self.assertEqual(cpu_result, gpu_result, precision) return tmp
def test_is_tensor(self): for t in types: tensor = get_gpu_type(t)() self.assertTrue(torch.is_tensor(tensor)) self.assertTrue(torch.is_tensor(torch.cuda.HalfTensor()))
def test_is_signed(self): # TODO: remove the Variable wrapper once we merge Variable and Tensor from torch.autograd import Variable self.assertEqual(Variable(torch.IntTensor(5)).is_signed(), True) self.assertEqual(Variable(torch.ByteTensor(5)).is_signed(), False) self.assertEqual(Variable(torch.FloatTensor(5)).is_signed(), True) self.assertEqual(Variable(torch.HalfTensor(10)).is_signed(), True)
def test_print(self): for t in torch._tensor_classes: if IS_WINDOWS and t in [torch.cuda.sparse.HalfTensor, torch.cuda.HalfTensor]: return # CUDA HalfTensor is not supported on Windows yet if t == torch.HalfTensor: continue # HalfTensor does not support fill if t in torch.sparse._sparse_tensor_classes: continue if t.is_cuda and not torch.cuda.is_available(): continue obj = t(100, 100).fill_(1) obj.__repr__() str(obj) for t in torch._storage_classes: if t.is_cuda and not torch.cuda.is_available(): continue obj = t(100).fill_(1) obj.__repr__() str(obj) x = torch.Tensor([4, float('inf'), 1.5, float('-inf'), 0, float('nan'), 1]) x.__repr__() str(x) x = torch.DoubleTensor([1e-324, 1e-323, 1e-322, 1e307, 1e308, 1e309]) x.__repr__() str(x),
def _worker_loop(dataset, index_queue, data_queue, collate_fn): global _use_shared_memory _use_shared_memory = True # torch.set_num_threads(1) while True: r = index_queue.get() if r is None: data_queue.put(None) break idx, batch_indices = r try: samples = collate_fn([dataset[i] for i in batch_indices]) except Exception: data_queue.put((idx, ExceptionWrapper(sys.exc_info()))) else: data_queue.put((idx, samples)) # numpy_type_map = { # 'float64': torch.DoubleTensor, # 'float32': torch.FloatTensor, # 'float16': torch.HalfTensor, # 'int64': torch.LongTensor, # 'int32': torch.IntTensor, # 'int16': torch.ShortTensor, # 'int8': torch.CharTensor, # 'uint8': torch.ByteTensor, # }
def test_from_numpy(self): dtypes = [ np.double, np.float, np.float16, np.int64, np.int32, np.int16, np.uint8 ] for dtype in dtypes: array = np.array([1, 2, 3, 4], dtype=dtype) tensor_from_array = torch.from_numpy(array) # TODO: change to tensor equality check once HalfTensor # implements `==` for i in range(len(array)): self.assertEqual(tensor_from_array[i], array[i]) # check storage offset x = np.linspace(1, 125, 125) x.shape = (5, 5, 5) x = x[1] expected = torch.arange(1, 126).view(5, 5, 5)[1] self.assertEqual(torch.from_numpy(x), expected) # check noncontiguous x = np.linspace(1, 25, 25) x.shape = (5, 5) expected = torch.arange(1, 26).view(5, 5).t() self.assertEqual(torch.from_numpy(x.T), expected) # check noncontiguous with holes x = np.linspace(1, 125, 125) x.shape = (5, 5, 5) x = x[:, 1] expected = torch.arange(1, 126).view(5, 5, 5)[:, 1] self.assertEqual(torch.from_numpy(x), expected) # check zero dimensional x = np.zeros((0, 2)) self.assertEqual(torch.from_numpy(x).shape, tuple()) self.assertEqual(torch.autograd.Variable.from_numpy(x).shape, [0])
def test_ctor_with_numpy_array(self): dtypes = [ np.double, np.float, np.float16, np.int64, np.int32, np.int16, np.uint8 ] for dtype in dtypes: array = np.array([1, 2, 3, 4], dtype=dtype) # Upcast tensor = torch.DoubleTensor(array) for i in range(len(array)): self.assertEqual(tensor[i], array[i]) if torch.cuda.is_available(): tensor = torch.cuda.DoubleTensor(array) for i in range(len(array)): self.assertEqual(tensor[i], array[i]) # Downcast (sometimes) tensor = torch.FloatTensor(array) for i in range(len(array)): self.assertEqual(tensor[i], array[i]) tensor = torch.HalfTensor(array) for i in range(len(array)): self.assertEqual(tensor[i], array[i]) if torch.cuda.is_available(): tensor = torch.cuda.FloatTensor(array) for i in range(len(array)): self.assertEqual(tensor[i], array[i]) # CUDA HalfTensor is not supported on Windows yet if not IS_WINDOWS: tensor = torch.cuda.HalfTensor(array) for i in range(len(array)): self.assertEqual(tensor[i], array[i])