TechTogetWorld

인공지능의 실전입문에 관한 글입니다.

알고리즘을 만드는것이 아니고, 만들어진 알로리즘을 활용하는 방법에 관한 글입니다.

자동차 운전을 위해 자동차를 만드는방법을 알필요는 없습니다. 물론 알면 좋기는 하겠지만, 서로 별도의 분야라고 할수있습니다.


본글은 학습용 데이터를 학습/테스트/측정하는 방법에 대한 글입니다.

글의 순서는 아래와 같습니다.


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


1. 외국어 문장 판별  => [ # 170814 7 xor-train ]

   . count=list(map(lambda n : n/total,count)) ==> 주의 " list " 를 넣치 않으면 error 발생

   . 언어별 알파벳 사용횟수가 틀리다는 점을 이용해서, 문장을 주고 영어인지 프랑스어인지 등 구분하는 알고리즘임. 유첨(학습,테스트 데이터)

lang.zip


2. 외국어별 알파벳 사용빈도을 그래프로 표식하여 비교 ==> [ 170816 3 lang-train graph ]

3. 독버섯과 식용버섯 구분==> [ #170819 3 mushroom-train ]

170819 1 mushroom.csv


4. 참고자료


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


[ # 170816 2 lang-train ]



from sklearn import svm, metrics

import glob, os.path ,re, json


files=glob.glob("./lang/train/*.txt")

train_data=[]

train_label=[]

for file_name in files :

    basename=os.path.basename(file_name)

    lang=basename.split("-")[0]

    file=open(file_name,"r",encoding="utf-8")

    text=file.read()

    text=text.lower()

    file.close()

    code_a = ord("a")

    code_z = ord("z")

    count = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]

    for character in text:

        code_current=ord(character)

        if code_a<=code_current <=code_z :

            count[code_current-code_a] +=1

    total=sum(count)

    count=list(map(lambda n : n/total,count))

#리스트에 넣기

    train_label.append(lang)

    train_data.append(count)

#print(train_data)

#print("=========================================================================")

#print(train_label)


files=glob.glob("./lang/test/*.txt")

test_data=[]

test_label=[]

for file_name in files :

    basename=os.path.basename(file_name)

    lang=basename.split("-")[0]

    file=open(file_name,"r",encoding="utf-8")

    text=file.read()

    text=text.lower()

    file.close()

    code_a = ord("a")

    code_z = ord("z")

    count = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]

    for character in text:

        code_current=ord(character)

        if code_a<=code_current <=code_z :

            count[code_current-code_a] +=1

    total=sum(count)

    count=list(map(lambda n : n/total,count))

#리스트에 넣기

    test_label.append(lang)

    test_data.append(count)

#print(train_data)

#print("=========================================================================")

#print(train_label)


clf = svm.SVC()

clf.fit(train_data, train_label)

predict=clf.predict(test_data)

score=metrics.accuracy_score(test_label, predict)

print("score=",score)

report=metrics.classification_report(test_label, predict)

print("--------report-----------")

print(report)



[ 170816 3 lang-train graph ]


from sklearn import svm, metrics

import glob, os.path ,re, json

import matplotlib.pyplot as plt

import pandas as pd



files=glob.glob("./lang/train/*.txt")

train_data=[]

train_label=[]

for file_name in files :

    basename=os.path.basename(file_name)

    lang=basename.split("-")[0]

    file=open(file_name,"r",encoding="utf-8")

    text=file.read()

    text=text.lower()

    file.close()

    code_a = ord("a")

    code_z = ord("z")

    count = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]

    for character in text:

        code_current=ord(character)

        if code_a<=code_current <=code_z :

            count[code_current-code_a] +=1

    total=sum(count)

    count=list(map(lambda n : n/total,count))

#리스트에 넣기

    train_label.append(lang)

    train_data.append(count)


# 그래픽 준비하기

graph_dict={}

for i in range(0,len(train_label)):

    label = train_label[i]

    data = train_data[i]

    if not (label in graph_dict) :

        graph_dict[label]=data

asclist=[[chr(n) for n in range(97,97+26)]]

print(asclist)

df=pd.DataFrame(graph_dict, index=asclist)


# 그래프 그리기

plt.style.use('ggplot')

df.plot(kind="bar",subplots=True,ylim=(0,0.15))

plt.savefig('lang-plot.png')





