Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Pytork:为什么打印(型号)不显示激活功能?_Python_Pytorch - Fatal编程技术网

Python Pytork:为什么打印(型号)不显示激活功能?

Python Pytork:为什么打印(型号)不显示激活功能?,python,pytorch,Python,Pytorch,我需要从pytorch中经过训练的神经网络中提取权重、偏差和至少激活函数的类型 我知道要提取权重和偏差,命令是: model.parameters() 但我不知道如何提取层上使用的激活函数。这是我的网络 class NetWithODE(torch.nn.Module): def __init__(self, n_feature, n_hidden, n_output, sampling_interval, scaler_features): super(NetWithO

我需要从pytorch中经过训练的神经网络中提取权重、偏差和至少激活函数的类型

我知道要提取权重和偏差,命令是:

model.parameters()

但我不知道如何提取层上使用的激活函数。这是我的网络

class NetWithODE(torch.nn.Module):
    def __init__(self, n_feature, n_hidden, n_output, sampling_interval, scaler_features):
        super(NetWithODE, self).__init__()
        self.hidden = torch.nn.Linear(n_feature, n_hidden)  # hidden layer
        self.predict = torch.nn.Linear(n_hidden, n_output)  # output layer
        self.sampling_interval = sampling_interval
        self.device = torch.device("cpu")
        self.dtype = torch.float
        self.scaler_features = scaler_features

    def forward(self, x):
        x0 = x.clone().requires_grad_(True)
        # activation function for hidden layer
        x = F.relu(self.hidden(x))
        # linear output, here r should be the output
        r = self.predict(x)
        # Now the r enters the integrator
        x = self.integrate(r, x0)

        return x

    def integrate(self, r, x0):
        # RK4 steps per interval
        M = 4
        DT = self.sampling_interval / M
        X = x0

        for j in range(M):
            k1 = self.ode(X, r)
            k2 = self.ode(X + DT / 2 * k1, r)
            k3 = self.ode(X + DT / 2 * k2, r)
            k4 = self.ode(X + DT * k3, r)
            X = X + DT / 6 * (k1 + 2 * k2 + 2 * k3 + k4)

        return X

    def ode(self, x0, r):
        qF = r[0, 0]
        qA = r[0, 1]
        qP = r[0, 2]
        mu = r[0, 3]

        FRU = x0[0, 0]
        AMC = x0[0, 1]
        PHB = x0[0, 2]
        TBM = x0[0, 3]

        fFRU = qF * TBM  
        fAMC = qA * TBM  
        fPHB = qP - mu * PHB
        fTBM = mu * TBM

        return torch.stack((fFRU, fAMC, fPHB, fTBM), 0)
如果我运行命令

print(model)

我明白了

但是在哪里可以获得激活函数(在本例中为Relu)


我有pytorch 1.4。

有两种向网络图添加操作的方法:低级函数方法和高级对象方法。您需要后者使您的结构可观察,在第一种情况下,它只是调用(不完全是,但…)一个函数,而不存储有关它的信息。因此,与其

    def forward(self, x):
    ...
        x = F.relu(self.hidden(x))
一定是这样的

def __init__(...):
    ...
    self.myFirstRelu= torch.nn.ReLU()

def forward(self, x):
    ...
    x1 = self.hidden(x)
    x2 = self.myFirstRelu(x1)
无论如何,混合使用这两种方法通常是个坏主意,尽管即使是
torchvision
模型也有这样的不一致性:
模型。例如,inception_v3
不注册池:-(编辑:它在2020年6月固定,谢谢,mitmul!)


UPD:
-谢谢,这很有效,现在如果我打印我看到了ReLU()。但是这似乎只是按照init中定义的顺序打印函数。有没有办法获得层和激活函数之间的关联?例如,我想知道哪个激活应用于层1,哪个应用于层2结束,等等

没有统一的方法,但这里有一些技巧: 对象方式:

-把它们按顺序排列好

-使用torch.nn

-在这样的节点上钩住回调-

def hook( m, i, o):
    print( m._get_name() )

for ( mo ) in model.modules():
    mo.register_forward_hook(hook)
功能和对象方式:


-使用内部模型图,建立在向前传球的基础上,如
torchviz
do(),或者只使用所述
torchviz

生成的绘图,谢谢,这很有效,现在如果我打印,我会看到ReLU()。但这似乎只是按照
\uuuu init\uuuu
中定义的相同顺序打印函数。有没有办法获取层和激活函数之间的关联?例如,我想知道哪个激活应用于层1,哪个应用于层2,依此类推。。。
def hook( m, i, o):
    print( m._get_name() )

for ( mo ) in model.modules():
    mo.register_forward_hook(hook)