Wolfram Language

Neural Networks

Multi-task Learning

Train a convolutional network to classify both the labels and sublabels of images in the CIFAR-100 dataset.

Obtain the training dataset.

In[1]:=
Click for copyable input
obj = ResourceObject["CIFAR-100"]; trainingData = ResourceData[obj, "TrainingDataset"]; RandomSample[trainingData, 5]
Out[1]=

Obtain the labels and sublabels of the images.

In[2]:=
Click for copyable input
labels = Union@Normal@trainingData[All, "Label"] sublabels = Union@Normal@trainingData[All, "SubLabel"]
Out[2]=
Out[2]=

Define a simple convolutional network.

In[3]:=
Click for copyable input
convnet = NetChain[{ ConvolutionLayer[20, {5, 5}], ElementwiseLayer[Ramp], PoolingLayer[{2, 2}, {2, 2}], ConvolutionLayer[50, {5, 5}], ElementwiseLayer[Ramp], PoolingLayer[{2, 2}, {2, 2}], FlattenLayer[], DotPlusLayer[500], ElementwiseLayer[Ramp] }, "Input" -> NetEncoder[{"Image", {32, 32}}]]
Out[3]=

Create a net that uses the result of the convolutional net to make label and sublabel predictions.

In[4]:=
Click for copyable input
net = NetGraph[{convnet, 100, SoftmaxLayer[], 20, SoftmaxLayer[]}, {NetPort["Image"] -> 1 -> 2 -> 3 -> NetPort["SubLabel"], 2 -> 4 -> 5 -> NetPort["Label"]}, "Label" -> NetDecoder[{"Class", labels}], "SubLabel" -> NetDecoder[{"Class", sublabels}]]
Out[4]=

Train the network, letting NetTrain automatically infer that it should attach cross-entropy loss functions to both outputs.

In[5]:=
Click for copyable input
net = NetTrain[net, trainingData]
Out[6]=

Classify an image and obtain both the label and the sublabel.