[ #170819 3 mushroom-train ]

import pandas as pd

from sklearn.ensemble import RandomForestClassifier

from sklearn import metrics

from sklearn.model_selection import train_test_split

# 데이터 읽어 들이기--- (※1)

mr = pd.read_csv("170819 1 mushroom.csv", header=None)

# 데이터 내부의 기호를 숫자로 변환하기--- (※2)

label = []

data = []

attr_list = []

for row_index, row in mr.iterrows():

    label.append(row.ix[0])

    row_data = []

    for v in row.ix[1:]:

        row_data.append(ord(v))

    data.append(row_data)

# 학습 전용과 테스트 전용 데이터로 나누기 --- (※3)

data_train, data_test, label_train, label_test = \

    train_test_split(data, label)

# 데이터 학습시키기 --- (※4)

clf = RandomForestClassifier()

clf.fit(data_train, label_train)

# 데이터 예측하기 --- (※5)

predict = clf.predict(data_test)

# 결과 테스트하기 --- (※6)

ac_score = metrics.accuracy_score(label_test, predict)

cl_report = metrics.classification_report(label_test, predict)

print("정답률 =", ac_score)

print("리포트 =\n", cl_report)



[참고자료]


https://www.docker.com/products/docker-toolbox  ==> docker 설치방법

https://www.data.go.kr/main.do

Comment +0

인공지능의 실전입문에 관한 글입니다.

알고리즘을 만드는것이 아니고, 만들어진 알로리즘을 활용하는 방법에 관한 글입니다.

자동차 운전을 위해 자동차를 만드는방법을 알필요는 없습니다. 물론 알면 좋기는 하겠지만, 서로 별도의 분야라고 할수있습니다.


본글은 학습용 데이터를 학습/테스트/측정하는 방법에 대한 글입니다.

글의 순서는 아래와 같습니다.


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


1. X-OR 구현 => [ # 170814 7 xor-train ]

2. 손글씨 맞추기

  - MNIST DOWN ==> [ # 170814 10 mnist-download ] 

  - DATA 변환 ==> CSV 로 , [# 170814 11 mnist-tocsv]    

t10k.csv

train.csv

  - 학습/테스트/평가 ==> [# 170814 9 mnist-train]

    . DATA숫자가 5000개 정도 수준으로, 정확도는 78% 수준임,  전체DATA로 하게되면 정확도는 95% 수준으로 올라감


3. 참고자료


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


[ # 170814 7 xor-train ]


from sklearn import svm

# XOR의 계산 결과 데이터 --- (※1)

xor_data = [

    #P, Q, result

    [0, 0, 0],

    [0, 1, 1],

    [1, 0, 1],

    [1, 1, 0]

]

# 학습을 위해 데이터와 레이블 분리하기 --- (※2)

data = []

label = []

for row in xor_data:

    p = row[0]

    q = row[1]

    r = row[2]

    data.append([p, q])

    label.append(r)

# 데이터 학습시키기 --- (※3)

clf = svm.SVC()

clf.fit(data, label)

# 데이터 예측하기 --- (※4)

pre = clf.predict(data)

print(" 예측결과:", pre)

# 결과 확인하기 --- (※5)

ok = 0; total = 0

for idx, answer in enumerate(label):

    p = pre[idx]

    if p == answer: ok += 1

    total += 1

print("정답률:", ok, "/", total, "=", ok/total)




[ # 170814 10 mnist-download ]

import urllib.request as req
import gzip, os, os.path
savepath = "./mnist"
baseurl = "http://yann.lecun.com/exdb/mnist"
files = [
    "train-images-idx3-ubyte.gz",
    "train-labels-idx1-ubyte.gz",
    "t10k-images-idx3-ubyte.gz",
    "t10k-labels-idx1-ubyte.gz"]
# 다운로드
if not os.path.exists(savepath): os.mkdir(savepath)
for f in files:
    url = baseurl + "/" + f
    loc = savepath + "/" + f
    print("download:", url)
    if not os.path.exists(loc):
        req.urlretrieve(url, loc)
# GZip 압축 해제
for f in files:
    gz_file = savepath + "/" + f
    raw_file = savepath + "/" + f.replace(".gz", "")
    print("gzip:", f)
    with gzip.open(gz_file, "rb") as fp:
        body = fp.read()
        with open(raw_file, "wb") as w:
            w.write(body)
print("ok")


[# 170814 11 mnist-tocsv]

import struct
def to_csv(name, maxdata):
    # 레이블 파일과 이미지 파일 열기
    lbl_f = open("./mnist/"+name+"-labels-idx1-ubyte", "rb")
    img_f = open("./mnist/"+name+"-images-idx3-ubyte", "rb")
    csv_f = open("./mnist/"+name+".csv", "w", encoding="utf-8")
    # 헤더 정보 읽기 --- (※1)
    mag, lbl_count = struct.unpack(">II", lbl_f.read(8))
    mag, img_count = struct.unpack(">II", img_f.read(8))
    rows, cols = struct.unpack(">II", img_f.read(8))
    pixels = rows * cols
    # 이미지 데이터를 읽고 CSV로 저장하기 --- (※2)
    res = []
    for idx in range(lbl_count):
        if idx > maxdata: break
        label = struct.unpack("B", lbl_f.read(1))[0]
        bdata = img_f.read(pixels)
        sdata = list(map(lambda n: str(n), bdata))
        csv_f.write(str(label)+",")
        csv_f.write(",".join(sdata)+"\r\n")
        # 잘 저장됐는지 이미지 파일로 저장해서 테스트하기 -- (※3)
        if idx < 10:
            s = "P2 28 28 255\n"
            s += " ".join(sdata)
            iname = "./mnist/{0}-{1}-{2}.pgm".format(name,idx,label)
            with open(iname, "w", encoding="utf-8") as f:
                f.write(s)
    csv_f.close()
    lbl_f.close()
    img_f.close()
# 결과를 파일로 출력하기 --- (※4)
to_csv("train", 1000)
to_csv("t10k", 500)



[# 170814 9 mnist-train]

from sklearn import model_selection,svm,metrics
import pandas

train_csv=pandas.read_csv("./mnist/train.csv",header=None)
tk_csv=pandas.read_csv("./mnist/t10k.csv",header=None)

def test(l) :
    output=[]
    for i in l:
        output.append(float(i)/256)
    return output

train_csv_data=list(map(test,train_csv.iloc[:,1:].values))
tk_csv_data=list(map(test,tk_csv.iloc[:,1:].values))

train_csv_label=train_csv[0].values
tk_csv_label=tk_csv[0].values

clf = svm.SVC()
clf.fit(train_csv_data, train_csv_label)
predict = clf.predict(tk_csv_data)
print(predict)
score = metrics.accuracy_score(tk_csv_label,predict)
print("정답률 =", score)





[참고자료]


https://www.docker.com/products/docker-toolbox  ==> docker 설치방법

https://www.data.go.kr/main.do

Comment +0


인공지능의 실전입문에 관한 글입니다.

알고리즘을 만드는것이 아니고, 만들어진 알로리즘을 활용하는 방법에 관한 글입니다.

자동차 운전을 위해 자동차를 만드는방법을 알필요는 없습니다. 물론 알면 좋기는 하겠지만, 서로 별도의 분야라고 할수있습니다.


본글은 인공지능에 입력할 학습data를 구하는 방법에 관한 글입니다 ( 인터넷에 흩어져 있는 자료수집 )

글의 순서는 아래와 같습니다.


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

[ 웹상의 데이타 가져오기 ]

1.개발환경 구성

  - 아나콘다 기준으로 설명합니다.  

  - Docker를 설치하고, 우분투를 설치하고, 우분투에서 각종 패키지(apt-get install)를 설치해도 무방합니다.
  - 아나콘다는 파이선 패키지를 쉽게 설치할수있도록 도와줍니다.
    . conda install ~
    . pip ~ 등으로 설치한다

2.웹사이트에 자료요청하는 방식 ==> get 요청

3.웹사이트에서 사진한장 가져오기 ==> download-png1

4.쇼핑몰에서 가격정보 가져오기 ==>bs-usd

5.포털에서 뉴스자료 가져오기 ==> download-bs-news2

6.웹 사이트에 파이선으로 프로그램 안에서 로그인 하기 ==> 170813 6 selenium-login

7.자바스크립트를 이용해서 자료 가져오기 selenium-js

8.날씨정보 가져오기 ==>170813 8 api-weather

9.네이버에서 이메일 가져오기 ==> 170813 10 selenium-login

   - 네이버 메일을 가져오는 코드입니다.

   - 네이버에 처음 로그인시에는

      . 1)로그인 ,2)새로운 기기 등록 , 3)로그인상태 유지 3단계의 단계를 통과해야 합니다. 각각 단계에 버튼입력코드가 필요합니다

      . 두번째 부터는 새로운 기기등록 , 로그인상태 유지  2단계의 코드는 필요없습니다, 주석처리를 해야합니다. 주석 처리 않하면 error 발생

10  날씨 가져오기 ==>170814 1 xml-forecast] 
11. 깃 허브에서 자료가져오기 ==>170814 2 json-github

12  csv 화일 읽기 ==> [# EUC_KR로 저장된 CSV 파일 읽기]

13. 엑셀화일 읽기==>[# 170814 4 excel-read]

  - 다운받은 엑셀은  다름이름으로 저장 " 엑셀통합문서" 로 저장해야 파이선에서 불러올수 있음

14. 엑셀에 자료 입력하기[ # 170814 5 write excel ]



[ Next Step ]

  ==> 가져온 데이터를 머신러닝으로 학습 시키기


[ 참고자료 ]

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



[ 개발환경 구성 ] 

1. 아나콘다 기준으로 설명합니다.  
  - Docker를 설치하고, 우분투를 설치하고, 우분투에서 각종 패키지(apt-get install)를 설치해도 무방합니다.
  - 아나콘다는 파이선 패키지를 쉽게 설치할수있도록 도와줍니다.


아나콘다에서 설치

phantomjs

conda install -c trent phantomjs 

conda install -c javascript phantomjs 

conda install -c javascript/label/dev phantomjs

conda install -c mutirri phantomjs 

conda install -c mutirri/label/bokeh-deps phantomjs 

conda install -c mutirri/label/all phantomjs 

conda install -c mutirri/label/selenium phantomjs

selenium

conda install -c metaperl selenium  ==> python 2.xx

conda install -c conda-forge selenium  ==> python 3.xx

beautifulsoup4

conda install -c anaconda beautifulsoup4 ==>python 4.xx

fontconfig

conda install -c anaconda fontconfig python 2.xx




[ Get 요청 ] 

# https://search.naver.com/search.naver?where=nexearch&sm=top_hty&fbm=1&ie=utf8&query=%EC%B4%88%EC%BD%9C%EB%A6%BF

import urllib.request
import urllib.parse
API = "https://search.naver.com/search.naver"
# 매개변수를 URL 인코딩합니다. --- (※1)
values = {
"where" : "nexearch" ,
"sm" : "top_hty" ,
"where" : "nexearch" ,
"fbm" : "1" ,
"ie" : "utf8" ,
"query" : "초콜릿"
}
params = urllib.parse.urlencode(values)
# 요청 전용 URL을 생성합니다. --- (※2)
url = API + "?" + params
print("url=", url)
# 다운로드합니다. --- (※3)
data = urllib.request.urlopen(url).read()
text = data.decode("utf-8")  #euc-kr"
print(text)


[ download-png1 ] 

# 170813 2 download-png1

import urllib.request

# data를 불러올 사이트/화일
url="http://uta.pw/shodou/img/28/214.png"

# 불러운 화일을 저장할 이름
savename="test.png"

# 다운로드 (1)
mem=urllib.request.urlopen(url).read()

#파일로 저장하기 (2)
#"wb" 는 binary file(인간이 읽을수 없음)을 의미함
with open(savename,mode="wb") as f:
    f.write(mem)

print("저장되었습니다")


[ bs-usd ] 

#170813 3 bs-usd

from bs4 import BeautifulSoup
import urllib.request as req
# HTML 가져오기
url = "http://info.finance.naver.com/marketindex/"
res = req.urlopen(url)
# HTML 분석하기
soup = BeautifulSoup(res, "html.parser")
# 원하는 데이터 추출하기 --- (※1)
price = soup.select_one("div.head_info > span.value").string
print("usd/krw =", price)


[ download-bs-news2 ]

# bs-news2

from bs4 import BeautifulSoup
# 분석 대상 HTML --- (※1)
html = """
<html><body>
<div id="meigen">
  <h1>위키북스 도서</h1>
  <ul class="items">
    <li>유니티 게임 이펙트 입문</li>
    <li>스위프트로 시작하는 아이폰 앱 개발 교과서</li>
    <li>모던 웹사이트 디자인의 정석</li>
  </ul>
</div>
</body></html>
"""
# HTML 분석하기 --- (※2)
soup = BeautifulSoup(html, 'html.parser')
# 필요한 부분을 CSS 쿼리로 추출하기
# 타이틀 부분 추출하기 --- (※3)
h1 = soup.select_one("div#meigen > h1").string
print("h1 =", h1)
# 목록 부분 추출하기 --- (※4)
li_list = soup.select("div#meigen > ul.items > li")
for li in li_list:
  print("li =", li.string)



170813 6 selenium-login ]

#"로그인 버튼을 클릭합니다.") 이후 부터 정상실행 않됨

# 170813 6 selenium-login
from selenium import webdriver
USER = "**********"
PASS = "*************^"
# PhantomJS 드라이버 추출하기 --- (※1)
browser = webdriver.PhantomJS()
browser.implicitly_wait(3)
# 로그인 페이지에 접근하기 --- (※2)
url_login = "https://nid.naver.com/nidlogin.login"
browser.get(url_login)
print("로그인 페이지에 접근합니다.")
# 텍스트 박스에 아이디와 비밀번호 입력하기 --- (※3)
e = browser.find_element_by_id("id")
e.clear()
e.send_keys(USER)
e = browser.find_element_by_id("pw")
e.clear()
e.send_keys(PASS)
# 입력 양식 전송해서 로그인하기 --- (※4)
form = browser.find_element_by_css_selector("input.btn_global[type=submit]")
form.submit()
print("로그인 버튼을 클릭합니다.")
# 여기까지만 정상실행됨
# 쇼핑 페이지의 데이터 가져오기 --- (※5)
browser.get("https://order.pay.naver.com/home?tabMenu=SHOPPING")
# 쇼핑 목록 출력하기 --- (※6)
products = browser.find_elements_by_css_selector(".sub_sc h4")
print(products)
for product in products:
    print("-", product.text)


[  170813 67selenium-js ]

# 170813 67selenium-js

from selenium import webdriver
# PhantomJS 드라이버 추출하기
browser = webdriver.PhantomJS()
browser.implicitly_wait(3)
# 적당한 웹 페이지 열기
browser.get("https://google.com")
# 자바스크립트 실행하기
r = browser.execute_script("return 100 + 50")
print(r)

[# 170813 8 api-weather]
mport requests
import json
# API 키를 지정합니다. 자신의 키로 변경해서 사용해주세요. --- (※1)
apikey = "474d59dd890c4108f62f192e0c6fce01"
# 날씨를 확인할 도시 지정하기 --- (※2)
cities = ["Seoul,KR", "Tokyo,JP", "New York,US"]
# API 지정 --- (※3)
api = "http://api.openweathermap.org/data/2.5/weather?q={city}&APPID={key}"
# 켈빈 온도를 섭씨 온도로 변환하는 함수 --- (※4)
k2c = lambda k: k - 273.15
# 각 도시의 정보 추출하기 --- (※5)
for name in cities:
    # API의 URL 구성하기 --- (※6)
    url = api.format(city=name, key=apikey)
    # API에 요청을 보내 데이터 추출하기
    r = requests.get(url)
    # 결과를 JSON 형식으로 변환하기 --- (※7)
    data = json.loads(r.text)    
    # 결과 출력하기 --- (※8)
    print("+ 도시 =", data["name"])
    print("| 날씨 =", data["weather"][0]["description"])
    print("| 최저 기온 =", k2c(data["main"]["temp_min"]))
    print("| 최고 기온 =", k2c(data["main"]["temp_max"]))
    print("| 습도 =", data["main"]["humidity"])
    print("| 기압 =", data["main"]["pressure"])
    print("| 풍향 =", data["wind"]["deg"])
    print("| 풍속 =", data["wind"]["speed"])
    print("")




[# 170813 10 selenium-login]

이메일 제목읽어오기

# 170813 10 selenium-login

from selenium import webdriver
# PhantomJS 드라이버 추출하기 --- (※1)
browser = webdriver.PhantomJS()
# 3초 대기하기 --- (※2) phantumjs의 버그로 무조건 3초를 대기하고 , 이후 진행해야함
browser.implicitly_wait(3)
# 로그인 --- (※3)
browser.get("https://nid.naver.com/nidlogin.login")
# 화면을 캡처해서 저장하기 --- (※4)
element_id=browser.find_element_by_id("id") # 아이디 텍스트 입력
element_id.clear()
element_id.send_keys("****")

element_pw=browser.find_element_by_id("pw") # 비번 텍스트 입력
element_pw.clear()
element_pw.send_keys("****^")
browser.save_screenshot("website_c.png")
# 브라우저 종료하기 --- (※5)

button=browser.find_element_by_css_selector("input.btn_global[type=submit]")
button.submit()

# 메일 페이지 열기
browser.get("https://mail.naver.com/")
browser.save_screenshot("website_D.png")

#브라우져 종료
browser.quit()




[ #170814 1 xml-forecast] 
 -. 날씨 가져오기

from bs4 import BeautifulSoup 
import urllib.request #as req
import os.path
url = "http://www.kma.go.kr/weather/forecast/mid-term-rss3.jsp?stnId=108"
request = urllib.request.urlopen(url)
xml = request.read()
soup=BeautifulSoup(xml,"html.parser")
seoul=soup.find_all("location")[0]
datas=seoul.find_all("data")
for item in datas :
   print(item.find("wf").text)


[ # 170814 2 json-github ]


# 170814 2 json-github

import urllib.request as request
import os.path, random
import json

# JSON 데이터 내려받기 --- (※1)
url = "https://api.github.com/repositories"
savename = "repo.json"
if not os.path.exists(url):
    request.urlretrieve(url, savename)
# JSON 파일 분석하기 --- (※2)
items = json.load(open(savename, "r", encoding="utf-8"))
# 또는
# s = open(savename, "r", encoding="utf-8").read()
# items = json.loads(s)
# 출력하기 --- (※3)
for item in items:
    print(item["name"] + " - " + item["owner"]["login"])

print("--------------------------------------")

json_str=request.urlopen("https://api.github.com/repositories").read()
output=json.loads(json_str)

for item in output :
    print(item["name"])
    print(item["full_name"])
    print(item["owner"]["login"])
print()


[# EUC_KR로 저장된 CSV 파일 읽기]

import codecs
# EUC_KR로 저장된 CSV 파일 읽기
filename = "list-euckr.csv"
csv = codecs.open(filename, "r", "euc_kr").read()
# CSV을 파이썬 리스트로 변환하기
data = []
rows = csv.split("\r\n")
for row in rows:
    if row == "": continue
    cells = row.split(",")
    data.append(cells)
# 결과 출력하기
for c in data:
    print(c[1], c[2])


[# 170814 4 excel-read]

import openpyxl
book=openpyxl.load_workbook("stats_104102.xlsx")
#print(book.get_sheet_names())
#print(book.get_sheet_by_name("stats_104102")

sheet = book.worksheets[0]
for row in sheet.rows:
    for data in row:
     print(data.value,end=" ")
    print("",end="\n")

http://www.index.go.kr/potal/main/EachDtlPageDetail.do?idx_cd=1041


[ # 170814 5 write excel ]

import openpyxl

workbook=openpyxl.Workbook()
sheet=workbook.active

sheet["A1"] = "테스트 화일"
sheet["A2"] = "안녕"

sheet.merge_cells("A1:C1")
sheet["A1"].font=openpyxl.styles.Font(size=20,color="FF0000")

workbook.save("newFile.xlsx")


[ 참고자료 ]

https://www.docker.com/products/docker-toolbox  ==> docker 설치방법

https://www.data.go.kr/main.do

Comment +0

인공지능 구현 에 대한 글입니다.(Deep Reinforcement Learning)


글의 순서는 아래와 같습니다.


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

요약

 - 신경망 (Neural Network)즉  Q-NETWORK 를 이용하여  Q-Learing 구현( q-talble 형식의경우 메모리가 기하 급수적으로 필요해짐 )

  ==> 실생활에 적용하게에는 무리가 있음(q-table 방식) , 따라서 신경망 방식이 필요해짐


1.q_net_frozenlake

 - Network 으로 변환 


2. 07_3_dqn_2015_cartpole

  - Q-NETWORk 이슈

   1) 데이터가 너무 적어 정확도가 좋치 못하다. 2개의 데이터로 학습을 하게되면 전혀 다른 직선이 나오게 되는것이다

      . 깊게(deep)

      . experience replay : action후 버퍼에 상태,action등 을 저장한다 , 이후 random(골고루) 하게 샘플링해서 학습한다

   2) 타겟이 흔들림 ( 같은 네트웍을 사용해서, 예측변경이 타겟치도 변경이 일어남) => 화살을 쏘자마자 과녁을 움직이는것 같음

      . network을 하나더 만든다 ( 각자 업데이트 하다가, 학습전에 복사해서 합친다)


3. Next Step

  ==> 신경망 (Neural Network)를 이용하여  Q-Learing 구현


4. 참고자료

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




[ 06_q_net_frozenlake ]



06_q_net_frozenlake


This code is based on

https://github.com/hunkim/DeepRL-Agents

'''

import gym

import numpy as np

import matplotlib.pyplot as plt

import time

import os

os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'    # default value = 0  From http://stackoverflow.com/questions/35911252/disable-tensorflow-debugging-information


import tensorflow as tf

env = gym.make('FrozenLake-v0')


# Input and output size based on the Env

input_size = env.observation_space.n;

output_size = env.action_space.n;

learning_rate = 0.1


# These lines establish the feed-forward part of the network used to choose actions

X = tf.placeholder(shape=[1, input_size], dtype=tf.float32)              # state input

W = tf.Variable(tf.random_uniform([input_size, output_size], 0, 0.01))   # weight


Qpred = tf.matmul(X, W)     # Out Q prediction

Y = tf.placeholder(shape=[1, output_size], dtype=tf.float32)    # Y label


loss = tf.reduce_sum(tf.square(Y-Qpred))

train = tf.train.GradientDescentOptimizer(learning_rate=learning_rate).minimize(loss)


# Set Q-learning parameters

dis = .99

num_episodes = 2000


# create lists to contain total rewards and steps per episode

rList = []


def one_hot(x):

    return np.identity(16)[x:x+1]


start_time = time.time()


init = tf.global_variables_initializer()

with tf.Session() as sess:

    sess.run(init)

    for i in range(num_episodes):

        # Reset environment and get first new observation

        s = env.reset()

        e = 1. / ((i / 50) + 10)

        rAll = 0

        done = False

        local_loss = []


        # The Q-Table learning algorithm

        while not done:

            # Choose an action by greedly (with a chance of random action)

            # from the Q-network

            Qs = sess.run(Qpred, feed_dict={X: one_hot(s)})

            if np.random.rand(1) < e:

                a = env.action_space.sample()

            else:

                a = np.argmax(Qs)


            # Get new state and reward from environment

            s1, reward, done, _ = env.step(a)

            if done:

                # Update Q, and no Qs+1, since it's a termial state

                Qs[0, a] = reward

            else:

                # Obtain the Q_s` values by feeding the new state through our network

                Qs1 = sess.run(Qpred, feed_dict={X: one_hot(s1)})

                # Update Q

                Qs[0, a] = reward + dis*np.max(Qs1)


            # Train our network using target (Y) and predicted Q (Qpred) values

            sess.run(train, feed_dict={X: one_hot(s), Y: Qs})


            rAll += reward

            s = s1


        rList.append(rAll)


print("--- %s seconds ---" % (time.time() - start_time))


print("Success rate: " + str(sum(rList) / num_episodes))

#plt.bar(range(len(rList)), rList, color="blue")

plt.bar(range(len(rList)), rList, color='b', alpha=0.4)

plt.show()



[07_3_dqn_2015_cartpole]


07_3_dqn_2015_cartpole


This code is based on

https://github.com/hunkim/DeepRL-Agents


CF https://github.com/golbin/TensorFlow-Tutorials

https://github.com/dennybritz/reinforcement-learning/blob/master/DQN/dqn.py


Q-NETWOR 이슈

 1. 데이터가 너무 적어 정확도가 좋치 못하다. 2개의 데이터로 학습을 하게되면 전혀 다른 직선이 나오게 되는것이다

   - 깊게(deep)

   - experience replay : action후 버퍼에 상태,action등 을 저장한다 , 이후 random(골고루) 하게 샘플링해서 학습한다

 2. 타겟이 흔들림 ( 같은 네트웍을 사용해서, 예측변경이 타겟치도 변경이 일어남) => 화살을 쏘자마자 과녁을 움직이는것 같음

    - network을 하나더 만든다

"""


import numpy as np

import tensorflow as tf

import random

from collections import deque

from dqn import dqn


import gym

from gym import wrappers


env = gym.make('CartPole-v0')


# Constants defining our neural network

input_size = env.observation_space.shape[0]

output_size = env.action_space.n


dis = 0.9

REPLAY_MEMORY = 50000


def replay_train(mainDQN, targetDQN, train_batch):

    x_stack = np.empty(0).reshape(0, input_size)

    y_stack = np.empty(0).reshape(0, output_size)


    # Get stored information from the buffer

    for state, action, reward, next_state, done in train_batch:

        Q = mainDQN.predic(state)


        # terminal?

        if done:

            Q[0, action] = reward

        else:

            # get target from target DQN (Q')

            Q[0, action] = reward + dis * np.max(targetDQN.predict(next_state))


        y_stack = np.vstack([y_stack, Q])

        x_stack = np.vstack( [x_stack, state])


    # Train our network using target and predicted Q values on each episode

    return mainDQN.update(x_stack, y_stack)


def ddqn_replay_train(mainDQN, targetDQN, train_batch):


#Double DQN implementation

#param mainDQN main DQN

#param targetDQN target DQN

#param train_batch minibatch for train

#return loss



    x_stack = np.empty(0).reshape(0, mainDQN.input_size)

    y_stack = np.empty(0).reshape(0, mainDQN.output_size)


    # Get stored information from the buffer

    for state, action, reward, next_state, done in train_batch:

        Q = mainDQN.predict(state)


        # terminal?

        if done:

            Q[0, action] = reward

        else:

            # Double DQN: y = r + gamma * targetDQN(s')[a] where

            # a = argmax(mainDQN(s'))

            Q[0, action] = reward + dis * targetDQN.predict(next_state)[0, np.argmax(mainDQN.predict(next_state))]


        y_stack = np.vstack([y_stack, Q])

        x_stack = np.vstack([x_stack, state])


    # Train our network using target and predicted Q values on each episode

    return mainDQN.update(x_stack, y_stack)


def get_copy_var_ops(*, dest_scope_name="target", src_scope_name="main"):


    # Copy variables src_scope to dest_scope

    op_holder = []


    src_vars = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope=src_scope_name)

    dest_vars = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope=dest_scope_name)


    for src_var, dest_var in zip(src_vars, dest_vars):

        op_holder.append(dest_var.assign(src_var.value()))


    return op_holder


def bot_play(mainDQN, env=env):

    # See our trained network in action

    state = env.reset()

    reward_sum = 0

    while True:

        env.render()

        action = np.argmax(mainDQN.predict(state))

        state, reward, done, _ = env.step(action)

        reward_sum += reward

        if done:

            print("Total score: {}".format(reward_sum))

            break


def main():

    max_episodes = 5000

    # store the previous observations in replay memory

    replay_buffer = deque()


    with tf.Session() as sess:

        mainDQN = dqn.DQN(sess, input_size, output_size, name="main")

        targetDQN = dqn.DQN(sess, input_size, output_size, name="target")

        tf.global_variables_initializer().run()


        #initial copy q_net -> target_net

        copy_ops = get_copy_var_ops(dest_scope_name="target", src_scope_name="main")

        sess.run(copy_ops)


        for episode in range(max_episodes):

            e = 1. / ((episode / 10) + 1)

            done = False

            step_count = 0

            state = env.reset()


            while not done:

                if np.random.rand(1) < e:

                    action = env.action_space.sample()

                else:

                    # Choose an action by greedily from the Q-network

                    action = np.argmax(mainDQN.predict(state))


                # Get new state and reward from environment

                next_state, reward, done, _ = env.step(action)

                if done: # Penalty

                    reward = -100


                # Save the experience to our buffer

                replay_buffer.append((state, action, reward, next_state, done))

                if len(replay_buffer) > REPLAY_MEMORY:

                      replay_buffer.popleft()


                state = next_state

                step_count += 1

                if step_count > 10000:   # Good enough. Let's move on

                    break


            print("Episode: {} steps: {}".format(episode, step_count))

            if step_count > 10000:

                pass

                ##10,000이면 정지(무한루프방지)

                # break


            if episode % 10 == 1: # train every 10 episode

                # Get a random batch of experiences

                for _ in range(50):

                    minibatch = random.sample(replay_buffer, 10)

                    loss, _ = ddqn_replay_train(mainDQN, targetDQN, minibatch)


                print("Loss: ", loss)

                # copy q_net -> target_net

                sess.run(copy_ops)


        # See our trained bot in action

        env2 = wrappers.Monitor(env, 'gym-results', force=True)


        for i in range(200):

            bot_play(mainDQN, env=env2)


        env2.close()

        # gym.upload("gym-results", api_key="sk_VT2wPcSSOylnlPORltmQ")


if __name__ == "__main__":

    main()



[ 참고자료 ]


  https://www.inflearn.com/course/기본적인-머신러닝-딥러닝-강좌

  https://github.com/hunkim/deeplearningzerotoall

  https://www.tensorflow.org/api_docs/python/tf/layers

  https://www.inflearn.com/course/reinforcement-learning/



Comment +0

인공지능 구현 에 대한 글입니다.(Deep Reinforcement Learning)


글의 순서는 아래와 같습니다.


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

요약

 - 얼음얼린 호수에서 구멍에 빠지지 않고 길을 찾아나오는 게임임

 - 얼음은 미끄럽습니다. 길을 안내해주는 사람의 말을 전적으로 의지할경우, 오히려 바람등에 의해(불확실한 환경)

   미끄러질수가 있습니다. 따라서 약간만 의지하고, 나의 의지를 좀더 반영하는 방식으로 정확도를 높일수 있음 ( 1.5% ==> 66% 수준)

 - 현실도 주변 환경에의해 예측이 불가능한 경우가 많습니다. 이럴경우에 적용이 가능한 방식임.


1.OpenAI gym 게임을 위한 프로그램 설치


2. # 01_play_frozenlake_det_windows

 => 화살키를 입력하는 방향으로 이동

 => 화일 실행은 터미널에서 한다(키 인을 받기위함).

 => frozenlake_det_windows.py 가 있는 폴더로 가서, python 명령어 실행

 => 홀에 빠지거나,끝으로 가면 게임이 종료된다.


3.# 03_0_q_table_frozenlake_det

  => 초기에는 임의의 장소로, 두번째 부터는 큰곳으로 이동, 홀을 피해 길을 찾는 알고리즘임

  => 참고(random argmax) : 같은면 아무곳이나, 큰곳이 있으면 큰곳으로 간다

 

4. # 03_2_q_table_frozenlake_det

   ==>e보다 작으면 임의의 장소로 가고,그렇치 않으면, 큰 리워드방향으로 이동한다(explot &exporation)

  ==> 이사간 동네에서 초기에는 랜덤하게 식당을 다니고, 파악이 다되면 맞집위주로 찾아간다게 

  ==> 노이즈 값을 주어서, 기존의 data를 일부 반영하는 방법도 있음 ( 상기 e의 경우는 기존 data를 무시하됨)

      . 차선책을 선택하는 방법임

  ==> discount(0.9) 나중에 받을 리워드는 0.9를 곱해서 비중을 낮춘다.

       최단거리를 찾는 방법임


5. play_frozenlake_windows

 ==> keyboard 인식이 잘 않됨, 추후 보완필요

 ==> 빙판길로, 키보드 조작대로 움직이지 않고, Q 선생의 말에 전적으로 의존하지 않는 상황을 의미함

 ==> 현실세계와 비슷한 환경을 구현하는것임.


6. #05_0_q_table_frozenlake

  ==>미끄러운 환경 ('FrozenLake-v0' ) 에서는 Q 형님의 조언을 그대로 따르면 않된다, 주변환경에 의해  미끄질수 있기때문임

  ==> 빙판에서, 기존과 같이 Q 형님의 말에 전적으로 의존할 경우  1.55%  , 일부 의존할경우 66%


7. # 05_q_table_frozenlake

  ==>미끄러운 환경 ('FrozenLake-v0' ) 에서는 Q 형님의 조언을 그대로 따르면 않된다, 주변환경에 의해  미끄질수 있기때문임

     따라서 Q 형님의 말을 조금만 반영할 필요가 있음

 ==> Q[state, action] = (1-learning_rate) * Q[state, action] \

   + learning_rate*(reward + dis * np.max(Q[new_state, :]))

 ==> 정확도가 어느정도 상승함 (1.55% ==>  66% )

   . 빙판에서, Q 형님의 말에 전적으로 의존할 경우  1.55%  , 일부 의존할경우 66%


8. Next Step

  ==> 신경망 (Neural Network)를 이용하여  Q-Learing 구현


9. 참고자료

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




[ 1.OpenAI gym 게임을 위한 프로그램 설치 ]


- 설치가이드 :https://gym.openai.com/docs

- step 1

  . anaconda3 prompt 실행

- step 2

  . git clone https\\github.com/openai/gym  ==> gym 다운받기

  . cd gym ==> gym 폴더로 이동

  . pip3 install -e . ==> minimal install , pip3로 해야함 , gym으로 다운받은 gym을 pc에 설치해 주는 과정임

- step3 ==> 해당 python 선택( 패키지 별로 python 설치경로가 틀려서, 해당 python을 찾아서 연결시켜 주어야함

  . python 편집기(pycharm 프로그램 설정) 의 interpreter 변경

  . 변경전 : tensorflow 폴더의 python

  . 변경후 : c:\\user\dhp\appdata3\python.exe



- step4 ==> 패키지 추가 ==> tensorflow 패키지 설치가 필요할경우 pycharm 에서 설치가능함

  . 우측상단의 " + " 버튼을 누르면, 설치가능한 패키지 목록이 나옵니다. 여기서 tesorflow를 선택해서 설치한다.

  . 이로서   c:\\user\dhp\appdata3\python.exe의 python에는  gym 과 tesorflow가 동시에 설치됨

  . 진행중 필요한 패키지는 상황에 맞게 추가 설치하면 됨.

- tensorflow 내의 python에 패키지 추가방법 추가확인 필요함. gym설치되어 있는데, 잘 동작하지 않고있음.



- 설치성공 여부 확인 : pycharm 화면에서 아래 코딩후 실행확인

  # cartpolo test


"""cartpolo test

"""

import gym

env = gym.make('CartPole-v0')

env.reset()

for _ in range(100):

    env.render()

    env.step(env.action_space.sample()) # take a random action


for i_episode in range(20):

    observation = env.reset()

    for t in range(100):

        env.render()

        print(observation)

        action = env.action_space.sample()

        observation, reward, done, info = env.step(action)

        if done:

            print("Episode finished after {} timesteps".format(t+1))

            break




[ # 01_play_frozenlake_det_windows ]


# 01_play_frozenlake_det_windows

"""

화일 실행은 터미널에서 한다(키 인을 받기위함).

frozenlake_det_windows.py 가 있는 폴더로 가서, python 명령어 실행

홀에 빠지거나,끝으로 가면 게임이 종료된다.

"""

import gym

from gym.envs.registration import register

from colorama import init

from kbhit import KBHit


init(autoreset=True)    # Reset the terminal mode to display ansi color


register(

    id='FrozenLake-v3',

    entry_point='gym.envs.toy_text:FrozenLakeEnv',

    kwargs={'map_name' : '4x4', 'is_slippery': False}

)


env = gym.make('FrozenLake-v3')        # is_slippery False

env.render()                             # Show the initial board


key = KBHit()


while True:


    action = key.getarrow();

    if action not in [0, 1, 2, 3]:

        print("Game aborted!")

        break


    state, reward, done, info = env.step(action)

    env.render()

    print("State: ", state, "Action: ", action, "Reward: ", reward, "Info: ", info)


    if done:

        print("Finished with reward", reward)

        break



[## 03_0_q_table_frozenlake_det ]


# 03_0_q_table_frozenlake_det

"""

 # random argmax : 같은면 아무곳이나, 큰곳이 있으면 큰곳으로 간다


"""


import gym

import numpy as np

import matplotlib.pyplot as plt

from gym.envs.registration import register

import random as pr


def rargmax(vector):    # https://gist.github.com/stober/1943451

    """ Argmax that chooses randomly among eligible maximum idices. """

    m = np.amax(vector)

    indices = np.nonzero(vector == m)[0]

    return pr.choice(indices)


register(

    id='FrozenLake-v3',

    entry_point='gym.envs.toy_text:FrozenLakeEnv',

    kwargs={'map_name' : '4x4', 'is_slippery': False}

)

env = gym.make('FrozenLake-v3')


# Initialize table with all zeros

Q = np.zeros([env.observation_space.n, env.action_space.n]) #16*4 사이즈임

# Set learning parameters

num_episodes = 2000


# create lists to contain total rewards and steps per episode

rList = []

for i in range(num_episodes):

    # Reset environment and get first new observation

    state = env.reset()

    rAll = 0

    done = False


    # The Q-Table learning algorithm

    while not done:

        action = rargmax(Q[state, :])  # random argmax : 같은면 아무곳이자, 큰곳이 있으면 큰곳으로 간다


        # Get new state and reward from environment

        new_state, reward, done, _ = env.step(action)


        # Update Q-Table with new knowledge using learning rate

        Q[state, action] = reward + np.max(Q[new_state, :])


        rAll += reward

        state = new_state

    rList.append(rAll)


print("Success rate: " + str(sum(rList) / num_episodes))

print("Final Q-Table Values")

print("LEFT DOWN RIGHT UP")

print(Q)


plt.bar(range(len(rList)), rList, color="blue")

#plt.bar(range(len(rList)), rList, color='b', alpha=0.4)

plt.show()



[# 03_2_q_table_frozenlake_det]


# 03_2_q_table_frozenlake_det

"""

   ==> e보다 작으면 임의의 장소로 가고,그렇치 않으면, 큰 리워드방향으로 이동한다(explot &exporation)

  ==> 이사간 동네에서 초기에는 랜덤하게 식당을 다니고, 파악이 다되면 맞집위주로 찾아간다게

  ==> 노이즈 값을 주어서, 기존의 data를 일부 반영하는 방법도 있음 ( 상기 e의 경우는 기존 data를 무시하됨)

      . 차선책을 선택하는 방법임

  ==> discount(0.9) 나중에 받을 리워드는 0.9를 곱해서 비중을 낮춘다.

       최단거리를 찾는 방법임



"""


import gym

import numpy as np

import matplotlib.pyplot as plt

from gym.envs.registration import register


register(

    id='FrozenLake-v3',

    entry_point='gym.envs.toy_text:FrozenLakeEnv',

    kwargs={'map_name' : '4x4', 'is_slippery': False}

)

env = gym.make('FrozenLake-v3')


# Initialize table with all zeros

Q = np.zeros([env.observation_space.n, env.action_space.n])

# Set learning parameters

dis = .99

num_episodes = 2000


# create lists to contain total rewards and steps per episode

rList = []

for i in range(num_episodes):

    # Reset environment and get first new observation

    state = env.reset()

    rAll = 0

    done = False


    e = 1. / ((i // 100) + 1)  # Python2 & 3

    # 후반부로 갈수록 e 값은 작아짐


    # The Q-Table learning algorithm

    while not done:

        # Choose an action by e-greedy

        if np.random.rand(1) < e:

            # e보다 작으면 임의의 장소로 가고

            # 랜더만 방향으로 많이 가게되면 정확도가 떨어질수 있음

            action = env.action_space.sample()

        else:

            # 그렇치 않으면, 큰 리워드방향으로 이동한다

            action = np.argmax(Q[state, :])


        # Get new state and reward from environment

        new_state, reward, done, _ = env.step(action)


        # Update Q-Table with new knowledge using decay rate

        Q[state, action] = reward + dis * np.max(Q[new_state, :])


        # 나중에 받을 리워드는 0.9를 곱해서 비중을 낮춘다. 최단거리를 찾는 방법임


        rAll += reward

        state = new_state

    rList.append(rAll)


print("Success rate: " + str(sum(rList) / num_episodes))

print("Final Q-Table Values")

print("LEFT DOWN RIGHT UP")

print(Q)

#plt.bar(range(len(rList)), rList, color="blue")

plt.bar(range(len(rList)), rList, color='b', alpha=0.4)

plt.show()



[# play_frozenlake_windows]


# play_frozenlake_windows


"""keyboard 인식이 잘 않됨, 추후 보완필요

빙판길로, 키보드 조작대로 움직이지 않고, Q 선생의 말에 전적으로 의존하지 않는 상황을 의미함"""


import gym

from gym.envs.registration import register

from colorama import init

from kbhit import KBHit


init(autoreset=True)    # Reset the terminal mode to display ansi color


env = gym.make('FrozenLake-v0')       # is_slippery True

env.render()                            # Show the initial board


key = KBHit()

while True:


    action = key.getarrow();

    if action not in [0, 1, 2, 3]:

        print("Game aborted!")

        break


    state, reward, done, info = env.step(action)

    env.render()

    print("State: ", state, "Action: ", action, "Reward: ", reward, "Info: ", info)


    if done:

        print("Finished with reward", reward)

        break


[#05_0_q_table_frozenlake]


#05_0_q_table_frozenlake

"""

미끄러운 환경 ('FrozenLake-v0' ) 에서는 Q 형님의 조언을 그대로 따르면 않된다, 주변환경에 의해  미끄질수 있기때문임

. 빙판에서, 기존과 같이 Q 형님의 말에 전적으로 의존할 경우  1.55%  , 일부 의존할경우 66%

"""


import gym

import numpy as np

import matplotlib.pyplot as plt

from gym.envs.registration import register

import random as pr


env = gym.make('FrozenLake-v0')


# Initialize table with all zeros

Q = np.zeros([env.observation_space.n, env.action_space.n])


# Set learning parameters

learning_rate = .85

dis = .99

num_episodes = 2000


# create lists to contain total rewards and steps per episode

rList = []

for i in range(num_episodes):

    # Reset environment and get first new observation

    state = env.reset()

    rAll = 0

    done = False


    # The Q-Table learning algorithm

    while not done:

        action = np.argmax(Q[state, :] + np.random.randn(1, env.action_space.n) / (i + 1))

        # Get new state and reward from environment

        new_state, reward, done, _ = env.step(action)


        # Update Q-Table with new knowledge using learning rate

        Q[state, action] = reward + dis * np.max(Q[new_state, :])

        state = new_state


        rAll += reward


    rList.append(rAll)


print("Success rate: " + str(sum(rList) / num_episodes))

print("Final Q-Table Values")

print("LEFT DOWN RIGHT UP")

print(Q)

plt.bar(range(len(rList)), rList, color="blue")

#plt.bar(range(len(rList)), rList, color='b', alpha=0.4)

plt.show()



[# 05_q_table_frozenlake]

# 05_q_table_frozenlake

"""

미끄러운 환경 ('FrozenLake-v0' ) 에서는 Q 형님의 조언을 그대로 따르면 않된다, 주변환경에 의해  미끄질수 있기때문임

따라서 Q 형님의 말을 조금만 반영할 필요가 있음

 ==> Q[state, action] = (1-learning_rate) * Q[state, action] \

   + learning_rate*(reward + dis * np.max(Q[new_state, :]))

 ==> 정확도가 어느정도 상승함 (1.55% ==>  66% )

   . 빙판에서, Q 형님의 말에 전적으로 의존할 경우  1.55%  , 일부 의존할경우 66%

"""


import gym

import numpy as np

import matplotlib.pyplot as plt

from gym.envs.registration import register

import random as pr


register(

    id='FrozenLake-v3',

    entry_point='gym.envs.toy_text:FrozenLakeEnv',

    kwargs={'map_name' : '4x4', 'is_slippery': False}

)


#env = gym.make('FrozenLake-v3')

env = gym.make('FrozenLake-v0')


# Initialize table with all zeros

Q = np.zeros([env.observation_space.n, env.action_space.n])


# Set learning parameters

learning_rate = .85

dis = .99

num_episodes = 2000


# create lists to contain total rewards and steps per episode

rList = []

for i in range(num_episodes):

    # Reset environment and get first new observation

    state = env.reset()

    rAll = 0

    done = False


    # The Q-Table learning algorithm

    while not done:

        action = np.argmax(Q[state, :] + np.random.randn(1, env.action_space.n) / (i + 1))


        # 노이즈 추가 ==>e 값 이용은, 처음부터 계산 , 노이지 값 이용은 기존값 반영  , 즉 차선책을 선택하는 방법임


        # Get new state and reward from environment

        new_state, reward, done, _ = env.step(action)

        # Q 형님의 말을 조금만 반영

        # Update Q-Table with new knowledge using learning rate

        Q[state, action] = (1-learning_rate) * Q[state, action] \

           + learning_rate*(reward + dis * np.max(Q[new_state, :]))


        rAll += reward

        state = new_state


    rList.append(rAll)


print("Success rate: " + str(sum(rList) / num_episodes))

print("Final Q-Table Values")

print("LEFT DOWN RIGHT UP")

print(Q)

#plt.bar(range(len(rList)), rList, color="blue")

plt.bar(range(len(rList)), rList, color='b', alpha=0.4)

plt.show()



[참고자료]


  https://www.inflearn.com/course/기본적인-머신러닝-딥러닝-강좌

  https://github.com/hunkim/deeplearningzerotoall

  https://www.tensorflow.org/api_docs/python/tf/layers

  https://www.inflearn.com/course/reinforcement-learning/



Comment +0

인공지능 구현에 대한 글입니다.


글의 순서는 아래와 같습니다.


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

1.#lab-12-1-hello-rnn

 전 단계의 출력이 다음단계의 출력에 영향을 주는 경우 적용함

  - 단어, 연관검색등..


2. # lab-12-2-char-seq-rnn

  -rnn 적용  ==> 정확도 높음

    . 49 loss: 0.000650434 Prediction: if you want you

    . y값 if you want you


3.  #lab-12-3-char-seq-softmax-only

  rnn 미적용 ==> 정확도 미흡함

  2999 loss: 0.277323 Prediction: yf you yant you

  y값 if you want you


4. # lab-12-4-rnn_long_char

  error : from __future__ import print_function ==> 실행불가로 주석처리함

  MultiRNNCell 로 여러단을 만들면 , 정확도가 높아짐 

  softmax =>reshape 수행


5. # lab-12-5-rnn_stock_prediction"""
  내일 주가 예측 : 기존의 7일의 data를 학습
  그래프 인쇄않되고 있음.


6. 코드탐구(추가)

  ==>lab-12-5-rnn_stock_prediction

      lab-13-1-mnist_using_scope

      lab-13-2-mnist_tensorboard

      lab-13-3-mnist_save_restore


7. 참고자료

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




#lab-12-1-hello-rnn ]


#lab-12-1-hello-rnn

"""

전 단계의 출력이 다음단계의 출력에 영향을 주는 경우 적용함

- 단어, 연관검색등..


"""

import os

os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'

# Lab 12 RNN

import tensorflow as tf

import numpy as np

tf.set_random_seed(777)  # reproducibility


idx2char = ['h', 'i', 'e', 'l', 'o']

# Teach hello: hihell -> ihello

x_data = [[0, 1, 0, 2, 3, 3]]   # hihell

x_one_hot = [[[1, 0, 0, 0, 0],   # h 0

              [0, 1, 0, 0, 0],   # i 1

              [1, 0, 0, 0, 0],   # h 0

              [0, 0, 1, 0, 0],   # e 2

              [0, 0, 0, 1, 0],   # l 3

              [0, 0, 0, 1, 0]]]  # l 3


y_data = [[1, 0, 2, 3, 3, 4]]    # ihello


num_classes = 5

input_dim = 5  # one-hot size

hidden_size = 5  # output from the LSTM. 5 to directly predict one-hot

batch_size = 1   # one sentence

sequence_length = 6  # |ihello| == 6

learning_rate = 0.1


X = tf.placeholder(

    tf.float32, [None, sequence_length, input_dim])  # X one-hot

Y = tf.placeholder(tf.int32, [None, sequence_length])  # Y label


cell = tf.contrib.rnn.BasicLSTMCell(num_units=hidden_size, state_is_tuple=True)

initial_state = cell.zero_state(batch_size, tf.float32)

outputs, _states = tf.nn.dynamic_rnn(

    cell, X, initial_state=initial_state, dtype=tf.float32)


# FC layer

X_for_fc = tf.reshape(outputs, [-1, hidden_size])

# fc_w = tf.get_variable("fc_w", [hidden_size, num_classes])

# fc_b = tf.get_variable("fc_b", [num_classes])

# outputs = tf.matmul(X_for_fc, fc_w) + fc_b

outputs = tf.contrib.layers.fully_connected(

    inputs=X_for_fc, num_outputs=num_classes, activation_fn=None)


# reshape out for sequence_loss

outputs = tf.reshape(outputs, [batch_size, sequence_length, num_classes])


weights = tf.ones([batch_size, sequence_length])

sequence_loss = tf.contrib.seq2seq.sequence_loss(

    logits=outputs, targets=Y, weights=weights)

loss = tf.reduce_mean(sequence_loss)

train = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(loss)


prediction = tf.argmax(outputs, axis=2)


with tf.Session() as sess:

    sess.run(tf.global_variables_initializer())

    for i in range(50):

        l, _ = sess.run([loss, train], feed_dict={X: x_one_hot, Y: y_data})

        result = sess.run(prediction, feed_dict={X: x_one_hot})

        print(i, "loss:", l, "prediction: ", result, "true Y: ", y_data)


        # print char using dic

        result_str = [idx2char[c] for c in np.squeeze(result)]

        print("\tPrediction str: ", ''.join(result_str))


'''

0 loss: 1.71584 prediction:  [[2 2 2 3 3 2]] true Y:  [[1, 0, 2, 3, 3, 4]]

Prediction str:  eeelle

1 loss: 1.56447 prediction:  [[3 3 3 3 3 3]] true Y:  [[1, 0, 2, 3, 3, 4]]

Prediction str:  llllll

2 loss: 1.46284 prediction:  [[3 3 3 3 3 3]] true Y:  [[1, 0, 2, 3, 3, 4]]

Prediction str:  llllll

3 loss: 1.38073 prediction:  [[3 3 3 3 3 3]] true Y:  [[1, 0, 2, 3, 3, 4]]

Prediction str:  llllll

4 loss: 1.30603 prediction:  [[3 3 3 3 3 3]] true Y:  [[1, 0, 2, 3, 3, 4]]

Prediction str:  llllll

5 loss: 1.21498 prediction:  [[3 3 3 3 3 3]] true Y:  [[1, 0, 2, 3, 3, 4]]

Prediction str:  llllll

6 loss: 1.1029 prediction:  [[3 0 3 3 3 4]] true Y:  [[1, 0, 2, 3, 3, 4]]

Prediction str:  lhlllo

7 loss: 0.982386 prediction:  [[1 0 3 3 3 4]] true Y:  [[1, 0, 2, 3, 3, 4]]

Prediction str:  ihlllo

8 loss: 0.871259 prediction:  [[1 0 3 3 3 4]] true Y:  [[1, 0, 2, 3, 3, 4]]

Prediction str:  ihlllo

9 loss: 0.774338 prediction:  [[1 0 2 3 3 4]] true Y:  [[1, 0, 2, 3, 3, 4]]

Prediction str:  ihello

10 loss: 0.676005 prediction:  [[1 0 2 3 3 4]] true Y:  [[1, 0, 2, 3, 3, 4]]

Prediction str:  ihello


...


'''


[# lab-12-2-char-seq-rnn ]


# lab-12-2-char-seq-rnn

"""

rnn 적용  ==> 정확도 높음

 - 49 loss: 0.000650434 Prediction: if you want you

 - y값 if you want you

"""

import os


os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'

# Lab 12 Character Sequence RNN

import tensorflow as tf

import numpy as np

tf.set_random_seed(777)  # reproducibility


sample = " if you want you"

idx2char = list(set(sample))  # index -> char

char2idx = {c: i for i, c in enumerate(idx2char)}  # char -> idex


# hyper parameters

dic_size = len(char2idx)  # RNN input size (one hot size)

hidden_size = len(char2idx)  # RNN output size

num_classes = len(char2idx)  # final output size (RNN or softmax, etc.)

batch_size = 1  # one sample data, one batch

sequence_length = len(sample) - 1  # number of lstm rollings (unit #)

learning_rate = 0.1


sample_idx = [char2idx[c] for c in sample]  # char to index

x_data = [sample_idx[:-1]]  # X data sample (0 ~ n-1) hello: hell

y_data = [sample_idx[1:]]   # Y label sample (1 ~ n) hello: ello


X = tf.placeholder(tf.int32, [None, sequence_length])  # X data

Y = tf.placeholder(tf.int32, [None, sequence_length])  # Y label


x_one_hot = tf.one_hot(X, num_classes)  # one hot: 1 -> 0 1 0 0 0 0 0 0 0 0

cell = tf.contrib.rnn.BasicLSTMCell(

    num_units=hidden_size, state_is_tuple=True)

initial_state = cell.zero_state(batch_size, tf.float32)

outputs, _states = tf.nn.dynamic_rnn(

    cell, x_one_hot, initial_state=initial_state, dtype=tf.float32)


# FC layer

X_for_fc = tf.reshape(outputs, [-1, hidden_size])

outputs = tf.contrib.layers.fully_connected(X_for_fc, num_classes, activation_fn=None)


# reshape out for sequence_loss

outputs = tf.reshape(outputs, [batch_size, sequence_length, num_classes])


weights = tf.ones([batch_size, sequence_length])

sequence_loss = tf.contrib.seq2seq.sequence_loss(

    logits=outputs, targets=Y, weights=weights)

loss = tf.reduce_mean(sequence_loss)

train = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(loss)


prediction = tf.argmax(outputs, axis=2)


with tf.Session() as sess:

    sess.run(tf.global_variables_initializer())

    for i in range(50):

        l, _ = sess.run([loss, train], feed_dict={X: x_data, Y: y_data})

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


        # print char using dic

        result_str = [idx2char[c] for c in np.squeeze(result)]


        print(i, "loss:", l, "Prediction:", ''.join(result_str))



'''

0 loss: 2.35377 Prediction: uuuuuuuuuuuuuuu

1 loss: 2.21383 Prediction: yy you y    you

2 loss: 2.04317 Prediction: yy yoo       ou

3 loss: 1.85869 Prediction: yy  ou      uou

4 loss: 1.65096 Prediction: yy you  a   you

5 loss: 1.40243 Prediction: yy you yan  you

6 loss: 1.12986 Prediction: yy you wann you

7 loss: 0.907699 Prediction: yy you want you

8 loss: 0.687401 Prediction: yf you want you

9 loss: 0.508868 Prediction: yf you want you

10 loss: 0.379423 Prediction: yf you want you

11 loss: 0.282956 Prediction: if you want you

12 loss: 0.208561 Prediction: if you want you


...


'''


[#lab-12-3-char-seq-softmax-only]

#lab-12-3-char-seq-softmax-only

"""

rnn 미적용 ==> 정확도 미흡함

 - 2999 loss: 0.277323 Prediction: yf you yant you

 - y값 if you want you

"""


import os

os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'

# Lab 12 Character Sequence Softmax only

import tensorflow as tf

import numpy as np

tf.set_random_seed(777)  # reproducibility


sample = " if you want you"

idx2char = list(set(sample))  # index -> char

char2idx = {c: i for i, c in enumerate(idx2char)}  # char -> idex


# hyper parameters

dic_size = len(char2idx)  # RNN input size (one hot size)

rnn_hidden_size = len(char2idx)  # RNN output size

num_classes = len(char2idx)  # final output size (RNN or softmax, etc.)

batch_size = 1  # one sample data, one batch

sequence_length = len(sample) - 1  # number of lstm rollings (unit #)

learning_rate = 0.1


sample_idx = [char2idx[c] for c in sample]  # char to index

x_data = [sample_idx[:-1]]  # X data sample (0 ~ n-1) hello: hell

y_data = [sample_idx[1:]]   # Y label sample (1 ~ n) hello: ello


X = tf.placeholder(tf.int32, [None, sequence_length])  # X data

Y = tf.placeholder(tf.int32, [None, sequence_length])  # Y label


# flatten the data (ignore batches for now). No effect if the batch size is 1

X_one_hot = tf.one_hot(X, num_classes)  # one hot: 1 -> 0 1 0 0 0 0 0 0 0 0

X_for_softmax = tf.reshape(X_one_hot, [-1, rnn_hidden_size])


# softmax layer (rnn_hidden_size -> num_classes)

softmax_w = tf.get_variable("softmax_w", [rnn_hidden_size, num_classes])

softmax_b = tf.get_variable("softmax_b", [num_classes])

outputs = tf.matmul(X_for_softmax, softmax_w) + softmax_b


# expend the data (revive the batches)

outputs = tf.reshape(outputs, [batch_size, sequence_length, num_classes])

weights = tf.ones([batch_size, sequence_length])


# Compute sequence cost/loss

sequence_loss = tf.contrib.seq2seq.sequence_loss(

    logits=outputs, targets=Y, weights=weights)

loss = tf.reduce_mean(sequence_loss)  # mean all sequence loss

train = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(loss)


prediction = tf.argmax(outputs, axis=2)


with tf.Session() as sess:

    sess.run(tf.global_variables_initializer())

    for i in range(3000):

        l, _ = sess.run([loss, train], feed_dict={X: x_data, Y: y_data})

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


        # print char using dic

        result_str = [idx2char[c] for c in np.squeeze(result)]

        print(i, "loss:", l, "Prediction:", ''.join(result_str))


'''

0 loss: 2.29513 Prediction: yu yny y y oyny

1 loss: 2.10156 Prediction: yu ynu y y oynu

2 loss: 1.92344 Prediction: yu you y u  you


..


2997 loss: 0.277323 Prediction: yf you yant you

2998 loss: 0.277323 Prediction: yf you yant you

2999 loss: 0.277323 Prediction: yf you yant you

'''


[# lab-12-4-rnn_long_char]


# lab-12-4-rnn_long_char

"""

error : from __future__ import print_function ==> 실행불가로 주석처리함

# MultiRNNCell 로 여러단을 만들면 , 정확도가 높아짐

# softmax =>reshape 수행

"""


import os

os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'

# from __future__ import print_function


import tensorflow as tf

import numpy as np

from tensorflow.contrib import rnn


tf.set_random_seed(777)  # reproducibility


sentence = ("if you want to build a ship, don't drum up people together to "

            "collect wood and don't assign them tasks and work, but rather "

            "teach them to long for the endless immensity of the sea.")


char_set = list(set(sentence))

char_dic = {w: i for i, w in enumerate(char_set)}


data_dim = len(char_set)

hidden_size = len(char_set)

num_classes = len(char_set)

sequence_length = 10  # Any arbitrary number

learning_rate = 0.1


dataX = []

dataY = []

for i in range(0, len(sentence) - sequence_length):

    x_str = sentence[i:i + sequence_length]

    y_str = sentence[i + 1: i + sequence_length + 1]

    print(i, x_str, '->', y_str)


    x = [char_dic[c] for c in x_str]  # x str to index

    y = [char_dic[c] for c in y_str]  # y str to index


    dataX.append(x)

    dataY.append(y)


batch_size = len(dataX)


X = tf.placeholder(tf.int32, [None, sequence_length])

Y = tf.placeholder(tf.int32, [None, sequence_length])


# One-hot encoding

X_one_hot = tf.one_hot(X, num_classes)

print(X_one_hot)  # check out the shape



# Make a lstm cell with hidden_size (each unit output vector size)

def lstm_cell():

    cell = rnn.BasicLSTMCell(hidden_size, state_is_tuple=True)

    return cell


multi_cells = rnn.MultiRNNCell([lstm_cell() for _ in range(2)], state_is_tuple=True)

# 위와 같이.MultiRNNCell 로 여러단을 만들면 , 정확도가 높아짐


# outputs: unfolding size x hidden size, state = hidden size

outputs, _states = tf.nn.dynamic_rnn(multi_cells, X_one_hot, dtype=tf.float32)


# softmax =>reshape 수행


# FC layer

X_for_fc = tf.reshape(outputs, [-1, hidden_size])

outputs = tf.contrib.layers.fully_connected(X_for_fc, num_classes, activation_fn=None)


# reshape out for sequence_loss

outputs = tf.reshape(outputs, [batch_size, sequence_length, num_classes])


# All weights are 1 (equal weights)

weights = tf.ones([batch_size, sequence_length])


sequence_loss = tf.contrib.seq2seq.sequence_loss(

    logits=outputs, targets=Y, weights=weights)

mean_loss = tf.reduce_mean(sequence_loss)

train_op = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(mean_loss)


sess = tf.Session()

sess.run(tf.global_variables_initializer())


for i in range(500):

    _, l, results = sess.run(

        [train_op, mean_loss, outputs], feed_dict={X: dataX, Y: dataY})

    for j, result in enumerate(results):

        index = np.argmax(result, axis=1)

        print(i, j, ''.join([char_set[t] for t in index]), l)


# Let's print the last char of each result to check it works

results = sess.run(outputs, feed_dict={X: dataX})

for j, result in enumerate(results):

    index = np.argmax(result, axis=1)

    if j is 0:  # print all for the first result to make a sentence

        print(''.join([char_set[t] for t in index]), end='')

    else:

        print(char_set[index[-1]], end='')


'''

0 167 tttttttttt 3.23111

0 168 tttttttttt 3.23111

0 169 tttttttttt 3.23111

499 167  of the se 0.229616

499 168 tf the sea 0.229616

499 169   the sea. 0.229616


g you want to build a ship, don't drum up people together to collect wood and don't assign them tasks and work, but rather teach them to long for the endless immensity of the sea.


'''


[# lab-12-5-rnn_stock_prediction]


# lab-12-5-rnn_stock_prediction
"""
내일 주가 예측 : 기존의 7일의 data를 학습
그래프 인쇄않되고 있음.
"""

import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
'''
This script shows how to predict stock prices using a basic RNN
'''
import tensorflow as tf
import numpy as np
import matplotlib
import os

tf.set_random_seed(777)  # reproducibility

if "DISPLAY" not in os.environ:
    # remove Travis CI Error
    matplotlib.use('Agg')

import matplotlib.pyplot as plt

def MinMaxScaler(data):
    ''' Min Max Normalization

    Parameters
    ----------
    data : numpy.ndarray
        input data to be normalized
        shape: [Batch size, dimension]

    Returns
    ----------
    data : numpy.ndarry
        normalized data
        shape: [Batch size, dimension]

    References
    ----------
    .. [1] http://sebastianraschka.com/Articles/2014_about_feature_scaling.html

    '''
    numerator = data - np.min(data, 0)
    denominator = np.max(data, 0) - np.min(data, 0)
    # noise term prevents the zero division
    return numerator / (denominator + 1e-7)


# train Parameters
seq_length = 7
data_dim = 5
hidden_dim = 10
output_dim = 1
learning_rate = 0.01
iterations = 500

# Open, High, Low, Volume, Close
xy = np.loadtxt('data-02-stock_daily.csv', delimiter=',')
xy = xy[::-1]  # reverse order (chronically ordered)
xy = MinMaxScaler(xy)
x = xy
y = xy[:, [-1]]  # Close as label

# build a dataset
dataX = []
dataY = []
for i in range(0, len(y) - seq_length):
    _x = x[i:i + seq_length]
    _y = y[i + seq_length]  # Next close price
    print(_x, "->", _y)
    dataX.append(_x)
    dataY.append(_y)

# train/test split
train_size = int(len(dataY) * 0.7)
test_size = len(dataY) - train_size
trainX, testX = np.array(dataX[0:train_size]), np.array(
    dataX[train_size:len(dataX)])
trainY, testY = np.array(dataY[0:train_size]), np.array(
    dataY[train_size:len(dataY)])

# input place holders
X = tf.placeholder(tf.float32, [None, seq_length, data_dim])
Y = tf.placeholder(tf.float32, [None, 1])

# build a LSTM network
cell = tf.contrib.rnn.BasicLSTMCell(
    num_units=hidden_dim, state_is_tuple=True, activation=tf.tanh)
outputs, _states = tf.nn.dynamic_rnn(cell, X, dtype=tf.float32)
Y_pred = tf.contrib.layers.fully_connected(
    outputs[:, -1], output_dim, activation_fn=None)  # We use the last cell's output

# cost/loss
loss = tf.reduce_sum(tf.square(Y_pred - Y))  # sum of the squares
# optimizer
optimizer = tf.train.AdamOptimizer(learning_rate)
train = optimizer.minimize(loss)

# RMSE
targets = tf.placeholder(tf.float32, [None, 1])
predictions = tf.placeholder(tf.float32, [None, 1])
rmse = tf.sqrt(tf.reduce_mean(tf.square(targets - predictions)))

with tf.Session() as sess:
    init = tf.global_variables_initializer()
    sess.run(init)

    # Training step
    for i in range(iterations):
        _, step_loss = sess.run([train, loss], feed_dict={
                                X: trainX, Y: trainY})
        print("[step: {}] loss: {}".format(i, step_loss))

    # Test step
    test_predict = sess.run(Y_pred, feed_dict={X: testX})
    rmse_val = sess.run(rmse, feed_dict={
                    targets: testY, predictions: test_predict})
    print("RMSE: {}".format(rmse_val))

    # Plot predictions
    plt.plot(testY)
    plt.plot(test_predict)
    plt.xlabel("Time Period")
    plt.ylabel("Stock Price")
    plt.show()


[참고자료]


  https://www.inflearn.com/course/기본적인-머신러닝-딥러닝-강좌

  https://github.com/hunkim/deeplearningzerotoall

  https://www.tensorflow.org/api_docs/python/tf/layers


Comment +0

인공지능 구현에 대한 글입니다.


글의 순서는 아래와 같습니다.


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

1.# lab-11-1-mnist_cnn

    ==> http://cs.stanford.edu/people/karpathy/convnetjs/demo/cifar10.html : cnn 시물레이션

    ==> 2단 CNN => 정확도 98.83%

    ==> CNN은 수행시간이 많이 소요됨, 코드 테스트시에는 TRAINING EPOCHS를 줄여 실행할 필요가 있음.

         물론 구글, 아마존 크라우드 서비스 활용도 가능함( 경우에 따라 비용발생)

         그래픽 카드 (GPU NVIDIA )를 적용하는 방법도 있음.


2. # lab-11-2-mnist_deep_cnn 

   ==> 3단 CNN : 정확도 99.38 %


3. # lab-11-3-mnist_cnn_class

   ==> python의 clss를 이용해서 코드의 반복을 줄이고, 코드를 단순화함


4. #lab-11-4-mnist_cnn_layers

   ==> tf.layers 패키지 사용하면 코드가 더욱 단순화 될수 있음

   ==> https://www.tensorflow.org/api_docs/python/tf/layers

       

5. # lab-11-5-mnist_cnn_ensemble_layers

  ==> 각각 예측시키고, 후에 병합을 해서 예측치를 출력시킴

  ==> 정확도 : 99.52%


6. 코드탐구(추가)

  ==>lab-11-X-mnist_cnn_low_memory


7. 참고자료

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


[  lab-11-1-mnist_cnn ]


# lab-11-1-mnist_cnn

"""http://cs.stanford.edu/people/karpathy/convnetjs/demo/cifar10.html : cnn 시물레이션

   2단 CNN => 정확도 98.83%

"""

import os

os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'

# Lab 11 MNIST and Convolutional Neural Network

import tensorflow as tf

import random

# import matplotlib.pyplot as plt


from tensorflow.examples.tutorials.mnist import input_data


tf.set_random_seed(777)  # reproducibility


mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)

# Check out https://www.tensorflow.org/get_started/mnist/beginners for

# more information about the mnist dataset


# hyper parameters

learning_rate = 0.001

training_epochs = 15

batch_size = 100


# input place holders

X = tf.placeholder(tf.float32, [None, 784])

X_img = tf.reshape(X, [-1, 28, 28, 1])   # img 28x28x1 (black/white)

Y = tf.placeholder(tf.float32, [None, 10])


# L1 ImgIn shape=(?, 28, 28, 1)

W1 = tf.Variable(tf.random_normal([3, 3, 1, 32], stddev=0.01))

#    Conv     -> (?, 28, 28, 32)

#    Pool     -> (?, 14, 14, 32)

L1 = tf.nn.conv2d(X_img, W1, strides=[1, 1, 1, 1], padding='SAME')

L1 = tf.nn.relu(L1)

L1 = tf.nn.max_pool(L1, ksize=[1, 2, 2, 1],

                    strides=[1, 2, 2, 1], padding='SAME')

'''

Tensor("Conv2D:0", shape=(?, 28, 28, 32), dtype=float32)

Tensor("Relu:0", shape=(?, 28, 28, 32), dtype=float32)

Tensor("MaxPool:0", shape=(?, 14, 14, 32), dtype=float32)

'''


# L2 ImgIn shape=(?, 14, 14, 32)

W2 = tf.Variable(tf.random_normal([3, 3, 32, 64], stddev=0.01))

#    Conv      ->(?, 14, 14, 64)

#    Pool      ->(?, 7, 7, 64)

L2 = tf.nn.conv2d(L1, W2, strides=[1, 1, 1, 1], padding='SAME')

L2 = tf.nn.relu(L2)

L2 = tf.nn.max_pool(L2, ksize=[1, 2, 2, 1],

                    strides=[1, 2, 2, 1], padding='SAME')

L2_flat = tf.reshape(L2, [-1, 7 * 7 * 64])

'''

Tensor("Conv2D_1:0", shape=(?, 14, 14, 64), dtype=float32)

Tensor("Relu_1:0", shape=(?, 14, 14, 64), dtype=float32)

Tensor("MaxPool_1:0", shape=(?, 7, 7, 64), dtype=float32)

Tensor("Reshape_1:0", shape=(?, 3136), dtype=float32)

'''


# Final FC 7x7x64 inputs -> 10 outputs

W3 = tf.get_variable("W3", shape=[7 * 7 * 64, 10],

                     initializer=tf.contrib.layers.xavier_initializer())

b = tf.Variable(tf.random_normal([10]))

logits = tf.matmul(L2_flat, W3) + b


# define cost/loss & optimizer

cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(

    logits=logits, labels=Y))

optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)


# initialize

sess = tf.Session()

sess.run(tf.global_variables_initializer())


# train my model

print('Learning started. It takes sometime.')

for epoch in range(training_epochs):

    avg_cost = 0

    total_batch = int(mnist.train.num_examples / batch_size)


    for i in range(total_batch):

        batch_xs, batch_ys = mnist.train.next_batch(batch_size)

        feed_dict = {X: batch_xs, Y: batch_ys}

        c, _ = sess.run([cost, optimizer], feed_dict=feed_dict)

        avg_cost += c / total_batch


    print('Epoch:', '%04d' % (epoch + 1), 'cost =', '{:.9f}'.format(avg_cost))


print('Learning Finished!')


# Test model and check accuracy

correct_prediction = tf.equal(tf.argmax(logits, 1), tf.argmax(Y, 1))

accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

print('Accuracy:', sess.run(accuracy, feed_dict={

      X: mnist.test.images, Y: mnist.test.labels}))


# Get one and predict

r = random.randint(0, mnist.test.num_examples - 1)

print("Label: ", sess.run(tf.argmax(mnist.test.labels[r:r + 1], 1)))

print("Prediction: ", sess.run(

    tf.argmax(logits, 1), feed_dict={X: mnist.test.images[r:r + 1]}))


# plt.imshow(mnist.test.images[r:r + 1].

#           reshape(28, 28), cmap='Greys', interpolation='nearest')

# plt.show()


'''

Epoch: 0001 cost = 0.340291267

Epoch: 0002 cost = 0.090731326

Epoch: 0003 cost = 0.064477619

Epoch: 0004 cost = 0.050683064

Epoch: 0005 cost = 0.041864835

Epoch: 0006 cost = 0.035760704

Epoch: 0007 cost = 0.030572132

Epoch: 0008 cost = 0.026207981

Epoch: 0009 cost = 0.022622454

Epoch: 0010 cost = 0.019055919

Epoch: 0011 cost = 0.017758641

Epoch: 0012 cost = 0.014156652

Epoch: 0013 cost = 0.012397016

Epoch: 0014 cost = 0.010693789

Epoch: 0015 cost = 0.009469977

Learning Finished!

Accuracy: 0.9885

'''



[ # lab-11-2-mnist_deep_cnn ]


# lab-11-2-mnist_deep_cnn

"""

3단 CNN : 정확도 99.38 %


"""

import os

os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'

# Lab 11 MNIST and Deep learning CNN

import tensorflow as tf

import random

# import matplotlib.pyplot as plt


from tensorflow.examples.tutorials.mnist import input_data


tf.set_random_seed(777)  # reproducibility


mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)

# Check out https://www.tensorflow.org/get_started/mnist/beginners for

# more information about the mnist dataset


# hyper parameters

learning_rate = 0.001

training_epochs = 15

batch_size = 100


# dropout (keep_prob) rate  0.7~0.5 on training, but should be 1 for testing

keep_prob = tf.placeholder(tf.float32)


# input place holders

X = tf.placeholder(tf.float32, [None, 784])

X_img = tf.reshape(X, [-1, 28, 28, 1])   # img 28x28x1 (black/white)

Y = tf.placeholder(tf.float32, [None, 10])


# L1 ImgIn shape=(?, 28, 28, 1)

W1 = tf.Variable(tf.random_normal([3, 3, 1, 32], stddev=0.01))

#    Conv     -> (?, 28, 28, 32)

#    Pool     -> (?, 14, 14, 32)

L1 = tf.nn.conv2d(X_img, W1, strides=[1, 1, 1, 1], padding='SAME')

L1 = tf.nn.relu(L1)

L1 = tf.nn.max_pool(L1, ksize=[1, 2, 2, 1],

                    strides=[1, 2, 2, 1], padding='SAME')

L1 = tf.nn.dropout(L1, keep_prob=keep_prob)

'''

Tensor("Conv2D:0", shape=(?, 28, 28, 32), dtype=float32)

Tensor("Relu:0", shape=(?, 28, 28, 32), dtype=float32)

Tensor("MaxPool:0", shape=(?, 14, 14, 32), dtype=float32)

Tensor("dropout/mul:0", shape=(?, 14, 14, 32), dtype=float32)

'''


# L2 ImgIn shape=(?, 14, 14, 32)

W2 = tf.Variable(tf.random_normal([3, 3, 32, 64], stddev=0.01))

#    Conv      ->(?, 14, 14, 64)

#    Pool      ->(?, 7, 7, 64)

L2 = tf.nn.conv2d(L1, W2, strides=[1, 1, 1, 1], padding='SAME')

L2 = tf.nn.relu(L2)

L2 = tf.nn.max_pool(L2, ksize=[1, 2, 2, 1],

                    strides=[1, 2, 2, 1], padding='SAME')

L2 = tf.nn.dropout(L2, keep_prob=keep_prob)

'''

Tensor("Conv2D_1:0", shape=(?, 14, 14, 64), dtype=float32)

Tensor("Relu_1:0", shape=(?, 14, 14, 64), dtype=float32)

Tensor("MaxPool_1:0", shape=(?, 7, 7, 64), dtype=float32)

Tensor("dropout_1/mul:0", shape=(?, 7, 7, 64), dtype=float32)

'''


# L3 ImgIn shape=(?, 7, 7, 64)

W3 = tf.Variable(tf.random_normal([3, 3, 64, 128], stddev=0.01))

#    Conv      ->(?, 7, 7, 128)

#    Pool      ->(?, 4, 4, 128)

#    Reshape   ->(?, 4 * 4 * 128) # Flatten them for FC

L3 = tf.nn.conv2d(L2, W3, strides=[1, 1, 1, 1], padding='SAME')

L3 = tf.nn.relu(L3)

L3 = tf.nn.max_pool(L3, ksize=[1, 2, 2, 1], strides=[

                    1, 2, 2, 1], padding='SAME')

L3 = tf.nn.dropout(L3, keep_prob=keep_prob)

L3_flat = tf.reshape(L3, [-1, 128 * 4 * 4])

'''

Tensor("Conv2D_2:0", shape=(?, 7, 7, 128), dtype=float32)

Tensor("Relu_2:0", shape=(?, 7, 7, 128), dtype=float32)

Tensor("MaxPool_2:0", shape=(?, 4, 4, 128), dtype=float32)

Tensor("dropout_2/mul:0", shape=(?, 4, 4, 128), dtype=float32)

Tensor("Reshape_1:0", shape=(?, 2048), dtype=float32)

'''


# L4 FC 4x4x128 inputs -> 625 outputs

W4 = tf.get_variable("W4", shape=[128 * 4 * 4, 625],

                     initializer=tf.contrib.layers.xavier_initializer())

b4 = tf.Variable(tf.random_normal([625]))

L4 = tf.nn.relu(tf.matmul(L3_flat, W4) + b4)

L4 = tf.nn.dropout(L4, keep_prob=keep_prob)

'''

Tensor("Relu_3:0", shape=(?, 625), dtype=float32)

Tensor("dropout_3/mul:0", shape=(?, 625), dtype=float32)

'''


# L5 Final FC 625 inputs -> 10 outputs

W5 = tf.get_variable("W5", shape=[625, 10],

                     initializer=tf.contrib.layers.xavier_initializer())

b5 = tf.Variable(tf.random_normal([10]))

logits = tf.matmul(L4, W5) + b5

'''

Tensor("add_1:0", shape=(?, 10), dtype=float32)

'''


# define cost/loss & optimizer

cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(

    logits=logits, labels=Y))

optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)


# initialize

sess = tf.Session()

sess.run(tf.global_variables_initializer())


# train my model

print('Learning started. It takes sometime.')

for epoch in range(training_epochs):

    avg_cost = 0

    total_batch = int(mnist.train.num_examples / batch_size)


    for i in range(total_batch):

        batch_xs, batch_ys = mnist.train.next_batch(batch_size)

        feed_dict = {X: batch_xs, Y: batch_ys, keep_prob: 0.7}

        c, _ = sess.run([cost, optimizer], feed_dict=feed_dict)

        avg_cost += c / total_batch


    print('Epoch:', '%04d' % (epoch + 1), 'cost =', '{:.9f}'.format(avg_cost))


print('Learning Finished!')


# Test model and check accuracy


# if you have a OOM error, please refer to lab-11-X-mnist_deep_cnn_low_memory.py


correct_prediction = tf.equal(tf.argmax(logits, 1), tf.argmax(Y, 1))

accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

print('Accuracy:', sess.run(accuracy, feed_dict={

      X: mnist.test.images, Y: mnist.test.labels, keep_prob: 1}))


# Get one and predict

r = random.randint(0, mnist.test.num_examples - 1)

print("Label: ", sess.run(tf.argmax(mnist.test.labels[r:r + 1], 1)))

print("Prediction: ", sess.run(

    tf.argmax(logits, 1), feed_dict={X: mnist.test.images[r:r + 1], keep_prob: 1}))


# plt.imshow(mnist.test.images[r:r + 1].

#           reshape(28, 28), cmap='Greys', interpolation='nearest')

# plt.show()


'''

Learning stared. It takes sometime.

Epoch: 0001 cost = 0.385748474

Epoch: 0002 cost = 0.092017397

Epoch: 0003 cost = 0.065854684

Epoch: 0004 cost = 0.055604566

Epoch: 0005 cost = 0.045996377

Epoch: 0006 cost = 0.040913645

Epoch: 0007 cost = 0.036924479

Epoch: 0008 cost = 0.032808939

Epoch: 0009 cost = 0.031791007

Epoch: 0010 cost = 0.030224456

Epoch: 0011 cost = 0.026849916

Epoch: 0012 cost = 0.026826763

Epoch: 0013 cost = 0.027188021

Epoch: 0014 cost = 0.023604777

Epoch: 0015 cost = 0.024607201

Learning Finished!

Accuracy: 0.9938




[ # lab-11-3-mnist_cnn_class ]


# lab-11-3-mnist_cnn_class

""" python의 clss를 이용해서 코드의 반복을 줄이고, 코드를 단순화함"""


import os

os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'

# Lab 11 MNIST and Deep learning CNN

import tensorflow as tf

# import matplotlib.pyplot as plt


from tensorflow.examples.tutorials.mnist import input_data


tf.set_random_seed(777)  # reproducibility


mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)

# Check out https://www.tensorflow.org/get_started/mnist/beginners for

# more information about the mnist dataset


# hyper parameters

learning_rate = 0.001

training_epochs = 15

batch_size = 100



class Model:


    def __init__(self, sess, name):

        self.sess = sess

        self.name = name

        self._build_net()


    def _build_net(self):

        with tf.variable_scope(self.name):

            # dropout (keep_prob) rate  0.7~0.5 on training, but should be 1

            # for testing

            self.keep_prob = tf.placeholder(tf.float32)


            # input place holders

            self.X = tf.placeholder(tf.float32, [None, 784])

            # img 28x28x1 (black/white)

            X_img = tf.reshape(self.X, [-1, 28, 28, 1])

            self.Y = tf.placeholder(tf.float32, [None, 10])


            # L1 ImgIn shape=(?, 28, 28, 1)

            W1 = tf.Variable(tf.random_normal([3, 3, 1, 32], stddev=0.01))

            #    Conv     -> (?, 28, 28, 32)

            #    Pool     -> (?, 14, 14, 32)

            L1 = tf.nn.conv2d(X_img, W1, strides=[1, 1, 1, 1], padding='SAME')

            L1 = tf.nn.relu(L1)

            L1 = tf.nn.max_pool(L1, ksize=[1, 2, 2, 1],

                                strides=[1, 2, 2, 1], padding='SAME')

            L1 = tf.nn.dropout(L1, keep_prob=self.keep_prob)

            '''

            Tensor("Conv2D:0", shape=(?, 28, 28, 32), dtype=float32)

            Tensor("Relu:0", shape=(?, 28, 28, 32), dtype=float32)

            Tensor("MaxPool:0", shape=(?, 14, 14, 32), dtype=float32)

            Tensor("dropout/mul:0", shape=(?, 14, 14, 32), dtype=float32)

            '''


            # L2 ImgIn shape=(?, 14, 14, 32)

            W2 = tf.Variable(tf.random_normal([3, 3, 32, 64], stddev=0.01))

            #    Conv      ->(?, 14, 14, 64)

            #    Pool      ->(?, 7, 7, 64)

            L2 = tf.nn.conv2d(L1, W2, strides=[1, 1, 1, 1], padding='SAME')

            L2 = tf.nn.relu(L2)

            L2 = tf.nn.max_pool(L2, ksize=[1, 2, 2, 1],

                                strides=[1, 2, 2, 1], padding='SAME')

            L2 = tf.nn.dropout(L2, keep_prob=self.keep_prob)

            '''

            Tensor("Conv2D_1:0", shape=(?, 14, 14, 64), dtype=float32)

            Tensor("Relu_1:0", shape=(?, 14, 14, 64), dtype=float32)

            Tensor("MaxPool_1:0", shape=(?, 7, 7, 64), dtype=float32)

            Tensor("dropout_1/mul:0", shape=(?, 7, 7, 64), dtype=float32)

            '''


            # L3 ImgIn shape=(?, 7, 7, 64)

            W3 = tf.Variable(tf.random_normal([3, 3, 64, 128], stddev=0.01))

            #    Conv      ->(?, 7, 7, 128)

            #    Pool      ->(?, 4, 4, 128)

            #    Reshape   ->(?, 4 * 4 * 128) # Flatten them for FC

            L3 = tf.nn.conv2d(L2, W3, strides=[1, 1, 1, 1], padding='SAME')

            L3 = tf.nn.relu(L3)

            L3 = tf.nn.max_pool(L3, ksize=[1, 2, 2, 1], strides=[

                                1, 2, 2, 1], padding='SAME')

            L3 = tf.nn.dropout(L3, keep_prob=self.keep_prob)


            L3_flat = tf.reshape(L3, [-1, 128 * 4 * 4])

            '''

            Tensor("Conv2D_2:0", shape=(?, 7, 7, 128), dtype=float32)

            Tensor("Relu_2:0", shape=(?, 7, 7, 128), dtype=float32)

            Tensor("MaxPool_2:0", shape=(?, 4, 4, 128), dtype=float32)

            Tensor("dropout_2/mul:0", shape=(?, 4, 4, 128), dtype=float32)

            Tensor("Reshape_1:0", shape=(?, 2048), dtype=float32)

            '''


            # L4 FC 4x4x128 inputs -> 625 outputs

            W4 = tf.get_variable("W4", shape=[128 * 4 * 4, 625],

                                 initializer=tf.contrib.layers.xavier_initializer())

            b4 = tf.Variable(tf.random_normal([625]))

            L4 = tf.nn.relu(tf.matmul(L3_flat, W4) + b4)

            L4 = tf.nn.dropout(L4, keep_prob=self.keep_prob)

            '''

            Tensor("Relu_3:0", shape=(?, 625), dtype=float32)

            Tensor("dropout_3/mul:0", shape=(?, 625), dtype=float32)

            '''


            # L5 Final FC 625 inputs -> 10 outputs

            W5 = tf.get_variable("W5", shape=[625, 10],

                                 initializer=tf.contrib.layers.xavier_initializer())

            b5 = tf.Variable(tf.random_normal([10]))

            self.logits = tf.matmul(L4, W5) + b5

            '''

            Tensor("add_1:0", shape=(?, 10), dtype=float32)

            '''


        # define cost/loss & optimizer

        self.cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(

            logits=self.logits, labels=self.Y))

        self.optimizer = tf.train.AdamOptimizer(

            learning_rate=learning_rate).minimize(self.cost)


        correct_prediction = tf.equal(

            tf.argmax(self.logits, 1), tf.argmax(self.Y, 1))

        self.accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))


    def predict(self, x_test, keep_prop=1.0):

        return self.sess.run(self.logits, feed_dict={self.X: x_test, self.keep_prob: keep_prop})


    def get_accuracy(self, x_test, y_test, keep_prop=1.0):

        return self.sess.run(self.accuracy, feed_dict={self.X: x_test, self.Y: y_test, self.keep_prob: keep_prop})


    def train(self, x_data, y_data, keep_prop=0.7):

        return self.sess.run([self.cost, self.optimizer], feed_dict={

            self.X: x_data, self.Y: y_data, self.keep_prob: keep_prop})


# initialize

sess = tf.Session()

m1 = Model(sess, "m1")


sess.run(tf.global_variables_initializer())


print('Learning Started!')


# train my model

for epoch in range(training_epochs):

    avg_cost = 0

    total_batch = int(mnist.train.num_examples / batch_size)


    for i in range(total_batch):

        batch_xs, batch_ys = mnist.train.next_batch(batch_size)

        c, _ = m1.train(batch_xs, batch_ys)

        avg_cost += c / total_batch


    print('Epoch:', '%04d' % (epoch + 1), 'cost =', '{:.9f}'.format(avg_cost))


print('Learning Finished!')


# Test model and check accuracy

print('Accuracy:', m1.get_accuracy(mnist.test.images, mnist.test.labels))



[#lab-11-4-mnist_cnn_layers]


#lab-11-4-mnist_cnn_layers

"""

tf.layers 패키지 사용하면 코드가 더욱 단순화 될수 있음

 https://www.tensorflow.org/api_docs/python/tf/layers

CNN은 수행시간이 많이 소요됨, 코드 테스트시에는 TRAINING EPOCHS를 줄여 실행할 필요가 있음.

물론 구글, 아마존 크라우드 서비스 활용도 가능함( 경우에 따라 비용발생)

그래픽 카드 (GPU NVIDIA )를 적용하는 방법도 있음.

"""


import os

os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'

# Lab 11 MNIST and Deep learning CNN

import tensorflow as tf

# import matplotlib.pyplot as plt


from tensorflow.examples.tutorials.mnist import input_data


tf.set_random_seed(777)  # reproducibility


mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)

# Check out https://www.tensorflow.org/get_started/mnist/beginners for

# more information about the mnist dataset


# hyper parameters

learning_rate = 0.001

training_epochs = 15

batch_size = 100



class Model:


    def __init__(self, sess, name):

        self.sess = sess

        self.name = name

        self._build_net()


    def _build_net(self):

        with tf.variable_scope(self.name):

            # dropout (keep_prob) rate  0.7~0.5 on training, but should be 1

            # for testing

            self.training = tf.placeholder(tf.bool)


            # input place holders

            self.X = tf.placeholder(tf.float32, [None, 784])


            # img 28x28x1 (black/white), Input Layer

            X_img = tf.reshape(self.X, [-1, 28, 28, 1])

            self.Y = tf.placeholder(tf.float32, [None, 10])


            # Convolutional Layer #1

            conv1 = tf.layers.conv2d(inputs=X_img, filters=32, kernel_size=[3, 3],

                                     padding="SAME", activation=tf.nn.relu)

            # Pooling Layer #1

            pool1 = tf.layers.max_pooling2d(inputs=conv1, pool_size=[2, 2],

                                            padding="SAME", strides=2)

            dropout1 = tf.layers.dropout(inputs=pool1,

                                         rate=0.7, training=self.training)


            # Convolutional Layer #2 and Pooling Layer #2

            conv2 = tf.layers.conv2d(inputs=dropout1, filters=64, kernel_size=[3, 3],

                                     padding="SAME", activation=tf.nn.relu)

            pool2 = tf.layers.max_pooling2d(inputs=conv2, pool_size=[2, 2],

                                            padding="SAME", strides=2)

            dropout2 = tf.layers.dropout(inputs=pool2,

                                         rate=0.7, training=self.training)


            # Convolutional Layer #2 and Pooling Layer #2

            conv3 = tf.layers.conv2d(inputs=dropout2, filters=128, kernel_size=[3, 3],

                                     padding="same", activation=tf.nn.relu)

            pool3 = tf.layers.max_pooling2d(inputs=conv3, pool_size=[2, 2],

                                            padding="same", strides=2)

            dropout3 = tf.layers.dropout(inputs=pool3,

                                         rate=0.7, training=self.training)


            # Dense Layer with Relu

            flat = tf.reshape(dropout3, [-1, 128 * 4 * 4])

            dense4 = tf.layers.dense(inputs=flat,

                                     units=625, activation=tf.nn.relu)

            dropout4 = tf.layers.dropout(inputs=dense4,

                                         rate=0.5, training=self.training)


            # Logits (no activation) Layer: L5 Final FC 625 inputs -> 10 outputs

            self.logits = tf.layers.dense(inputs=dropout4, units=10)


        # define cost/loss & optimizer

        self.cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(

            logits=self.logits, labels=self.Y))

        self.optimizer = tf.train.AdamOptimizer(

            learning_rate=learning_rate).minimize(self.cost)


        correct_prediction = tf.equal(

            tf.argmax(self.logits, 1), tf.argmax(self.Y, 1))

        self.accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))


    def predict(self, x_test, training=False):

        return self.sess.run(self.logits,

                             feed_dict={self.X: x_test, self.training: training})


    def get_accuracy(self, x_test, y_test, training=False):

        return self.sess.run(self.accuracy,

                             feed_dict={self.X: x_test,

                                        self.Y: y_test, self.training: training})


    def train(self, x_data, y_data, training=True):

        return self.sess.run([self.cost, self.optimizer], feed_dict={

            self.X: x_data, self.Y: y_data, self.training: training})


# initialize

sess = tf.Session()

m1 = Model(sess, "m1")


sess.run(tf.global_variables_initializer())


print('Learning Started!')


# train my model

for epoch in range(training_epochs):

    avg_cost = 0

    total_batch = int(mnist.train.num_examples / batch_size)


    for i in range(total_batch):

        batch_xs, batch_ys = mnist.train.next_batch(batch_size)

        c, _ = m1.train(batch_xs, batch_ys)

        avg_cost += c / total_batch


    print('Epoch:', '%04d' % (epoch + 1), 'cost =', '{:.9f}'.format(avg_cost))


print('Learning Finished!')


# Test model and check accuracy

print('Accuracy:', m1.get_accuracy(mnist.test.images, mnist.test.labels))




[ # lab-11-5-mnist_cnn_ensemble_layers ]


# lab-11-5-mnist_cnn_ensemble_layers


" 각각 예측시키고, 후에 병합을 해서 예측치를 출력시킴"


import os

os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'

# Lab 11 MNIST and Deep learning CNN

# https://www.tensorflow.org/tutorials/layers

import tensorflow as tf

import numpy as np


from tensorflow.examples.tutorials.mnist import input_data


tf.set_random_seed(777)  # reproducibility


mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)

# Check out https://www.tensorflow.org/get_started/mnist/beginners for

# more information about the mnist dataset


# hyper parameters

learning_rate = 0.001

training_epochs = 20

batch_size = 100



class Model:


    def __init__(self, sess, name):

        self.sess = sess

        self.name = name

        self._build_net()


    def _build_net(self):

        with tf.variable_scope(self.name):

            # dropout (keep_prob) rate  0.7~0.5 on training, but should be 1

            # for testing

            self.training = tf.placeholder(tf.bool)


            # input place holders

            self.X = tf.placeholder(tf.float32, [None, 784])


            # img 28x28x1 (black/white), Input Layer

            X_img = tf.reshape(self.X, [-1, 28, 28, 1])

            self.Y = tf.placeholder(tf.float32, [None, 10])


            # Convolutional Layer #1

            conv1 = tf.layers.conv2d(inputs=X_img, filters=32, kernel_size=[3, 3],

                                     padding="SAME", activation=tf.nn.relu)

            # Pooling Layer #1

            pool1 = tf.layers.max_pooling2d(inputs=conv1, pool_size=[2, 2],

                                            padding="SAME", strides=2)

            dropout1 = tf.layers.dropout(inputs=pool1,

                                         rate=0.7, training=self.training)


            # Convolutional Layer #2 and Pooling Layer #2

            conv2 = tf.layers.conv2d(inputs=dropout1, filters=64, kernel_size=[3, 3],

                                     padding="SAME", activation=tf.nn.relu)

            pool2 = tf.layers.max_pooling2d(inputs=conv2, pool_size=[2, 2],

                                            padding="SAME", strides=2)

            dropout2 = tf.layers.dropout(inputs=pool2,

                                         rate=0.7, training=self.training)


            # Convolutional Layer #3 and Pooling Layer #3

            conv3 = tf.layers.conv2d(inputs=dropout2, filters=128, kernel_size=[3, 3],

                                     padding="SAME", activation=tf.nn.relu)

            pool3 = tf.layers.max_pooling2d(inputs=conv3, pool_size=[2, 2],

                                            padding="SAME", strides=2)

            dropout3 = tf.layers.dropout(inputs=pool3,

                                         rate=0.7, training=self.training)


            # Dense Layer with Relu

            flat = tf.reshape(dropout3, [-1, 128 * 4 * 4])

            dense4 = tf.layers.dense(inputs=flat,

                                     units=625, activation=tf.nn.relu)

            dropout4 = tf.layers.dropout(inputs=dense4,

                                         rate=0.5, training=self.training)


            # Logits (no activation) Layer: L5 Final FC 625 inputs -> 10 outputs

            self.logits = tf.layers.dense(inputs=dropout4, units=10)


        # define cost/loss & optimizer

        self.cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(

            logits=self.logits, labels=self.Y))

        self.optimizer = tf.train.AdamOptimizer(

            learning_rate=learning_rate).minimize(self.cost)


        correct_prediction = tf.equal(

            tf.argmax(self.logits, 1), tf.argmax(self.Y, 1))

        self.accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))


    def predict(self, x_test, training=False):

        return self.sess.run(self.logits,

                             feed_dict={self.X: x_test, self.training: training})


    def get_accuracy(self, x_test, y_test, training=False):

        return self.sess.run(self.accuracy,

                             feed_dict={self.X: x_test,

                                        self.Y: y_test, self.training: training})


    def train(self, x_data, y_data, training=True):

        return self.sess.run([self.cost, self.optimizer], feed_dict={

            self.X: x_data, self.Y: y_data, self.training: training})


# initialize

sess = tf.Session()


models = []

num_models = 2

for m in range(num_models):

    models.append(Model(sess, "model" + str(m)))


sess.run(tf.global_variables_initializer())


print('Learning Started!')


# train my model

for epoch in range(training_epochs):

    avg_cost_list = np.zeros(len(models))

    total_batch = int(mnist.train.num_examples / batch_size)

    for i in range(total_batch):

        batch_xs, batch_ys = mnist.train.next_batch(batch_size)


        # train each model

        for m_idx, m in enumerate(models):

            c, _ = m.train(batch_xs, batch_ys)

            avg_cost_list[m_idx] += c / total_batch


    print('Epoch:', '%04d' % (epoch + 1), 'cost =', avg_cost_list)


print('Learning Finished!')


# Test model and check accuracy

test_size = len(mnist.test.labels)

predictions = np.zeros(test_size * 10).reshape(test_size, 10)

for m_idx, m in enumerate(models):

    print(m_idx, 'Accuracy:', m.get_accuracy(

        mnist.test.images, mnist.test.labels))

    p = m.predict(mnist.test.images)

    predictions += p


ensemble_correct_prediction = tf.equal(

    tf.argmax(predictions, 1), tf.argmax(mnist.test.labels, 1))

ensemble_accuracy = tf.reduce_mean(

    tf.cast(ensemble_correct_prediction, tf.float32))

print('Ensemble accuracy:', sess.run(ensemble_accuracy))


'''

0 Accuracy: 0.9933

1 Accuracy: 0.9946

2 Accuracy: 0.9934

3 Accuracy: 0.9935

4 Accuracy: 0.9935

5 Accuracy: 0.9949

6 Accuracy: 0.9941


Ensemble accuracy: 0.9952

'''




[ 참고자료 ]

  https://www.inflearn.com/course/기본적인-머신러닝-딥러닝-강좌

  https://github.com/hunkim/deeplearningzerotoall

  https://www.tensorflow.org/api_docs/python/tf/layers


Comment +0

인공지능 구현에 대한 글입니다.


글의 순서는 아래와 같습니다.


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

1. # lab-10-1-mnist_softmax 

    ==># 정확도 90%

2. # lab-10-2-mnist_nn

   3 단 깊이 /relu 적용=> 정확도 94 %

3. # lab-10-3-mnist_nn_xavier
   xavier 적용(초기값 세팅) ==> 정확도 97.6 %

4. #lab-10-4-mnist_nn_deep

   더 넓게(512), 더깊게 (L5) ==> 정확도 향상효과 없음

   원인: OVERFITTING  대책 : Drop out 적용

5. lab-10-5-mnist_nn_dropout
   drop out 적용 : 정확도 97.96%
6. 코드 탐구(추가)
   lab-10-6-mnist_nn_batchnorm
   lab-10-7-mnist_nn_higher_level_API
   lab-10-8-mnist_nn_selu(wip)
   lab-10-X1-mnist_back_prop

7. 참고자료

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


[  lab-10-1-mnist_softmax]


# lab-10-1-mnist_softmax

# 정확도 90%

import os

os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'

# Lab 7 Learning rate and Evaluation

import tensorflow as tf

import random

#import matplotlib.pyplot as plt

from tensorflow.examples.tutorials.mnist import input_data

tf.set_random_seed(777)  # reproducibility


mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)

# Check out https://www.tensorflow.org/get_started/mnist/beginners for

# more information about the mnist dataset


# parameters

learning_rate = 0.001

training_epochs = 15

batch_size = 100


# input place holders

X = tf.placeholder(tf.float32, [None, 784])

Y = tf.placeholder(tf.float32, [None, 10])


# weights & bias for nn layers

W = tf.Variable(tf.random_normal([784, 10]))

b = tf.Variable(tf.random_normal([10]))


hypothesis = tf.matmul(X, W) + b


# define cost/loss & optimizer

cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(

    logits=hypothesis, labels=Y))

optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)


