블로그 이미지
윤영식
Full Stacker, Application Architecter, KnowHow Dispenser and Bike Rider

Publication

Category

Recent Post

2018. 10. 17. 15:11 Data Visualization/Vega

Vega는 선언적인 차트 생성 라이브러리이다. 좀 더 추상화하고 쉽게 사용할 수 있도록 Vega-lite를 제공한다. C3.js 또는 네이버의 C3.js 확장판인 Billboard.js를 생각하면 된다.




특징


- SVG, Canvas를 지원한다. 

- Javascript코드없이 JSON환경으로 차트를 생성한다. 

- Editor, Recommendation Charting해주는 Voyager등을 제공한다.


테스트: https://vega.github.io/editor/#/ 접속해서 다음 환경값을 넣는다. 


{
"$schema": "https://vega.github.io/schema/vega-lite/v2.json",
"data": {"url": "data/seattle-weather.csv", "format": {"type": "csv"}},
"mark": "bar",
"encoding": {
"x": {"timeUnit": "month", "field": "date", "type": "ordinal"},
"y": {"aggregate": "mean", "field": "precipitation", "type": "quantitative"}
},
"config": {"axisY": {"minExtent": 30}}
}





개념 


Vega-lite 소개 동영상을 보고 전반적으로 이해하자. 


좀 더 자세한 개념설명





사용예


Elastic Search에서는 Customizing Chart를 만들기 위해 Vega를 기능으로 추가하여 사용할 수도 있다.


Elastic Search에서 Vega 구현 예제 소스

Vega, Vega-lite, 또는 Vega Editor, Vega Voyager등은 Standalone library 형태로 애플리케이션에 포함하여 사용할 수 있다.




posted by 윤영식
2018. 9. 14. 17:31 React

실제 프로젝트에서 사용을 안하다보니 자꾸 처음 내용이 익숙해 지지 않은 상태에서 잊어버리고 만다. React + Typescript기반을 다시 시작해 본다. 



Install React with Typescript

React와 Typescript 환경을 만든다

// 2018.9 현재 LTS NodeJS 버전

$ nvm use 8.12.0

// typescript v3.0.3 으로 업데이트

$ npm i -g typescript 

$ npm i -g create-react-app (or yarn global add create-react-app)


// TYPESCRIPT-CSS 

$ create-react-app my-app --scrips-version=react-scripts-ts


package.json에 eject기능이 있어서 webpack config를 밖으로 추출하여 직접 핸들링할 수 있게 한다. 

$ cd my-app

$ npm run eject



SCSS 환경구축

css 환경을 scss 환경으로 바꿔준다. 

// scss loader 설치

$ yarn add node-sass sass-loader --dev


App.css와 index.css의 확장자를 .scss로 바꾸고, App.tsx, index.tsx의 import 문 확장자를 .scss로 바꾼다. 

import './App.scss';


config/webpack.config.dev.js 와 webpack.config.prod.js 파일안에 scss설정을 추가한다. 빨간 부분을 

      {

            test: /\.(css|scss)$/,

            use: [

              require.resolve('style-loader'),

              {

                loader: require.resolve('css-loader'),

                options: {

                  importLoaders: 1,

                },

              },

              {

                loader: require.resolve('postcss-loader'),

                options: {

                  // Necessary for external CSS imports to work

                  // https://github.com/facebookincubator/create-react-app/issues/2677

                  ident: 'postcss',

                  plugins: () => [

                    require('postcss-flexbugs-fixes'),

                    autoprefixer({

                      browsers: [

                        '>1%',

                        'last 4 versions',

                        'Firefox ESR',

                        'not ie < 9', // React doesn't support IE8 anyway

                      ],

                      flexbox: 'no-2009',

                    }),

                  ],

                },

              },

              {

                loader: require.resolve("sass-loader"),                

                options: { } 

              }

            ],

      },


