Iはpytorchに訓練されたNNからの重み、バイアス及び活性化関数の少なくとも種類を抽出する必要があります。
私は、コマンドがある重みとバイアスを抽出することを知っています:
model.parameters()
私はlayers.Hereで使用される活性化関数が私のネットワークでも抽出する方法を見つけ出すことはできません
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)
I GET
NetWithODE(
(hidden): Linear(in_features=4, out_features=10, bias=True)
(predict): Linear(in_features=10, out_features=4, bias=True)
)
しかし、ここで私は(この場合はReluで)活性化機能を得ることができますか?
私はpytorch 1.4を持っています。
低レベルの機能的な方法や、より高度なオブジェクト方法:ネットワークグラフへの操作を追加するには、2つの方法があります。あなたの構造が観測可能にするために後者の必要がある、最初のケースではちょうどそれについての情報を格納することなく(...ではない正確に、しかし)関数を呼び出しています。だから、代わりに
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)
でもとにかくものの2つのtheeseの方法の組み合わせは、一般的に悪い考えであるtorchvision
モデルは、このようなinconsistienciesを持っている:models.inception_v3
例えばプーリングを登録していない> :-(。
UPD: -おかげで、私は印刷する場合作品は、今私がReLUを参照することを()。しかし、これは、彼らがで定義されている同じ順序で機能を印刷するようだINIT。層と活性化関数間の関連を取得する方法はありますか?たとえば、私は、上のように、2の端をレイヤーにレイヤー1にapplyedされた活性化を知りたいです...
そこには統一された方法はありませんが、ここではいくつかのトリックです:オブジェクト方法:
-JUSTために、それらを初期化
-use torch.nn.Sequential
そのようなノード上でコールバックを-hook -
def hook( m, i, o):
print( m._get_name() )
for ( mo ) in model.modules():
mo.register_forward_hook(hook)
機能およびオブジェクト方法:
-make往路上に建て内部モデルグラフを使用する、などtorchviz
を行う(https://github.com/szagoruyko/pytorchviz/blob/master/torchviz/dot.py)、または前記によって生成されるだけ使用プロットtorchviz
。