# initialize

sess = tf.Session()

sess.run(tf.global_variables_initializer())


# train my model

for epoch in range(training_epochs):

    avg_cost = 0

    total_batch = int(mnist.train.num_examples / batch_size)


    for i in range(total_batch):

        batch_xs, batch_ys = mnist.train.next_batch(batch_size)

        feed_dict = {X: batch_xs, Y: batch_ys}

        c, _ = sess.run([cost, optimizer], feed_dict=feed_dict)

        avg_cost += c / total_batch


    print('Epoch:', '%04d' % (epoch + 1), 'cost =', '{:.9f}'.format(avg_cost))


print('Learning Finished!')


# Test model and check accuracy

correct_prediction = tf.equal(tf.argmax(hypothesis, 1), tf.argmax(Y, 1))

accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

print('Accuracy:', sess.run(accuracy, feed_dict={

      X: mnist.test.images, Y: mnist.test.labels}))


# Get one and predict

r = random.randint(0, mnist.test.num_examples - 1)

print("Label: ", sess.run(tf.argmax(mnist.test.labels[r:r + 1], 1)))

print("Prediction: ", sess.run(

    tf.argmax(hypothesis, 1), feed_dict={X: mnist.test.images[r:r + 1]}))


#plt.imshow(mnist.test.images[r:r + 1].

 #   reshape(28, 28), cmap='Greys', interpolation='nearest')