기존 App.scss내용을 다음과 같이 바꾸어 확인해 본다. 

.App {

  text-align: center;

  &-logo {

    animation: App-logo-spin infinite 20s linear;

    height: 80px;

  }


  &-header {

    background-color: rgb(197, 40, 40);

    height: 150px;

    padding: 20px;

    color: white;

  }


  &-title {

    font-size: 1.5em;

  }


  &-intro {

    font-size: large;

  }

}

yarn start하여 점검!



Component LifeCycle


일반 컴포넌트

  constructor -> componentWillMount -> render -> componentDidMount -> componentWillUnmount


props, state 사용 컴포넌트

  componentWillReceiveProps -> shouldComponentUpdate -> componentWillUpdate -> render -> componentDidUpdate

  - shouldComponentUpdate: true, false로 다음으로 이벤트를 넘길수도 안할수도 있음




Component Type

PureComponent

  shouldComponentUpdate에서 Shallow compare하므로 reference를 바꾸어 주어야 render가 호출됨. 


Functional Component (Stateless Function Component)

  const myComp: Reat.SFC<Props> = (props) => {...}

import * as React from“ react”

interface WelcomeProps {

  name: string,

}


const Welcome: React.SFC < WelcomeProps > = (props) => {

  return <h1 > Hello, {

    props.name

  } < /h1>;

}



Router for SPA

react-router v4

   BrowserRouter, Route, Link, NavLink, Redirect 사용

   BrowserRouter는 window.history.pushState() 로 동자하는 라우터

   RouteComponentProps: route되면서 url의 파라미터를 props로 받음. history, match 속성을 가짐.

<BrowserRouter>

   <Route exact={true} path="/" component={} or render={} or children={} >

      <Link to="/a" />

      <NavLink activeStyle={{ color: red }} to="/b" /> 

   </Route>

   <Redirect from="/a" to="/b" />

</BrowserRouter>


Switch로 감쌈

  <Switch>

    <Route ... />

    <Route ... /> 

    <Route ... />

 </Switch>



Redux

react-redux

  여러  action을 reducer를 통해 하나의 store를 만든다.

     - action 타입을 만들고 action 객체를 만드는 펑션을 (action creator) 정의한다.

     - action을 처리하는 reducer 펑션을 정의한다. (Pure function, immutable)

        reducer 펑션을 action별로 나눈 다음 사용시에는 combineReducers 로 합쳐서 사용한다. 

     - reducer를 createStore에 넣어주면 single Store가 만들어진다. 

        store에는 getState(), dispatch(액션), subscribe(리스너),  replaceReducer(다른리듀서) 4개 메소드가 있음

   redux를 react에 연결하기

      - componentDidMount: subscribe

      - componentWillUnMount: unsubscribe

      - Provider는 context에 store를 담아 모든 component에서 store를 사용할 수 있도록 한다. 

         Provider를 제공하는 react-redux를 사용한다.  

   connect를 통해 컴포넌트에 연결한다. 

      - App에 대한 High order component이다.

      - 전체 state의 모양과 action creator를 파라미터로 넣어준다.

$ yarn add redux react-redux @types/redux @types/react-redux


const mapStateToProps = (state: { age: number}) => {

  return {

     age: state.age

  }

}


const mapDispatchToProps = (dispatch: Function) => {

  return {

      onAddAge: () => { dispatch(addAge()); }

  };

}


interface AppProps {

  age: number;

  onAddAge: void;

}


connect(mapStateToProps, mapDispatchToProps)(App);


class App<{props: AppProps}> extends Component {

    return <div>{this.props.age}</div>

}


  dispatch안에서 async처리를 할 수 있다.   

  applyMiddleware는 dispatch 전후에 처리할 수 있다. 

  action이 async일 때 미들웨어인 redux-thunk를 이용한다.



<참조>

- 2017 Typescript Korea 강의 (유튜브)

- scss 적용하기

