Convert tensorflow code to pytorch

Mohamed_Ragab: <p>Hi all, I am trying to convert TensorFlow code to PyTorch. But I don’t have any prior knowledge with TensorFlow, I will be grateful if someone can help with this situation. Here is the code</p>
<p>def trial_fun(xs, xt):<br>
xs = xs - tf.reduce_mean(xs, axis=0)<br>
xt = xt - tf.reduce_mean(xt, axis=0)<br>
xs=tf.expand_dims(xs,axis=-1)<br>
xs = tf.expand_dims(xs, axis=-1)<br>
xt = tf.expand_dims(xt, axis=-1)<br>
xt = tf.expand_dims(xt, axis=-1)<br>
xs_1=tf.transpose(xs,[0,2,1,3])<br>
xs_2 = tf.transpose(xs, [0, 2, 3, 1])<br>
xt_1 = tf.transpose(xt, [0, 2, 1, 3])<br>
xt_2 = tf.transpose(xt, [0, 2, 3, 1])<br>
HR_Xs=xs<em>xs_1</em>xs_2   # dim: b<em>L</em>L<em>L<br>
HR_Xs=tf.reduce_mean(HR_Xs,axis=0)   <span class="hashtag">#dim:</span> L</em>L*L<br>
HR_Xt = xt * xt_1 * xt_2<br>
HR_Xt = tf.reduce_mean(HR_Xt, axis=0)<br>
return tf.reduce_mean(tf.square(tf.subtract(HR_Xs, HR_Xt)))</p>

ptrblck: <p>The mapping should be:</p>
<ul>
<li>
<code>tf.reduce_mean</code> -&gt; <code>tensor.mean</code>
</li>
<li>
<code>tf.expand_dims</code> -&gt; <code>tensor.expand</code>
</li>
<li>
<code>tf.transpose</code> -&gt; <code>tensor.permute</code>
</li>
</ul>
<p>Let us know, if you have any trouble.</p>

Mohamed_Ragab: <p>Thank you so much, it works</p>

JTiC: <p>Is there any mapping list from tensorflow to pytorch? (I cannot search one from google but I guess it mush be existed <img src="https://discuss.pytorch.org/images/emoji/apple/zipper_mouth_face.png?v=9" title=":zipper_mouth_face:" class="emoji" alt=":zipper_mouth_face:">)</p>

manoj_batra: <p>If the input tensor becomes empty torch.max(), will give an error vs tf.reduce_max will give -inf.</p>
<p>Is there someway we can retain the same behavior as tf.</p>
<p>Example:<br>
torch.max(torch.tensor([]))<br>
RuntimeError: max(): Expected reduction dim to be specified for input.numel() == 0. Specify the reduction dim with the ‘dim’ argument.</p>
<p>tf.reduce_max(tf.constant([]))<br>
&lt;tf.Tensor: shape=(), dtype=float32, numpy=-inf&gt;</p>

ptrblck: <p>Could you explain why a <code>-Inf</code> return value makes sense for the <code>max</code> operation of en empty tensor?<br>
I can see why raising an error makes sense, but I’m unsure how the <code>-Inf</code> is defined.</p>

manoj_batra: <p>Couple of things IMHO, may be they are not strong enough reasons.</p>
<p>a.<br>
Other operations on empty tensor:<br>
torch.mean(torch.tensor([]))<br>
tensor(nan)<br>
torch.sum(torch.tensor([]))<br>
tensor(0.)</p>
<p>The behaviour for these reduction operations is not consistent with max.</p>
<p>b.<br>
Like in sum output is zero, if you use this further with other reduction method or use the value then it will be consistent, without any side effects.</p>
<p>Similarly, for max → -Inf means max is smallest possible value and if we use this with lets say another max(max(empty), [1,2,3]) then the output will be consistent without any side effects.</p>
<p>Same logic for min → +Inf</p>
<p>c. I faced this issue while migrating network/training from TensorFlow to PyTorch, in such cases if we do not have identical behaviour then there are high chances of the migrated code having side effects, as we need to modify the code elsewhere to ensure we get similar behaviour.</p>

ptrblck: <p>Thanks for the explanation. I believe PyTorch sticks to the <code>numpy</code> reference, which shows the same behavior:</p>
<pre><code class="lang-python">np.mean(np.array([]))
# nan
np.sum(np.array([]))
# 0.0
np.max(np.array([]))
# ValueError: zero-size array to reduction operation maximum which has no identity
</code></pre>
<p>In any case, I think you should create a feature request on <a href="https://github.com/pytorch/pytorch/issues">GitHub</a> as your use case of having a “consistent” user interface makes sense.</p>

manoj_batra: <p>Thanks for the numpy reference, now I can understand the logic to keep things this way in PyTorch.</p>
<p>But yes in my case i will need the behaviour to be consistent with tensorflow rather than numpy.</p>

abushaim: <p>Sorry for re-opening this thread after a while, but I wanted to share a potentially useful tool for those who may still be encountering issues with code conversion. As someone working with Unify, the team behind the open-source tool Ivy, I’d like to suggest considering Ivy for converting TensorFlow code to PyTorch. Ivy aims to simplify this process by providing a unified API, which could make the transition smoother and more efficient. For more information, you can check out Ivy’s <a href="https://unify.ai/docs/ivy/" rel="noopener nofollow ugc">documentation</a> or the <a href="https://github.com/unifyai/ivy" rel="noopener nofollow ugc">repository</a>.</p>