#plt.show()


'''

Epoch: 0001 cost = 5.888845987

Epoch: 0002 cost = 1.860620173

Epoch: 0003 cost = 1.159035648

Epoch: 0004 cost = 0.892340870

Epoch: 0005 cost = 0.751155428

Epoch: 0006 cost = 0.662484806

Epoch: 0007 cost = 0.601544010

Epoch: 0008 cost = 0.556526115

Epoch: 0009 cost = 0.521186961

Epoch: 0010 cost = 0.493068354

Epoch: 0011 cost = 0.469686249

Epoch: 0012 cost = 0.449967254

Epoch: 0013 cost = 0.433519321

Epoch: 0014 cost = 0.419000337

Epoch: 0015 cost = 0.406490815

Learning Finished!

Accuracy: 0.9035

'''


[# lab-10-2-mnist_nn]

# lab-10-2-mnist_nn

# 3 단 깊이 /relu 적용=> 정확도 94 %

import os

os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'

# Lab 10 MNIST and NN

import tensorflow as tf

import random

# import matplotlib.pyplot as plt


from tensorflow.examples.tutorials.mnist import input_data


tf.set_random_seed(777)  # reproducibility


mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)

# Check out https://www.tensorflow.org/get_started/mnist/beginners for

# more information about the mnist dataset