- scss loader 설정하기, Class 사용 in Velopert

- Typescript + React 환경 만들기 in Velopert

- React LifeCycle 설명

- Component Type - SFC 설명

- React Router in Velopert 소개





'React' 카테고리의 다른 글

[React] Semantic-UI를 React 컴포넌트로 만들기 - 1  (0) 2015.08.23
[React] CSS Framework 선정하기  (0) 2015.08.15
[Flux] Flux 배우는 방법  (0) 2015.07.04
[React] AngularJS와 ReactJS 비교  (0) 2015.07.01
[React] 배우는 방법  (0) 2015.05.15
posted by 윤영식
2018. 8. 22. 14:40 AI Deep Learning/Tensorflow

이찬우님의 텐서플로우 유튜브강좌를 정리한다. 




강좌 3


로지스틱 비용함수를 만들기

  - 좌측과 우측에 대한 convex 를 만들기 위한 식

cost = tf.reduce_sum(-y*tf.log(output)-(1-y)*tf.log(1-output), reduction_indices=1)


prediction과 label끼리의 정확도를 판단하기 

  - 각자의 벡터 매칭이 맞으면 1, 틀리면 0으로 하는 값을 다시 n*1 벡터로 만든다. 

  - 해당 백터의 값에 대한 평균을 구한다. 

  - 이때 1, 0값은 bool이어서 float32로 변환하여 계산한다.

  - 잘 맞으면 평균 1 이 나온다. 

comp_pred = tf.equal(tf.argmax(output, 1), tf.argmax(y, 1))

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


모델 저장하기

  - Training시킨 모델을 저장하는 것을 Checkpoint라 한다.

  - 저장내용에는 Weight과 Model이 저장될 수 있다. 

  - Weight관련 Variable을 저장한후 Save한다.

  - 저장시 유의점은 Variable, placeholder 함수의 파라미터중 하나인 Name이 자동으로 지정된다. 

W_o = tf.Variable(tf.truncated_normal(shape=[HIDDEN2_SIZE, CLASSES], dtype=tf.float32))

b_o = tf.Variable( tf.zeros([CLASSES]), dtype=tf.float32)


param_list = [W_h1, b_h1, W_h2, b_h2, W_o, b_o]

saver = tf.train.Saver(param_list)


hidden1 = tf.sigmoid(tf.matmul(x, W_h1) + b_h1)

hidden2 = tf.sigmoid(tf.matmul(hidden1, W_h2) + b_h2)


....

for i in range(1000):

    _, loss = sess.run([train, cost, accuracy], feed_dict = feed_dict)

    if i % 100 == 0:

        saver.save(sess, './tensorflow_3_lec.ckpt')

        ...




강좌 4


저장된 Weight Restoring하기 

  - save 할 때 Widget의 Variable에 name을 지정한다. 

  - 

x = tf.placeholder(tf.float32, shape=[None, INPUT_SIZE], name='x')

y = tf.placeholder(tf.float32, shape=[None, CLASSES], name='y')

W_h1 = tf.Variable(tf.truncated_normal(shape=[INPUT_SIZE, HIDDEN1_SIZE], dtype=tf.float32), name='W_h1')

b_h1 = tf.Variable(tf.zeros([HIDDEN1_SIZE]), dtype=tf.float32, name='b_h1')

hidden1 = tf.sigmoid(tf.matmul(x, W_h1) + b_h1, name='hidden1')

hidden2 = tf.sigmoid(tf.matmul(hidden1, W_h2) + b_h2, name='hidden2')

output = tf.sigmoid(tf.matmul(hidden2, W_o) + b_o, name='output')

...

saver.restore(sess, './tensorflow_3.ckpt')




강좌 5


Tensorboard는 디버깅 용도이다. 공식 튜토리얼을 참조한다.

  - name_scope는 묶음 단위이다.

  - scalar: 로그 데이터 남기기

  - tf.summary.merge_all()

  - tf.summary.FileWriter(<dir>, session.graph) 

