-
TensorFlow 기초 2 - 변수, 값 치환, 누적(+=)TensorFlow 2022. 11. 28. 13:14
# 변수 : 모델 학습 시, 매개변수 갱신 등을 위해 사용 import tensorflow as tf f = tf.Variable(1.0) v = tf.Variable(tf.ones((2,))) m = tf.Variable(tf.ones((2, 1))) print(f) print(v) print(m) print(m.numpy()) print() v1 = tf.Variable(1) print(v1) v1.assign(10) # 값 치환 print(v1, v1.numpy(), type(v1)) v2 = tf.Variable(tf.ones(shape=(1))) # 1-d tensor v2.assign([20]) print(v2, v2.numpy(), type(v2)) v3 = tf.Variable(tf.ones(shape=(1, 2))) # 2-d tensor v3.assign([[20, 30]]) print(v3, v3.numpy(), type(v3)) print() v1 = tf.Variable([3]) v2 = tf.Variable([5]) v3 = v1 * v2 + 10 print(v3) print() var = tf.Variable([1,2,3,4,5], dtype = tf.float32) result1 = var + 10 print(result1) print() w = tf.Variable(tf.ones(shape=(1,))) b = tf.Variable(tf.ones(shape=(1,))) w.assign([2]) b.assign([3]) def func1(x): return w * x + b out_a1 = func1([[3]]) # x값에 의해서 리턴값의 차원이 달라질 수 있다. print('out_a1 :', out_a1) print() @tf.function # auto graph 기능이 적용된 함수 : tf.Graph + tf.Session이 적용 def func2(x): return w * x + b print(type(func2)) out_a2 = func2([1, 2]) print('out_a1 :', out_a2) print() rand = tf.random.uniform([1], 0, 1) # 균등분포 print(rand.numpy()) rand2 = tf.random.normal([5], mean=0, stddev=1) # 정규분포 print(rand2.numpy()) print() aa = tf.ones((2, 1)) print(aa.numpy()) m = tf.Variable(tf.zeros((2, 1))) print(m.numpy()) m.assign(aa) print(m.numpy()) m.assign_add(aa) # m += aa의 개념 print(m.numpy()) m.assign_sub(aa) # m -= aa의 개념 print(m.numpy()) <console> 2022-11-28 14:41:27.387413: I tensorflow/core/platform/cpu_feature_guard.cc:193] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations: AVX AVX2 To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags. <tf.Variable 'Variable:0' shape=() dtype=float32, numpy=1.0> <tf.Variable 'Variable:0' shape=(2,) dtype=float32, numpy=array([1., 1.], dtype=float32)> <tf.Variable 'Variable:0' shape=(2, 1) dtype=float32, numpy= array([[1.], [1.]], dtype=float32)> [[1.] [1.]] <tf.Variable 'Variable:0' shape=() dtype=int32, numpy=1> <tf.Variable 'Variable:0' shape=() dtype=int32, numpy=10> 10 <class 'tensorflow.python.ops.resource_variable_ops.ResourceVariable'> <tf.Variable 'Variable:0' shape=(1,) dtype=float32, numpy=array([20.], dtype=float32)> [20.] <class 'tensorflow.python.ops.resource_variable_ops.ResourceVariable'> <tf.Variable 'Variable:0' shape=(1, 2) dtype=float32, numpy=array([[20., 30.]], dtype=float32)> [[20. 30.]] <class 'tensorflow.python.ops.resource_variable_ops.ResourceVariable'> tf.Tensor([25], shape=(1,), dtype=int32) tf.Tensor([11. 12. 13. 14. 15.], shape=(5,), dtype=float32) out_a1 : tf.Tensor([[9.]], shape=(1, 1), dtype=float32) <class 'tensorflow.python.eager.polymorphic_function.polymorphic_function.Function'> out_a1 : tf.Tensor([5. 7.], shape=(2,), dtype=float32) [0.35073256] [-2.0095115 -0.3110462 -1.9975616 0.3110392 0.5237793] [[1.] [1.]] [[0.] [0.]] [[1.] [1.]] [[2.] [2.]] [[1.] [1.]]
Variable() 함수는 값을 치환할 경우 assign() 함수를 사용하여 친환하여야 된다. 평범하게 변수를 치환하면 에러를 발생시킨다.
tensor는 tensor끼리 연산해야 더 빠르게 연산된다.
'TensorFlow' 카테고리의 다른 글
TensorFlow 기초 5 - 사칙연산, 관계연산, 논리연산, 차원 축소, 차원 확대, one-hot, argmax (1) 2022.11.28 TensorFlow 기초 4 - 사칙연산, tf.constant(), tf.Variable(), autograph 기능 (0) 2022.11.28 TensorFlow 기초 3 - TF의 구조 (Graph로 설계된 내용은 Session에 실행) (0) 2022.11.28 TensorFlow 기초 1 - TensorFlow에서의 상수 선언(constant) 및 사칙연산, type 변환 (0) 2022.11.28 TensorFlow 설치 (0) 2022.11.28