# parameters

learning_rate = 0.001

training_epochs = 15

batch_size = 100


# input place holders

X = tf.placeholder(tf.float32, [None, 784])

Y = tf.placeholder(tf.float32, [None, 10])


# weights & bias for nn layers

W1 = tf.Variable(tf.random_normal([784, 256]))

b1 = tf.Variable(tf.random_normal([256]))

L1 = tf.nn.relu(tf.matmul(X, W1) + b1)


W2 = tf.Variable(tf.random_normal([256, 256]))

b2 = tf.Variable(tf.random_normal([256]))

L2 = tf.nn.relu(tf.matmul(L1, W2) + b2)


W3 = tf.Variable(tf.random_normal([256, 10]))

b3 = tf.Variable(tf.random_normal([10]))

hypothesis = tf.matmul(L2, W3) + b3


# define cost/loss & optimizer

cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(

    logits=hypothesis, labels=Y))

optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)


# initialize

sess = tf.Session()

sess.run(tf.global_variables_initializer())


# train my model

for epoch in range(training_epochs):

    avg_cost = 0

    total_batch = int(mnist.train.num_examples / batch_size)


    for i in range(total_batch):

        batch_xs, batch_ys = mnist.train.next_batch(batch_size)

        feed_dict = {X: batch_xs, Y: batch_ys}

        c, _ = sess.run([cost, optimizer], feed_dict=feed_dict)

        avg_cost += c / total_batch


    print('Epoch:', '%04d' % (epoch + 1), 'cost =', '{:.9f}'.format(avg_cost))


