yes you can do exactly this. You can use nn.LookupTable for this purpose, but it’s easier to simply write your own cute autograd.Function, like this:

  
      github.com
  
  
    pytorch/pytorch/blob/master/torch/autograd/_functions/basic_ops.py#L41-L54 37

import torch
from ..function import Function, traceable
import math




def sort_args(a, b, key=torch.is_tensor):
return (a, b, True) if key(a) else (b, a, False)




@traceable
class PowConstant(Function):


@staticmethod
def forward(ctx, a, b):
    tensor, ctx.constant, ctx.tensor_first = sort_args(a, b)
    if ctx.tensor_first:
        ctx.save_for_backward(tensor)
        return tensor.pow(ctx.constant)
    else:
        result = torch.pow(ctx.constant, tensor)


  This file has been truncated. show original