David C. Lay의 Linear Algebra의 1장의 예제를 tensorflow로 확인.

colab으로 간단히 확인 가능함.

import tensorflow as tf
import numpy as np

A = np.array([
    [1 ,-2, 1],
    [0 , 2,-8],
    [-4, 5, 9]
], dtype=float)

cmtx = tf.constant(A)

b = np.array([0,8,-9],dtype=float)

# tf.constant 를 이용. (1)
# b = b.reshape(-1,1)
# bvec = tf.constant(b)

# tf.constant 를 이용. (2)
bvec = tf.constant(b, shape=(3,1))

# tf.Varialbe을 이용.
# b = b.reshape(-1,1)
# bvec = tf.Variable(b, shape=(3,1))

solution = tf.linalg.solve(cmtx,bvec)
solution

결과는 다음과 같음.

<tf.Tensor: shape=(3, 1), dtype=float64, numpy=
array([[29.],
       [16.],
       [ 3.]])>

References

https://www.tensorflow.org/api_docs/python/tf/linalg/solve

 

tf.linalg.solve  |  TensorFlow v2.9.1

Solves systems of linear equations.

www.tensorflow.org

https://gist.github.com/dsaint31x/88ec4f787feba7df702c89b0cb6fd9ae

 

LA_Ch01_01_Ex.ipynb

LA_Ch01_01_Ex.ipynb. GitHub Gist: instantly share code, notes, and snippets.

gist.github.com

 

반응형

+ Recent posts