print('Learning Finished!')


# Test model and check accuracy

correct_prediction = tf.equal(tf.argmax(hypothesis, 1), tf.argmax(Y, 1))

accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

print('Accuracy:', sess.run(accuracy, feed_dict={

      X: mnist.test.images, Y: mnist.test.labels}))


# Get one and predict

r = random.randint(0, mnist.test.num_examples - 1)

print("Label: ", sess.run(tf.argmax(mnist.test.labels[r:r + 1], 1)))

print("Prediction: ", sess.run(

    tf.argmax(hypothesis, 1), feed_dict={X: mnist.test.images[r:r + 1]}))


# plt.imshow(mnist.test.images[r:r + 1].

#           reshape(28, 28), cmap='Greys', interpolation='nearest')

# plt.show()


'''

Epoch: 0001 cost = 141.207671860

Epoch: 0002 cost = 38.788445864

Epoch: 0003 cost = 23.977515479

Epoch: 0004 cost = 16.315132428

Epoch: 0005 cost = 11.702554882

Epoch: 0006 cost = 8.573139748

Epoch: 0007 cost = 6.370995680

Epoch: 0008 cost = 4.537178684

Epoch: 0009 cost = 3.216900532

Epoch: 0010 cost = 2.329708954

Epoch: 0011 cost = 1.715552875

Epoch: 0012 cost = 1.189857912

Epoch: 0013 cost = 0.820965160

Epoch: 0014 cost = 0.624131458

Epoch: 0015 cost = 0.454633765

Learning Finished!

Accuracy: 0.9455


'''

[ lab-10-3-mnist_nn_xavier]

# lab-10-3-mnist_nn_xavier
# xavier 적용 ==> 정확도 97.6 %
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
# Lab 10 MNIST and Xavier
import tensorflow as tf
import random
# import matplotlib.pyplot as plt

from tensorflow.examples.tutorials.mnist import input_data

tf.set_random_seed(777)  # reproducibility

mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
# Check out https://www.tensorflow.org/get_started/mnist/beginners for
# more information about the mnist dataset

# parameters
learning_rate = 0.001
training_epochs = 15
batch_size = 100

# input place holders
X = tf.placeholder(tf.float32, [None, 784])
Y = tf.placeholder(tf.float32, [None, 10])

# weights & bias for nn layers
# http://stackoverflow.com/questions/33640581/how-to-do-xavier-initialization-on-tensorflow
W1 = tf.get_variable("W1", shape=[784, 256],
                     initializer=tf.contrib.layers.xavier_initializer())
b1 = tf.Variable(tf.random_normal([256]))
L1 = tf.nn.relu(tf.matmul(X, W1) + b1)

W2 = tf.get_variable("W2", shape=[256, 256],
                     initializer=tf.contrib.layers.xavier_initializer())
b2 = tf.Variable(tf.random_normal([256]))
L2 = tf.nn.relu(tf.matmul(L1, W2) + b2)

W3 = tf.get_variable("W3", shape=[256, 10],
                     initializer=tf.contrib.layers.xavier_initializer())
b3 = tf.Variable(tf.random_normal([10]))
hypothesis = tf.matmul(L2, W3) + b3

# define cost/loss & optimizer
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(
    logits=hypothesis, labels=Y))
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)

# initialize
sess = tf.Session()
sess.run(tf.global_variables_initializer())

# train my model
for epoch in range(training_epochs):
    avg_cost = 0
    total_batch = int(mnist.train.num_examples / batch_size)

    for i in range(total_batch):
        batch_xs, batch_ys = mnist.train.next_batch(batch_size)
        feed_dict = {X: batch_xs, Y: batch_ys}
        c, _ = sess.run([cost, optimizer], feed_dict=feed_dict)
        avg_cost += c / total_batch

    print('Epoch:', '%04d' % (epoch + 1), 'cost =', '{:.9f}'.format(avg_cost))

print('Learning Finished!')

