파이썬 딥러닝 ai 스쿨 기초/lecture01

lecture01 2교시 인공지능 텐서플로우 실습

junny1997 2021. 3. 14. 19:39
import tensorflow as tf

node1 = tf.constant(3.0, tf.float32)
node2 = tf.constant(4.0) #암묵적으로 tf.float32(32비트 실수)
node3 = tf.add(node1, node2)

print("node1:", node1, "node2:", node2)

sess = tf.Session()
print("sess.run(node1, node2):",sess.run([node1, node2]))
print("sess.run(node3):", sess.run(node3))

node1: Tensor("Const:0", shape=(), dtype=float32) node2: Tensor("Const_1:0", shape=(), dtype=float32)

 

sess.run(node1, node2): [3.0, 4.0]

sess.run(node3): 7.0

 

텐서플로우는 변수 함수등의 노드를 준비할 필요가 있다

 

노드는 Session을 준비하여 Session 내에서 실행해야한다

 

 

다른 표현법

import tensorflow as tf

a = tf.placeholder(tf.float32)
b = tf.placeholder(tf.float32)
#Placeholder 실행중 값을 변경할 수 있는 가역함수
adder_node = a + b

Sess = tf.Session()

print(Sess.run(adder_node, feed_dict = {a: 3, b: 4.5}))
print(Sess.run(adder_node, feed_dict = {a: [1, 3], b: [2, 4]}))
#feed_dict에 데이터 입력 , 자료 여러개 동시에 가능(인덱스 끼리)

7.5
[3. 7.]

import tensorflow as tf

a = tf.placeholder(tf.float32)
b = tf.placeholder(tf.float32)
y = tf.add(a,b)

sess = tf.Session()

print(sess.run(y, feed_dict = {a: 3, b: 4.5}))

7.5

 

 

tf.constant: 변하지 않는 상수 생성

tf.Variable: 값이 바뀔 수도 있는 변수 생성

tf.placeholder: 일정 값을 받을 수 있게 만들어주는 그릇을 생성(feed_dict = {x = }으로 값지정)

 

텐서플로우 Perceptron 구현

perceptron 출처 AI School

import tensorflow as tf

x_data = [[0.12, 2.4]]
#데이터 준비 x1 x2

X = tf.placeholder(tf.float32, shape = [None, 2])
#shape Nonde(미정 유동)x2 행렬

W = tf.Variable(tf.random_normal([2,1], name = 'weight'))
b = tf.Variable(tf.random_normal([1]), name = 'bias')
#초기값 모름 random_normal로 값 랜덤생성

hypothesis = tf.sigmoid(tf.matmul(X, W)+b)
#hypothesis = tf.relu(tf.matmul(X, W)+b)
#tf.matmul행렬곱

with tf.Session()as sess:
    sess.run(tf.global_variables_initializer())
    #global_variables_initializer로 데이터들 들어감

    prediction = sess.run(hypothesis, feed_dict={X: x_data})
    print(prediction)

[[0.15867561]]