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
torchTensor.isclose -> ?torch.all -> ?torchTensor.clamp_max -> ?torchTensor.gt -> ?https://pytorch.org/docs/stable/generated/torch.gt.html#torch.gttorchTensor.lt -> ?torchTensor.any -> ?Is there .all() or .any() equivalent in python Tensorflowtorch.unsqueeze -> ?https://pytorch.org/docs/stable/generated/torch.unsqueeze.html#torch.unsqueezetorch.broadcastTensors -> ?torchTensor.dim -> ?How to get the dimensions of a tensor (in TensorFlow) at graph construction time?torch.narrow -> ?https://pytorch.org/docs/stable/generated/torch.narrow.html#torch.narrowtorch.masked_fill -> ?(this)def mask_fill_inf(matrix, mask): negmask = 1 - mask num = 3.4 * math.pow(10, 38) return (matrix * mask) + (-((negmask * num + num) - num))
Figured Out
torch.numel -> tf.shape.num_elementshttps://www.tensorflow.org/api_docs/python/tf/TensorShape#num_elementstorchTensor.norm -> tf.normtorch.cat -> tf.concattorch.prod -> tf.math.reduce_prodtorch.squeeze -> tf.squeezetorch.zeros -> tf.zerostorchTensor.reciprocal -> tf.math.reciprocaltorchTensor.size -> tf.size
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.