Advertisement
Guest User

Untitled

a guest
Jan 29th, 2015
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.36 KB | None | 0 0
  1. import numpy as np
  2. import itertools
  3.  
  4. class RBM:
  5.  
  6. def __init__(self, num_visible, num_hidden, learning_rate = 0.1,max_epochs = 1000):
  7. self.num_hidden = num_hidden
  8. self.num_visible = num_visible
  9. self.learning_rate = learning_rate
  10. self.norm_dict = {}
  11. # Initialize a weight matrix, of dimensions (num_visible x num_hidden), using
  12. # a Gaussian distribution with mean 0 and standard deviation 0.1.
  13. self.weights = 0.1 * np.random.randn(self.num_visible, self.num_hidden)
  14. # Insert weights for the bias units into the first row and first column.
  15. self.weights = np.insert(self.weights, 0, 0, axis = 0)
  16. self.weights = np.insert(self.weights, 0, 0, axis = 1)
  17. self.max_epochs = max_epochs
  18.  
  19. def fit(self, data):
  20. """
  21. Train the machine.
  22.  
  23. Parameters
  24. ----------
  25. data: A matrix where each row is a training example consisting of the states of visible units.
  26. """
  27.  
  28. num_examples = data.shape[0]
  29.  
  30. # Insert bias units of 1 into the first column.
  31. data = np.insert(data, 0, 1, axis = 1)
  32.  
  33. for epoch in range(self.max_epochs):
  34. # Clamp to the data and sample from the hidden units.
  35. # (This is the "positive CD phase", aka the reality phase.)
  36. pos_hidden_activations = np.dot(data, self.weights)
  37. pos_hidden_probs = self._logistic(pos_hidden_activations)
  38. pos_hidden_states = pos_hidden_probs > np.random.rand(num_examples, self.num_hidden + 1)
  39.  
  40. tmp = np.array(pos_hidden_states).astype(float)
  41. pos_visible_states = self.run_hidden(tmp[:,1:])
  42.  
  43. # While alternating h and v's are generated, remember these in a dictionary
  44. # so the universe of h,v can later be used to sum over all probabilities for
  45. # the normalization constant
  46. for h,v in itertools.izip(pos_hidden_states.astype(float), pos_visible_states):
  47. v = np.insert(v, 0, 1)
  48. self.norm_dict[(tuple(h),tuple(v))] = 1
  49.  
  50. # Note that we're using the activation *probabilities* of the hidden states, not the hidden states
  51. # themselves, when computing associations. We could also use the states; see section 3 of Hinton's
  52. # "A Practical Guide to Training Restricted Boltzmann Machines" for more.
  53. pos_associations = np.dot(data.T, pos_hidden_probs)
  54.  
  55. # Reconstruct the visible units and sample again from the hidden units.
  56. # (This is the "negative CD phase", aka the daydreaming phase.)
  57. neg_visible_activations = np.dot(pos_hidden_states, self.weights.T)
  58. neg_visible_probs = self._logistic(neg_visible_activations)
  59. neg_visible_probs[:,0] = 1 # Fix the bias unit.
  60. neg_hidden_activations = np.dot(neg_visible_probs, self.weights)
  61. neg_hidden_probs = self._logistic(neg_hidden_activations)
  62. # Note, again, that we're using the activation *probabilities* when computing associations, not the states
  63. # themselves.
  64. neg_associations = np.dot(neg_visible_probs.T, neg_hidden_probs)
  65.  
  66. # Update weights.
  67. self.weights += self.learning_rate * ((pos_associations - neg_associations) / num_examples)
  68.  
  69. error = np.sum((data - neg_visible_probs) ** 2)
  70. #print("Epoch %s: error is %s" % (epoch, error))
  71.  
  72. self.norm_c = self.norm_constant()
  73.  
  74. # TODO: Remove the code duplication between this method and `run_visible`?
  75. def run_hidden(self, data):
  76. """
  77. Assuming the RBM has been trained (so that weights for the network have been learned),
  78. run the network on a set of hidden units, to get a sample of the visible units.
  79.  
  80. Parameters
  81. ----------
  82. data: A matrix where each row consists of the states of the hidden units.
  83.  
  84. Returns
  85. -------
  86. visible_states: A matrix where each row consists of the visible units activated from the hidden
  87. units in the data matrix passed in.
  88. """
  89.  
  90. num_examples = data.shape[0]
  91.  
  92. # Create a matrix, where each row is to be the visible units (plus a bias unit)
  93. # sampled from a training example.
  94. visible_states = np.ones((num_examples, self.num_visible + 1))
  95.  
  96. # Insert bias units of 1 into the first column of data.
  97. data = np.insert(data, 0, 1, axis = 1)
  98.  
  99. # Calculate the activations of the visible units.
  100. visible_activations = np.dot(data, self.weights.T)
  101. # Calculate the probabilities of turning the visible units on.
  102. visible_probs = self._logistic(visible_activations)
  103. # Turn the visible units on with their specified probabilities.
  104. visible_states[:,:] = visible_probs > np.random.rand(num_examples, self.num_visible + 1)
  105.  
  106. # Ignore the bias units.
  107. visible_states = visible_states[:,1:]
  108. return visible_states
  109.  
  110. def run_visible(self, data):
  111. """
  112. Assuming the RBM has been trained (so that weights for the network have been learned),
  113. run the network on a set of visible units, to get a sample of the hidden units.
  114.  
  115. Parameters
  116. ----------
  117. data: A matrix where each row consists of the states of the visible units.
  118.  
  119. Returns
  120. -------
  121. hidden_states: A matrix where each row consists of the hidden units activated from the visible
  122. units in the data matrix passed in.
  123. """
  124.  
  125. num_examples = data.shape[0]
  126.  
  127. # Create a matrix, where each row is to be the hidden units (plus a bias unit)
  128. # sampled from a training example.
  129. hidden_states = np.ones((num_examples, self.num_hidden + 1))
  130.  
  131. # Insert bias units of 1 into the first column of data.
  132. data = np.insert(data, 0, 1, axis = 1)
  133.  
  134. # Calculate the activations of the hidden units.
  135. hidden_activations = np.dot(data, self.weights)
  136. # Calculate the probabilities of turning the hidden units on.
  137. hidden_probs = self._logistic(hidden_activations)
  138. # Turn the hidden units on with their specified probabilities.
  139. hidden_states[:,:] = hidden_probs > np.random.rand(num_examples, self.num_hidden + 1)
  140. # Ignore the bias units.
  141. hidden_states = hidden_states[:,1:]
  142. return hidden_states
  143.  
  144. def norm_constant(self):
  145. """
  146. Calculates the normalization constant
  147. """
  148. sum = 0
  149. for h,v in self.norm_dict:
  150. h = np.array(h); v = np.array(v)
  151. sum += np.dot(np.dot(h.T,self.weights.T), v)
  152. return sum
  153.  
  154. def predict_proba(self, X):
  155. hs = self.run_visible(X)
  156. hs = np.insert(hs, 0, 1,axis=1)
  157. res = []
  158. for i in range(len(X)):
  159. tmp = np.dot(hs[i],self.weights.T)
  160. res.append(np.dot(tmp.T,np.insert(X[i], 0, 1)))
  161. return np.array(res) / self.norm_c
  162.  
  163. def _logistic(self, x):
  164. return 1.0 / (1 + np.exp(-x))
  165.  
  166. if __name__ == '__main__':
  167. from sklearn import neighbors
  168. from sklearn.cross_validation import train_test_split
  169. import numpy as np
  170.  
  171. x = np.loadtxt('binarydigits.txt')
  172. labels = np.ravel(np.loadtxt('bindigitlabels.txt'))
  173. X_train, X_test, y_train, y_test = train_test_split(x, labels, test_size=0.2,random_state=0)
  174. print X_train.shape
  175.  
  176. clfs = {}
  177. for label in [0,5,7]:
  178. x = X_train[y_train==label]
  179. print x.shape
  180. clf = RBM(num_visible = 64, num_hidden = 2, max_epochs = 500)
  181. clf.fit(x)
  182. clfs[label] = clf
  183.  
  184. res = []
  185. for label in [0,5,7]:
  186. res.append(clfs[label].predict_proba(X_test))
  187. res3 = np.argmax(np.array(res).T,axis=1)
  188. res3[res3==1] = 5
  189. res3[res3==2] = 7
  190. res3 = map(lambda x: float(x), res3)
  191. print 'RBM', np.sum(res3==y_test) / float(len(y_test))
  192.  
  193. clf = neighbors.KNeighborsClassifier()
  194. clf.fit(X_train,y_train)
  195. res3 = clf.predict(X_test)
  196. print 'KNN', np.sum(res3==y_test) / float(len(y_test))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement