def ook_decode_distribution(self, array): assert len(array) == len(self), "%d != %d" % (len(array), len(self)) check.check_pdist(array) return { self.decode(i): probability for i, probability in enumerate(array) }
def sampling_ook_decode(self, array): assert len(array) == len(self), "%d != %d" % (len(array), len(self)) check.check_pdist(array) # Sample 1 thing v # from [0..N] vvvvvvvvv # with probabilties vvvvvvv index = np.random.choice(len(self), 1, p=array)[0] return self.decode(index)
def ook_decode(self, array): assert len(array) == len(self), "%d != %d" % (len(array), len(self)) check.check_pdist(array) # If the array is all zeros if not np.any(array): return None # The index of the maximum value from the ook_encoding. # vvvvvvvvvvvvvv return self.decode(array.argmax())
def __init__(self, scope, output_labels, output_distribution): super(CustomOutput, self).__init__(scope) self.output_labels = check.check_instance(output_labels, mlbase.Labels) self.output_distribution = check.check_pdist(output_distribution) assert len(self.output_labels) == len( self.output_distribution), "%d != %d" % (len( self.output_labels), len(self.output_distribution))
def ook_encode(self, value, handle_unknown=False): encoding = [0] * len(self) if isinstance(value, dict): for key, probability in check.check_pdist(value).items(): encoding[self.encode(key, handle_unknown)] = probability else: encoding[self.encode(value, handle_unknown)] = 1 return np.array(encoding, dtype="float32")
def vector_decode_probability(self, array, value, handle_oov=False): assert len(array) == len(self), "%d != %d" % (len(array), len(self)) check.check_pdist(array) return array[self.encode(value, handle_oov)]
def __init__(self, prediction, distribution, encoding): self.prediction = prediction self.distribution = check.check_pdist(distribution) self.encoding = encoding