2015-09-07 61 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