Is it possible for mxnet to implement multi-task learning?
For example: On training: Task A: Data_A, Label_A, loss_A Task B: Data_B, Label_B, loss_B The two tasks share the same hidden layer and cnn layer, the data layer, fully connected layer and loss layer are different. On testing: Input a sample, predict the task B & task B label.
yes, but the default FeedForward model only support single loss. You'll need to write your own model and solver. Check examples/autoencoder/model.py&solver.py for example.
I think Feedforward already support multiple losses, as long as you group together? At least seems that was the case in julia @pluskid . We might need a toy example for this
Yes! I believe multiple loss in Python is also supported. Something like this:
loss1 = mx.sym.SoftmaxOutput(data1, name='softmax1')
loss2 = mx.sym.SoftmaxOutput(data2, name='softmax2')
loss_comb = mx.sym.Group(loss1, loss2)
model = FeedForward(loss_comb, ctx=...)
Agreed with @pluskid .
Hi, @pluskid, could you share more about how to prepare the training data and how to feed them into model.fit to train the model?
@qianguih The easiest way of using multiple data / labels is to use NDArrayIterator. Unfortunately, there seem to be no enough document or example on this. Please refer to this code. Basically, you can pass a list of data / labels, or actually a dictionary of name-array pairs to construct your data iterators. Something like
iter = mx.NDArrayIter({'data1': nd_data1, 'data2': nd_data2}, {'label1': nd_label1, 'label2': nd_label2})
The names data1, label1 etc. should match the name you used to construct the network. e.g.
d1 = mx.sym.Variable('data1')
fc1 = mx.sym.FullyConnected(data=d1, ...)
...
@pluskid Get it. Thanks !
@pluskid I try to train a multi-label net. I use the im2rec tool to create a 2-label dataset. As you said, I define the symbols as follows
input_data = mx.symbol.Variable(name="data")
. . . shared ConvLayer . . .
softmax1 = mx.symbol.SoftmaxOutput(data=fc3, name='softmax1')
softmax2 = mx.symbol.SoftmaxOutput(data=fc3, name='softmax2')
softmax = mx.symbol.Group([softmax1, softmax2])
then I run the code and an error occured:
Symbol.InterShapeKeyword argument name softmax_label not found.
I have no idea how to fix it . and how cound I know whether softmax1 corresponds to label_1 and softmax2 corresponds to label_2
@pluskid What is the semantics of using multiple loss functions in a Group within the context of FeedForward?
I would guess that the sum of all elements in the Group is being minimized. If this is the case, maybe the documentation of FeedForward and multiple losses should clearly state this.
@nowozin Yes, one could think of it a a sum. Or alternatively, one could think that each loss function is back-propagating gradients (independently) and the gradients get merged for each parameter. The LSTM example (in Julia) use grouped multi-loss in FeedForward for example.
I'm not sure how and where should we document this. Because it seems very natural to me (maybe because I already know the implementation details). Do you have any suggestions?
@baoqingping did you figure out the fix?
@suhangpro no, I have no idea how to fix it
The name of the unspecified arguments are automatically generated. In your case
try to use softmax1_label and softmax2_label instead to refer to each of the input labels.
Alternatively, specify the name by
label1=mx.symbol.Variable("sm1_label");
label2=mx.symbol.Variable("sm2_label");
softmax1 = mx.symbol.SoftmaxOutput(data=fc3, label=label1, name='softmax1')
softmax2 = mx.symbol.SoftmaxOutput(data=fc3, label=label2, name='softmax2')
please check if it works for you, it would be great if any of you are interested in contributing an example of multi-labels even a mock up on with MNIST
@lyttonhao Could you contribute an example after finishing your ongoing work?
@winstywang Sure. I'm willing to make some contributions.
@Iyttonhao have you committed the example about multi_label? if yes, please give me a link to get the example! I followed the above tips, however, still get the message: Symbol.InterShapeKeyword argument name softmax_label not found.
I guess probably because the data iterator I use is not right as:
def ilsvrc12_iterator(batch_size, input_shape): """return train and val iterators for imagenet""" train_dataiter = mx.io.ImageRecordIter( path_imgrec = "./data/train116.rec", mean_img = "./data/mean.bin", path_imglist = "./data/labt.txt", label_width = 2, rand_crop = True, rand_mirror = True, prefetch_buffer = 4, preprocess_threads = 4, data_shape = input_shape, batch_size = batch_size) val_dataiter = mx.io.ImageRecordIter( path_imgrec = "./data/val116.rec", mean_img = "./data/mean.bin", path_imglist = "./data/labv.txt", label_width = 2, rand_crop = False, rand_mirror = False, prefetch_buffer = 4, preprocess_threads = 4, data_shape = input_shape, batch_size = batch_size)
return (train_dataiter, val_dataiter)
i got similar issue as i created the data iterator as @luxiangju did.
@furlat any guidance here?
any guidance here? how to save the multi-output symbol? i used this,
arg, aux = model.get_params()
all_layers = model.symbol.get_internals()
_sym = all_layers['fc1_output', 'fc2_output', 'fc3_output', 'fc4_output']
_arg = {}
for k in arg:
_arg[k] = arg[k]
mx.model.save_checkpoint(prefix, msave, _sym, _arg, aux)
but failed....
Traceback (most recent call last):
File "train_110511.py", line 422, in <module>
main()
File "train_110511.py", line 419, in main
train_net(args)
File "train_110511.py", line 414, in train_net
epoch_end_callback = epoch_cb )
File "/home/usr1/miniconda3/envs/py377/lib/python3.7/site-packages/mxnet/module/base_module.py", line 553, in fit
callback(batch_end_params)
File "train_110511.py", line 382, in _batch_callback
_sym = all_layers['fc1_output', 'fc2_output', 'fc3_output', 'fc4_output']
File "/home/usr1/miniconda3/envs/py377/lib/python3.7/site-packages/mxnet/symbol/symbol.py", line 550, in __getitem__
raise TypeError('Symbol only support integer index to fetch i-th output')
TypeError: Symbol only support integer index to fetch i-th output
i don't know why, because when i run single-task network, _sym = all_layers['fc1_output'] worked.
@tqchen @pluskid @piiswrong @ChrisYang @arank