# Test model and check accuracy
correct_prediction = tf.equal(tf.argmax(hypothesis, 1), tf.argmax(Y, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
print('Accuracy:', sess.run(accuracy, feed_dict={
      X: mnist.test.images, Y: mnist.test.labels}))

# Get one and predict
r = random.randint(0, mnist.test.num_examples - 1)
print("Label: ", sess.run(tf.argmax(mnist.test.labels[r:r + 1], 1)))
print("Prediction: ", sess.run(
    tf.argmax(hypothesis, 1), feed_dict={X: mnist.test.images[r:r + 1]}))

# plt.imshow(mnist.test.images[r:r + 1].
#           reshape(28, 28), cmap='Greys', interpolation='nearest')
# plt.show()

'''
Epoch: 0001 cost = 0.301498963
Epoch: 0002 cost = 0.107252513
Epoch: 0003 cost = 0.064888892
Epoch: 0004 cost = 0.044463030
Epoch: 0005 cost = 0.029951642
Epoch: 0006 cost = 0.020663404
Epoch: 0007 cost = 0.015853033
Epoch: 0008 cost = 0.011764387
Epoch: 0009 cost = 0.008598264
Epoch: 0010 cost = 0.007383116
Epoch: 0011 cost = 0.006839140
Epoch: 0012 cost = 0.004672963
Epoch: 0013 cost = 0.003979437
Epoch: 0014 cost = 0.002714260
Epoch: 0015 cost = 0.004707661
Learning Finished!
Accuracy: 0.9783
'''
[ lab-10-4-mnist_nn_deep ]

#lab-10-4-mnist_nn_deep

# 더 넓게(512), 더깊게 (L5) ==> 정확도 향상효과 없음

# 원인: OVERFITTING  대책 : Drop out 적용



import os

os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'

# Lab 10 MNIST and Deep learning

import tensorflow as tf

import random

# import matplotlib.pyplot as plt


from tensorflow.examples.tutorials.mnist import input_data


tf.set_random_seed(777)  # reproducibility


mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)

# Check out https://www.tensorflow.org/get_started/mnist/beginners for

# more information about the mnist dataset


# parameters

learning_rate = 0.001

training_epochs = 15

batch_size = 100


# input place holders

X = tf.placeholder(tf.float32, [None, 784])

Y = tf.placeholder(tf.float32, [None, 10])


# weights & bias for nn layers

# http://stackoverflow.com/questions/33640581/how-to-do-xavier-initialization-on-tensorflow

W1 = tf.get_variable("W1", shape=[784, 512],

                     initializer=tf.contrib.layers.xavier_initializer())

b1 = tf.Variable(tf.random_normal([512]))

L1 = tf.nn.relu(tf.matmul(X, W1) + b1)


W2 = tf.get_variable("W2", shape=[512, 512],

                     initializer=tf.contrib.layers.xavier_initializer())

b2 = tf.Variable(tf.random_normal([512]))

L2 = tf.nn.relu(tf.matmul(L1, W2) + b2)


W3 = tf.get_variable("W3", shape=[512, 512],

                     initializer=tf.contrib.layers.xavier_initializer())

b3 = tf.Variable(tf.random_normal([512]))

L3 = tf.nn.relu(tf.matmul(L2, W3) + b3)


W4 = tf.get_variable("W4", shape=[512, 512],

                     initializer=tf.contrib.layers.xavier_initializer())

b4 = tf.Variable(tf.random_normal([512]))

L4 = tf.nn.relu(tf.matmul(L3, W4) + b4)


W5 = tf.get_variable("W5", shape=[512, 10],

                     initializer=tf.contrib.layers.xavier_initializer())

b5 = tf.Variable(tf.random_normal([10]))

hypothesis = tf.matmul(L4, W5) + b5


# define cost/loss & optimizer

cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(

    logits=hypothesis, labels=Y))

optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)


# initialize

sess = tf.Session()

sess.run(tf.global_variables_initializer())


# train my model

for epoch in range(training_epochs):

    avg_cost = 0

    total_batch = int(mnist.train.num_examples / batch_size)


    for i in range(total_batch):

        batch_xs, batch_ys = mnist.train.next_batch(batch_size)

        feed_dict = {X: batch_xs, Y: batch_ys}

        c, _ = sess.run([cost, optimizer], feed_dict=feed_dict)

        avg_cost += c / total_batch


    print('Epoch:', '%04d' % (epoch + 1), 'cost =', '{:.9f}'.format(avg_cost))


print('Learning Finished!')


# Test model and check accuracy

correct_prediction = tf.equal(tf.argmax(hypothesis, 1), tf.argmax(Y, 1))

accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

print('Accuracy:', sess.run(accuracy, feed_dict={

      X: mnist.test.images, Y: mnist.test.labels}))


# Get one and predict

r = random.randint(0, mnist.test.num_examples - 1)

print("Label: ", sess.run(tf.argmax(mnist.test.labels[r:r + 1], 1)))

print("Prediction: ", sess.run(

    tf.argmax(hypothesis, 1), feed_dict={X: mnist.test.images[r:r + 1]}))


# plt.imshow(mnist.test.images[r:r + 1].

#           reshape(28, 28), cmap='Greys', interpolation='nearest')

# plt.show()


'''

Epoch: 0001 cost = 0.266061549

Epoch: 0002 cost = 0.080796588

Epoch: 0003 cost = 0.049075800

Epoch: 0004 cost = 0.034772298

Epoch: 0005 cost = 0.024780529

Epoch: 0006 cost = 0.017072763

Epoch: 0007 cost = 0.014031383

Epoch: 0008 cost = 0.013763446

Epoch: 0009 cost = 0.009164047

Epoch: 0010 cost = 0.008291388

Epoch: 0011 cost = 0.007319742

Epoch: 0012 cost = 0.006434021

Epoch: 0013 cost = 0.005684378

Epoch: 0014 cost = 0.004781207

Epoch: 0015 cost = 0.004342310

Learning Finished!

Accuracy: 0.9742

'''


[ lab-10-5-mnist_nn_dropout ]

#lab-10-5-mnist_nn_dropout
# drop out 적용 : 정확도 97.96%
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
# Lab 10 MNIST and Dropout
import tensorflow as tf
import random
# import matplotlib.pyplot as plt

from tensorflow.examples.tutorials.mnist import input_data

tf.set_random_seed(777)  # reproducibility

mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
# Check out https://www.tensorflow.org/get_started/mnist/beginners for
# more information about the mnist dataset

# parameters
learning_rate = 0.001
training_epochs = 15
batch_size = 100

# input place holders
X = tf.placeholder(tf.float32, [None, 784])
Y = tf.placeholder(tf.float32, [None, 10])

# dropout (keep_prob) rate  0.7 on training, but should be 1 for testing
keep_prob = tf.placeholder(tf.float32)

# weights & bias for nn layers
# http://stackoverflow.com/questions/33640581/how-to-do-xavier-initialization-on-tensorflow
W1 = tf.get_variable("W1", shape=[784, 512],
                     initializer=tf.contrib.layers.xavier_initializer())
b1 = tf.Variable(tf.random_normal([512]))
L1 = tf.nn.relu(tf.matmul(X, W1) + b1)
L1 = tf.nn.dropout(L1, keep_prob=keep_prob)

W2 = tf.get_variable("W2", shape=[512, 512],
                     initializer=tf.contrib.layers.xavier_initializer())
b2 = tf.Variable(tf.random_normal([512]))
L2 = tf.nn.relu(tf.matmul(L1, W2) + b2)
L2 = tf.nn.dropout(L2, keep_prob=keep_prob)

W3 = tf.get_variable("W3", shape=[512, 512],
                     initializer=tf.contrib.layers.xavier_initializer())
b3 = tf.Variable(tf.random_normal([512]))
L3 = tf.nn.relu(tf.matmul(L2, W3) + b3)
L3 = tf.nn.dropout(L3, keep_prob=keep_prob)

W4 = tf.get_variable("W4", shape=[512, 512],
                     initializer=tf.contrib.layers.xavier_initializer())
b4 = tf.Variable(tf.random_normal([512]))
L4 = tf.nn.relu(tf.matmul(L3, W4) + b4)
L4 = tf.nn.dropout(L4, keep_prob=keep_prob)

W5 = tf.get_variable("W5", shape=[512, 10],
                     initializer=tf.contrib.layers.xavier_initializer())
b5 = tf.Variable(tf.random_normal([10]))
hypothesis = tf.matmul(L4, W5) + b5

# define cost/loss & optimizer
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(
    logits=hypothesis, labels=Y))
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)

# initialize
sess = tf.Session()
sess.run(tf.global_variables_initializer())

# train my model
for epoch in range(training_epochs):
    avg_cost = 0
    total_batch = int(mnist.train.num_examples / batch_size)

    for i in range(total_batch):
        batch_xs, batch_ys = mnist.train.next_batch(batch_size)
        feed_dict = {X: batch_xs, Y: batch_ys, keep_prob: 0.7}
        c, _ = sess.run([cost, optimizer], feed_dict=feed_dict)
        avg_cost += c / total_batch

    print('Epoch:', '%04d' % (epoch + 1), 'cost =', '{:.9f}'.format(avg_cost))

print('Learning Finished!')