# 가설함수 

with tf.name_scope('hidden_layer_1') as h1scope:

    hidden1 = tf.sigmoid(tf.matmul(x, W_h1) + b_h1, name='hidden1')


with tf.name_scope('hidden_layer_2') as h2scope:

    hidden2 = tf.sigmoid(tf.matmul(hidden1, W_h2) + b_h2, name='hidden2')

    

with tf.name_scope('output_layer') as oscope:

    output = tf.sigmoid(tf.matmul(hidden2, W_o) + b_o, name='output')


....


# 수행 

sess= tf.Session()

sess.run(tf.global_variables_initializer())


merge = tf.summary.merge_all()


for i in range(1000):

    _, loss, acc = sess.run([train, cost, accuracy], feed_dict = feed_dict)

    if i % 100 == 0:

        train_writer = tf.summary.FileWriter('./summaries/', sess.graph)


$ tensorboard --logdir=./summaries 수행한다. 




강좌 6


Loading Data in Tensorflow 참조. CSV파일 읽기

  - decode_csv로 콤마기반의 csv파일을 읽어들인다.

  - record_defaults = [[1], [1], [1], [1], [1], [1], [1], [1], [1]]  Fixed 자리수에서 비어있는 값에 대한 default value이다. 

  - start_queue_runners는 Session.run이 수행되기전에 호출해서 queue를 실행해서 file이 queue에서 대기토록 한다.

  - Coordinator 는 Thread 관리를 수행한다.

!

Image 읽기

  - FixedLengthRecordReader로 읽음

  - decode_raw를 사용함




to be continue...




<참조>

  - 텐서플로우의 체크포인트 설명

  - 텐서플로우 Save & Restore

  - 파이썬의 With 구문 이해 





posted by 윤영식
2018. 8. 21. 16:52 AI Deep Learning/Read Paper

Data2Vis 논문에 대한 개념을 알아본 후 다른 곳에 응용을 하려면 어떻게 어떤 단계를 거쳐서 진행해야 할지 실험을 해본다. 




준비


컴파일 환경

Python v3.7

Tensorflow v1.9

Anaconda기반에서 구동한다.




Step-1) 모델 환경설정


train_options.json 에 정의된 Model의 파라미터 내용

  - Data2Vis는 Attention 메카니즘을 가지는 Encoder-Decoder 아키텍쳐이다. 

  - 2-layer bidirectional RNN encoder/decoder를 사용한다.

  - GRU보다 LSTM이 보다 좋은 성능을 나타내서 LSTM을 사용한다. 


Loss(Cost)와 Training 함수를 포함한 모델(Model)은 AttentionSeq2Seq를 사용하고, 해당 모델에 대한 환경설정 파일은 example_configs/nmt_large.yml에 정의되어 있다. 

  - 데이터: source와 target 정보의 위치를 지정한다. 

  - 가설/비용 함수: Encoder/Decoder 를 구성하고 inference 파라미터등도 설정한다. Encoder/Decoder의 Cell은 LSTMCell을 사용한다.

  - Training 함수: Adam optimizer 사용



Step-2) Data 전처리


