예제 #1
0
파일: dense.py 프로젝트: amumu/divisi2
 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)
예제 #2
0
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
예제 #3
0
 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
예제 #4
0
 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)
예제 #5
0
 def right_category(self, vec):
     return dot(self.left, aligned_matrix_multiply(self.right, vec))
예제 #6
0
 def left_category(self, vec):
     return dot(aligned_matrix_multiply(vec, self.left), self.right)
예제 #7
0
 def matvec_transp(self, vec):
     return dot(self.right.T, dot(self.left.T, vec))
예제 #8
0
 def matvec(self, vec):
     return dot(self.left, dot(self.right, vec))
예제 #9
0
 def to_dense(self):
     return dot(self.left, self.right)
예제 #10
0
 def to_dense(self):
     return dot(self.left, self.right)
예제 #11
0
 def right_category(self, vec):
     return dot(self.left, aligned_matrix_multiply(self.right, vec))
예제 #12
0
 def left_category(self, vec):
     return dot(aligned_matrix_multiply(vec, self.left), self.right)
예제 #13
0
 def matvec_transp(self, vec):
     return dot(self.right.T, dot(self.left.T, vec))
예제 #14
0
 def matvec(self, vec):
     return dot(self.left, dot(self.right, vec))