Hi, I’m going through the examples and the line numbers in the explanation for this function seems to be messed up.
def weather():
cloudy = torch.distributions.Bernoulli(0.3).sample()
cloudy = 'cloudy' if cloudy.item() == 1.0 else 'sunny'
mean_temp = {'cloudy': 55.0, 'sunny': 75.0}[cloudy]
scale_temp = {'cloudy': 10.0, 'sunny': 15.0}[cloudy]
temp = torch.distributions.Normal(mean_temp, scale_temp).rsample()
return cloudy, temp.item()
Let’s go through this line-by-line. First, in lines 2-3 we define a binary random variable ‘cloudy’, which is given by a draw from the bernoulli distribution with a parameter of
0.3
. Since the bernoulli distributions returns0
s or1
s, in line 4 we convert the valuecloudy
to a string so that return values ofweather
are easier to parse. So according to this model 30% of the time it’s cloudy and 70% of the time it’s sunny.In lines 5-6 we define the parameters we’re going to use to sample the temperature in lines 7-9. These parameters depend on the particular value of
cloudy
we sampled in line 2. For example, the mean temperature is 55 degrees (Fahrenheit) on cloudy days and 75 degrees on sunny days. Finally we return the two valuescloudy
andtemp
in line 10.
Obviously the function only has 7 lines and the explanation talks about 10 lines. Maybe the lines are truncated when this documentation was been written? I highlighted the correct line numbers.
Let’s go through this line-by-line. First, in lines 2 we define a binary random variable ‘cloudy’, which is given by a draw from the bernoulli distribution with a parameter of
0.3
. Since the bernoulli distributions returns0
s or1
s, in line 3 we convert the valuecloudy
to a string so that return values ofweather
are easier to parse. So according to this model 30% of the time it’s cloudy and 70% of the time it’s sunny.In lines 4-5 we define the parameters we’re going to use to sample the temperature in lines 6. These parameters depend on the particular value of
cloudy
we sampled in line 2. For example, the mean temperature is 55 degrees (Fahrenheit) on cloudy days and 75 degrees on sunny days. Finally we return the two valuescloudy
andtemp
in line 7.