"Mxnet"의 두 판 사이의 차이

ph
이동: 둘러보기, 검색
잔글 (→‎Basics)
잔글 (→‎Module)
29번째 줄: 29번째 줄:
  
 
===Module===
 
===Module===
 +
대충 flow만 보자면,
 +
mx.test_utils.download
 +
np.genfromtxt
 +
... # data와 label분리.
 +
mx.io.NDArrayIter # batch 분리
 +
... # net만들고
 +
mod = mx.mod.Module(symbol=net, context=mx.cpu(), ...)
 +
mod.bind(data_shapes, label_shapes)
 +
mod.init_params(...)
 +
metric = mx.metric.create('acc')
 +
for epoch in range(5):
 +
    train_iter.reset()
 +
    for batch in train_iter:
 +
        mod.forward(batch, is_train=True)
 +
        mod.update_metric(metric, batch.label)  # accumulate prediction accuracy
 +
        mod.backward()                          # compute gradients
 +
        mod.update()                            # update parameters
 +
    print('Epoch %d, Training %s' % (epoch, metric.get()))
 +
- 13행 {{c|batch.label}}이 어디서 나오나 했는데 다음 section([http://mxnet.io/tutorials/basic/data.html iterators])에 보면 나온다.
 +
- 13행에서 mod가 metric을 가지므로, 14행에서 {{c|backward}}만 불러도, gradient계산한다.
 +
- metric.create에서 여러가지 할 수 있는데, [http://mxnet.io/api/python/model.html?highlight=mxnet.metric#mxnet.metric.CrossEntropy mxnet.metric api]에서 볼 수 있다. {{c|Accuracy, TopKAccuracy, F1, Perplexity, MAE, MSE, RMSE, CrossEntropy, Loss, Torch, Caffe<ref>Loss, Torch, Caffe는 Dummy metrics</ref>, CustomMetric, np<ref>numpy array를 입력으로 받는 custom metric</ref>가 있다.
 +
- {{c|forward}}에서 {{c|is_train}}은,
  
 
==Training and Inference==
 
==Training and Inference==

2017년 7월 3일 (월) 17:29 판

http://mxnet.io

뭘 이렇게들 만들어 대는지. tf가 맘에 안들기는 하지만.

Basics

걍 numpy를 쓰지 않는 이유는 cpu, gpu등 자유로이 알아서(?) 처리해주고, 병렬까지도 알아서(?) 한다고 함. [1] tf도 해주지 않냐?

broadcast[2]: rep같은건가봄.

pickle.dump말고 mx.nd.load, mx.nd.save를 쓸 수 있다. [3]

symbolic api를 설명[4]하면서 중간에 장점이 하나 나오는데 이런게 있었네 싶었음. ㅎㅎ : 미리 그래프를 짜 놓으면 나중에 어떤 결과값이 필요할지 미리 알 수 있어서 계산중간값들을 모두 저장해둘 필요가 없다. 메모리가 절약됨.
관련해서, symbolic programming을 declarative programming이라고도 하고 이 반대를 imperative programming이라고 하는 모양. imperative programming은 단어만 보면 이게 도대체 뭔소린가 싶었다. Declarative programming의 예: regular expression, SQL.

매뉴얼 따라하다가 graphviz때문에 에러남

ExecutableNotFound: failed to execute ['dot', '-Tsvg'], make sure the Graphviz executables are on your systems' PATH

맥이라 걍 포기. brew하면 된다는데 걍 안하고 원격 리눅스에서나. 시스템에도 있어야 하고, pip로도 있어야 한다.(우분투에서 apt ~pip install ~ 다 해줘야 한다는 얘기)

bind → forward해서 output을 얻지 않고, 바로 eval할 수도 있다.

tojson()

print(c.tojson())
c.save('symbol-c.json')
c2 = mx.sym.load('symbol-c.json')

type cast

a = mx.sym.Variable('data')
b = mx.sym.cast(data=a, dtype='float16’)

Module

대충 flow만 보자면,

mx.test_utils.download
np.genfromtxt
... # data와 label분리.
mx.io.NDArrayIter # batch 분리
... # net만들고 
mod = mx.mod.Module(symbol=net, context=mx.cpu(), ...) 
mod.bind(data_shapes, label_shapes)
mod.init_params(...)
metric = mx.metric.create('acc')
for epoch in range(5):
    train_iter.reset()
    for batch in train_iter:
        mod.forward(batch, is_train=True)
        mod.update_metric(metric, batch.label)  # accumulate prediction accuracy
        mod.backward()                          # compute gradients
        mod.update()                            # update parameters
    print('Epoch %d, Training %s' % (epoch, metric.get()))

- 13행 batch.label이 어디서 나오나 했는데 다음 section(iterators)에 보면 나온다. - 13행에서 mod가 metric을 가지므로, 14행에서 backward만 불러도, gradient계산한다. - metric.create에서 여러가지 할 수 있는데, mxnet.metric api에서 볼 수 있다. {{c|Accuracy, TopKAccuracy, F1, Perplexity, MAE, MSE, RMSE, CrossEntropy, Loss, Torch, Caffe[1], CustomMetric, np[2]가 있다. - forward에서 is_train은,

Training and Inference

Linear Regression

Handwritten Digit Recognition

Predict with pre-trained models

Large Scale Image Classification

  1. Loss, Torch, Caffe는 Dummy metrics
  2. numpy array를 입력으로 받는 custom metric