■ matmul 함수를 사용해 행렬을 곱셈하는 방법을 보여준다.
▶ 예제 코드 (PY)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
import numpy as np import tensorflow as tf ndarray1 = np.array([(1, 2, 3), (4, 5, 6), (7, 8, 9)], dtype = 'int32') ndarray2 = np.array([(1, 0, 0), (0, 1, 0), (0, 0, 1)], dtype = 'int32') tensor1 = tf.constant(ndarray1) tensor2 = tf.constant(ndarray2) tensor3 = tf.matmul(tensor1, tensor2) with tf.Session() as sess: print(sess.run(tensor1)) print(sess.run(tensor2)) print(sess.run(tensor3)) [결과] [[1 2 3] [4 5 6] [7 8 9]] [[1 0 0] [0 1 0] [0 0 1]] [[1 2 3] [4 5 6] [7 8 9]] |