def test_softmax2(self): x = np.ones(10) x[0] = 2 self.assertTrue(np.argmax(softmax(x)) == 0) x[0] = 1 x[5] = 2 self.assertTrue(np.argmax(softmax(x)) == 5)
def get_matrix_and_derivatives(self, x): """Produces the circuit matrix and partials for this gate.""" alpha, l = self.partition_input(x) l = utils.softmax(l, 10) Hv = [] stride = self.working_sigmav.shape[1] for i in range(self.working_sigmav.shape[0]): Hv.append( utils.dot_product(alpha[i * stride:(i + 1) * stride], self.working_sigmav[i])) H = utils.dot_product(l, Hv) # Partials of H with respect to function variables alpha_der = np.array([ l[i] * self.working_sigmav[i] for i in range(self.get_location_count()) ]) alpha_der = alpha_der.reshape( (-1, alpha_der.shape[-2], alpha_der.shape[-1])) # Partials of H with respect to location variables L = np.tile(l, (self.working_sigmav.shape[0], 1)) L = np.identity(self.working_sigmav.shape[0]) - L L = 10 * (np.diag(l) @ L) l_der = np.array([utils.dot_product(Lr, Hv) for Lr in L]) _, dav = utils.dexpmv(H, alpha_der) _, dlv = utils.dexpmv(H, l_der) return sp.linalg.expm(H), np.concatenate([dav, dlv])
def get_matrix ( self, x ): """Produces the circuit matrix for this gate.""" alpha, l = self.partition_input( x ) l = utils.softmax( l, 10 ) H = utils.dot_product( alpha, self.sigmav ) U = sp.linalg.expm( H ) P = utils.dot_product( l, self.working_perms ) return P @ np.kron( U, self.I ) @ P.T
def get_matrix(self, x): """Produces the circuit matrix for this gate.""" alpha, l = self.partition_input(x) l = utils.softmax(l, 10) Hv = [] stride = self.working_sigmav.shape[1] for i in range(self.working_sigmav.shape[0]): Hv.append( utils.dot_product(alpha[i * stride:(i + 1) * stride], self.working_sigmav[i])) H = utils.dot_product(l, Hv) return sp.linalg.expm(H)
def get_matrix_and_derivatives ( self, x ): """Produces the circuit matrix and partials for this gate.""" alpha, l = self.partition_input( x ) l = utils.softmax( l, 10 ) H = utils.dot_product( alpha, self.sigmav ) P = utils.dot_product( l, self.working_perms ) U = np.kron( sp.linalg.expm( H ), self.I ) PU = P @ U UP = U @ P.T PUP = PU @ P.T _, dav = utils.dexpmv( H, self.sigmav ) dav = np.kron( dav, self.I ) dav = P @ dav @ P.T dlv = self.working_perms @ UP + PU @ self.working_perms.transpose( ( 0, 2, 1 ) ) - 2*PUP dlv = np.array( [ x*y for x, y in zip( 10*l, dlv ) ] ) return PUP, np.concatenate( [ dav, dlv ] )
def test_softmax1(self): for i in range(10): x = 10 * np.random.random(100) self.assertTrue(np.abs(np.sum(softmax(x)) - 1) < 1e-15)