본문 바로가기

개발하자/python

18.05.30 파이선기초

# -*- coding: utf-8 -*-  #주석이지만 파이선이 인지하는 주석이다. 빠지면 절대안됨
"""
Spyder Editor

This is a temporary script file.
"""


import tensorflow as tf
#tensorflow라는 클래스를 임포트하고 as는 별칭지정

 

sess = tf.Session()
#텐서플로우는 세션을 무조건 열고 닫아야한다. (여기서는 열기만)
#여기서 세션은 공간이다. 세션을 열어야만 실제로 동작이 가능하다.

 

hello = tf.constant('Hello')
#constant는 상수를 선언하는 함수
#Hello라는 정해진 문자를 Hello라는 상수로 선언

 

print(hello)
#Tensor("Const_1:0", shape=(), dtype=string)이렇게 출력되는데
#세션을 열지않고 출력하여 텐서그대로 (자료형그대로) 출력되는것이다.

 

print(sess.run(hello)) 
#세션에 접속하고 run이란 함수로 실행
#b'Hello'라고 출력되는데 순수스트링이 아니라 바이너리로 출력된다.

 

print(str(sess.run(hello),encoding = 'utf-8'))
#유니코드로 바꾸라는 것 -> 원하는 값인 Hello가 출력된다.
#str는 바이너리를 스트링으로 형변환하라는것이고 변환시 utf-8로 바꾸라는 것이다.
#print(str(sess.run(hello),'utf-8')) 이렇게 하여도 된다.


#출력결과
#Tensor("Const_1:0", shape=(), dtype=string)
#b'Hello'
#Hello

 

 

'''
여기는 주석이다.
'''

 

 

 

================================================================

# -*- coding: utf-8 -*-
"""
Created on Wed May 30 11:15:40 2018

@author: goo30
"""

import tensorflow as tf

a = tf.constant(1)
print(a)

with tf.Session() as sess: #with를 쓰면 유효한 범위내에서 세션사용후 알아서 닫는다.
    print(a.eval())
   
    #ㅇㅇㅇㅇ #만약 이렇게 되있으면 이전에 세션은 닫힌다. (with덕분에 알아서 닫힘)

 

 

=========================================================

 

# -*- coding: utf-8 -*-
"""
Created on Wed May 30 11:19:57 2018

@author: goo30
"""

import tensorflow as tf

x=tf.constant(35,name='x') #이름은 x고 값은 35인 상수를 선언한것이다.
y=tf.Variable(x+5,name='y') #name은 자료구조내에서의 이
#

model = tf.global_variables_initializer()
#실행영역에 값을 전달함

with tf.Session() as session:
    session.run(model)   #런타임의 실행인자로 model를 넘김
    print(session.run(y))
    #끝나면 세션이 알아서 닫힘

 

==================================================================

 

# -*- coding: utf-8 -*-
"""
Created on Wed May 30 11:25:19 2018

@author: goo30
"""
import tensorflow as tf

x2 = tf.linspace(-1.0, 1.0, 10) #-1부터 1사이에 10개 선형으로 내놓으라는 뜻
print(x2)
#Tensor("LinSpace_1:0", shape=(10,), dtype=float32)

g = tf.get_default_graph()
#그래프 알고리즘을 통해서 추적

print([op.name for op in g.get_operations()])
#g.get_operations()는 g가 가지고 있는 실제계산하는것을 돌려서 op에 넣고 name을 출력하라

sess = tf.Session() #with를 사용하지 않고 명시적으롤 세션을 열고
print(sess.run(x2))
sess.close()    #명시적으로 세션을 닫음


 

 

===================================================================

 

# -*- coding: utf-8 -*-
"""
Created on Wed May 30 11:35:04 2018

@author: goo30
"""

import tensorflow as tf

a = tf.add(1,2,)
b = tf.multiply(a,3)
c = tf.add(4,5,)
d = tf.multiply(c,6,)
e = tf.multiply(4,5,)
f = tf.div(c,6,)
g = tf.add(b,d)
h = tf.multiply(g,f)

with tf.Session() as sess:
    writer = tf.summary.FileWriter(r"c:\tensorlog",sess.graph)
    #r은 역슬래쉬대신이라고 보면
    print(sess.run(h))
    writer.close()
   
    #pip install tensorboard 로 설치후 실행창에서 아래명령어 입력
    #tensorboard --logdir="c:\tensorlog"