mxnet icon indicating copy to clipboard operation
mxnet copied to clipboard

Is it possible for mxnet to implement multi-task learning?

Open supercloud76 opened this issue 11 years ago • 19 comments

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.

supercloud76 avatar Nov 20 '15 04:11 supercloud76

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.

piiswrong avatar Nov 20 '15 05:11 piiswrong

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

tqchen avatar Nov 20 '15 05:11 tqchen

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=...)

pluskid avatar Nov 20 '15 05:11 pluskid

Agreed with @pluskid .

XericZephyr avatar Nov 20 '15 05:11 XericZephyr

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 avatar Nov 22 '15 05:11 qianguih

@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 avatar Nov 23 '15 04:11 pluskid

@pluskid Get it. Thanks !

qianguih avatar Nov 23 '15 04:11 qianguih

@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

baoqingping avatar Nov 27 '15 15:11 baoqingping

@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 avatar Nov 28 '15 21:11 nowozin

@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?

pluskid avatar Nov 28 '15 23:11 pluskid

@baoqingping did you figure out the fix?

suhangpro avatar Dec 02 '15 23:12 suhangpro

@suhangpro no, I have no idea how to fix it

baoqingping avatar Dec 09 '15 02:12 baoqingping

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

tqchen avatar Dec 09 '15 16:12 tqchen

@lyttonhao Could you contribute an example after finishing your ongoing work?

winstywang avatar Dec 10 '15 04:12 winstywang

@winstywang Sure. I'm willing to make some contributions.

lyttonhao avatar Dec 10 '15 04:12 lyttonhao

@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)

luxiangju avatar Jan 26 '16 08:01 luxiangju

i got similar issue as i created the data iterator as @luxiangju did.

ChrisYang avatar Jan 28 '16 03:01 ChrisYang

@furlat any guidance here?

arank avatar Apr 28 '17 22:04 arank

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

Light-- avatar Nov 06 '20 01:11 Light--