"Mxnet"의 두 판 사이의 차이
잔글 (→Module) |
|||
43번째 줄: | 43번째 줄: | ||
mod = mx.mod.Module(symbol=net, context=mx.cpu(), ...) | mod = mx.mod.Module(symbol=net, context=mx.cpu(), ...) | ||
한 다음, | 한 다음, | ||
− | < | + | <source lang=php># memory alloc |
mod.bind(data_shapes=train_iter.provide_data, label_shapes=train_iter.provide_label) | mod.bind(data_shapes=train_iter.provide_data, label_shapes=train_iter.provide_label) | ||
56번째 줄: | 56번째 줄: | ||
mod.forward(batch, is_train=True) # (1) | mod.forward(batch, is_train=True) # (1) | ||
mod.update_metric(metric, batch.label) # (2) | mod.update_metric(metric, batch.label) # (2) | ||
− | mod.backward() | + | mod.backward() # (3) |
mod.update() | mod.update() | ||
− | print('Epoch %d, Training %s' % (epoch, metric.get()))</ | + | print('Epoch %d, Training %s' % (epoch, metric.get()))</source> |
* (2)의 {{c|batch.label}}이 어디서 나오나 했는데 다음 section([http://mxnet.io/tutorials/basic/data.html iterators])에 보면 나온다. | * (2)의 {{c|batch.label}}이 어디서 나오나 했는데 다음 section([http://mxnet.io/tutorials/basic/data.html iterators])에 보면 나온다. | ||
** [http://mxnet.io/api/python/io.html#mxnet.io.DataBatch API문서]도 있음. | ** [http://mxnet.io/api/python/io.html#mxnet.io.DataBatch API문서]도 있음. |
2017년 7월 4일 (화) 12:04 판
뭘 이렇게들 만들어 대는지. 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
Creation
mod = mx.mod.Module(symbol=net, context=mx.cpu(), data_names=['data'], label_names=['softmax_label' ])
Intermediate-level Interface
먼저 대강
mx.test_utils.download np.genfromtxt ... # data와 label분리. mx.io.NDArrayIter # batch 분리 ... # net만들고 mod = mx.mod.Module(symbol=net, context=mx.cpu(), ...)
한 다음,
# memory alloc
mod.bind(data_shapes=train_iter.provide_data, label_shapes=train_iter.provide_label)
mod.init_params(initializer=mx.init.Uniform(scale=.1))
mod.init_optimizer(optimizer='sgd', optimizer_params=(('learning_rate', 0.1), ))
metric = mx.metric.create('acc')
for epoch in range(5):
train_iter.reset()
metric.reset()
for batch in train_iter:
mod.forward(batch, is_train=True) # (1)
mod.update_metric(metric, batch.label) # (2)
mod.backward() # (3)
mod.update()
print('Epoch %d, Training %s' % (epoch, metric.get()))
- (2)의 batch.label이 어디서 나오나 했는데 다음 section(iterators)에 보면 나온다.
- API문서도 있음.
- (2)에서 mod에 metric을 알려주므로, (3)에서 backward만 불러도, gradient계산한다.
- metric.create에서 여러가지 할 수 있는데, mxnet.metric api에서 볼 수 있다.
- (1)의 forward에서 is_train은, undocumented인것 같다. 걍 train할때는 무조건 True주기로. 기본값은 None. [6] [7]
High-level Interface
train_iter.reset() mod = mx.mod.Module(symbol=net, context=mx.cpu(), data_names=['data'], label_names=['softmax_label']) mod.fit(train_iter, eval_data=val_iter, optimizer='sgd', optimizer_params={'learning_rate':0.1}, eval_metric='acc', num_epoch=8)
y = mod.predict(val_iter)
predict결과 없이 그냥 evaluation만 하려면,
score = mod.score(val_iter, ['mse', ‘acc'])
Save and Load
체크포인트 설정
model_prefix = 'mx_mlp' checkpoint = mx.callback.do_checkpoint(model_prefix) mod = mx.mod.Module(symbol=net) mod.fit(train_iter, num_epoch=5, epoch_end_callback=checkpoint)
불러오기
sym, arg_params, aux_params = mx.model.load_checkpoint(model_prefix, 3) # 3:epoch
불러와서 다시 설정
mod.set_params(arg_params, aux_params)
Simply resume training.
mod = mx.mod.Module(symbol=sym) mod.fit(train_iter, num_epoch=8, arg_params=arg_params, aux_params=aux_params, begin_epoch=3)
Iterators - Loading data
- Data iterator는 DataBatch를 반환한다.
- csv파일로부터 읽기: CSVIter
- custom iterator도 지원한다.
- mx.image쓰려면 OpenCV있어야 한다.(CV2 아니다)
- ImageRecordIter나 ImageIter를 사용해서 이미지파일 데이터를 학습데이터로 쓸 수 있다. 이때 RecoredIO파일 미리 있어야 한다.
- 학습할때 뿐 아니라 score계산할때도 iterator쓴다.
eval_iter = mx.io.NDArrayIter(eval_data, eval_label, batch_size, shuffle=False) model.score(eval_iter, metric)
Training and Inference
Linear Regression
X = mx.sym.Variable('data') Y = mx.symbol.Variable('lin_reg_label') fully_connected_layer = mx.sym.FullyConnected(data=X, name='fc1', num_hidden = 1) lro = mx.sym.LinearRegressionOutput(data=fully_connected_layer, label=Y, name="lro")
mx.sym.LinearRegressionOutput은 l2 loss계산함.
위에서 lin_reg_label이라고 준 것과 NDArrayIter에서 이름이 일치해야 한다. train_iter = mx.io.NDArrayIter(..., label_name='lin_reg_label' )
model.fit에서 batch_end_callback으로 mx.callback.Speedometer줄 수 있다. arguments = (batch_size, frequent=50, auto_reset=True) : 배치 50번마다 로깅하고, 로깅 후 reset할것.
>>> # Print training speed and evaluation metrics every ten batches. Batch size is one. >>> module.fit(iterator, num_epoch=n_epoch, ... batch_end_callback=mx.callback.Speedometer(1, 10)) Epoch[0] Batch [10] Speed: 1910.41 samples/sec Train-accuracy=0.200000 Epoch[0] Batch [20] Speed: 1764.83 samples/sec Train-accuracy=0.400000 Epoch[0] Batch [30] Speed: 1740.59 samples/sec Train-accuracy=0.500000