텐서플로 기초
텐서플로 설치
!pip install tensorflow
텐서플로 불러오기
import tensorflow as tf
버전 확인
tf.__version__ # 밑줄(_)이 2개씩 붙음
'2.0.0'
텐서
import numpy as np
a = np.array([1, 2, 3], dtype='float32')
t = tf.Variable(a)
t.shape
TensorShape([3])
텐서를 배열로
t.numpy()
array([1., 2., 3.], dtype=float32)
텐서 계산
t + 1
<tf.Tensor: id=26, shape=(3,), dtype=float32, numpy=array([2., 3., 4.], dtype=float32)>
tf.sqrt(t)
<tf.Tensor: id=28, shape=(3,), dtype=float32, numpy=array([1. , 1.4142135, 1.7320508], dtype=float32)>
자동 미분
x = tf.Variable(1, dtype='float32')
with tf.GradientTape() as tape:
y = x ** 2
tape.gradient(y, x)
<tf.Tensor: id=57, shape=(), dtype=float32, numpy=2.0>