How to use a variable to represent input’s data in a forward_hook?

I used forward_hook this function, it seems only print input and output, if I want to call the input and output data with variables that should I do? I tried to return input in the printnorm function, but failed.

> def printnorm(self,input,output):
>     print('Inside '+ self.__class__.__name__ +'forward')
>     print('')
>     print('input: ',input)
> model.conv2.register_forward_hook(printnorm)
> out = model(input)

Do you want to fetch the input and output of conv2?

tem_input = torch.zeros(input.size())
def fun(model, input, output): tmp_input.copy_(input.data)
hook = model.conv2.register_forward_hook(fun)
model(x)
hook.remove()
  • Do not do change variable in hook, that is unsafe.If you really need a variable, maybe you need write it in forward.
  • the hook return nothing
  • remember to remove the hook after using it.

yes,I want to fetch the input and output of each layer, I try to use the global variable in the fun function to fetch, but the fetched variable can not be changed to numpy, suggesting that ‘tuple’ object has no attribute ‘numpy’ In the following code

def fun(self,input,output):
    print('Inside '+ self.__class__.__name__ +'forward')
    print('')
    #print('input: ',input)
    global xg
    xg=input
model.conv2.register_forward_hook(fun)
out = model(input)
xi=xg
xi=xi.numpy()

so,If hook can not solve my problem, there are other ways to solve my problem?
forward can fetch the input and output of each layer? thank you very much

it should be

xi[0].data.numpy()

Thank you for your reply.I solved the above problem! I also have a question that I wish you can help me.
I have seen this Questions about ImageFolder about imagefolder. But I’m not really understand.
I have a picture datasets. Divided into trian and val two files Folder. Each folder has 0 and 1 subfolders.

I also create lables.txt about train and val. How do I load this dataset into pytorch?

Are the images in subfolders 0 all belong to class 0?
if So, simply use ImageFolder

train_dataset = ImageFolder('train',Transofroms)
data,label = train_dataset[100]# the 100th picture
train_loader = Dataloader(train_dataset)
for data,label in train_loader:
    train()

have a look at the example of Imagenet

yes, subfolders 0,1 represent class 0,1.
So I only need to use imagefolder without having to use torch.utils.data.DataLoader?

dataset only return one sample at a time, you need loader to cat them into a batch. also dataloader can utilize multiprocessing to speed up the program.