Search code examples
tensorflowpytorch

Tensorflow to PyTorch


I'm transffering a Tensorflow code to a PyTorch code.
Below lines are the problem I couldn't solve yet.
I'm not familiar with PyTorch so that it's not easy for me to find the matching methods in PyTorch library.
Anyone can help me?
p.s. The shape of alpha is (batch, N).

alpha_cumsum = tf.cumsum(alpha, axis = 1)
len_batch = tf.shape(alpha_cumsum)[0]
rand_prob = tf.random_uniform(shape = [len_batch, 1], minval = 0., maxval = 1.)
alpha_relu = tf.nn.relu(rand_prob - alpha_cumsum)
alpha_index = tf.count_nonzero(alpha_relu, 1)
alpha_hard  = tf.one_hot(alpha_index, len(a))

Solution

  • I've put all your functions followed by the corresponding pytorch function. Most are the same name and put in the pytorch docs (https://pytorch.org/docs/stable/index.html)

    tf.cumsum(alpha, axis = 1)  
    torch.cumsum(alpha, dim=1)
    
    tf.shape(alpha_cumsum)[0]
    alpha_cumsum.shape[0]
    
    tf.random_uniform(shape = [len_batch, 1], minval = 0., maxval = 1.)
    torch.rand([len_batch,1])
    
    tf.nn.relu(rand_prob - alpha_cumsum)
    torch.nn.functional.relu(rand_prob - alpha_cumsum)
    
    tf.count_nonzero(alpha_relu, 1)
    torch.count_nonzero(alpha_relu, dim=1)
    
    tf.one_hot(alpha_index, len(a))
    torch.nn.functional.one_hot(alpha_index, len(a)) # assuming len(a) is number of classes