https://github.com/cloneofsimo/poly2sop

Transformer takes a polynomial, expresses it as sum of powers.

https://github.com/cloneofsimo/poly2sop

Science Score: 10.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
  • codemeta.json file
  • .zenodo.json file
  • DOI references
  • Academic publication links
    Links to: arxiv.org
  • Academic email domains
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (8.8%) to scientific vocabulary
Last synced: 9 months ago · JSON representation

Repository

Transformer takes a polynomial, expresses it as sum of powers.

Basic Info
  • Host: GitHub
  • Owner: cloneofsimo
  • Language: Python
  • Default Branch: main
  • Size: 3.94 MB
Statistics
  • Stars: 11
  • Watchers: 2
  • Forks: 1
  • Open Issues: 0
  • Releases: 0
Created over 5 years ago · Last pushed over 4 years ago

https://github.com/cloneofsimo/poly2SOP/blob/main/

# poly2SOP
Transformer takes a polynomial, expresses it as sum of powers.  Implemented with pytorch.

## Introduction


Tasks such as https://en.wikipedia.org/wiki/Sums_of_three_cubes, https://en.wikipedia.org/wiki/Taxicab_number and conjectures such as finding non - trivial integer solution of  requires deeper understanding of polynomial's characteristics. 

For example, if one finds non - trivial polynomial with no odd degree that is sum of 2 quintics (), then by clever substitution :


This approach was actually first used by Ramanujan, in his set of parameterized solutions of . Inspired from https://arxiv.org/pdf/1912.01412.pdf, I wondered if deep NNs are capable of such natural polynomial manipulations. Here, we implement seq2seq model, using default pytorch's TransformerEncoder, TransformerDecoder module. Datasets were created with SymPy. ```python from models import SOP chars = list("0987654321-+*()^xy") n_vocab = len(chars) + 2 #One for paddings, one for init token. device = torch.device("cuda:0") model = SOP( d_model = 512, n_head = 8, num_layers = 6, n_vocab = n_vocab, max_len = max_len, chars = chars, device = device ) ``` Simple use case with dataset I've created can be found in the repository too! ```python ... chars = list("0987654321-+*()^xy") n_vocab = len(chars) + 2 model = SOP( d_model = 512, n_head = 8, num_layers = 6, n_vocab = n_vocab, max_len = max_len, chars = chars, device = device ) opt = optim.AdamW(model.parameters(), lr = lr, weight_decay = 1e-10) dataset = eq_dataset(max_len = max_len, chars = chars) dl = DataLoader(dataset, shuffle= True, batch_size= batch_size, drop_last= True, num_workers = 3) criterion = nn.CrossEntropyLoss() model.to(device) for epoch in range(1, epochs + 1): pbar = tqdm(dl) tot_loss = 0 cnt = 0 for (x, yin, yout) in pbar: x = x.to(device) yin = torch.cat([torch.ones(batch_size, 1) * (n_vocab - 1), yin], dim = 1).long() yin = yin.to(device) yout = yout.to(device) y_pred = model(x, yin) loss = criterion(y_pred.view(-1, n_vocab - 1), yout.view(-1)) model.zero_grad() loss.backward() opt.step() tot_loss += loss.item() cnt += 1 pbar.set_description(f"current loss : {tot_loss/cnt:.5f}") eq = "2*y^4-2*y^3-y^2+1" ans = "(1-y^2)^2+(-y^2+y)^2" ral = model.toSOP(eq, gen_len = max_len - 1) print(f'Epoch {epoch} : Loss : {tot_loss/cnt :.5f}, Example : {ral[0]}') ``` You can easily create your own dataset with sympy. ```python x, y = symbols('x y') pol = [x, y, 1, x*y, x*x, y*y] n = 2 def random_function(cr = 2): f = 0 for mo in pol: f = f + mo*ri(-cr, cr) return expand(f) # Later on... f1, f2 = random_function(), random_function() f3 = f1**n + f2**n f4 = expand(f3) FILE_x.write(str(f4).replace(' ', '').replace('**', '^') + '\n') FILE_y.write(str(f3).replace(' ', '').replace('**', '^') + '\n') ```

Owner

  • Name: Simo Ryu
  • Login: cloneofsimo
  • Kind: user
  • Company: Corca AI

Cats are Turing machines cloneofsimo@gmail.com

GitHub Events

Total
Last Year

Issues and Pull Requests

Last synced: over 1 year ago

All Time
  • Total issues: 2
  • Total pull requests: 0
  • Average time to close issues: 21 days
  • Average time to close pull requests: N/A
  • Total issue authors: 2
  • Total pull request authors: 0
  • Average comments per issue: 3.0
  • Average comments per pull request: 0
  • Merged pull requests: 0
  • Bot issues: 0
  • Bot pull requests: 0
Past Year
  • Issues: 0
  • Pull requests: 0
  • Average time to close issues: N/A
  • Average time to close pull requests: N/A
  • Issue authors: 0
  • Pull request authors: 0
  • Average comments per issue: 0
  • Average comments per pull request: 0
  • Merged pull requests: 0
  • Bot issues: 0
  • Bot pull requests: 0
Top Authors
Issue Authors
  • Ritam-M (1)
  • jatan12 (1)
Pull Request Authors
Top Labels
Issue Labels
Pull Request Labels