def test_wrapped_normal_StereographicProductManifold(): manifold = geoopt.StereographicProductManifold( (geoopt.PoincareBall(), 2), (geoopt.SphereProjection(), 2), (geoopt.Stereographic(), 2), ) mean = manifold.random(6) point = manifold.wrapped_normal(4, 6, mean=mean) manifold.assert_check_point_on_manifold(point) assert point.manifold is manifold
def __init__(self, curvature: float, dim_hidden: int, **kwargs): print("Constructing PoincareProbeNoSquare") super().__init__(**kwargs) self.ball = gt.Stereographic(curvature) self.dim_hidden = dim_hidden self.proj = nn.Parameter(data=th.zeros(self.dim_in, self.dim_hidden)) self.trans = nn.Parameter(data=th.zeros(self.dim_out, self.dim_hidden)) nn.init.uniform_(self.proj, -0.05, 0.05) nn.init.uniform_(self.trans, -0.05, 0.05)
def stereographic_case(): torch.manual_seed(42) shape = manifold_shapes[geoopt.manifolds.Stereographic] ex = torch.randn(*shape, dtype=torch.float64) / 3 ev = torch.randn(*shape, dtype=torch.float64) / 3 x = ex # default curvature = 0 ex = x.clone() v = ev.clone() manifold = geoopt.Stereographic().to(dtype=torch.float64) x = geoopt.ManifoldTensor(x, manifold=manifold) case = UnaryCase(shape, x, ex, v, ev, manifold) yield case manifold = geoopt.StereographicExact().to(dtype=torch.float64) x = geoopt.ManifoldTensor(x, manifold=manifold) case = UnaryCase(shape, x, ex, v, ev, manifold) yield case
def __init__(self, db_dir='data/database', db_content=True): super(Preprocessor, self).__init__() self.db_dir = db_dir self.db_content = db_content self.nlp = stanza.Pipeline( 'en', processors='tokenize,pos,lemma') #, use_gpu=False) self.stopwords = stopwords.words("english") self.device = torch.device("cuda:0") self.hidden_size = 1024 self.max_batch_size = 8 self.plm_model = AutoModel.from_pretrained( os.path.join('./pretrained_models', 'electra-large-discriminator')).to(self.device) self.plm_tokenizer = AutoTokenizer.from_pretrained( os.path.join('./pretrained_models', 'electra-large-discriminator')) self.config = self.plm_model.config self.ball = gt.Stereographic(-1) self.threshold = 0.4
def test_adam_StereographicProductManifold(params): torch.manual_seed(42) pman = geoopt.StereographicProductManifold( (geoopt.PoincareBall(), 2), (geoopt.SphereProjection(), 2), (geoopt.Stereographic(k=0), 2), ) ideal = pman.random(6) start = geoopt.ManifoldParameter(pman.random(6), manifold=pman) def closure(): optim.zero_grad() loss = pman.dist(start, ideal)**2 loss.backward() return loss.item() optim = geoopt.optim.RiemannianAdam([start], **params) for _ in range(2000): optim.step(closure) np.testing.assert_allclose(start.data, ideal, atol=1e-5, rtol=1e-5)
def __init__(self, curvature: float, dim_hidden: int, **kwargs): super().__init__(**kwargs) self.ball = gt.Stereographic(curvature) self.dim_hidden = dim_hidden