"Mxnet"의 두 판 사이의 차이
잔글 (→Linear Regression) |
|||
111번째 줄: | 111번째 줄: | ||
==Training and Inference== | ==Training and Inference== | ||
− | ===Linear Regression===[http://mxnet.io/tutorials/python/linear-regression.html#linear-regression] | + | ===Linear Regression=== |
− | + | [http://mxnet.io/tutorials/python/linear-regression.html#linear-regression 원문] | |
− | + | <br>전체 소스 | |
− | + | <pre>import mxnet as mx | |
− | + | import numpy as np | |
− | |||
− | |||
− | model. | + | #Training data |
+ | train_data = np.random.uniform(0, 1, [100, 2]) | ||
+ | train_label = np.array([train_data[i][0] + 2 * train_data[i][1] for i in range(100)]) | ||
+ | batch_size = 1 | ||
+ | |||
+ | #Evaluation Data | ||
+ | eval_data = np.array([[7,2],[6,10],[12,2]]) | ||
+ | eval_label = np.array([11,26,16]) | ||
+ | |||
+ | train_iter = mx.io.NDArrayIter(train_data,train_label, batch_size, shuffle=True,label_name='lin_reg_label') #(1) | ||
+ | eval_iter = mx.io.NDArrayIter(eval_data, eval_label, batch_size, shuffle=False) | ||
+ | |||
+ | X = mx.sym.Variable('data') | ||
+ | Y = mx.symbol.Variable('lin_reg_label') #(2) | ||
+ | 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") #(3) | ||
+ | |||
+ | model = mx.mod.Module( | ||
+ | symbol = lro , | ||
+ | data_names=['data'], | ||
+ | label_names = ['lin_reg_label']# network structure (4) | ||
+ | ) | ||
+ | |||
+ | mx.viz.plot_network(symbol=lro) | ||
+ | # (5) | ||
+ | model.fit(train_iter, eval_iter, | ||
+ | optimizer_params={'learning_rate':0.005, 'momentum': 0.9}, | ||
+ | num_epoch=1000, | ||
+ | batch_end_callback = mx.callback.Speedometer(batch_size, 2)) | ||
+ | |||
+ | model.predict(eval_iter).asnumpy() | ||
+ | |||
+ | metric = mx.metric.MSE() | ||
+ | model.score(eval_iter, metric) | ||
+ | |||
+ | eval_data = np.array([[7,2],[6,10],[12,2]]) | ||
+ | eval_label = np.array([11.1,26.1,16.1]) #Adding 0.1 to each of the values | ||
+ | eval_iter = mx.io.NDArrayIter(eval_data, eval_label, batch_size, shuffle=False) | ||
+ | model.score(eval_iter, metric)</pre> | ||
+ | |||
+ | (3) {{c| mx.sym.LinearRegressionOutput}}은 l2 loss계산함. <br> | ||
+ | <i>(1),(2)에서 {{c|lin_reg_label}}이라고 준 것과 {{c|NDArrayIter}}에서 이름이 일치해야 한다.</i> <code>train_iter = mx.io.NDArrayIter(..., label_name='<span style="color:red">lin_reg_label</span>' ) </code> <i> 다시말해, 입력단의 이름이 일치해야 한다는 얘기. 결국 (4)에 나오는 이름까지 일치해야 해서 같은 이름이 세번 나온다</i> | ||
+ | |||
+ | (5)의 {{c|model.fit}}에서 {{c|batch_end_callback}}으로 <code>[http://mxnet.io/api/python/callback.html?highlight=ck.speedometer#mxnet.callback.Speedometer mx.callback.Speedometer]</code>줄 수 있다. arguments = {{c|(batch_size, frequent=50, auto_reset=True)} : 배치 50번마다 로깅하고, 로깅 후 reset할것. | ||
<pre>>>> # Print training speed and evaluation metrics every ten batches. Batch size is one. | <pre>>>> # Print training speed and evaluation metrics every ten batches. Batch size is one. | ||
>>> module.fit(iterator, num_epoch=n_epoch, | >>> module.fit(iterator, num_epoch=n_epoch, |
2017년 7월 4일 (화) 12:29 판
뭘 이렇게들 만들어 대는지. 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
원문
전체 소스
import mxnet as mx import numpy as np #Training data train_data = np.random.uniform(0, 1, [100, 2]) train_label = np.array([train_data[i][0] + 2 * train_data[i][1] for i in range(100)]) batch_size = 1 #Evaluation Data eval_data = np.array([[7,2],[6,10],[12,2]]) eval_label = np.array([11,26,16]) train_iter = mx.io.NDArrayIter(train_data,train_label, batch_size, shuffle=True,label_name='lin_reg_label') #(1) eval_iter = mx.io.NDArrayIter(eval_data, eval_label, batch_size, shuffle=False) X = mx.sym.Variable('data') Y = mx.symbol.Variable('lin_reg_label') #(2) 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") #(3) model = mx.mod.Module( symbol = lro , data_names=['data'], label_names = ['lin_reg_label']# network structure (4) ) mx.viz.plot_network(symbol=lro) # (5) model.fit(train_iter, eval_iter, optimizer_params={'learning_rate':0.005, 'momentum': 0.9}, num_epoch=1000, batch_end_callback = mx.callback.Speedometer(batch_size, 2)) model.predict(eval_iter).asnumpy() metric = mx.metric.MSE() model.score(eval_iter, metric) eval_data = np.array([[7,2],[6,10],[12,2]]) eval_label = np.array([11.1,26.1,16.1]) #Adding 0.1 to each of the values eval_iter = mx.io.NDArrayIter(eval_data, eval_label, batch_size, shuffle=False) model.score(eval_iter, metric)
(3) mx.sym.LinearRegressionOutput은 l2 loss계산함.
(1),(2)에서 lin_reg_label이라고 준 것과 NDArrayIter에서 이름이 일치해야 한다. train_iter = mx.io.NDArrayIter(..., label_name='lin_reg_label' )
다시말해, 입력단의 이름이 일치해야 한다는 얘기. 결국 (4)에 나오는 이름까지 일치해야 해서 같은 이름이 세번 나온다
(5)의 model.fit에서 batch_end_callback으로 mx.callback.Speedometer
줄 수 있다. arguments = {{c|(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