2015-09-07 60 views
-3

我有这class InputModel.py中定义,现在在同一个文件中我有另一个类和一个单独的功能,在这两个我叫class Input多个类调用相同的输出

Model.py我有2类 - MLPInput和一个单独的function iter_minibatches。在MLP类中,我使用这个语句Inp = Input(traindata).X,它返回一个二维数组(例如5000 * 2100)。然后在另一个模块base.py我调用function iter_minibatches其中我想Inp相同的值作为输入传递给函数(iter_minibatches)或我想要在函数的开头(iter_minibatches)分配相同的值,所以我尝试调用iter_minibatches中的同一个类,但是因为我使用随机函数进行排列,所以得到不同的输出(例如5000 * 2102),但我的要求是在函数iter_minibatches中也获得相同的值。

现在我想要同样的值X应该从这两个调用中返回。那我该怎么做?

class Input(object): 
    def __init__(self, X): 
     self.step = 1.0 
     self.noise = 1.0 
     self.triuind = (np.arange(23)[:,np.newaxis] <= np.arange(23)[np.newaxis,:]).flatten() 
     self.max = 0 
     for _ in range(10): self.max = np.maximum(self.max,self.realize(X).max(axis=0)) 
     X = self.expand(self.realize(X)) 
     X.append(X) 
     self.nbout = X.shape[1] 
     self.mean = X.mean(axis=0) 
     self.std = (X - self.mean).std() 
     self.X = X 

    def realize(self,X): 
     def _realize_(x): 
      inds = np.argsort(-(x**2).sum(axis=0)**.5+np.random.normal(0,self.noise,x[0].shape)) 
      x = x[inds,:][:,inds]*1 
      x = x.flatten()[self.triuind] 
      return x 
     return np.array([_realize_(z) for z in X]) 

    def expand(self,X): 
     Xexp = [] 
     for i in range(X.shape[1]): 
      for k in np.arange(0,self.max[i]+self.step,self.step): 
       Xexp += [np.tanh((X[:, i]-k)/self.step)] 

     return np.array(Xexp).T 
+1

这是不明确的。请详细说明您想要达到的目标以及您期望的输出。 –

+0

我不确定我是否理解这个问题。你想要从哪个电话返回?哪个'X'(你在展示的代码的不同时间分配给'X'的几个不同的值)?如果你正在讨论调用类(如'foo = Input(something)'),你将得到'Input'实例,而不是任何返回值。您可以将该实例分配给一个变量并访问其方法和属性(包括'X'属性)。 – Blckknght

+0

在Model.py我有2个类 - MLP和输入和一个单独的函数iter_minibatches。 在类MLP,我使用此语句INP =输入(traindata).X它返回一个2维阵列 然后在功能iter_minibatches我想有INP的相同的值,所以我试图调用同一类,但因为我使用随机函数获得不同的输出 但我的要求是在函数中获得相同的值iter_minibatches –

回答

1

你的问题似乎主要是关于如何在位于不同模块中的几个函数之间共享一段数据。有两种好方法可以解决这个问题:

首先,您可以将数据存储在全局变量中,并在需要时随时访问它。例如:

# top level code of module1.py 
inp_x = Input(traindata).X # I'm assuming traindata is also a global 

# later code in the same module 
do_stuff(inp_x) 

# code in other modules can do: 
import module1 
do_other_stuff(module1.inp_x) # a module's global variables are attributes on the module 

第二个选项是在你的程序的某些特定部分,以创建数据并将其存储在本地,然后把它传递给每一个需要使用它的其他地方的。这使您可以对代码使用更一般的结构,并且可以在不同的时间传递不同的值。下面是可能的样子:

# functions that use an X value: 
def func1(arg1, arg2, X): 
    do_stuff(X) 

def func2(X): 
    do_other_stuff(X) 

# main module (or wherever you call func1 and func2 from) 
from module1 import func1 
from module2 import func2 

def main(): 
    x = Input(traindata).X 
    func1("foo", "bar", x) 
    func2(x) 

在这些例子中,我只保存(或作为参数传递)这是在Input类,这似乎计算X值是你如何使用也是类。

这有点傻。如果你不需要保留Input的实例,那么你最好不要让Input成为一门课。相反,使它成为一个函数,最后返回X值。您将在realizeexpand函数之间传递更多参数,但代码总体上可能更清晰。

。另一方面,如果Input的情况下,有一些其他用途(你只是在你的例子没有显示),这可能是有意义的保存你创建的,而不是它的X属性实例:

inp = Input(traindata) # save the Input instance, not only X 

# later code: 
do_stuff(inp.X) # use the X attribute, as above 

# other code 
do_other_stuff(inp) # pass the instance, so you can use other attributes or methods