In[7]:=
Click for copyable input
net[\!\(\* GraphicsBox[ TagBox[RasterBox[CompressedData[" 1:eJwtlHdQ01m7x3fu/ePdO2/ZdS1Ib2mUBAgQSiAJpAEJJJBiCkkgIYQUEtIo BiT0qlIV1EXFq+jrYlvLigUVkVVYZFdZ6wICFooiAlJC8rt5Z+7M95x5zvnj Oef5zme+nmJNSuZ/ffPNN/pv7VtKel6MTpdewPjefmCp9XKZWpoRrzZIZVJd uPi/7Zce/7/+U8+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hUbIlnEQ+EaNM IWfSiQxilIhLk0rYaLSfUsXPN2oYdDoYDIL5g2OJOH8E1A8OCwgM/g+lAQH2 3BZJs079+3RzSz2BFBMaGSnJkp2/cOTWL4fvXD9448LRg3vLuDQqnUAoUatO 1FVUa5Qiavyh+uozHe0UAmZPvu5M5+H25gYOKzk+DoOOhApEZL4wITgcHhIW AoJ4OzjtgPpCCwpyThxv0uvk0dFRoWEoiZyfkc3nSBLn39wd7DtZW6reV2ns aqvo6Wy+duRAXprwt5u/vBx8KKTTfz59avj+9VNHWo1qmUrGKcwX1VQpOHy8 g+v3UH9fJxe3v337t3/+/Vsvd0e4n7eHh5OTi6OblzskAAYLDwwlo578Vvp0 pGL094bRgabXd5qnbx568r9N+7MELbs1LUWGGH+IIB4vpJFIkShidBg9HiPi UIS8JBDI5du//+OH7S47HT22fL9t63dbtn73r21bvw9C+tAYJBaXrDNw8ovT 247m9t0zP35Y92Lk4Iu+5ueXG4aPV/ZW5p3MkZXzmVoKXhQVIsJG4P39Ed5e vmBPONTb29MFCgU7Ou3cuuM7VzdHKMTT3887PNyfz4szFwnPnS2+1VN+/Wrh /d6y+3fLBgeqtQWswmJhqVlUVCDQK1kaEcUkYdZq+I25/P16TletuqMkKz8z SSqIFfAwHFZEKg+TJiSmC4nZKkpFGa+5Udp2IKvrlLG/v+Hx46bHw1XDQ+an I9UjQ9WPHpQN9Jn/D3Y/x6I= "], {{0, 32}, {32, 0}}, {0, 255}, ColorFunction->RGBColor], BoxForm`ImageTag["Byte", ColorSpace -> "RGB", Interleaving -> True], Selectable->False], DefaultBaseStyle->"ImageGraphics", ImageSizeRaw->{32, 32}, PlotRange->{{0, 32}, {0, 32}}]\)]
Out[7]=

Obtain the most likely classifications for the "SubLabel" output.

In[8]:=
Click for copyable input
net[\!\(\* GraphicsBox[ TagBox[RasterBox[CompressedData[" 1:eJwtlHdQ01m7x3fu/ePdO2/ZdS1Ib2mUBAgQSiAJpAEJJJBiCkkgIYQUEtIo BiT0qlIV1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n/t7IWztRmiRDwukMMgqiWsTE4C kxITR8DFYnFRURGIIFhpZZHJVEAgkINDw+BIRAQm0hPiERiCiCXgvbxAYIhP anqWylCUKlEggkO3OOyEIZFJPN7lGzenx54uTv9+6piptlyhUbIlnEQ+EaNM IWfSiQxilIhLk0rYaLSfUsXPN2oYdDoYDIL5g2OJOH8E1A8OCwgM/g+lAQH2 3BZJs079+3RzSz2BFBMaGSnJkp2/cOTWL4fvXD9448LRg3vLuDQqnUAoUatO 1FVUa5Qiavyh+uozHe0UAmZPvu5M5+H25gYOKzk+DoOOhApEZL4wITgcHhIW AoJ4OzjtgPpCCwpyThxv0uvk0dFRoWEoiZyfkc3nSBLn39wd7DtZW6reV2ns aqvo6Wy+duRAXprwt5u/vBx8KKTTfz59avj+9VNHWo1qmUrGKcwX1VQpOHy8 g+v3UH9fJxe3v337t3/+/Vsvd0e4n7eHh5OTi6OblzskAAYLDwwlo578Vvp0 pGL094bRgabXd5qnbx568r9N+7MELbs1LUWGGH+IIB4vpJFIkShidBg9HiPi UIS8JBDI5du//+OH7S47HT22fL9t63dbtn73r21bvw9C+tAYJBaXrDNw8ovT 247m9t0zP35Y92Lk4Iu+5ueXG4aPV/ZW5p3MkZXzmVoKXhQVIsJG4P39Ed5e vmBPONTb29MFCgU7Ou3cuuM7VzdHKMTT3887PNyfz4szFwnPnS2+1VN+/Wrh /d6y+3fLBgeqtQWswmJhqVlUVCDQK1kaEcUkYdZq+I25/P16TletuqMkKz8z SSqIFfAwHFZEKg+TJiSmC4nZKkpFGa+5Udp2IKvrlLG/v+Hx46bHw1XDQ+an I9UjQ9WPHpQN9Jn/D3Y/x6I= "], {{0, 32}, {32, 0}}, {0, 255}, ColorFunction->RGBColor], BoxForm`ImageTag["Byte", ColorSpace -> "RGB", Interleaving -> True], Selectable->False], DefaultBaseStyle->"ImageGraphics", ImageSizeRaw->{32, 32}, PlotRange->{{0, 32}, {0, 32}}]\), "SubLabel" -> "TopProbabilities"]
Out[8]=

Related Examples

de es fr ja ko pt-br ru zh