示例#1
0
 def forward(self, observation=[]):
     states = [pyprob.sample(init_dist)]
     for o in observation:
         state = pyprob.sample(self.trans_dists[int(states[-1])])
         pyprob.observe(self.obs_dists[int(state)], o)
         states.append(state)
     return torch.stack([util.one_hot(3, int(s)) for s in states])
示例#2
0
 def forward(self):
     mu = pyprob.sample(Normal(self.prior_mean, self.prior_stddev))
     likelihood = Normal(mu, self.likelihood_stddev)
     # pyprob.observe usage alternative #1
     pyprob.observe(likelihood, name='obs0')
     pyprob.observe(likelihood, name='obs1')
     return mu
示例#3
0
 def forward(self):
     mu = self.marsaglia(self.prior_mean, self.prior_stddev)
     likelihood = Normal(mu, self.likelihood_stddev)
     pyprob.tag(mu, name='mu')
     pyprob.observe(likelihood, name='obs0')
     pyprob.observe(likelihood, name='obs1')
     return mu
示例#4
0
 def forward(self):
     states = [pyprob.sample(init_dist)]
     for i in range(self.obs_length):
         state = pyprob.sample(self.trans_dists[int(states[-1])])
         pyprob.observe(self.obs_dists[int(state)], name='obs{}'.format(i))
         states.append(state)
     return torch.stack([util.one_hot(3, int(s)) for s in states])
示例#5
0
	def forward(self, output_distance=True):
		g = genModel(self.input_symbols, self.output_symbols)
		distance = compute_score(g, self.IO)
		dist_obj = make_uniform_under_dist(distance)
		pyprob.observe(dist_obj, name="dist")
		if output_distance:
			return g, distance
		else:
			return g
示例#6
0
            def forward(self):
                count_prior = Poisson(4)
                r = pyprob.sample(count_prior)
                if 4 < float(r):
                    l = 6
                else:
                    l = 1 + self.fibonacci(3 * int(r)) + pyprob.sample(count_prior)

                pyprob.observe(Poisson(l), name='obs')
                return r
示例#7
0
            def forward(self):
                uniform = Uniform(-1, 1)
                for i in range(2):
                    x = pyprob.sample(uniform)
                    y = pyprob.sample(uniform)
                    s = x * x + y * y

                likelihood = Normal(s, 0.1)
                pyprob.observe(likelihood, name='obs0')
                pyprob.observe(likelihood, name='obs1')
                return s
示例#8
0
    def forward(self):
        format_index = int(
            pyprob.sample(dists.Categorical(torch.tensor([1 / 6] * 6))).item())

        firstname_probs = FIRST_NAMES['count'].tolist(
        ) / FIRST_NAMES['count'].sum()
        firstname_index = int(
            pyprob.sample(dists.Categorical(
                torch.tensor(firstname_probs))).item())
        firstname = FIRST_NAMES['name'][firstname_index].lower()

        lastname_probs = LAST_NAMES['count'].tolist(
        ) / LAST_NAMES['count'].sum()
        lastname_index = int(
            pyprob.sample(dists.Categorical(
                torch.tensor(lastname_probs))).item())
        lastname = LAST_NAMES['name'][lastname_index].lower()

        if format_index == 0 or format_index == 1:
            # The person has no middle name
            middlename = ""
        if format_index == 2 or format_index == 3:
            # The person has a middle name
            middlename_probs = MIDDLE_NAMES['count'].tolist(
            ) / MIDDLE_NAMES['count'].sum()
            middlename_index = int(
                pyprob.sample(dists.Categorical(
                    torch.tensor(middlename_probs))).item())
            middlename = MIDDLE_NAMES['name'][middlename_index].lower()
        if format_index == 4 or format_index == 5:
            # The person has a middle name initial
            middlename_index = int(
                pyprob.sample(dists.Categorical(torch.tensor([1 / 26] *
                                                             26))).item())
            middlename = ALL_LETTERS[middlename_index]

        # make a categorical distribution that observes each letter independently (like 50 independent categoricals)
        output = pad_string(original=format_name(firstname, middlename,
                                                 lastname, format_index),
                            desired_len=MAX_STRING_LEN)

        probs = torch.ones(MAX_STRING_LEN, N_CHARACTERS) * (
            (1 - self.peak_prob) / (N_CHARACTERS - 1))
        for i, character in enumerate(output):
            probs[i, character_to_index(character)] = self.peak_prob
        pyprob.observe(OneHot2DCategorical(probs), name=f"name_string")

        return output, {
            'firstname': firstname,
            'middlename': middlename,
            'lastname': lastname
        }
