I used the pytorch’s hook ‘register_forward_hook’ to get the feature map of the middle layer output. The loss calculation uses the feature map.Here is my loss function：
class StudentLoss(nn.Module):
    def __init__(self):
        super(StudentLoss, self).__init__()    
    def forward(self, tfmaps_list, sfmaps_list, open_conv):
        tfmaps_list_new = []
        ## get specific feature maps
        for idl in range(0, len(sfmaps_list)):
            sfmaps = sfmaps_list[idl]
            tfmaps = tfmaps_list[idl]
            tfmaps_new = torch.FloatTensor(sfmaps_list[idl].shape[0], sfmaps_list[idl].shape[1],
                                           sfmaps_list[idl].shape[2], sfmaps_list[idl].shape[3]).zero_()
            tfmaps_new.requires_grad_()
            for idx in range(0, sfmaps.shape[0]):
                for idy in range(0, sfmaps.shape[1]):
                    tfmaps_new[idx][idy] = tfmaps[idx][open_conv[idl % 3][idy]]
            tfmaps_list_new.append(tfmaps_new)

        fmaps_losses = torch.FloatTensor(1).zero_().cuda()
        for idl in range(0, len(sfmaps_list)):
            sfmaps = sfmaps_list[idl]
            tfmaps = tfmaps_list_new[idl]
            fmaps_loss = (torch.pow(F.pairwise_distance(sfmaps.view(sfmaps.shape[0], -1).cuda(), \
                                                        tfmaps.view(tfmaps.shape[0], -1).cuda()), 2))

            fmaps_losses += fmaps_loss.sum()

	return fmaps_losses


And the part of my main function is here:
result_tfeature = []
result_sfeature = []


def get_tfeature_hook(self, input, output):
    result_tfeature.append(output)

def get_sfeature_hook(self, input, output):
    result_sfeature.append(output)

def set_optimizer(model, lr_base, lr_mult=opts['lr_mult'], momentum=opts['momentum'], w_decay=opts['w_decay']):
    params = model.get_learnable_params()
    param_list = []
    for k, p in params.iteritems():
        lr = lr_base
        for l, m in lr_mult.iteritems():
            if k.startswith(l):
                lr = lr_base * m
        param_list.append({'params': [p], 'lr': lr})
    optimizer = optim.SGD(param_list, lr=lr, momentum=momentum, weight_decay=w_decay)
    return optimizer


def train_smdnet():

    ## Init dataset ##
    with open(data_path, 'rb') as fp:
        data = pickle.load(fp)

    ## Init model ##
    tmodel = MDNet(opts['model_path'], K)
    smodel = SMDNet(opts['init_model_path'], K)
    if opts['use_gpu']:
        tmodel = tmodel.cuda()
        smodel = smodel.cuda()
    smodel.set_learnable_params(['conv'])

    ## Set teacher and student network's hook to get feature maps ##
    tConv = torch.nn.ModuleList()
    for lname, list in tmodel.named_children():
        if lname in ['layers']:
            for name, module in list.named_children():
                if name in ['conv1','conv2','conv3']:
                    tConv.append(module)
    thandle_feat_conv1 = tConv[0].register_forward_hook(get_tfeature_hook)  # conv1
    thandle_feat_conv2 = tConv[1].register_forward_hook(get_tfeature_hook)  # conv1
    thandle_feat_conv3 = tConv[2].register_forward_hook(get_tfeature_hook)  # conv1

    sConv = torch.nn.ModuleList()
    for lname, list in smodel.named_children():
        if lname in ['layers']:
            for name, module in list.named_children():
                if name in ['conv1', 'conv2', 'conv3']:
                    sConv.append(module)
    shandle_feat_conv1 = sConv[0].register_forward_hook(get_sfeature_hook)  # conv1
    shandle_feat_conv2 = sConv[1].register_forward_hook(get_sfeature_hook)  # conv1
    shandle_feat_conv3 = sConv[2].register_forward_hook(get_sfeature_hook)  # conv1

    ## Init criterion and optimizer ##
    criterion = StudentLoss()
    evaluator = Precision()
    optimizer = set_optimizer(smodel, opts['lr'])

    best_prec = 0.
    for i in range(opts['n_cycles']):
        print "==== Start Cycle %d ====" % (i)
        k_list = np.random.permutation(K)
        prec = np.zeros(K)
        for j, k in enumerate(k_list):
            tic = time.time()
            pos_regions, neg_regions = dataset[k].next()

            pos_regions = Variable(pos_regions)
            neg_regions = Variable(neg_regions)

            if opts['use_gpu']:
                pos_regions = pos_regions.cuda()
                neg_regions = neg_regions.cuda()

            pos_score = smodel(pos_regions, k)
            neg_score = smodel(neg_regions, k)
            tmodel(pos_regions, k, out_layer='conv3')
            tmodel(neg_regions, k, out_layer='conv3')

            loss = criterion(result_tfeature, result_sfeature, smodel.open_conv)

            smodel.zero_grad()
            loss.backward()

            torch.nn.utils.clip_grad_norm(smodel.parameters(), opts['grad_clip'])
            optimizer.step()

            # remove hook
            thandle_feat_conv1.remove()
            thandle_feat_conv2.remove()
            thandle_feat_conv3.remove()
            shandle_feat_conv1.remove()
            shandle_feat_conv2.remove()
            shandle_feat_conv3.remove()

            prec[k] = evaluator(pos_score, neg_score)