After training the autoencoder, say linear 221, 1500, 1500, 1500, 221. Is the next step to compute activations for each datapoint and storing them as their new features ?
In this case: 4500 new features -> activations stacked for each row.
Yes it is.
I have trained the autoencoder, now I am trying to extract features (stacked 4500 activations of each forward pass of input), but getting the following error. Any tips ?
Thanks
class FeatureExtractor(nn.Module):
def __init__(self, submodule, extracted_layers):
super().__init__()
self.submodule = submodule
​
def forward(self, x):
outputs = []
for name, module in self.submodule._modules.items():
x = module(x)
if name in self.extracted_layers:
outputs += [x]
return outputs
In [241]:
.eval()
fextractor = FeatureExtractor(da.eval(), extracted_layers=['fc0', 'fc1', 'fc2'])
In [245]:
np.array
# Get activations for input_
input_arr = np.array(input_)
In [256]:
# example datapoint for feature extraction
fextractor(torch.from_numpy(input_arr[0]).float().unsqueeze(0))
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
<ipython-input-256-36fab960cefd> in <module>()
----> 1 fextractor(torch.from_numpy(input_arr[0]).float().unsqueeze(0))
~/anaconda3/envs/fastai/lib/python3.6/site-packages/torch/nn/modules/module.py in call(self, *input, **kwargs)
222 for hook in self._forward_pre_hooks.values():
223 hook(self, input)
–> 224 result = self.forward(*input, **kwargs)
225 for hook in self._forward_hooks.values():
226 hook_result = hook(self, input, result)
in forward(self, x)
7 outputs = []
8 for name, module in self.submodule._modules.items():
----> 9 x = module(x)
10 if name in self.extracted_layers:
11 outputs += [x]
~/anaconda3/envs/fastai/lib/python3.6/site-packages/torch/nn/modules/module.py in call(self, *input, **kwargs)
222 for hook in self._forward_pre_hooks.values():
223 hook(self, input)
–> 224 result = self.forward(*input, **kwargs)
225 for hook in self._forward_hooks.values():
226 hook_result = hook(self, input, result)
~/anaconda3/envs/fastai/lib/python3.6/site-packages/torch/nn/modules/linear.py in forward(self, input)
51
52 def forward(self, input):
—> 53 return F.linear(input, self.weight, self.bias)
54
55 def repr(self):
~/anaconda3/envs/fastai/lib/python3.6/site-packages/torch/nn/functional.py in linear(input, weight, bias)
551 if input.dim() == 2 and bias is not None:
552 # fused op is marginally faster
–> 553 return torch.addmm(bias, input, weight.t())
554
555 output = input.matmul(weight.t())
~/anaconda3/envs/fastai/lib/python3.6/site-packages/torch/autograd/variable.py in addmm(cls, *args)
922 @classmethod
923 def addmm(cls, *args):
–> 924 return cls._blas(Addmm, args, False)
925
926 @classmethod
~/anaconda3/envs/fastai/lib/python3.6/site-packages/torch/autograd/variable.py in _blas(cls, args, inplace)
918 else:
919 tensors = args
–> 920 return cls.apply(*(tensors + (alpha, beta, inplace)))
921
922 @classmethod
RuntimeError: save_for_backward can only save input or output tensors, but argument 0 doesn’t satisfy this condition