示例#9
0
 def forward(self, observation=[]):
     uniform = Uniform(0, 1)
     val = pyprob.sample(uniform)
     val = pyprob.sample(uniform)
     val = pyprob.sample(uniform, control=False)
     val = pyprob.sample(uniform, control=False)
     val = pyprob.sample(uniform, control=False)
     pyprob.observe(value=val, name='val')
     pyprob.observe(uniform, 0.5)
     pyprob.observe(uniform, 0.5)
     pyprob.observe(uniform, 0.5)
     pyprob.observe(uniform, 0.5)
     return val
示例#10
0
	def forward(self):
		nPrims = rangeSample([4,9], "nPrims").long().item()
		outRules = []
		#print(nPrims)
		for i in range(nPrims):
			#print(len(rules))
			outRules.append(dummySample(i))

		dist_obj = make_uniform_under_dist(self.distance)
		pyprob.observe(dist_obj, name="dist")
		#last = pyprob.observe(pyprob.distributions.Categorical(torch.tensor([0.5,0.5])), name="dist")
		#outRules.append(last)
		
		return tuple(outRules)
示例#11
0
    def forward(self):
        country_index = int(pyprob.sample(dists.Categorical(torch.tensor([1/len(COUNTRY_INFO)]*len(COUNTRY_INFO)))).item())
        country_info = COUNTRY_INFO[country_index]

        # Obtain formatted country code
        country_code = country_info['cc']
        cc_format = int(pyprob.sample(dists.Categorical(torch.tensor([1/3] + [1/9]*6))).item())
        full_cc = format_cc(country_code, cc_format)
        
        structure_index = int(pyprob.sample(dists.Categorical(torch.tensor([1/len(country_info['structure'])]*len(country_info['structure'])))).item())
        number_structure = country_info['structure'][structure_index]

        # Obtain formatted area code
        area_code_len = number_structure[0]
        area_code = ""
        for _ in range(area_code_len):
            curr_digit = int(pyprob.sample(dists.Categorical(torch.tensor([1/N_DIGIT]*N_DIGIT))).item())
            area_code += str(curr_digit)
        ac_format = int(pyprob.sample(dists.Categorical(torch.tensor([1/6]*6))).item())
        full_ac = format_ac(area_code, ac_format)

        # Obtain formatted line number
        line_number_structure = number_structure[1:]
        line_number_block_len = len(line_number_structure)
        line_number_blocks = []
        for i in range(line_number_block_len):
            number_block_len = line_number_structure[i]
            number_block_digits = ""
            for _ in range(number_block_len):
                number = int(pyprob.sample(dists.Categorical(torch.tensor([1/N_DIGIT]*N_DIGIT))).item())
                number_block_digits += str(number)
            line_number_blocks.append(number_block_digits)
        line_number = " ".join(line_number_blocks)
        line_format = int(pyprob.sample(dists.Categorical(torch.tensor([1/3]*3))).item())
        full_line = format_line_number(line_number_blocks, line_format)

        # make a categorical distribution that observes each letter independently (like 30 independent categoricals)
        output = pad_string(original=full_cc+full_ac+full_line, desired_len=MAX_STRING_LEN)
        probs = torch.ones(MAX_STRING_LEN, N_LETTER)*0.001
        for i, letter in enumerate(output):
            probs[i, letter_to_index(letter)] = 1.
        pyprob.observe(OneHot2DCategorical(probs), name=f"phone_string")

        return output, {'country': country_info['country'],'country code': country_code, 'area code': area_code, 'line number': line_number}
示例#12
0
 def forward(self, observation=[]):
     uniform = Uniform(0, 1)
     ret = pyprob.sample(uniform)
     ret = pyprob.sample(uniform)
     ret = pyprob.sample(uniform, control=False)
     ret = pyprob.sample(uniform, control=False)
     ret = pyprob.sample(uniform, control=False)
     pyprob.observe(uniform, 0.5)
     pyprob.observe(uniform, 0.5)
     pyprob.observe(uniform, 0.5)
     pyprob.observe(uniform, 0.5)
     return ret
示例#13
0
 def forward(self, observation=[]):
     mu = self.marsaglia(self.prior_mean, self.prior_stddev)
     likelihood = Normal(mu, self.likelihood_stddev)
     for o in observation:
         pyprob.observe(likelihood, o)
     return mu
示例#14
0
 def forward(self, observation=[]):
     mu = pyprob.sample(Normal(self.prior_mean, self.prior_stddev))
     likelihood = Normal(mu, self.likelihood_stddev)
     for o in observation:
         pyprob.observe(likelihood, o)
     return mu
示例#15
0
 def forward(self):
     mu = self.rejection_sampling()
     likelihood = Normal(mu, self.likelihood_stddev)
     pyprob.observe(likelihood, name='obs0')
     return mu
示例#16
0
 def forward(self):
     letter_id = pyprob.sample(Categorical(self._probs))
     image = self.render(self._alphabet[letter_id]).view(-1)
     likelihood = Normal(image, self._noise)
     pyprob.observe(likelihood, name='query_image')
     return letter_id