Python으로 시작하는 TensorFlow
Isaiah Hull
Visiting Associate Professor of Finance, BI Norwegian Business School




#Import constant and add from tensorflow
from tensorflow import constant, add
# Define 0-dimensional tensors
A0 = constant([1])
B0 = constant([2])
# Define 1-dimensional tensors
A1 = constant([1, 2])
B1 = constant([3, 4])
# Define 2-dimensional tensors
A2 = constant([[1, 2], [3, 4]])
B2 = constant([[5, 6], [7, 8]])
# Perform tensor addition with add()
C0 = add(A0, B0)
C1 = add(A1, B1)
C2 = add(A2, B2)
add()는 두 텐서의 원소별 덧셈을 수행합니다
원소별 덧셈은 두 텐서의 형태가 같아야 합니다:
add() 연산자는 오버로드되어 있습니다
원소별 곱셈은 multiply()로 수행합니다
행렬 곱셈은 matmul()로 수행합니다
matmul(A,B)는 A에 B를 곱합니다# Import operators from tensorflow
from tensorflow import ones, matmul, multiply
# Define tensors
A0 = ones(1)
A31 = ones([3, 1])
A34 = ones([3, 4])
A43 = ones([4, 3])
multiply(A0, A0), multiply(A31, A31), multiply(A34, A34)matmul(A43, A34)는 가능, matmul(A43, A43)는 불가reduce_sum()은 텐서의 차원에 따라 합을 구합니다reduce_sum(A)는 A의 모든 차원을 합칩니다reduce_sum(A, i)는 i차원으로 합칩니다# Import operations from tensorflow
from tensorflow import ones, reduce_sum
# Define a 2x3x4 tensor of ones
A = ones([2, 3, 4])
# Sum over all dimensions
B = reduce_sum(A)
# Sum over dimensions 0, 1, and 2
B0 = reduce_sum(A, 0)
B1 = reduce_sum(A, 1)
B2 = reduce_sum(A, 2)
Python으로 시작하는 TensorFlow