# Test model and check accuracy
correct_prediction = tf.equal(tf.argmax(hypothesis, 1), tf.argmax(Y, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
print('Accuracy:', sess.run(accuracy, feed_dict={
      X: mnist.test.images, Y: mnist.test.labels, keep_prob: 1}))

# Get one and predict
r = random.randint(0, mnist.test.num_examples - 1)
print("Label: ", sess.run(tf.argmax(mnist.test.labels[r:r + 1], 1)))
print("Prediction: ", sess.run(
    tf.argmax(hypothesis, 1), feed_dict={X: mnist.test.images[r:r + 1], keep_prob: 1}))

# plt.imshow(mnist.test.images[r:r + 1].
#           reshape(28, 28), cmap='Greys', interpolation='nearest')
# plt.show()

'''
Epoch: 0001 cost = 0.447322626
Epoch: 0002 cost = 0.157285590
Epoch: 0003 cost = 0.121884535
Epoch: 0004 cost = 0.098128681
Epoch: 0005 cost = 0.082901778
Epoch: 0006 cost = 0.075337573
Epoch: 0007 cost = 0.069752543
Epoch: 0008 cost = 0.060884363
Epoch: 0009 cost = 0.055276413
Epoch: 0010 cost = 0.054631256
Epoch: 0011 cost = 0.049675195
Epoch: 0012 cost = 0.049125314
Epoch: 0013 cost = 0.047231930
Epoch: 0014 cost = 0.041290121
Epoch: 0015 cost = 0.043621063
Learning Finished!
Accuracy: 0.9804
'''

[ 참고자료 ]

https://www.inflearn.com/course/기본적인-머신러닝-딥러닝-강좌

https://github.com/hunkim/deeplearningzerotoall



Comment +0

인공지능 구현에 대한 글입니다.


글의 순서는 아래와 같습니다.


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

1. # lab-09-1-xor 

    ==># 코딩은 이상이 없으나, 정확도는 50% 수준밖에는 않됩니다.

2. # lab-09-2-xor-nn

   ==>정확도 50%를 개선하는 방법으로 neural network ( 신경망 )을 이용하는 방법을 구현 ==> 정확도 100%

  ==>layer 1 이 다른곳에 입력으로 들어감 : 신경망 구성

    layer1은 출력 2개 로 구성한다 , 이유는 최종 가설의 입력(2개의 data)으로 들어가기 때문임

    정확도 1.0으로 개선이 됨

3. # lab-09-3-xor-nn-wide-deep

  ==> layer1의 출력을 10개  넓게 펴주고, 최종 가설에는 출력을 1개로 설정함 

       layer1,2,3로 더 깊게 만들고, 최종 출력을 1개로 설정함

  ==> 즉 더 넓게, 깊게 신경망을 구성해서  정확도 1.0으로 개선됨, 정밀도 높아짐 (큰것은 더 크게, 작은것은 더 작게)

4. 코딩탐구 필요(추가)

  ==> lab-09-4-xor_tensorboard

       lab-09-5-linear_back_prop

       lab-09-6-multi-linear_back_prop

       lab-09-7-sigmoid_back_prop

       lab-09-x-xor-nn-back_prop

5. 참고자료

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


[rate overfitting , regularization tips]


1. COST 값이 줄지않고, 늘어난다면  Learning rate을 더 작게 변경해주어야함

   반대로 너무 작게 줄거나, 도중에 멈춘다면 learing rate을 좀더 크게 해주어야함

   보통 0.01을 기준으로 늘리거나 줄이면서 조정해 나가면 된다.


2.  x data 값이 차이가 큰 경우, cost가 잘 줄지않거나, 학습이 잘 일어나지 않음 , 이럴때는 NOMALIZATION 을 해주어야 함.

3. OVERFITTING

  - 더많은 TRAINING DATA

  - REDUCE FEATURES

  - REGULARIZATION ==> 구별선이 구부러지지 않토록 , X값을 일반화 시킴


[lab-09-1-xor]

# lab-09-1-xor

# 코딩은 이상이 없으나, 정확도는 50% 수준밖에는 않됩니다.



import os

os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'

# Lab 9 XOR

import tensorflow as tf

import numpy as np


tf.set_random_seed(777)  # for reproducibility

learning_rate = 0.1


x_data = [[0, 0],

          [0, 1],

          [1, 0],

          [1, 1]]

y_data = [[0],

          [1],

          [1],

          [0]]

x_data = np.array(x_data, dtype=np.float32)

y_data = np.array(y_data, dtype=np.float32)


X = tf.placeholder(tf.float32, [None, 2])

Y = tf.placeholder(tf.float32, [None, 1])


W = tf.Variable(tf.random_normal([2, 1]), name='weight')

b = tf.Variable(tf.random_normal([1]), name='bias')


# Hypothesis using sigmoid: tf.div(1., 1. + tf.exp(tf.matmul(X, W)))

hypothesis = tf.sigmoid(tf.matmul(X, W) + b)


# cost/loss function

cost = -tf.reduce_mean(Y * tf.log(hypothesis) + (1 - Y) *

                       tf.log(1 - hypothesis))


train = tf.train.GradientDescentOptimizer(learning_rate=learning_rate).minimize(cost)


# Accuracy computation

# True if hypothesis>0.5 else False

predicted = tf.cast(hypothesis > 0.5, dtype=tf.float32)

accuracy = tf.reduce_mean(tf.cast(tf.equal(predicted, Y), dtype=tf.float32))


# Launch graph

with tf.Session() as sess:

    # Initialize TensorFlow variables

    sess.run(tf.global_variables_initializer())


    for step in range(10001):

        sess.run(train, feed_dict={X: x_data, Y: y_data})

        if step % 100 == 0:

            print(step, sess.run(cost, feed_dict={

                  X: x_data, Y: y_data}), sess.run(W))


    # Accuracy report

    h, c, a = sess.run([hypothesis, predicted, accuracy],

                       feed_dict={X: x_data, Y: y_data})

    print("\nHypothesis: ", h, "\nCorrect: ", c, "\nAccuracy: ", a)


'''

Hypothesis:  [[ 0.5]

 [ 0.5]

 [ 0.5]

 [ 0.5]]

Correct:  [[ 0.]

 [ 0.]

 [ 0.]

 [ 0.]]

Accuracy:  0.5


'''

[ lab-09-2-xor-nn ]

# lab-09-2-xor-nn

""" 정확도 50%를 개선하는 방법으로 neural network ( 신경망 )을 이용하는 방법을 구현하함"""

import os

os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'

# Lab 9 XOR

import tensorflow as tf

import numpy as np


tf.set_random_seed(777)  # for reproducibility

learning_rate = 0.1


x_data = [[0, 0],

          [0, 1],

          [1, 0],

          [1, 1]]

y_data = [[0],

          [1],

          [1],

          [0]]

x_data = np.array(x_data, dtype=np.float32)

y_data = np.array(y_data, dtype=np.float32)


X = tf.placeholder(tf.float32, [None, 2])

Y = tf.placeholder(tf.float32, [None, 1])


W1 = tf.Variable(tf.random_normal([2, 2]), name='weight1')

b1 = tf.Variable(tf.random_normal([2]), name='bias1')

layer1 = tf.sigmoid(tf.matmul(X, W1) + b1)


""" layer 1 이 다른곳에 입력으로 연결함 : 신경망 구성

    layer1은 입력1개, 출력 2로 구성한다 , 이유는 최종 가설의 입력으로 들어가기 때문임

    정확도 1.0으로 개선이 됨

"""

W2 = tf.Variable(tf.random_normal([2, 1]), name='weight2')

b2 = tf.Variable(tf.random_normal([1]), name='bias2')

hypothesis = tf.sigmoid(tf.matmul(layer1, W2) + b2)


# cost/loss function

cost = -tf.reduce_mean(Y * tf.log(hypothesis) + (1 - Y) *

                       tf.log(1 - hypothesis))


train = tf.train.GradientDescentOptimizer(learning_rate=learning_rate).minimize(cost)


# Accuracy computation

# True if hypothesis>0.5 else False

predicted = tf.cast(hypothesis > 0.5, dtype=tf.float32)

accuracy = tf.reduce_mean(tf.cast(tf.equal(predicted, Y), dtype=tf.float32))


# Launch graph

with tf.Session() as sess:

    # Initialize TensorFlow variables

    sess.run(tf.global_variables_initializer())


    for step in range(10001):

        sess.run(train, feed_dict={X: x_data, Y: y_data})

        if step % 100 == 0:

            print(step, sess.run(cost, feed_dict={

                  X: x_data, Y: y_data}), sess.run([W1, W2]))


    # Accuracy report

    h, c, a = sess.run([hypothesis, predicted, accuracy],

                       feed_dict={X: x_data, Y: y_data})

    print("\nHypothesis: ", h, "\nCorrect: ", c, "\nAccuracy: ", a)



'''

Hypothesis:  [[ 0.01338218]

 [ 0.98166394]

 [ 0.98809403]

 [ 0.01135799]]

Correct:  [[ 0.]

 [ 1.]

 [ 1.]

 [ 0.]]

Accuracy:  1.0

'''


[ lab-09-3-xor-nn-wide-deep ]


# lab-09-3-xor-nn-wide-deep


import os

os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'

# Lab 9 XOR

import tensorflow as tf

import numpy as np


tf.set_random_seed(777)  # for reproducibility

learning_rate = 0.1


x_data = [[0, 0],

          [0, 1],

          [1, 0],

          [1, 1]]

y_data = [[0],

          [1],

          [1],

          [0]]

x_data = np.array(x_data, dtype=np.float32)

y_data = np.array(y_data, dtype=np.float32)


X = tf.placeholder(tf.float32, [None, 2])

Y = tf.placeholder(tf.float32, [None, 1])


W1 = tf.Variable(tf.random_normal([2, 10]), name='weight1')

b1 = tf.Variable(tf.random_normal([10]), name='bias1')

layer1 = tf.sigmoid(tf.matmul(X, W1) + b1)

""" layer1의 출력을 10개  넓게 펴주고, 최종 가설에는 출력을 1개로 설정함 

    layer1,2,3로 더 깊게 만들고, 최종 출력을 1개로 설정함

    ==> 즉 더 넓게, 깊게 신경망을 구성해서  정확도 1.0으로 개선됨 """


W2 = tf.Variable(tf.random_normal([10, 10]), name='weight2')

b2 = tf.Variable(tf.random_normal([10]), name='bias2')

layer2 = tf.sigmoid(tf.matmul(layer1, W2) + b2)


W3 = tf.Variable(tf.random_normal([10, 10]), name='weight3')

b3 = tf.Variable(tf.random_normal([10]), name='bias3')

layer3 = tf.sigmoid(tf.matmul(layer2, W3) + b3)


W4 = tf.Variable(tf.random_normal([10, 1]), name='weight4')

b4 = tf.Variable(tf.random_normal([1]), name='bias4')

hypothesis = tf.sigmoid(tf.matmul(layer3, W4) + b4)


# cost/loss function

cost = -tf.reduce_mean(Y * tf.log(hypothesis) + (1 - Y) *

                       tf.log(1 - hypothesis))


train = tf.train.GradientDescentOptimizer(learning_rate=learning_rate).minimize(cost)


# Accuracy computation

# True if hypothesis>0.5 else False

predicted = tf.cast(hypothesis > 0.5, dtype=tf.float32)

accuracy = tf.reduce_mean(tf.cast(tf.equal(predicted, Y), dtype=tf.float32))


# Launch graph

with tf.Session() as sess:

    # Initialize TensorFlow variables

    sess.run(tf.global_variables_initializer())


    for step in range(10001):

        sess.run(train, feed_dict={X: x_data, Y: y_data})

        if step % 100 == 0:

            print(step, sess.run(cost, feed_dict={

                  X: x_data, Y: y_data}), sess.run([W1, W2]))


    # Accuracy report

    h, c, a = sess.run([hypothesis, predicted, accuracy],

                       feed_dict={X: x_data, Y: y_data})

    print("\nHypothesis: ", h, "\nCorrect: ", c, "\nAccuracy: ", a)



'''

Hypothesis:  [[  7.80511764e-04]

 [  9.99238133e-01]

 [  9.98379230e-01]

 [  1.55659032e-03]]

Correct:  [[ 0.]

 [ 1.]

 [ 1.]

 [ 0.]]

Accuracy:  1.0

'''


[참고자료 ]

https://www.inflearn.com/course/기본적인-머신러닝-딥러닝-강좌

https://github.com/hunkim/deeplearningzerotoall




Comment +0

인공지능 구현에 대한 글입니다.

글의 순서는 아래와 같습니다.


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

1. rate overfitting , regularization tips

2. train/test data sheet , learning rate , normalization(new)

 - 학습과 테스트 data를 구분하는것이 합리적임

 - mnist 소개

3. 참고자료

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


[rate overfitting , regularization tips]


1. COST 값이 줄지않고, 늘어난다면  Learning rate을 더 작게 변경해주어야함

   반대로 너무 작게 줄거나, 도중에 멈춘다면 learing rate을 좀더 크게 해주어야함

   보통 0.01을 기준으로 늘리거나 줄이면서 조정해 나가면 된다.


2.  x data 값이 차이가 큰 경우, cost가 잘 줄지않거나, 학습이 잘 일어나지 않음 , 이럴때는 NOMALIZATION 을 해주어야 함.

3. OVERFITTING

  - 더많은 TRAINING DATA

  - REDUCE FEATURES

  - REGULARIZATION ==> 구별선이 구부러지지 않토록 , X값을 일반화 시킴


[train/test data sheet , learning rate , normalization(new)]


# <lab-07-1-learning_rate_and_evaluation>


import os

os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'

# Lab 7 Learning rate and Evaluation

import tensorflow as tf

tf.set_random_seed(777)  # for reproducibility


x_data = [[1, 2, 1],

          [1, 3, 2],

          [1, 3, 4],

          [1, 5, 5],

          [1, 7, 5],

          [1, 2, 5],

          [1, 6, 6],

          [1, 7, 7]]

y_data = [[0, 0, 1],

          [0, 0, 1],

          [0, 0, 1],

          [0, 1, 0],

          [0, 1, 0],

          [0, 1, 0],

          [1, 0, 0],

          [1, 0, 0]]


# Evaluation our model using this test dataset


x_test = [[2, 1, 1],

          [3, 1, 2],

          [3, 3, 4]]

y_test = [[0, 0, 1],

          [0, 0, 1],

          [0, 0, 1]]


X = tf.placeholder("float", [None, 3])

Y = tf.placeholder("float", [None, 3])


W = tf.Variable(tf.random_normal([3, 3]))

b = tf.Variable(tf.random_normal([3]))


# tf.nn.softmax computes softmax activations

# softmax = exp(logits) / reduce_sum(exp(logits), dim)

hypothesis = tf.nn.softmax(tf.matmul(X, W) + b)


# Cross entropy cost/loss

cost = tf.reduce_mean(-tf.reduce_sum(Y * tf.log(hypothesis), axis=1))

# Try to change learning_rate to small numbers

optimizer = tf.train.GradientDescentOptimizer(

    learning_rate=0.1).minimize(cost)


# dhp Learning rate을 1.5로 움직이는 크기를 크게하면, H(y)값은 발산을 하게된다 : overfitting )


# Correct prediction Test model

prediction = tf.arg_max(hypothesis, 1)

is_correct = tf.equal(prediction, tf.arg_max(Y, 1))

accuracy = tf.reduce_mean(tf.cast(is_correct, tf.float32))


# Launch graph

with tf.Session() as sess:

    # Initialize TensorFlow variables

    sess.run(tf.global_variables_initializer())


    for step in range(201):

        cost_val, W_val, _ = sess.run(

            [cost, W, optimizer], feed_dict={X: x_data, Y: y_data})

        print(step, cost_val, W_val)


# 여기부터는 test data 로 test 진행함

# tf 입장에서는 처움보는 data (x)가 되는것이다.


    # predict

    print("Prediction:", sess.run(prediction, feed_dict={X: x_test}))

    # Calculate the accuracy

    print("Accuracy: ", sess.run(accuracy, feed_dict={X: x_test, Y: y_test}))


'''

when lr = 1.5

0 5.73203 [[-0.30548954  1.22985029 -0.66033536]

 [-4.39069986  2.29670858  2.99386835]

 [-3.34510708  2.09743214 -0.80419564]]

1 23.1494 [[ 0.06951046  0.29449689 -0.0999819 ]

 [-1.95319986 -1.63627958  4.48935604]

 [-0.90760708 -1.65020132  0.50593793]]

2 27.2798 [[ 0.44451016  0.85699677 -1.03748143]

 [ 0.48429942  0.98872018 -0.57314301]

 [ 1.52989244  1.16229868 -4.74406147]]

3 8.668 [[ 0.12396193  0.61504567 -0.47498202]

 [ 0.22003263 -0.2470119   0.9268558 ]

 [ 0.96035379  0.41933775 -3.43156195]]

4 5.77111 [[-0.9524312   1.13037777  0.08607888]

 [-3.78651619  2.26245379  2.42393875]

 [-3.07170963  3.14037919 -2.12054014]]

5 inf [[ nan  nan  nan]

 [ nan  nan  nan]

 [ nan  nan  nan]]

6 nan [[ nan  nan  nan]

 [ nan  nan  nan]

 [ nan  nan  nan]]


 ...

Prediction: [0 0 0]

Accuracy:  0.0


-------------------------------------------------

When lr = 1e-10

0 5.73203 [[ 0.80269563  0.67861295 -1.21728313]

 [-0.3051686  -0.3032113   1.50825703]

 [ 0.75722361 -0.7008909  -2.10820389]]

1 5.73203 [[ 0.80269563  0.67861295 -1.21728313]

 [-0.3051686  -0.3032113   1.50825703]

 [ 0.75722361 -0.7008909  -2.10820389]]

2 5.73203 [[ 0.80269563  0.67861295 -1.21728313]

 [-0.3051686  -0.3032113   1.50825703]

 [ 0.75722361 -0.7008909  -2.10820389]]

...


198 5.73203 [[ 0.80269563  0.67861295 -1.21728313]

 [-0.3051686  -0.3032113   1.50825703]

 [ 0.75722361 -0.7008909  -2.10820389]]

199 5.73203 [[ 0.80269563  0.67861295 -1.21728313]

 [-0.3051686  -0.3032113   1.50825703]

 [ 0.75722361 -0.7008909  -2.10820389]]

200 5.73203 [[ 0.80269563  0.67861295 -1.21728313]

 [-0.3051686  -0.3032113   1.50825703]

 [ 0.75722361 -0.7008909  -2.10820389]]

Prediction: [0 0 0]

Accuracy:  0.0

-------------------------------------------------

When lr = 0.1


0 5.73203 [[ 0.72881663  0.71536207 -1.18015325]

 [-0.57753736 -0.12988332  1.60729778]

 [ 0.48373488 -0.51433605 -2.02127004]]

1 3.318 [[ 0.66219079  0.74796319 -1.14612854]

 [-0.81948912  0.03000021  1.68936598]

 [ 0.23214608 -0.33772916 -1.94628811]]

2 2.0218 [[ 0.64342022  0.74127686 -1.12067163]

 [-0.81161296 -0.00900121  1.72049117]

 [ 0.2086665  -0.35079569 -1.909742  ]]


...


199 0.672261 [[-1.15377033  0.28146935  1.13632679]

 [ 0.37484586  0.18958236  0.33544877]

 [-0.35609841 -0.43973011 -1.25604188]]

200 0.670909 [[-1.15885413  0.28058422  1.14229572]

 [ 0.37609792  0.19073224  0.33304682]

 [-0.35536593 -0.44033223 -1.2561723 ]]

Prediction: [2 2 2]

Accuracy:  1.0

'''


# < lab-07-2-linear_regression_without_min_max >


# dhp data 편차가 터무니없이 크게되면, 학습이 잘 되질 않됨


import os

os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'

import tensorflow as tf

import numpy as np

tf.set_random_seed(777)  # for reproducibility



xy = np.array([[828.659973, 833.450012, 908100, 828.349976, 831.659973],

               [823.02002, 828.070007, 1828100, 821.655029, 828.070007],

               [819.929993, 824.400024, 1438100, 818.97998, 824.159973],

               [816, 820.958984, 1008100, 815.48999, 819.23999],

               [819.359985, 823, 1188100, 818.469971, 818.97998],

               [819, 823, 1198100, 816, 820.450012],

               [811.700012, 815.25, 1098100, 809.780029, 813.669983],

               [809.51001, 816.659973, 1398100, 804.539978, 809.559998]])


x_data = xy[:, 0:-1]

y_data = xy[:, [-1]]


# placeholders for a tensor that will be always fed.

X = tf.placeholder(tf.float32, shape=[None, 4])

Y = tf.placeholder(tf.float32, shape=[None, 1])


W = tf.Variable(tf.random_normal([4, 1]), name='weight')

b = tf.Variable(tf.random_normal([1]), name='bias')


# Hypothesis

hypothesis = tf.matmul(X, W) + b


# Simplified cost/loss function

cost = tf.reduce_mean(tf.square(hypothesis - Y))


# Minimize

optimizer = tf.train.GradientDescentOptimizer(learning_rate=1e-5)

train = optimizer.minimize(cost)


# Launch the graph in a session.

sess = tf.Session()

# Initializes global variables in the graph.

sess.run(tf.global_variables_initializer())


for step in range(101):

    cost_val, hy_val, _ = sess.run(

        [cost, hypothesis, train], feed_dict={X: x_data, Y: y_data})

    print(step, "Cost: ", cost_val, "\nPrediction:\n", hy_val)



'''

0 Cost:  2.45533e+12

Prediction:

 [[-1104436.375]

 [-2224342.75 ]

 [-1749606.75 ]

 [-1226179.375]

 [-1445287.125]

 [-1457459.5  ]

 [-1335740.5  ]

 [-1700924.625]]

1 Cost:  2.69762e+27

Prediction:

 [[  3.66371490e+13]

 [  7.37543360e+13]

 [  5.80198785e+13]

 [  4.06716290e+13]

 [  4.79336847e+13]

 [  4.83371348e+13]

 [  4.43026590e+13]

 [  5.64060907e+13]]

2 Cost:  inf

Prediction:

 [[ -1.21438790e+21]

 [ -2.44468702e+21]

 [ -1.92314724e+21]

 [ -1.34811610e+21]

 [ -1.58882674e+21]

 [ -1.60219962e+21]

 [ -1.46847142e+21]

 [ -1.86965602e+21]]

3 Cost:  inf

Prediction:

 [[  4.02525216e+28]

 [  8.10324465e+28]

 [  6.37453079e+28]

 [  4.46851237e+28]

 [  5.26638074e+28]

 [  5.31070676e+28]

 [  4.86744608e+28]

 [  6.19722623e+28]]

4 Cost:  inf

Prediction:

 [[ -1.33422428e+36]

 [ -2.68593010e+36]

 [ -2.11292430e+36]

 [ -1.48114879e+36]

 [ -1.74561303e+36]

 [ -1.76030542e+36]

 [ -1.61338091e+36]

 [ -2.05415459e+36]]

5 Cost:  inf

Prediction:

 [[ inf]

 [ inf]

 [ inf]

 [ inf]

 [ inf]

 [ inf]

 [ inf]

 [ inf]]

6 Cost:  nan

Prediction:

 [[ nan]

 [ nan]

 [ nan]

 [ nan]

 [ nan]

 [ nan]

 [ nan]

 [ nan]]

'''



# <lab-07-3-linear_regression_min_max >


# dhp minmaxscala 함수를 이용해서, 정규화 (0~1사이의수)로 변환을 시킨다(nomallize)

import os

os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'

import tensorflow as tf

import numpy as np

tf.set_random_seed(777)  # for reproducibility



def MinMaxScaler(data):

    numerator = data - np.min(data, 0)

    denominator = np.max(data, 0) - np.min(data, 0)

    # noise term prevents the zero division

    return numerator / (denominator + 1e-7)



xy = np.array([[828.659973, 833.450012, 908100, 828.349976, 831.659973],

               [823.02002, 828.070007, 1828100, 821.655029, 828.070007],

               [819.929993, 824.400024, 1438100, 818.97998, 824.159973],

               [816, 820.958984, 1008100, 815.48999, 819.23999],

               [819.359985, 823, 1188100, 818.469971, 818.97998],

               [819, 823, 1198100, 816, 820.450012],

               [811.700012, 815.25, 1098100, 809.780029, 813.669983],

               [809.51001, 816.659973, 1398100, 804.539978, 809.559998]])


# very important. It does not work without it.

xy = MinMaxScaler(xy)

print(xy)


x_data = xy[:, 0:-1]

y_data = xy[:, [-1]]


# placeholders for a tensor that will be always fed.

X = tf.placeholder(tf.float32, shape=[None, 4])

Y = tf.placeholder(tf.float32, shape=[None, 1])


W = tf.Variable(tf.random_normal([4, 1]), name='weight')

b = tf.Variable(tf.random_normal([1]), name='bias')


# Hypothesis

hypothesis = tf.matmul(X, W) + b


# Simplified cost/loss function

cost = tf.reduce_mean(tf.square(hypothesis - Y))


# Minimize

optimizer = tf.train.GradientDescentOptimizer(learning_rate=1e-5)

train = optimizer.minimize(cost)


# Launch the graph in a session.

sess = tf.Session()

# Initializes global variables in the graph.

sess.run(tf.global_variables_initializer())


for step in range(101):

    cost_val, hy_val, _ = sess.run(

        [cost, hypothesis, train], feed_dict={X: x_data, Y: y_data})

    print(step, "Cost: ", cost_val, "\nPrediction:\n", hy_val)


'''

100 Cost:  0.152254

Prediction:

 [[ 1.63450289]

 [ 0.06628087]

 [ 0.35014752]

 [ 0.67070574]



# <lab-07-4-mnist_introduction>


import os

os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'

# Lab 7 Learning rate and Evaluation

import tensorflow as tf

import random

# import matplotlib.pyplot as plt

tf.set_random_seed(777)  # for reproducibility


from tensorflow.examples.tutorials.mnist import input_data

# Check out https://www.tensorflow.org/get_started/mnist/beginners for

# more information about the mnist dataset

mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)


nb_classes = 10


# MNIST data image of shape 28 * 28 = 784

X = tf.placeholder(tf.float32, [None, 784])

# 0 - 9 digits recognition = 10 classes

Y = tf.placeholder(tf.float32, [None, nb_classes])


W = tf.Variable(tf.random_normal([784, nb_classes]))

b = tf.Variable(tf.random_normal([nb_classes]))


# Hypothesis (using softmax)

hypothesis = tf.nn.softmax(tf.matmul(X, W) + b)


cost = tf.reduce_mean(-tf.reduce_sum(Y * tf.log(hypothesis), axis=1))

optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.1).minimize(cost)


# Test model

is_correct = tf.equal(tf.arg_max(hypothesis, 1), tf.arg_max(Y, 1))

# Calculate accuracy

accuracy = tf.reduce_mean(tf.cast(is_correct, tf.float32))


# parameters

training_epochs = 15

batch_size = 100


with tf.Session() as sess:

    # Initialize TensorFlow variables

    sess.run(tf.global_variables_initializer())

    # Training cycle

    for epoch in range(training_epochs):

        avg_cost = 0

        total_batch = int(mnist.train.num_examples / batch_size)


        for i in range(total_batch):

            batch_xs, batch_ys = mnist.train.next_batch(batch_size)

            c, _ = sess.run([cost, optimizer], feed_dict={

                            X: batch_xs, Y: batch_ys})

            avg_cost += c / total_batch


        print('Epoch:', '%04d' % (epoch + 1),

              'cost =', '{:.9f}'.format(avg_cost))


    print("Learning finished")


    # Test the model using test sets

    print("Accuracy: ", accuracy.eval(session=sess, feed_dict={

          X: mnist.test.images, Y: mnist.test.labels}))


    # Get one and predict

    r = random.randint(0, mnist.test.num_examples - 1)

    print("Label: ", sess.run(tf.argmax(mnist.test.labels[r:r + 1], 1)))

    print("Prediction: ", sess.run(

        tf.argmax(hypothesis, 1), feed_dict={X: mnist.test.images[r:r + 1]}))


    # 그래프 인쇄가 않됨, 향후 원인 파악필요함, 일단은 #로 주석처리 함 

    # don't know why this makes Travis Build error.

    # plt.imshow(

    #     mnist.test.images[r:r + 1].reshape(28, 28),

    #     cmap='Greys',

    #     interpolation='nearest')

    # plt.show()



'''

Epoch: 0001 cost = 2.868104637

Epoch: 0002 cost = 1.134684615

Epoch: 0003 cost = 0.908220728

Epoch: 0004 cost = 0.794199896

Epoch: 0005 cost = 0.721815854

Epoch: 0006 cost = 0.670184430

Epoch: 0007 cost = 0.630576546

Epoch: 0008 cost = 0.598888191

Epoch: 0009 cost = 0.573027079

Epoch: 0010 cost = 0.550497213



[참고자료 ]

https://www.inflearn.com/course/기본적인-머신러닝-딥러닝-강좌

https://github.com/hunkim/deeplearningzerotoall

Comment +0