示例#1
0
 def out(self, input, alpha=None, theta=None):
     _input = input if isinstance(input, np.ndarray) else np.array(input)
     _alpha = alpha if alpha else self._alpha
     _theta = theta if theta else self._theta
     logging.debug('PARAMETERS (alpha, theta): (%s, %s)' % (_alpha, _theta))
     logging.debug('INPUT: (%s), SIGMOID: %s' % (','.join(
         [str(i)
          for i in _input]), sigmoid(self._w.dot(_input), _alpha, _theta)))
     if np.random.rand(1, 1) < sigmoid(self._w.dot(_input), _alpha, _theta):
         logging.debug('OUTPUT: %s' % 1)
         return 1
     else:
         logging.debug('OUTPUT: %s' % 0)
         return 0
 def data_frame(self):
     bin_values = []
     for state in self._states:
         bin_values.append(0 if sigmoid(np.round(state, 4)) < 0.5 else 1)
     return pd.DataFrame(
         data={"states": self._states, "values": self._values, "binaries": bin_values}, index=self.domain
     )
 def __init__(self, name, w, initial_state, alpha=1.0, theta=0.0, time_delta=0.001):
     self._name = name
     self._w = w if isinstance(w, np.ndarray) else np.array(w)
     self._alpha = alpha
     self._theta = theta
     self._time_delta = time_delta
     self._states = [initial_state]
     self._values = [sigmoid(self._states[-1], self._alpha, self._theta)]
 def activate(self, x):
     assert len(x) == len(self.W[0][0]) - 1
     self.Y = [np.append(x, [1.0, ]), ]
     for l in range(len(self.W)):
         self.Y.append(np.ones(len(self.W[l]) + (1 if l < len(self.W[l]) - 1 else 0)))
         for i, w in enumerate(self.W[l]):
             self.Y[-1][i] = sigmoid(w.dot(self.Y[-2]))
     return self.Y[-1]
示例#5
0
 def data_frame(self):
     bin_values = []
     for state in self._states:
         bin_values.append(0 if sigmoid(np.round(state, 4)) < 0.5 else 1)
     return pd.DataFrame(data={
         'states': self._states,
         'values': self._values,
         'binaries': bin_values
     },
                         index=self.domain)
 def out(self, input, alpha=None, theta=None):
     _input = input if isinstance(input, np.ndarray) else np.array(input)
     _alpha = alpha if alpha else self._alpha
     _theta = theta if theta else self._theta
     logging.debug('PARAMETERS (alpha, theta): (%s, %s)' % (_alpha, _theta))
     logging.debug('INPUT: (%s), SIGMOID: %s' % (','.join([str(i) for i in _input]), sigmoid(self._w.dot(_input), _alpha, _theta)))
     if np.random.rand(1, 1) < sigmoid(self._w.dot(_input), _alpha, _theta):
         logging.debug('OUTPUT: %s' % 1)
         return 1
     else:
         logging.debug('OUTPUT: %s' % 0)
         return 0
示例#7
0
 def __init__(self,
              name,
              w,
              initial_state,
              alpha=1.0,
              theta=0.0,
              time_delta=0.001):
     self._name = name
     self._w = w if isinstance(w, np.ndarray) else np.array(w)
     self._alpha = alpha
     self._theta = theta
     self._time_delta = time_delta
     self._states = [
         initial_state,
     ]
     self._values = [
         sigmoid(self._states[-1], self._alpha, self._theta),
     ]
示例#8
0
 def get_value(self, x):
     return sigmoid(self._w.dot(np.append([
         1.0,
     ], x)))
 def update(self, input):
     _input = input if isinstance(input, np.ndarray) else np.array(input)
     self._states.append(self._states[-1] + self._time_delta * (-self._states[-1] + self._w.dot(_input)))
     self._values.append(sigmoid(self._states[-1], alpha=self._alpha, theta=self._theta))
 def get_value(self, x):
     return sigmoid(self._w.dot(np.append([1.0, ], x)))
示例#11
0
 def update(self, input):
     _input = input if isinstance(input, np.ndarray) else np.array(input)
     self._states.append(self._states[-1] + self._time_delta *
                         (-self._states[-1] + self._w.dot(_input)))
     self._values.append(
         sigmoid(self._states[-1], alpha=self._alpha, theta=self._theta))
示例#12
0
            return 0

    def __call__(self, input, alpha=None, theta=None):
        return self.out(input, alpha, theta)


if __name__ == '__main__':
    #logging.basicConfig(level=logging.DEBUG)

    w = np.array([3, 2, 1])
    alpha = 1
    theta = 1
    p_element = ProbabilisticElement(w, alpha, theta)

    num_of_loops = 50000

    for i in [
        [1, 0, 1],
        [1, -1, 0],
    ]:
        num_of_ones = 0
        for l in range(0, num_of_loops):
            if p_element(i):
                num_of_ones += 1
        probability = num_of_ones / num_of_loops

        print('Sigmoid: ', sigmoid(w.dot(i), theta=theta))
        print('Probability of one: ', probability)
        assert np.round(probability,
                        2) == np.round(sigmoid(w.dot(i), theta=theta), 2)
            logging.debug('OUTPUT: %s' % 1)
            return 1
        else:
            logging.debug('OUTPUT: %s' % 0)
            return 0

    def __call__(self, input, alpha=None, theta=None):
        return self.out(input, alpha, theta)


if __name__ == '__main__':
    #logging.basicConfig(level=logging.DEBUG)

    w = np.array([3, 2, 1])
    alpha = 1
    theta = 1
    p_element = ProbabilisticElement(w, alpha, theta)

    num_of_loops = 50000

    for i in [[1, 0, 1], [1, -1, 0], ]:
        num_of_ones = 0
        for l in range(0, num_of_loops):
            if p_element(i):
                num_of_ones += 1
        probability = num_of_ones / num_of_loops

        print('Sigmoid: ', sigmoid(w.dot(i), theta=theta))
        print('Probability of one: ', probability)
        assert np.round(probability, 2) == np.round(sigmoid(w.dot(i), theta=theta), 2)
 def f(self, s):
     return sigmoid(s)