Guest User

Untitled

a guest
Aug 10th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.81 KB | None | 0 0
  1. class Input(object):
  2. def __init__(self, X):
  3. self.step = 1.0
  4. self.noise = 1.0
  5. self.triuind = (np.arange(23)[:,np.newaxis] <= np.arange(23)[np.newaxis,:]).flatten()
  6. self.max = 0
  7. for _ in range(10): self.max = np.maximum(self.max,self.realize(X).max(axis=0))
  8. X = self.expand(self.realize(X))
  9. X.append(X)
  10. self.nbout = X.shape[1]
  11. self.mean = X.mean(axis=0)
  12. self.std = (X - self.mean).std()
  13. self.X = X
  14.  
  15. def realize(self,X):
  16. def _realize_(x):
  17. inds = np.argsort(-(x**2).sum(axis=0)**.5+np.random.normal(0,self.noise,x[0].shape))
  18. x = x[inds,:][:,inds]*1
  19. x = x.flatten()[self.triuind]
  20. return x
  21. return np.array([_realize_(z) for z in X])
  22.  
  23. def expand(self,X):
  24. Xexp = []
  25. for i in range(X.shape[1]):
  26. for k in np.arange(0,self.max[i]+self.step,self.step):
  27. Xexp += [np.tanh((X[:, i]-k)/self.step)]
  28.  
  29. return np.array(Xexp).T
  30.  
  31. # top level code of module1.py
  32. inp_x = Input(traindata).X # I'm assuming traindata is also a global
  33.  
  34. # later code in the same module
  35. do_stuff(inp_x)
  36.  
  37. # code in other modules can do:
  38. import module1
  39. do_other_stuff(module1.inp_x) # a module's global variables are attributes on the module
  40.  
  41. # functions that use an X value:
  42. def func1(arg1, arg2, X):
  43. do_stuff(X)
  44.  
  45. def func2(X):
  46. do_other_stuff(X)
  47.  
  48. # main module (or wherever you call func1 and func2 from)
  49. from module1 import func1
  50. from module2 import func2
  51.  
  52. def main():
  53. x = Input(traindata).X
  54. func1("foo", "bar", x)
  55. func2(x)
  56.  
  57. inp = Input(traindata) # save the Input instance, not only X
  58.  
  59. # later code:
  60. do_stuff(inp.X) # use the X attribute, as above
  61.  
  62. # other code
  63. do_other_stuff(inp) # pass the instance, so you can use other attributes or methods
Add Comment
Please, Sign In to add comment