0

I am looking to port something from PyTorch to Tensorflow, and could use some help in making sure I get the functions mapped correctly from one framework to the other. I have already started, for example both frameworks have the same torch.where and tf.where function, and torchTensor.clamp is tf.clipByValue. But some of the others are harder to find and I'm not sure the exact mapping.

Questionable

Figured Out

Basically I am porting this file form PyTorch to TensorFlow, so these are the functions it uses.

Interestingly, ChatGPT gave me this for narrow after a few tries:

def narrow(tensor, dim, start, size):
    if dim < 0:
        dim = tensor.shape.rank + dim
    begin = [0] * dim + [start] + [0] * (tensor.shape.rank - dim - 1)
    size = [-1] * dim + [size] + [-1] * (tensor.shape.rank - dim - 1)
    return tf.slice(tensor, begin, size)

def _sproj(x, k, dim=-1):
    inv_r = tf.sqrt(tf.abs(k))
    last_element = narrow(x, dim, -1, 1)
    proj = narrow(x, dim, 0, x.shape[dim] - 1)
    factor = 1.0 / (1.0 + inv_r * last_element)
    return factor * proj

Is that correct?

GPT also says:

The comparison operator gt in PyTorch returns a tensor with the same shape as the input tensor, containing boolean values indicating whether the corresponding element in the input tensor is greater than the given value, whereas in TensorFlow the greater function returns a boolean tensor with the same shape as the input tensors, containing the element-wise comparison result. To translate this specific line of code to TensorFlow, you would use tf.math.greater(k, 0) which returns a boolean tensor, then use tf.reduce_any() to check if any of the values in the boolean tensor are True.

Lance
  • 75,200
  • 93
  • 289
  • 503
  • Actually using ChatGPT gets me 90% of the way there, though they are missing some still like `isclose`. – Lance Jan 25 '23 at 06:32

1 Answers1

0

If you use latest tf version there is tf.experimental.numpy.isclose that should do the trick.

Igor
  • 130
  • 1
  • 6