Advertisement
Guest User

Untitled

a guest
Oct 21st, 2017
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.11 KB | None | 0 0
  1. class Value:
  2.     """
  3.    Dense object that holds parameters.
  4.    """
  5.  
  6.     def __init__(self, n):
  7.         self._params = np.ones(n)
  8.         self._n = n
  9.  
  10.     def dot(self, update):
  11.         score = 0
  12.         for position, value in zip(update._positions, update._values):
  13.             score += self._params[int(position % self._n)] * value
  14.         return score
  15.  
  16.     def assign(self, other):
  17.         """
  18.        self = other
  19.        other is Value.
  20.        """
  21.         self = np.copy(other)
  22.  
  23.     def assign_mul(self, coeff):
  24.         """
  25.        self = self * coeff
  26.        coeff is float.
  27.        """
  28.         self._params = np.multiply(self._params, coeff)
  29.  
  30.     def assign_madd(self, x, coeff):
  31.         """
  32.        self = self + x * coeff
  33.        x can be either Value or Update.
  34.        coeff is float.
  35.        """
  36.         if isinstance(x, Value):
  37.             self._params = np.add(self._params, np.multiply(x._params, coeff))
  38.  
  39.         if isinstance(x, Update):
  40.             for position, value in zip(x._positions, x._values):
  41.                 self._params[int(position % self._n)] += value * coeff
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement