Pytorch RNG seeds fix

Currently I am generated normal random number by the following,

zn = torch.FloatTensor(dim1, dim2)

I am wondering how could i set seed for each tensor?

Hi Nick!

manual_seed() should work, e.g.:

zn1 = torch.FloatTensor(dim1, dim2)
zn2 = torch.FloatTensor(dim1, dim2)
zn3 = torch.FloatTensor(dim1, dim2)

torch.manual_seed (seed1)
torch.manual_seed (seed2)
torch.manual_seed (seed3)

Is this what you were asking?

Bear in mind, though, that you don’t generally want to do this.
You are probably better off letting pytorch’s (pseudo) random
number generator do its thing, rather than potentially making
it less random (at the margins – probably won’t matter in
practice) by injecting new seeds all the time.

My typical use of setting the random-number seed is to make
some overall run repeatable from run to run, so that, for example,
my random network weights start out as the same random weights
for each run. But I wouldn’t use different seeds, for example, for
the weights in different layers.

Good luck.

K. Frank

Thanks Frank.

What you gave is fix seed for each RNG.

What I want is fix seed for each tensor, kind of like followings,

zn = torch.FloatTensor(dim)
for i:

Thanks again

Hello Nick!

Not really. Pytorch has (roughly speaking) a single global RNG.
This global RNG is used (drawing random number from its
current state) by things like torch.FloatTensor.normal_().
I simply reseeded the global RNG three times.

I’m not aware of any way to attach a per-tensor RNG to a specific
tensor. (But you shouldn’t want to do this.)

This code suggests that you want to be able to attach a separate
(and separately seeded) RNG to each “row” of your tensor. Is
that what you mean?

But, in any event, other than the (nominally irrelevant) actual values
of the randomly set elements of the tensor, how are the results of
your pseudocode (or what you are trying to achieve) intended to
differ from the results of the code I posted? In both cases I see
the elements of your tensors being set to (pseudo) random values drawn from a normal distribution.

What – in terms of results – is it that you want that differs from
what my code snippet does?


K. Frank