본문 바로가기
Tensorflow

Tensorflow vs Pytorch 명령어 비교 -(6)

by 블쭌 2021. 6. 3.
728x90
  • tf.zeros(shape, dtype=tf.dtypes.float32, name=None)
with tf.Session() as sess:
    print(sess.run(tf.zeros([3, 4], tf.int32)))
    print(sess.run(tf.zeros([3, 4], tf.float32)))
    
[[0 0 0 0]
 [0 0 0 0]
 [0 0 0 0]]
 
 [[0. 0. 0. 0.]
 [0. 0. 0. 0.]
 [0. 0. 0. 0.]]

  • torch.zeros(*size, *, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) → Tensor
torch.zeros([3, 4], dtype=torch.int32)
torch.zeros([3, 4], dtype=torch.float32)

tensor([[0, 0, 0, 0],
        [0, 0, 0, 0],
        [0, 0, 0, 0]], dtype=torch.int32)

tensor([[0., 0., 0., 0.],
        [0., 0., 0., 0.],
        [0., 0., 0., 0.]])
        
  • tf.ones_like(input, dtype=None, name=None)
x = tf.constant([[1, 2, 3], [4, 5, 6]])

with tf.Session() as sess:
    print(sess.run(tf.ones_like(x)))
    
[[1 1 1]
 [1 1 1]]

  • torch.ones_like(input, *, dtype=None, layout=None, device=None, requires_grad=False, memory_format=torch.preserve_format) → Tensor
x = torch.tensor([[1, 2, 3], [4, 5, 6]])

torch.ones_like(x)

tensor([[1, 1, 1],
        [1, 1, 1]])

shape와 같은 형대로 1로 값을 mapping 시켜준다. zeros_like도 동일하다


  • tf.math.abs(x, name=None)
x = tf.constant([[-2.25], [-3.75]])

with tf.Session() as sess:
    print(sess.run(tf.abs(x)))
    
[[2.25]
 [3.75]]

  • torch.abs(x)
x = torch.tensor([[-2.25], [-3.75]])

torch.abs(x)

tensor([[2.2500],
        [3.7500]])

  • tf.math.cumsum(x, axis=0, exclusive=False, reverse=False, name=None)
a = tf.constant([[1, 2], [3, 4]])

with tf.Session() as sess:
    print(sess.run(tf.math.cumsum(a, axis=0)))
    print(sess.run(tf.cumsum(a, axis=1)))
    
[[1 2]
 [4 6]]
 
[[1 3]
 [3 7]]

축을 기준으로 누적합을 진행 / tensorflow에서 tf.cumsum을 해도 괜찮고 tf.math.cumsum을 진행해도 괜찮다.


  • torch.cumsum(input, dim, *, dtype=None, out=None) → Tensor
a = torch.tensor([[1, 2], [3, 4]])

print(torch.cumsum(a, axis=0))
print(torch.cumsum(a, axis=1))

tensor([[1, 2],
        [4, 6]])
        
tensor([[1, 3],
        [3, 7]])

 

728x90

댓글