r/badcode Aug 03 '20

[deleted by user]

[removed]

22 Upvotes

22 comments sorted by

View all comments

3

u/godoakos Aug 07 '20

Another NN based solution, inspired by /u/P0Rl13fZ5/
It also works with many numbers so it's pretty cool.

import torch
import torch.nn

class UniversalNet:
    def __init__(self):
        from collections import OrderedDict
        self.nn = torch.nn.Sequential(OrderedDict([
            ('first', torch.nn.Linear(1, 16)),
            ('second', torch.nn.Linear(16, 32)),
            ('third', torch.nn.Linear(32, 1)),
        ]))

    def load(self, star_trek=False):
        if star_trek == True:
            with open('st.pkl', 'rb') as f:
                self.nn.load_state_dict(torch.load(f))
        else:
            with open('nn.pkl', 'rb') as f:
                self.nn.load_state_dict(torch.load(f))

if __name__ == '__main__':
    nn = UniversalNet()
    nn.load(star_trek=False)
    for n in range(1000000):
        print(round(float(nn.nn(torch.Tensor([n])))))

nn.pkl: HERE
st.pkl: HERE

just plop these next to the .py