samedi 23 septembre 2017

Different Pytorch random initialization with the same seed

I am very new to Pytorch, so I apologise if the question is very straightforward. My problem is that I have defined class net1 and initialised its parameters randomly with a fixed manual seed.

random.seed(opt.manualSeed)
torch.manual_seed(opt.manualSeed)
if torch.cuda.is_available():
    torch.cuda.manual_seed_all(opt.manualSeed)    

class net1(nn.Module):
    def __init__(self):
        super(net1, self).__init__()
        self.main_body = nn.Sequential(
            # Define the layers...#
        )
    def forward(self, x):
        return self.main_body(x)

# custom weights initialization called on net1
def weights_init(m):
    classname = m.__class__.__name__
    if classname.find('Conv') != -1:
        m.weight.data.normal_(0.0, 0.02)
    elif classname.find('BatchNorm') != -1:
        m.weight.data.normal_(1.0, 0.02)
        m.bias.data.fill_(0)

net1_ = net1()
net1_.apply(weights_init)

However, when I add another class net2 to the code:

class net2(nn.Module):
    def __init__(self):
        super(net2, self).__init__()
        self.main_body = nn.Sequential(
            # Define the layers
        )
    def forward(self, x):
        return self.main_body(x)

net2_ = net2()

and instantiate it, even though I do not use it anywhere else and it is not connected to my main graph (which is built on net1_), I get different outputs from my graph. Is it a reasonable outcome?




Generate random Strings without a shuffle and repetition of previous ones?

My code already generates a random String of an array when I press a button, but sometimes a String gets repeated. What do I have to do so that the String "Mango" only gets called again when all the other Strings where already called without using a shuffle, I want to call one String at a time?

Example: "Mango", "Kiwi", "Banana", "Pineapple", "Melon", "Mango", "Kiwi",.....

Here is my code:

var array = ["Mango", "Banana", "Apple","Kiwi", "Melon", "Pineapple"]

let fruits = Int(arc4random_uniform(UInt32(array.count)))

print(array[fruits])



How to generate random Strings without repeating any previous one?

My code already generates random Strings of an array when I press a button, but sometimes Strings get repeated. What do I have to do so that the String "Mango" only gets called again when all the other Strings where already called?

Example: "Mango", "Kiwi", "Banana", "Pineapple", "Melon", "Mango", "Kiwi",.....

Here is my code:

import UIKit

  var array = ["Mango", "Banana", "Apple","Kiwi", "Melon", "Pineapple"]

  let fruits = Int(arc4random_uniform(UInt32(array.count)))

  print(array[fruits])



Generating random indexes

When you want to generate random indexes to use on an array, make sure you use Math.floor instead of Math.round or your randomness could suffer.

Some novice programmers might generate a random index like this:

Math.round(Math.random() * LENGTH_OF_ARRAY);

But using Math.round has some issues. What you actually want to use is Math.floor:

Math.floor(Math.random() * LENGTH_OF_ARRAY);

Frequencies for array using both round and floor methods. The round method shows that the indexes 0 and array length + 1 are only half as likely to be generated

You can run the experiment yourself here: http://ift.tt/2wIfxh7




java, generate random, frequency, letter grade

Assume 25 students in a class. Generate random numbers ranging from 50 to 100 to be the average score of each student. Find the frequency of each letter grade, that is, the number of students in each letter grade. Print the number of students getting an “A”, a “B”, a “C, a “D” and an “F” separately.




How to generate 8 "pairs" of random numbers in one array ? (JAVA)

I need to create an array of 16 numbers. Each number ranges from 1-8. And there needs to be 2 of the same numbers.

for example 1 1 4 4 7 7 2 2 6 6 3 3 8 8 5 5




How to show name of instance in c++?

I am trying to output the names of the monsters that have been instantiated (and moved into a vector).

I also need to randomize the positions of the monsters before output-ing the names.

the code I have for randomizing them is:

random_shuffle(monsters.begin(), monsters.end());
for (vector<Unit*>::iterator it = monsters.begin(); it != monsters.end(); ++it) cout << " " << *it;

but it only outputs the address of them and not the name, probably because they aren't strings (?)

What is a way for me to output the names when they've been instatiated like this:

Monster *deyvor = new Monster("Deyvor", 2000); 

"deyvor" being the name and "2000" being the amount of strength it has.

I was thinking of creating a function that gets the name so I can do something like it->getName() but I am also unsure of how to do it.


I also added them into a vector by doing this:

monsters.push_back(Deyvor);