hojeomi blog

1. TensorFlow와 간단한 Linear Regression 본문

AI/Machine & Deep Learning

1. TensorFlow와 간단한 Linear Regression

호저미 2021. 1. 12. 13:54
Tensorflow 1
In [25]:
from IPython.core.display import display, HTML

display(HTML("<style> .container{width:90% !important;}</style>"))


import tensorflow as tf
import os

# info 로그를 필터링 하려면 1, warning 로그는 2, error 로그는 3
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
In [26]:
hellow = tf.constant("hellow!")
print(hellow)
tf.Tensor(b'hellow!', shape=(), dtype=string)
In [ ]:
 
In [27]:
# 앞쪽 레이어
In [28]:
node1 = tf.constant(3.0, tf.float32)
node2 = tf.constant(4.0, tf.float32)
print(node1, node2)
tf.Tensor(3.0, shape=(), dtype=float32) tf.Tensor(4.0, shape=(), dtype=float32)
In [29]:
node3 = node1 + node2
print(node3)
tf.Tensor(7.0, shape=(), dtype=float32)
In [ ]:
 
In [30]:
# 뒷쪽 레이어의 노드를 함수로 정의
In [31]:
@tf.function
def forward():
    return node1 + node2
In [32]:
out_a = forward()
In [33]:
print(out_a)
tf.Tensor(7.0, shape=(), dtype=float32)
In [34]:
if out_a == node3:
    print("오잉")
오잉
In [ ]:
 

(구버전)placeholder -> @tf.function 사용하기

In [35]:
@tf.function
def Add(a, b):
    return a + b
In [36]:
a = tf.constant(1)
b = tf.constant(2)
c = Add(a, b)
print(c)
tf.Tensor(3, shape=(), dtype=int32)
In [37]:
c = tf.constant([1,2])
d = tf.constant([3,4])
e = Add(c,d)
print(e)
tf.Tensor([4 6], shape=(2,), dtype=int32)
In [38]:
f = Add(c,a)
In [39]:
f
Out[39]:
<tf.Tensor: shape=(2,), dtype=int32, numpy=array([2, 3])>
In [ ]:
 
In [ ]:
 
Comments