Python torch.set_default_tensor_type() Examples
The following are 30
code examples of torch.set_default_tensor_type().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
torch
, or try the search function
.
Example #1
Source File: plane.py From nsf with MIT License | 7 votes |
def _test(): device = torch.device('cuda') torch.set_default_tensor_type('torch.cuda.FloatTensor') dataset = DiamondDataset(num_points=int(1e6), width=20, bound=2.5, std=0.04) from utils import torchutils from matplotlib import pyplot as plt data = torchutils.tensor2numpy(dataset.data) fig, ax = plt.subplots(1, 1, figsize=(5, 5)) # ax.scatter(data[:, 0], data[:, 1], s=2, alpha=0.5) bound = 4 bounds = [[-bound, bound], [-bound, bound]] # bounds = [ # [0, 1], # [0, 1] # ] ax.hist2d(data[:, 0], data[:, 1], bins=256, range=bounds) ax.set_xlim(bounds[0]) ax.set_ylim(bounds[1]) plt.show()
Example #2
Source File: images.py From nsf with MIT License | 6 votes |
def set_device(use_gpu, multi_gpu, _log): # Decide which device to use. if use_gpu and not torch.cuda.is_available(): raise RuntimeError('use_gpu is True but CUDA is not available') if use_gpu: device = torch.device('cuda') torch.set_default_tensor_type('torch.cuda.FloatTensor') else: device = torch.device('cpu') if multi_gpu and torch.cuda.device_count() == 1: raise RuntimeError('Multiple GPU training requested, but only one GPU is available.') if multi_gpu: _log.info('Using all {} GPUs available'.format(torch.cuda.device_count())) return device
Example #3
Source File: base_trainer.py From vae-audio with MIT License | 6 votes |
def _prepare_device(self, n_gpu_use): """ setup GPU device if available, move model into configured device """ n_gpu = torch.cuda.device_count() if n_gpu_use > 0 and n_gpu == 0: self.logger.warning("Warning: There\'s no GPU available on this machine," "training will be performed on CPU.") n_gpu_use = 0 if n_gpu_use > n_gpu: self.logger.warning("Warning: The number of GPU\'s configured to use is {}, but only {} are available " "on this machine.".format(n_gpu_use, n_gpu)) n_gpu_use = n_gpu device = torch.device('cuda:0' if n_gpu_use > 0 else 'cpu') if device.type == 'cuda': torch.set_default_tensor_type('torch.cuda.FloatTensor') list_ids = list(range(n_gpu_use)) return device, list_ids
Example #4
Source File: core.py From D4LCN with MIT License | 6 votes |
def init_torch(rng_seed, cuda_seed): """ Initializes the seeds for ALL potential randomness, including torch, numpy, and random packages. Args: rng_seed (int): the shared random seed to use for numpy and random cuda_seed (int): the random seed to use for pytorch's torch.cuda.manual_seed_all function """ # default tensor torch.set_default_tensor_type('torch.cuda.FloatTensor') # seed everything torch.manual_seed(rng_seed) np.random.seed(rng_seed) random.seed(rng_seed) torch.cuda.manual_seed_all(cuda_seed) # make the code deterministic torch.backends.cudnn.deterministic = True torch.backends.cudnn.benchmark = False
Example #5
Source File: eye.py From pytorch_sparse with MIT License | 6 votes |
def eye(m, dtype=None, device=None): """Returns a sparse matrix with ones on the diagonal and zeros elsewhere. Args: m (int): The first dimension of corresponding dense matrix. dtype (`torch.dtype`, optional): The desired data type of returned value vector. (default is set by `torch.set_default_tensor_type()`) device (`torch.device`, optional): The desired device of returned tensors. (default is set by `torch.set_default_tensor_type()`) :rtype: (:class:`LongTensor`, :class:`Tensor`) """ row = torch.arange(m, dtype=torch.long, device=device) index = torch.stack([row, row], dim=0) value = torch.ones(m, dtype=dtype, device=device) return index, value
Example #6
Source File: test_rnn.py From apex with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_rnn_packed_sequence(self): num_layers = 2 rnn = nn.RNN(input_size=self.h, hidden_size=self.h, num_layers=num_layers) for typ in [torch.float, torch.half]: x = torch.randn((self.t, self.b, self.h), dtype=typ).requires_grad_() lens = sorted([random.randint(self.t // 2, self.t) for _ in range(self.b)], reverse=True) # `pack_padded_sequence` breaks if default tensor type is non-CPU torch.set_default_tensor_type(torch.FloatTensor) lens = torch.tensor(lens, dtype=torch.int64, device=torch.device('cpu')) packed_seq = nn.utils.rnn.pack_padded_sequence(x, lens) torch.set_default_tensor_type(torch.cuda.FloatTensor) hidden = torch.zeros((num_layers, self.b, self.h), dtype=typ) output, _ = rnn(packed_seq, hidden) self.assertEqual(output.data.type(), HALF) output.data.float().sum().backward() self.assertEqual(x.grad.dtype, x.dtype)
Example #7
Source File: test_fused.py From sparse-structured-attention with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_jv(alpha): torch.manual_seed(1) torch.set_default_tensor_type('torch.DoubleTensor') for _ in range(30): x = Variable(torch.randn(15)) dout = torch.randn(15) y_hat = FusedProxFunction(alpha=alpha)(x).data ref = _fused_prox_jacobian(y_hat, dout) din_slow = fused_prox_jv_slow(y_hat, dout) din_fast = fused_prox_jv_fast(y_hat, dout) assert_allclose(ref.numpy(), din_slow.numpy(), atol=1e-5) assert_allclose(ref.numpy(), din_fast.numpy(), atol=1e-5)
Example #8
Source File: benchmark.py From bindsnet with GNU Affero General Public License v3.0 | 6 votes |
def BindsNET_gpu(n_neurons, time): if torch.cuda.is_available(): t0 = t() torch.set_default_tensor_type("torch.cuda.FloatTensor") t1 = t() network = Network() network.add_layer(Input(n=n_neurons), name="X") network.add_layer(LIFNodes(n=n_neurons), name="Y") network.add_connection( Connection(source=network.layers["X"], target=network.layers["Y"]), source="X", target="Y", ) data = {"X": poisson(datum=torch.rand(n_neurons), time=time)} network.run(inputs=data, time=time) return t() - t0, t() - t1
Example #9
Source File: pytorch_executor.py From rlgraph with Apache License 2.0 | 6 votes |
def __init__(self, **kwargs): super(PyTorchExecutor, self).__init__(**kwargs) self.global_training_timestep = 0 self.cuda_enabled = torch.cuda.is_available() # In PyTorch, tensors are default created on the CPU unless assigned to a visible CUDA device, # e.g. via x = tensor([0, 0], device="cuda:0") for the first GPU. self.available_devices = os.environ.get("CUDA_VISIBLE_DEVICES") # TODO handle cuda tensors self.default_torch_tensor_type = self.execution_spec.get("dtype", "torch.FloatTensor") if self.default_torch_tensor_type is not None: torch.set_default_tensor_type(self.default_torch_tensor_type) self.torch_num_threads = self.execution_spec.get("torch_num_threads", 1) self.omp_num_threads = self.execution_spec.get("OMP_NUM_THREADS", 1) # Squeeze result dims, often necessary in tests. self.remove_batch_dims = True
Example #10
Source File: benchmark.py From bindsnet with GNU Affero General Public License v3.0 | 6 votes |
def BindsNET_cpu(n_neurons, time): t0 = t() torch.set_default_tensor_type("torch.FloatTensor") t1 = t() network = Network() network.add_layer(Input(n=n_neurons), name="X") network.add_layer(LIFNodes(n=n_neurons), name="Y") network.add_connection( Connection(source=network.layers["X"], target=network.layers["Y"]), source="X", target="Y", ) data = {"X": poisson(datum=torch.rand(n_neurons), time=time)} network.run(inputs=data, time=time) return t() - t0, t() - t1
Example #11
Source File: model.py From MLDG with MIT License | 6 votes |
def __init__(self, flags): torch.set_default_tensor_type('torch.cuda.FloatTensor') # fix the random seed or not fix_seed() self.setup_path(flags) self.network = mlp.MLPNet(num_classes=flags.num_classes) self.network = self.network.cuda() print(self.network) print('flags:', flags) if not os.path.exists(flags.logs): os.mkdir(flags.logs) flags_log = os.path.join(flags.logs, 'flags_log.txt') write_log(flags, flags_log) self.load_state_dict(flags.state_dict) self.configure(flags)
Example #12
Source File: option.py From TextSnake.pytorch with MIT License | 6 votes |
def initialize(self, fixed=None): # Parse options self.args = self.parse(fixed) # Setting default torch Tensor type if self.args.cuda and torch.cuda.is_available(): torch.set_default_tensor_type('torch.cuda.FloatTensor') cudnn.benchmark = True else: torch.set_default_tensor_type('torch.FloatTensor') # Create weights saving directory if not os.path.exists(self.args.save_dir): os.mkdir(self.args.save_dir) # Create weights saving directory of target model model_save_path = os.path.join(self.args.save_dir, self.args.exp_name) if not os.path.exists(model_save_path): os.mkdir(model_save_path) return self.args
Example #13
Source File: core.py From M3D-RPN with MIT License | 6 votes |
def init_torch(rng_seed, cuda_seed): """ Initializes the seeds for ALL potential randomness, including torch, numpy, and random packages. Args: rng_seed (int): the shared random seed to use for numpy and random cuda_seed (int): the random seed to use for pytorch's torch.cuda.manual_seed_all function """ # default tensor torch.set_default_tensor_type('torch.cuda.FloatTensor') # seed everything torch.manual_seed(rng_seed) np.random.seed(rng_seed) random.seed(rng_seed) torch.cuda.manual_seed_all(cuda_seed) # make the code deterministic torch.backends.cudnn.deterministic = True torch.backends.cudnn.benchmark = False
Example #14
Source File: base_train_box.py From sanet_relocal_demo with GNU General Public License v3.0 | 5 votes |
def _set_dev_id(self, id: int): if not torch.cuda.is_available(): raise Exception("No CUDA device founded.") self.dev_id = id torch.set_default_tensor_type('torch.cuda.FloatTensor') torch.cuda.set_device(id)
Example #15
Source File: predict.py From pytorch-asr with GNU General Public License v3.0 | 5 votes |
def predict(argv): parser = argparse.ArgumentParser(description="DenseNet prediction") parser.add_argument('--decode', default=False, action='store_true', help="retrieve Kaldi's latgen decoder") parser.add_argument('--use-cuda', default=False, action='store_true', help="use cuda") parser.add_argument('--log-dir', default='./logs', type=str, help="filename for logging the outputs") parser.add_argument('--continue-from', type=str, help="model file path to make continued from") parser.add_argument('wav_files', type=str, nargs='+', help="list of wav_files for prediction") args = parser.parse_args(argv) print(f"begins logging to file: {str(Path(args.log_dir).resolve() / 'predict.log')}") set_logfile(Path(args.log_dir, "predict.log")) logger.info(f"PyTorch version: {torch.__version__}") logger.info(f"Prediction started with command: {' '.join(sys.argv)}") args_str = [f"{k}={v}" for (k, v) in vars(args).items()] logger.info(f"args: {' '.join(args_str)}") if args.use_cuda: logger.info("using cuda") torch.set_default_tensor_type("torch.cuda.FloatTensor") if args.continue_from is None: logger.error("model name is missing: add '--continue-from <model-name>' in options") #parser.print_help() sys.exit(1) # run prediction predict = Predict(args) if args.decode: predict.decode(args.wav_files) else: predict.predict(args.wav_files, verbose=True)
Example #16
Source File: test_train_mp_mnist.py From ru_transformers with Apache License 2.0 | 5 votes |
def _mp_fn(index, flags): global FLAGS FLAGS = flags torch.set_default_tensor_type('torch.FloatTensor') accuracy = train_mnist() if FLAGS.tidy and os.path.isdir(FLAGS.datadir): shutil.rmtree(FLAGS.datadir) if accuracy < FLAGS.target_accuracy: print('Accuracy {} is below target {}'.format(accuracy, FLAGS.target_accuracy)) sys.exit(21)
Example #17
Source File: utils.py From tatk with Apache License 2.0 | 5 votes |
def use_cuda(enabled, device_id=0): """Verifies if CUDA is available and sets default device to be device_id.""" if not enabled: return None assert torch.cuda.is_available(), 'CUDA is not available' torch.set_default_tensor_type('torch.cuda.FloatTensor') torch.cuda.set_device(device_id) return device_id
Example #18
Source File: gradient_selector.py From nni with MIT License | 5 votes |
def fit(self, X, y, groups=None): """ Select Features via a gradient based search on (X, y). Parameters ---------- X : array-like Shape = [n_samples, n_features] The training input samples. y : array-like Shape = [n_samples] The target values (class labels in classification, real numbers in regression). groups : array-like Optional, shape = [n_features] Groups of columns that must be selected as a unit e.g. [0, 0, 1, 2] specifies the first two columns are part of a group. """ try: self._fit(X, y, groups=groups) except constants.NanError: if self.verbose: print('Loss was nan, trying with Doubles') torch.set_default_tensor_type(torch.DoubleTensor) self._fit(X, y, groups=groups) return self
Example #19
Source File: utils.py From NeuralDialog-LaRL with Apache License 2.0 | 5 votes |
def use_cuda(enabled, device_id=0): """Verifies if CUDA is available and sets default device to be device_id.""" if not enabled: return None assert torch.cuda.is_available(), 'CUDA is not available' torch.set_default_tensor_type('torch.cuda.FloatTensor') torch.cuda.set_device(device_id) return device_id
Example #20
Source File: utils.py From apex with BSD 3-Clause "New" or "Revised" License | 5 votes |
def common_init(test_case): test_case.h = 64 test_case.b = 16 test_case.c = 16 test_case.k = 3 test_case.t = 10 torch.set_default_tensor_type(torch.cuda.FloatTensor)
Example #21
Source File: test_oscar.py From sparse-structured-attention with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_jv(alpha, beta): torch.manual_seed(1) torch.set_default_tensor_type('torch.DoubleTensor') for _ in range(30): x = Variable(torch.randn(15)) dout = torch.randn(15) y_hat = OscarProxFunction(alpha=alpha, beta=beta)(x).data ref = _oscar_prox_jacobian(y_hat, dout) din = oscar_prox_jv(y_hat, dout) assert_allclose(ref.numpy(), din.numpy(), atol=1e-5)
Example #22
Source File: test_oscar.py From sparse-structured-attention with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_finite_diff(alpha, beta): torch.manual_seed(1) torch.set_default_tensor_type('torch.DoubleTensor') for _ in range(30): x = Variable(torch.randn(20), requires_grad=True) func = OscarProxFunction(alpha, beta=beta) assert gradcheck(func, (x,), eps=1e-5, atol=1e-3)
Example #23
Source File: test_sparsemax.py From sparse-structured-attention with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_sparsemax(): torch.manual_seed(1) torch.set_default_tensor_type('torch.DoubleTensor') for _ in range(30): func = SparsemaxFunction() x = Variable(torch.randn(20), requires_grad=True) assert gradcheck(func, (x,), eps=1e-4, atol=1e-3)
Example #24
Source File: test_fused.py From sparse-structured-attention with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_finite_diff(alpha): torch.manual_seed(1) torch.set_default_tensor_type('torch.DoubleTensor') for _ in range(30): x = Variable(torch.randn(20), requires_grad=True) func = FusedProxFunction(alpha=alpha) assert gradcheck(func, (x,), eps=1e-4, atol=1e-3)
Example #25
Source File: train.py From pytorch-asr with GNU General Public License v3.0 | 5 votes |
def parse_options(argv): parser = argparse.ArgumentParser(description="First CapsuleNet AM with fully supervised training") # for training parser.add_argument('--data-path', default='data/aspire', type=str, help="dataset path to use in training") parser.add_argument('--num-workers', default=4, type=int, help="number of dataloader workers") parser.add_argument('--num-epochs', default=500, type=int, help="number of epochs to run") parser.add_argument('--batch-size', default=16, type=int, help="number of images (and labels) to be considered in a batch") parser.add_argument('--init-lr', default=0.0001, type=float, help="initial learning rate for Adam optimizer") parser.add_argument('--num-iterations', default=3, type=float, help="number of routing iterations") # optional parser.add_argument('--use-cuda', default=False, action='store_true', help="use cuda") parser.add_argument('--seed', default=None, type=int, help="seed for controlling randomness in this example") parser.add_argument('--log-dir', default='./logs', type=str, help="filename for logging the outputs") parser.add_argument('--model-prefix', default='capsule_aspire', type=str, help="model file prefix to store") parser.add_argument('--continue-from', default=None, type=str, help="model file path to make continued from") args = parser.parse_args(argv) print(f"begins logging to file: {str(Path(args.log_dir).resolve() / 'train.log')}") set_logfile(Path(args.log_dir, "train.log")) logger.info(f"PyTorch version: {torch.__version__}") logger.info(f"Training started with command: {' '.join(sys.argv)}") args_str = [f"{k}={v}" for (k, v) in vars(args).items()] logger.info(f"args: {' '.join(args_str)}") if args.use_cuda: logger.info("using cuda") torch.set_default_tensor_type("torch.cuda.FloatTensor") if args.seed is not None: torch.manual_seed(args.seed) np.random.seed(args.seed) if args.use_cuda: torch.cuda.manual_seed(args.seed) return args
Example #26
Source File: predict.py From pytorch-asr with GNU General Public License v3.0 | 5 votes |
def predict(argv): parser = argparse.ArgumentParser(description="DenseNet prediction") parser.add_argument('--use-cuda', default=False, action='store_true', help="use cuda") parser.add_argument('--log-dir', default='./logs', type=str, help="filename for logging the outputs") parser.add_argument('--continue-from', type=str, help="model file path to make continued from") parser.add_argument('wav_files', type=str, nargs='+', help="list of wav_files for prediction") args = parser.parse_args(argv) print(f"begins logging to file: {str(Path(args.log_dir).resolve() / 'predict.log')}") set_logfile(Path(args.log_dir, "predict.log")) logger.info(f"PyTorch version: {torch.__version__}") logger.info(f"Prediction started with command: {' '.join(sys.argv)}") args_str = [f"{k}={v}" for (k, v) in vars(args).items()] logger.info(f"args: {' '.join(args_str)}") if args.use_cuda: logger.info("using cuda") torch.set_default_tensor_type("torch.cuda.FloatTensor") if args.continue_from is None: logger.error("model name is missing: add '--continue-from <model-name>' in options") #parser.print_help() sys.exit(1) # run prediction predict = Predict(args) predict(args.wav_files, verbose=True)
Example #27
Source File: train.py From pytorch-asr with GNU General Public License v3.0 | 5 votes |
def parse_options(argv): parser = argparse.ArgumentParser(description="First CapsuleNet AM with fully supervised training") # for training parser.add_argument('--data-path', default='data/aspire', type=str, help="dataset path to use in training") parser.add_argument('--num-workers', default=4, type=int, help="number of dataloader workers") parser.add_argument('--num-epochs', default=500, type=int, help="number of epochs to run") parser.add_argument('--batch-size', default=16, type=int, help="number of images (and labels) to be considered in a batch") parser.add_argument('--init-lr', default=0.0001, type=float, help="initial learning rate for Adam optimizer") parser.add_argument('--num-iterations', default=3, type=float, help="number of routing iterations") # optional parser.add_argument('--use-cuda', default=False, action='store_true', help="use cuda") parser.add_argument('--seed', default=None, type=int, help="seed for controlling randomness in this example") parser.add_argument('--log-dir', default='./logs', type=str, help="filename for logging the outputs") parser.add_argument('--model-prefix', default='capsule_aspire', type=str, help="model file prefix to store") parser.add_argument('--continue-from', default=None, type=str, help="model file path to make continued from") args = parser.parse_args(argv) print(f"begins logging to file: {str(Path(args.log_dir).resolve() / 'train.log')}") set_logfile(Path(args.log_dir, "train.log")) logger.info(f"PyTorch version: {torch.__version__}") logger.info(f"Training started with command: {' '.join(sys.argv)}") args_str = [f"{k}={v}" for (k, v) in vars(args).items()] logger.info(f"args: {' '.join(args_str)}") if args.use_cuda: logger.info("using cuda") torch.set_default_tensor_type("torch.cuda.FloatTensor") if args.seed is not None: torch.manual_seed(args.seed) np.random.seed(args.seed) if args.use_cuda: torch.cuda.manual_seed(args.seed) return args
Example #28
Source File: train.py From pytorch-asr with GNU General Public License v3.0 | 5 votes |
def parse_options(argv): parser = argparse.ArgumentParser(description="DenseNet AM with fully supervised training") # for training parser.add_argument('--data-path', default='data/aspire', type=str, help="dataset path to use in training") parser.add_argument('--num-workers', default=4, type=int, help="number of dataloader workers") parser.add_argument('--num-epochs', default=1000, type=int, help="number of epochs to run") parser.add_argument('--batch-size', default=64, type=int, help="number of images (and labels) to be considered in a batch") parser.add_argument('--init-lr', default=0.0001, type=float, help="initial learning rate for Adam optimizer") # optional parser.add_argument('--use-cuda', default=False, action='store_true', help="use cuda") parser.add_argument('--seed', default=None, type=int, help="seed for controlling randomness in this example") parser.add_argument('--log-dir', default='./logs', type=str, help="filename for logging the outputs") parser.add_argument('--model-prefix', default='dense_aspire', type=str, help="model file prefix to store") parser.add_argument('--continue-from', default=None, type=str, help="model file path to make continued from") args = parser.parse_args(argv) print(f"begins logging to file: {str(Path(args.log_dir).resolve() / 'train.log')}") set_logfile(Path(args.log_dir, "train.log")) logger.info(f"PyTorch version: {torch.__version__}") logger.info(f"Training started with command: {' '.join(sys.argv)}") args_str = [f"{k}={v}" for (k, v) in vars(args).items()] logger.info(f"args: {' '.join(args_str)}") if args.use_cuda: logger.info("using cuda") torch.set_default_tensor_type("torch.cuda.FloatTensor") if args.seed is not None: torch.manual_seed(args.seed) np.random.seed(args.seed) if args.use_cuda: torch.cuda.manual_seed(args.seed) return args
Example #29
Source File: spinup_ppo_tests.py From cherry with Apache License 2.0 | 5 votes |
def tearDown(self): torch.set_default_tensor_type(torch.FloatTensor) torch.set_default_dtype(torch.float32)
Example #30
Source File: spinup_ppo_tests.py From cherry with Apache License 2.0 | 5 votes |
def setUp(self): torch.set_default_tensor_type(torch.DoubleTensor) torch.set_default_dtype(torch.float64)