모델을 Training시키기 위해서 Dataset의 Field를 numeric, string, temporal, ordinal, categorical등으로 분류를 해놓는다. 이에 대한 Output(Labeled)으로 Vega-lite문법에 맞추어 환경파일을 각각 만든다. 

  - sourcedata/*.sources 또는 *.targets 파일중에 dev.sources와 dev.targets를 보면 dataset의 index당 vega-lite spec을 매칭했다.

  - vega-lite문법에서 data 필드만 제외한다.

  - 총 3가 성격의 sources, targets를 준비한다.

    + dev

    + train

    + vocab

  - dataset의 필드를 특별히 str<index>, num<index> 로 변환한다.

  - 데이터 전처리를 위한 스크립트는 utils/*.py에 있다.

  - 데이터 전처리 전의 실데이터는 testdata/*.json에 vega-lite의 다양한 spec은 examples/*.json 에 있다.

//dev.sources 

[{"num0": 0, "num1": null, "str0": "Small", "str1": "AMERICAN AIRLINES", "str2": "AUSTIN-BERGSTROM INTL", "str3": "Approach", "str4": "Day", "str5": "None", "str6": "Unknown bird - small", "num2": 0, "str7": "MD-80", "str8": "8/1/95 0:00", "str9": "Texas", "num3": 0}]

[{"num0": 0, "num1": 140, "str0": "Small", "str1": "US AIRWAYS*", "str2": "CHARLOTTE/DOUGLAS INTL ARPT", "str3": "Approach", "str4": "Day", "str5": "None", "str6": "European starling", "num2": 0, "str7": "B-737-300", "str8": "7/19/99 0:00", "str9": "North Carolina", "num3": 0}]


//dev.targets
{"encoding": {"y": {"field": "str0", "type": "nominal", "selected": true, "primitiveType": "string"}, "x": {"type": "quantitative", "field": "num2"}}, "mark": "point"}
{"encoding": {"y": {"field": "str3", "type": "nominal", "selected": true, "primitiveType": "string"}, "x": {"type": "quantitative", "field": "num0"}}, "mark": "tick"}




Step-3) 모델 생성하기


모델 환경설정과 Training을 위한 source, target 데이터가 준비되었다면 모델을 생성한다. 

  - procject-directory 위치를 변경한다.

  - bin/train.py를 수행을 위한 파라미터이다.

  - vizmodel로 ckpt파일을 생성되므로 별도 지정을 해보자. (data2vis에 이미 생성된 ckpt가 존재한다.)



Step-4) 추론 검증


Data2Vis는 Model을 미리 ckpt로 저장해 놓았고, WebDemo가 존재한다. webserver.py 는 Flask로 구성하여 간단하게 다음의 작업을 수행한다.

  - 웹화면에서 Generate Example 버튼을 클릭하면 examplesdata/*.json에서 실제 dataset 을 random하게 읽어온다.

  - 실데이터의 field를 str, num으로 바꾸어 inference에 넣은후 Vega-lite spec를 output로 받는다.

  - 출력으로 나온 Vega-lite spec에 data 필드에 실데이터를 맵핑하여 최종 Vega-lite spec를 만들어 HTTP response를 한다. 

또는 command console에서 직접 수행해 볼 수 있다. 



<참조>


- 구글 
   tf-seq2seq 튜토리얼
   seq2seq NMT 튜토리얼

- Data2Vis 논문

posted by 윤영식
2018. 8. 16. 15:06 AI Deep Learning/Read Paper

Data2Vis는 seq2seq를 통해 입력되는 데이터를 기반으로 출력으로 차트를 자동생성한다.




개념


해당 논문을 이해하기 위해 다음과 같은 단어의 개념을 이해해야 한다. 소스에서도 같은 용어를 쓰기 때문에 소스이해를 위해서도 중요하다. 


 - Data2Vis의 데모 사이트에 가면 간략한 설명이 나와 있다.


    

  

  - Attention mechanism을 이용한 encoder-decoder 아키텍쳐 모델이다.  

  - key/value 쌍의 데이터를 입력으로 하고 Vega-Lite기반의 출력을 생성한다. Vega-Lite는 JSON기반으로 차트를 생성해주는 스펙이다. 

  - 특징

     + encoder는 최종 context vector 하나로 만든다. 이것을 C 라고 표현한다. 위이 그림에서 가운데 위치한 C이다. 

     + decoder는 학습할 때 encoder의 "C"와 "<go>답안"을 입력받아 "답안<eos>"를 출력하는 학습을 한다. (참조)

     + encoder, decoder의 길이를 정해야 한다. 무한정일 수 없다.

     + 여기서 encoder, decoder는 동시에 학습할 수 있다. (참조)

     + 정답이 있는 데이터만 S2S 학습이 가능하다.

     + 단어들에 대한 벡터화한 수치 사전이 필요하다. (참조)

  - beam search

     + RNN의 학습 과정에서 트리 탐색 기법으로 쓰임

     + 최고우선탐색(Best-First-Search)기번을 기본으로 회되 기억해야 하는 노드 수를 제한해 효율성을 높이는 방식

     + beam : 사용자가 기억해야 하는 노드 수

  - LSTM

     + Backward Propagation(역전파)할 때에 Gradient Vanishing이나 Exploding되는 현상을 막기 위해 LSTM을 사용한다. 

     + 역전파할 때 미분한다. Gradient는 결국 기울기 이고, 미분또한 기울기를 구하는 것으로 역전파를 할 때 미분의 값이 작을 때 Gradient Vanshing이 발생하고, 클때 exploding이 발생한다.

     + Gradient Vanishing에 대한 자세한 설명은 영덕의 연구소를 참조한다.  





소스 설치 및 실행


소스를 깃헙에서 클론한다.

$ git clone https://github.com/victordibia/data2vis.git


환경 설정

  - Anacoda를 설치

  - Python v3.6.5 사용

  - Tensorflow v1.9.0 사용 (Anaconda Navigator UI에서 설치하지 않고 conda CLI로 버전을 지정해서 설치한다.)

$ conda install -c conda-forge tensorflow=1.9.0


모듈 설치

  - requirements.txt는 node.js의 package.json역할

$ cd data2vis

$ pip install -r requirements.txt


실행하기 

$ python webserver.py


브라우져에서 http://localhost:5016/  호출


디버깅하기

MS Code에서 다음 항목을 추가한다.

  - port: listen 포트

  - model_dir: 모델이 있는곳, 이곳에 seq2seq의 환경파일인 train_options.json 파일이 존재해야 한다. 

     해당 파일은 training 시킨 결과를 통해 자동으로 생성된다. 훈련시키는 방법에 대해서는 두번째 글 참조.

  - beam_width: 사용자가 기억해야 하는 노드수 5개

{

    "version": "0.2.0",

    "configurations": [

        {

            "name": "Python: Data2Vis - Flask (0.11.x or later)",

            "type": "python",

            "request": "launch",

            "program": "${workspaceFolder}/webserver.py",

            "env": {

                "FLASK_APP": "${workspaceFolder}/webserver.py",

                "FLASK_ENV": "development"

            },

            "args": [

                "--port=5016",

                "--model_dir=vizmodel",

                "--beam_width=15"

            ]

        },

        {

            "name": "Python: Current File",

            "type": "python",

            "request": "launch",

            "program": "${file}"

        },

        .....

}


샘플 실행

  - 좌측 examples 메뉴를 클릭하고 입력창에 1이상의 값을 넣고, "Generate Examples" 버튼을 클릭하면 차트가 생성된다. 





seq2seq 모듈


구글이 개발한 tf-seq2seq 모듈 소스을 data2vis 폴더에 그대로 copy해 놓은 상태이다.  모델을 학습하고 검증하는 것은 실제 seq2seq가 하므로 tf-seq2seq 사용방법을 알아야 한다. 

   - tf-seq2seq 소개 블로그: Goolge NMT 논문 필수

   - seq2seq에 대한 기본 설명은 Arxiv의 Neural Machine Translation 논문을 참조한다. 

   - Tensorflow의 seq2seq 사용법



Configuration Training

  - 환경파일에는 Input data, model, training parameter를 정의한다.

  - vismodel의 train_options.json 파일을 사용한다.

  - optimizer 종류와 learning_rate등을 지정. Adam 옵티마이저를 사용.

  - vocab_target, vocab_source 임베딩을 위한 벡터 카운트를 만들기 위해 파일 지정

  - decoder, encoder class와 params을 설정. 둘 다 LSTMCell 사용

  - attention class와 params 설정

  - inference, bridge, embedding 설정

  - source/target.max_seq_len 으로 string의 크기 지정

  - 모델 옵션 설명

  - 인코더 옵션 설명

  - 디코더 옵션 설명


{

    "model_class": "AttentionSeq2Seq",

    "model_params": {

        "optimizer.name": "Adam",

        "decoder.class": "seq2seq.decoders.AttentionDecoder",

        "inference.beam_search.beam_width": 5,

        "decoder.params": {

            "rnn_cell": {

                "dropout_input_keep_prob": 0.5,

                "num_layers": 2,

                "cell_params": {

                    "num_units": 512

                },

                "dropout_output_keep_prob": 1.0,

                "cell_class": "LSTMCell"

            },

            "max_decode_length": 2000

        },

        "optimizer.learning_rate": 0.0001,

        "source.reverse": false,

        "source.max_seq_len": 500,

        "attention.params": {

            "num_units": 512

        },

        "attention.class": "seq2seq.decoders.attention.AttentionLayerDot",

        "vocab_target": "sourcedata/vocab.target",

        "target.max_seq_len": 500,

        "optimizer.params": {

            "epsilon": 8e-07

        },

        "bridge.class": "seq2seq.models.bridges.ZeroBridge",

        "vocab_source": "sourcedata/vocab.source",

        "encoder.params": {

            "rnn_cell": {

                "dropout_input_keep_prob": 0.5,

                "num_layers": 2,

                "cell_params": {

                    "num_units": 512

                },

                "dropout_output_keep_prob": 1.0,

                "cell_class": "LSTMCell"

            }

        },

        "encoder.class": "seq2seq.encoders.BidirectionalRNNEncoder",

        "embedding.dim": 512

    }

}



Training 

  - 모델과 교육데이터가 갖추어져 있으면 훈련을 수행한다.

  - /sourcedata안에 source, target의 trainig data가 존재한다.

  - utils/data_gen.py에서 /examples 폴더의 vega spec을 읽어와 training data를 만들고 있다. 



Prediction

  - 모델 Training을 받은 후 예측을 시작할 수 있다. 

  - DecodeText 클래스를 사용하고, Input pipeline은 ParallelTextInputPipeline을 사용함. 

    + DecodeText는 모델 예측을 가져와 표준 출력으로 추력하는 작업을 수행함

    + DumpAttention과 DumpBeams을 이용해 모델 수행시 디버깅을 할 수 있다. 파일로 쓰는 것임.

    + input pipline은 데이터를 읽는 방법을 정의한다.



Decoding with Beam Search

  - 빔 검색은 번역 성능을 향상시키는 일반적으로 사용되는 디코딩 기술이다. 

  - 빔 검색은 메모리에 가설 또는 빔(beam)을 놓고 가장 높은 점수인 것을 선택한다. 



Evaluating specific checkpoint

  - Training을 통해 다양한 모델의 체크포인트를 저장한다.

  - BLEU (bilingual evaluation understudy)를 통해 번역 성능 평가. 

 


Checkpoint에 대한 설명

- Saving

  + model 을 만드는 코드 의존적인 포멧을 갖는다.

  + 체크포인트는 training하며 생성된 모델의 버전이다.

  + Estimator가 checkpoint를 model_dir 위치에 저장한다. 

  + events 파일은 tensorboard가 시각시에 사용한다. 

  + Saver를 통해 체크포인트를 Saving/Restoring 한다. 


checkpoint

events.out.tfevents.timestamp.hostname

graph.pbtxt

model.ckpt-1.data-00000-of-00001

model.ckpt-1.index

model.ckpt-1.meta

model.ckpt-200.data-00000-of-00001

model.ckpt-200.index

model.ckpt-200.meta


- Restoring

  + Estimator는 train()을 호출하면 model의 그래프를 model_fn()을 호출해서 생성한다. 

  + Estimator는 최근의 checkpoint를 통해 새로운 모델의 weight을 초기화 한다. 






webserver.py 이해


파이썬 웹서비스는 Flask를 이용한다.  data2vis/static 과 templates가 Flask운영을 위해 사용된다. 


webserver.py 실행 순서

- port, vizmodel, beam_width를 아규먼트를 받는다.

- vizmodel/train_options.json을 기반으로 TrainOption 오브젝트를 생성

train_options = training_utils.TrainOptions.load(model_dir_input)


- model params을 사용해 model class를 생성함. AttensionSeq2Seq.py (attension_seq2seq.py)

model_params = _deep_merge_dict(model_params, _maybe_load_yaml(model_params))

model = model_cls(params=model_params, mode=tf.contrib.learn.ModeKeys.INFER)


- inference task 생성. DecodeText 생성

if (str(tdict["class"]) == "DecodeText"):

        task = task_cls(tdict["params"], callback_func=_save_prediction_to_dict)


- ParallelTextInputPipeline pipeline 생성

input_pipeline_infer = input_pipeline.make_input_pipeline_from_def(

    fl_input_pipeline,

    mode=tf.contrib.learn.ModeKeys.INFER,

    shuffle=False,

    num_epochs=1)


- inference를 사용하는 (Tensorflow) graph 생성.

  + seq2seq/inference/inference.py에서 pipeline과 batch_size를 통해 input function을 만들고

  + input function의  feature와 label을 model의 파라미터로 사용해서 model의 build를 호출한다. 

predictions, _, _ = create_inference_graph( model=model, input_pipeline=input_pipeline_infer, batch_size=batch_size)


- Listen을 하고, Flask의 routing을 설정한다. "Generate Examples" 버튼 클릭시 호출 

  + test data를 사용한다. 

  + normalize를 해준다. (foward_norm, backward_norm)

  + decode result를 가지고 vega spec을 만들어 return한다. 

@app.route("/examplesdata")

def examplesdata():

    source_data = data_utils.load_test_dataset()

    f_names = data_utils.generate_field_types(source_data)

    data_utils.forward_norm(source_data, destination_file, f_names)


    run_inference()

    

    decoded_string_post = data_utils.backward_norm(decoded_string[0], f_names)


    try:

        vega_spec = json.loads(decoded_string_post)

        vega_spec["data"] = {"values": source_data}

        response_payload = {"vegaspec": vega_spec, "status": True}

    except JSONDecodeError as e:

        response_payload = {

            "status": False,

            "reason": "Model did not produce a valid vegalite JSON",

            "vegaspec": decoded_string

        }

    return jsonify(response_payload)





<참조>


  - Data2Vis 소개글, 깃헙 소스, Arxiv 링크

  - deeep 블로그
     seq2seq 에 대한 쉬운 설명글

     Attention 메카니즘설명 (소개한 Arxiv 링크)

  - ratsgo 블로그
     RNN과 LSTM 이해

     seq2seq를 이용한 뉴스 제목 추출하기
     설명에 대한 소스 (2018년 tensorflow 버전에 맞지않다)

     beam search 이해 in Recursive Neural Network

  - 구글 제공
     seq2seq 문서

  - Tensorflow의 seq2seq 한글 설명, 2014년 Arxiv에 소개된 seq2seq pdf

  - 라온피플 블로그
     RNN, LSTM, GRU 소개

  - 영덕의 연구소 블로그
     Gradient Vanishing 문제 개념

  - Naivsphere 블로그

    SGD (Stochastic Gradient Descent)에 대한 글

  - 카카오 IT 브런치
     BLEU: NMT 평가 방식 설명

  - skymind.ai

  - epoch, batch_size 용어 이해 (MNIST epoch, batch 설명)

posted by 윤영식