Convert tensorflow code to pytorch

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

def trial_fun(xs, xt):
xs = xs - tf.reduce_mean(xs, axis=0)
xt = xt - tf.reduce_mean(xt, axis=0)
xs=tf.expand_dims(xs,axis=-1)
xs = tf.expand_dims(xs, axis=-1)
xt = tf.expand_dims(xt, axis=-1)
xt = tf.expand_dims(xt, axis=-1)
xs_1=tf.transpose(xs,[0,2,1,3])
xs_2 = tf.transpose(xs, [0, 2, 3, 1])
xt_1 = tf.transpose(xt, [0, 2, 1, 3])
xt_2 = tf.transpose(xt, [0, 2, 3, 1])
HR_Xs=xsxs_1xs_2 # dim: bLLL
HR_Xs=tf.reduce_mean(HR_Xs,axis=0) #dim: L
L*L
HR_Xt = xt * xt_1 * xt_2
HR_Xt = tf.reduce_mean(HR_Xt, axis=0)
return tf.reduce_mean(tf.square(tf.subtract(HR_Xs, HR_Xt)))

The mapping should be:

  • tf.reduce_mean -> tensor.mean
  • tf.expand_dims -> tensor.expand
  • tf.transpose -> tensor.permute

Let us know, if you have any trouble.

4 Likes

Thank you so much, it works

Is there any mapping list from tensorflow to pytorch? (I cannot search one from google but I guess it mush be existed :zipper_mouth_face:)