src/proteingnn/util.py
def graph_saliency_map(model: Any, input: Union[torchg.data.data.Data, torchg.data.batch.Batch], method: str = 'norm')\
        -> torch.Tensor:
    """Calculate saliency map on node features.

    There is no restriction on the model object but only its forward method, i.e.
    model.forward(input.x, input.edge_index, input.batch)

    This function does not support edge features yet.

    Warning: The saliency map only propagates to the input level. If embedding is used, the graph saliency does not
        account for the embedding generator.
    """
    input = input.clone()
    if type(input) == torchg.data.data.Data:
        batch = torch.zeros(input.x.shape[0], dtype=torch.long)
    else:
        batch = input.batch
        if len(batch.unique()) > 1:
            raise ValueError('Only support single-example saliency map.')

    input.x.requires_grad_()
    output = model.forward(input.x, input.edge_index, batch)
    input.x.retain_grad()
    output.backward()
    grad_ = input.x.grad

    if method == 'norm':
        grad = grad_.norm(p=2, dim=1)
    elif method == 'mean':
        grad = grad_.abs().mean(axis=1)
    elif method == 'max':
        grad = grad_.abs().max(axis=1)
    else:
        raise ValueError('Only norm, mean, max supported.')

    return grad
Calculate saliency map on node features.

    There is no restriction on the model object but only its forward method, i.e.
    model.forward(input.x, input.edge_index, input.batch)

    This function does not support edge features yet.

    Warning: The saliency map only propagates to the input level. If embedding is used, the graph saliency does not
        account for the embedding generator.
    
Calculate saliency map on node features.
There is no restriction on the model object but only its forward method, i.e.

This function does not support edge features yet.

The saliency map only propagates to the input level. If embedding is used, the graph saliency does not
account for the embedding generator.
Calculate saliency map on node features . There is no restriction on the model object but only its forward method i . e . This function does not support edge features yet . The saliency map only propagates to the input level . If embedding is used the graph saliency does not account for the embedding generator .
def graph_saliency_map(model: Any, input: Union[torchg.data.data.Data, torchg.data.batch.Batch], method: str = 'norm')\
        -> torch.Tensor:
    input = input.clone()
    if type(input) == torchg.data.data.Data:
        batch = torch.zeros(input.x.shape[0], dtype=torch.long)
    else:
        batch = input.batch
        if len(batch.unique()) > 1:
            raise ValueError('Only support single-example saliency map.')
    input.x.requires_grad_()
    output = model.forward(input.x, input.edge_index, batch)
    input.x.retain_grad()
    output.backward()
    grad_ = input.x.grad
    if method == 'norm':
        grad = grad_.norm(p=2, dim=1)
    elif method == 'mean':
        grad = grad_.abs().mean(axis=1)
    elif method == 'max':
        grad = grad_.abs().max(axis=1)
    else:
        raise ValueError('Only norm, mean, max supported.')
    return grad
def graph_saliency_map ( model : Any , input : Union [ torchg . data . data . Data , torchg . data . batch . Batch ] , method : str = 'norm' ) -> torch . Tensor : input = input . clone ( ) if type ( input ) == torchg . data . data . Data : batch = torch . zeros ( input . x . shape [ 0 ] , dtype = torch . long ) else : batch = input . batch if len ( batch . unique ( ) ) > 1 : raise ValueError ( 'Only support single-example saliency map.' ) input . x . requires_grad_ ( ) output = model . forward ( input . x , input . edge_index , batch ) input . x . retain_grad ( ) output . backward ( ) grad_ = input . x . grad if method == 'norm' : grad = grad_ . norm ( p = 2 , dim = 1 ) elif method == 'mean' : grad = grad_ . abs ( ) . mean ( axis = 1 ) elif method == 'max' : grad = grad_ . abs ( ) . max ( axis = 1 ) else : raise ValueError ( 'Only norm, mean, max supported.' ) return grad
Calculate saliency map on node features.
Calculate saliency map on node features .
"""Calculate saliency map on node features.

    There is no restriction on the model object but only its forward method, i.e.
    model.forward(input.x, input.edge_index, input.batch)

    This function does not support edge features yet.

    Warning: The saliency map only propagates to the input level. If embedding is used, the graph saliency does not
        account for the embedding generator.
    """
model
Any
input
Union[torchg.data.data.Data, torchg.data.batch.Batch]
method
str