def dot(self, other): """ Matrix or scalar multiplication. Delegate to divisi2.operators to sort it out. """ from divisi2 import operators return operators.dot(self, other)
def test_incremental_svd(): U_sparse, S_sparse, V_sparse = mat_4x3.svd(2) rec = dot(U_sparse * S_sparse, V_sparse.T) rec2 = ReconstructedMatrix.make_random(mat_4x3.row_labels, mat_4x3.col_labels, 2, learning_rate = 0.01) for iter in xrange(1000): for row in xrange(4): for col in xrange(3): rec2.hebbian_step(row, col, mat_4x3[row, col]) print np.linalg.norm(rec2.to_dense() - rec) dense = rec2.to_dense() assert rec.same_labels_as(rec2) assert np.linalg.norm(rec2.to_dense() - rec) < 0.1
def __getitem__(self, indices): if not isinstance(indices, tuple): indices = (indices,) if len(indices) < 2: indices += (SLICE_ALL,) * (2 - len(indices)) leftpart = self.left[indices[0], :] rightpart = self.right[:, indices[1]] if (not np.isscalar(indices[0])) and (not np.isscalar(indices[1])): # slice by slice. Reconstruct a new matrix to avoid huge # computations. return ReconstructedMatrix(leftpart, rightpart, self.shifts) else: # in any other case, just return the dense result row_shift = self.row_shift[indices[0]] col_shift = self.col_shift[indices[1]] return dot(leftpart, rightpart) + row_shift + col_shift + self.total_shift
def __getitem__(self, indices): if not isinstance(indices, tuple): indices = (indices, ) if len(indices) < 2: indices += (SLICE_ALL, ) * (2 - len(indices)) leftpart = self.left[indices[0], :] rightpart = self.right[:, indices[1]] if (not np.isscalar(indices[0])) and (not np.isscalar(indices[1])): # slice by slice. Reconstruct a new matrix to avoid huge # computations. return ReconstructedMatrix(leftpart, rightpart, self.shifts) else: # in any other case, just return the dense result row_shift = self.row_shift[indices[0]] col_shift = self.col_shift[indices[1]] return (dot(leftpart, rightpart) + row_shift + col_shift + self.total_shift)
def right_category(self, vec): return dot(self.left, aligned_matrix_multiply(self.right, vec))
def left_category(self, vec): return dot(aligned_matrix_multiply(vec, self.left), self.right)
def matvec_transp(self, vec): return dot(self.right.T, dot(self.left.T, vec))
def matvec(self, vec): return dot(self.left, dot(self.right, vec))
def to_dense(self): return dot(self.left, self.right)