Python torch.initial_seed() Examples

The following are 18 code examples of torch.initial_seed(). 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: utils.py    From moses with MIT License 5 votes vote down vote up
def set_torch_seed_to_all_gens(_):
    seed = torch.initial_seed() % (2**32 - 1)
    random.seed(seed)
    np.random.seed(seed) 
Example #2
Source File: adaptor_dataset.py    From video_analyst with MIT License 5 votes vote down vote up
def __getitem__(self, item):
        if self.datapipeline is None:
            # build datapipeline with random seed the first time when __getitem__ is called
            # usually, dataset is already spawned (into subprocess) at this point.
            seed = (torch.initial_seed() + item * self._SEED_STEP +
                    self.ext_seed * self._EXT_SEED_STEP) % self._SEED_DIVIDER
            self.datapipeline = datapipeline_builder.build(self.task,
                                                           self.cfg,
                                                           seed=seed)
            logger.info("AdaptorDataset #%d built datapipeline with seed=%d" %
                        (item, seed))

        training_data = self.datapipeline[item]

        return training_data 
Example #3
Source File: HenonAttractor.py    From EchoTorch with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, sample_len, n_samples, xy, a, b, washout=0, normalize=False, seed=None):
        """
        Constructor
        :param sample_len: Length of the time-series in time steps.
        :param n_samples: Number of samples to generate.
        :param a:
        :param b:
        :param c:
        """
        # Properties
        self.sample_len = sample_len
        self.n_samples = n_samples
        self.a = a
        self.b = b
        self.xy = xy
        self.normalize = normalize
        self.washout = washout

        # Seed
        if seed is not None:
            torch.initial_seed(seed)
        # end if

        # Generate data set
        self.outputs = self._generate()
    # end __init__

    #############################################
    # OVERRIDE
    #############################################

    # Length 
Example #4
Source File: LorenzAttractor.py    From EchoTorch with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, sample_len, n_samples, xyz, sigma, b, r, dt=0.01, washout=0, normalize=False, seed=None):
        """
        Constructor
        :param sample_len: Length of the time-series in time steps.
        :param n_samples: Number of samples to generate.
        :param a:
        :param b:
        :param c:
        """
        # Properties
        self.sample_len = sample_len
        self.n_samples = n_samples
        self.xyz = xyz
        self.dt = dt
        self.normalize = normalize
        self.washout = washout
        self.sigma = sigma
        self.b = b
        self.r = r

        # Seed
        if seed is not None:
            torch.initial_seed(seed)
        # end if

        # Generate data set
        self.outputs = self._generate()
    # end __init__

    #############################################
    # OVERRIDE
    #############################################

    # Length 
Example #5
Source File: __init__.py    From torchfunc with MIT License 5 votes vote down vote up
def __init__(self, value, cuda: bool = False):
        self.value = value
        self.cuda = cuda

        self._last_seed = torch.initial_seed()
        np.random.seed(self.value)
        torch.manual_seed(self.value)

        if self.cuda:
            torch.backends.cudnn.deterministic = True
            torch.backends.cudnn.benchmark = False 
Example #6
Source File: torchfunc_test.py    From torchfunc with MIT License 5 votes vote down vote up
def test_seed_decorator():
    first_seed = torch.initial_seed()

    @torchfunc.seed(0)
    def wrapped():
        assert 0 == torch.initial_seed()

    wrapped()
    assert torch.initial_seed() == first_seed 
Example #7
Source File: torchfunc_test.py    From torchfunc with MIT License 5 votes vote down vote up
def test_seed_context_manager():
    first_seed = torch.initial_seed()
    with torchfunc.seed(0):
        assert 0 == torch.initial_seed()
    assert torch.initial_seed() == first_seed 
Example #8
Source File: torchfunc_test.py    From torchfunc with MIT License 5 votes vote down vote up
def test_seed():
    torchfunc.seed(0)
    assert 0 == torch.initial_seed() 
Example #9
Source File: utils.py    From 3D-ResNets-PyTorch with MIT License 5 votes vote down vote up
def worker_init_fn(worker_id):
    torch_seed = torch.initial_seed()

    random.seed(torch_seed + worker_id)

    if torch_seed >= 2**32:
        torch_seed = torch_seed % 2**32
    np.random.seed(torch_seed + worker_id) 
Example #10
Source File: trainer.py    From elektronn3 with MIT License 5 votes vote down vote up
def _worker_init_fn(worker_id: int) -> None:
    """Sets a unique but deterministic random seed for background workers.

    Only sets the seed for NumPy because PyTorch and Python's own RNGs
    take care of reseeding on their own.
    See https://github.com/numpy/numpy/issues/9650."""
    # Modulo 2**32 because np.random.seed() only accepts values up to 2**32 - 1
    initial_seed = torch.initial_seed() % 2**32
    worker_seed = initial_seed + worker_id
    np.random.seed(worker_seed)


# Be careful from where you call this! Not sure if this is concurrency-safe. 
Example #11
Source File: datasets.py    From PointFlow with MIT License 5 votes vote down vote up
def init_np_seed(worker_id):
    seed = torch.initial_seed()
    np.random.seed(seed % 4294967296) 
Example #12
Source File: __init__.py    From margipose with Apache License 2.0 5 votes vote down vote up
def worker_init(worker_id):
    seed_all(torch.initial_seed() % 2**32) 
Example #13
Source File: main_fullv_gpd.py    From PointNetGPD with MIT License 5 votes vote down vote up
def worker_init_fn(pid):
    np.random.seed(torch.initial_seed() % (2**31-1)) 
Example #14
Source File: main_fullv.py    From PointNetGPD with MIT License 5 votes vote down vote up
def worker_init_fn(pid):
    np.random.seed(torch.initial_seed() % (2**31-1)) 
Example #15
Source File: main_1v_gpd.py    From PointNetGPD with MIT License 5 votes vote down vote up
def worker_init_fn(pid):
    np.random.seed(torch.initial_seed() % (2**31-1)) 
Example #16
Source File: main_fullv_mc.py    From PointNetGPD with MIT License 5 votes vote down vote up
def worker_init_fn(pid):
    np.random.seed(torch.initial_seed() % (2**31-1)) 
Example #17
Source File: main_1v.py    From PointNetGPD with MIT License 5 votes vote down vote up
def worker_init_fn(pid):
    np.random.seed(torch.initial_seed() % (2**31-1)) 
Example #18
Source File: common.py    From adviser with GNU General Public License v3.0 4 votes vote down vote up
def init_random(seed: int = None):
    """
    Initializes the random generators to allow seeding.

    Args:
        seed (int): The seed used for all random generators.

    """
    global GLOBAL_SEED  # pylint: disable=global-statement
    if GLOBAL_SEED is not None:
        return

    if seed is None:
        tmp_random = numpy.random.RandomState(None)
        GLOBAL_SEED = tmp_random.randint(2**32-1, dtype='uint32')
    else:
        GLOBAL_SEED = seed

    # initialize random generators
    numpy.random.seed(GLOBAL_SEED)
    random.seed(GLOBAL_SEED)

    try:
        # try to load torch and initialize random generator if available
        import torch
        torch.cuda.manual_seed_all(GLOBAL_SEED)  # gpu
        torch.manual_seed(GLOBAL_SEED)  # cpu
    except ImportError:
        pass

    try:
        # try to load tensorflow and initialize random generator if available
        import tensorflow
        tensorflow.random.set_random_seed(GLOBAL_SEED)
    except ImportError:
        pass

    # check whether all calls to torch.* use the same random generator (i.e. same instance)
    # works in a short test -- MS
    # print(torch.initial_seed())

    # logger.info("Seed is {:d}".format(GLOBAL_SEED))
    return GLOBAL_SEED