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])
def __init__(self, *args, **kwargs): class MiniCaptcha(Model): def __init__(self, alphabet=['A', 'B', 'C', 'D', 'E', 'F'], noise=0.1): self._alphabet = alphabet self._probs = [1/len(alphabet) for i in range(len(alphabet))] self._noise = noise super().__init__('MiniCaptcha') def render(self, text, size=18, height=28, width=28, x=6, y=6): pil_font = ImageFont.truetype('Ubuntu-B.ttf', size=size) text_width, text_height = pil_font.getsize(text) canvas = Image.new('RGB', [height, width], (255, 255, 255)) draw = ImageDraw.Draw(canvas) draw.text((x, y), text, font=pil_font, fill='#000000') return torch.from_numpy(1 - (np.asarray(canvas) / 255.0))[:, :, 0].unsqueeze(0).float() 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 self._model = MiniCaptcha() self._test_images = [self._model.render(letter).view(-1) for letter in self._model._alphabet] self._true_posteriors = [Categorical(util.one_hot(len(self._model._alphabet), i) + util._epsilon) for i in range(len(self._model._alphabet))] super().__init__(*args, **kwargs)
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])
def get_sample(s): address = s.Address().decode("utf-8") distribution = None # sample.instance = s.Instance() value = NDArray_to_Tensor(s.Value()) distribution_type = s.DistributionType() if distribution_type != infcomp.protocol.Distribution.Distribution().NONE: if distribution_type == infcomp.protocol.Distribution.Distribution( ).UniformDiscrete: p = infcomp.protocol.UniformDiscrete.UniformDiscrete() p.Init(s.Distribution().Bytes, s.Distribution().Pos) distribution = UniformDiscrete(p.PriorMin(), p.PriorSize()) if value.dim() > 0: value = util.one_hot(distribution.prior_size, int(value[0]) - distribution.prior_min) elif distribution_type == infcomp.protocol.Distribution.Distribution( ).MultivariateNormal: p = infcomp.protocol.MultivariateNormal.MultivariateNormal() p.Init(s.Distribution().Bytes, s.Distribution().Pos) distribution = MultivariateNormal(NDArray_to_Tensor(p.PriorMean()), NDArray_to_Tensor(p.PriorCov())) elif distribution_type == infcomp.protocol.Distribution.Distribution( ).Normal: p = infcomp.protocol.Normal.Normal() p.Init(s.Distribution().Bytes, s.Distribution().Pos) distribution = Normal(p.PriorMean(), p.PriorStd()) elif distribution_type == infcomp.protocol.Distribution.Distribution( ).Flip: distribution = Flip() elif distribution_type == infcomp.protocol.Distribution.Distribution( ).Discrete: p = infcomp.protocol.Discrete.Discrete() p.Init(s.Distribution().Bytes, s.Distribution().Pos) distribution = Discrete(p.PriorSize()) if value.dim() > 0: value = util.one_hot(distribution.prior_size, int(value[0])) elif distribution_type == infcomp.protocol.Distribution.Distribution( ).Categorical: p = infcomp.protocol.Categorical.Categorical() p.Init(s.Distribution().Bytes, s.Distribution().Pos) distribution = Categorical(p.PriorSize()) if value.dim() > 0: value = util.one_hot(distribution.prior_size, int(value[0])) elif distribution_type == infcomp.protocol.Distribution.Distribution( ).UniformContinuous: p = infcomp.protocol.UniformContinuous.UniformContinuous() p.Init(s.Distribution().Bytes, s.Distribution().Pos) distribution = UniformContinuous(p.PriorMin(), p.PriorMax()) elif distribution_type == infcomp.protocol.Distribution.Distribution( ).UniformContinuousAlt: p = infcomp.protocol.UniformContinuousAlt.UniformContinuousAlt() p.Init(s.Distribution().Bytes, s.Distribution().Pos) distribution = UniformContinuousAlt(p.PriorMin(), p.PriorMax()) elif distribution_type == infcomp.protocol.Distribution.Distribution( ).Laplace: p = infcomp.protocol.Laplace.Laplace() p.Init(s.Distribution().Bytes, s.Distribution().Pos) distribution = Laplace(p.PriorLocation(), p.PriorScale()) elif distribution_type == infcomp.protocol.Distribution.Distribution( ).Gamma: distribution = Gamma() elif distribution_type == infcomp.protocol.Distribution.Distribution( ).Beta: distribution = Beta() else: util.logger.log( 'get_sample: Unknown distribution:Distribution id: {0}.'. format(distribution_type)) sample = Sample(address, distribution, value) return sample