{"id":398,"date":"2021-02-27T23:09:25","date_gmt":"2021-02-27T23:09:25","guid":{"rendered":"http:\/\/laserphotonics.uk\/?p=398"},"modified":"2021-02-27T23:09:25","modified_gmt":"2021-02-27T23:09:25","slug":"ml-keras-multiple-input-multiple-output","status":"publish","type":"post","link":"https:\/\/laserphotonics.uk\/?p=398","title":{"rendered":"ML: Keras multiple input multiple output"},"content":{"rendered":"\n<p><a href=\"https:\/\/colab.research.google.com\/drive\/1ZNLC7QMG0Y2Zf4evf6JG_L0rybzmscoj#scrollTo=lwm64LEEt5NX\">https:\/\/colab.research.google.com\/drive\/1ZNLC7QMG0Y2Zf4evf6JG_L0rybzmscoj#scrollTo=lwm64LEEt5NX<\/a><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h3>Models with multiple inputs and outputs<\/h3>\n\n\n\n<p>The functional API makes it easy to manipulate multiple inputs and outputs. This cannot be handled with the&nbsp;<code>Sequential<\/code>&nbsp;API.<\/p>\n\n\n\n<p>For example, if you&#8217;re building a system for ranking customer issue tickets by priority and routing them to the correct department, then the model will have three inputs:<\/p>\n\n\n\n<ul><li>the title of the ticket (text input),<\/li><li>the text body of the ticket (text input), and<\/li><li>any tags added by the user (categorical input)<\/li><\/ul>\n\n\n\n<p>This model will have two outputs:<\/p>\n\n\n\n<ul><li>the priority score between 0 and 1 (scalar sigmoid output), and<\/li><li>the department that should handle the ticket (softmax output over the set of departments).<\/li><\/ul>\n\n\n\n<p>\n\nnum_tags&nbsp;=&nbsp;12&nbsp;&nbsp;#&nbsp;Number&nbsp;of&nbsp;unique&nbsp;issue&nbsp;tagsnum_words&nbsp;=&nbsp;10000&nbsp;&nbsp;#&nbsp;Size&nbsp;of&nbsp;vocabulary&nbsp;obtained&nbsp;when&nbsp;preprocessing&nbsp;text&nbsp;datanum_departments&nbsp;=&nbsp;4&nbsp;&nbsp;#&nbsp;Number&nbsp;of&nbsp;departments&nbsp;for&nbsp;predictions<br>title_input&nbsp;=&nbsp;keras.Input(&nbsp;&nbsp;&nbsp;&nbsp;shape=(None,),&nbsp;name=&#8221;title&#8221;)&nbsp;&nbsp;#&nbsp;Variable-length&nbsp;sequence&nbsp;of&nbsp;intsbody_input&nbsp;=&nbsp;keras.Input(shape=(None,),&nbsp;name=&#8221;body&#8221;)&nbsp;&nbsp;#&nbsp;Variable-length&nbsp;sequence&nbsp;of&nbsp;intstags_input&nbsp;=&nbsp;keras.Input(&nbsp;&nbsp;&nbsp;&nbsp;shape=(num_tags,),&nbsp;name=&#8221;tags&#8221;)&nbsp;&nbsp;#&nbsp;Binary&nbsp;vectors&nbsp;of&nbsp;size&nbsp;`num_tags`<br>#&nbsp;Embed&nbsp;each&nbsp;word&nbsp;in&nbsp;the&nbsp;title&nbsp;into&nbsp;a&nbsp;64-dimensional&nbsp;vectortitle_features&nbsp;=&nbsp;layers.Embedding(num_words,&nbsp;64)(title_input)#&nbsp;Embed&nbsp;each&nbsp;word&nbsp;in&nbsp;the&nbsp;text&nbsp;into&nbsp;a&nbsp;64-dimensional&nbsp;vectorbody_features&nbsp;=&nbsp;layers.Embedding(num_words,&nbsp;64)(body_input)<br>#&nbsp;Reduce&nbsp;sequence&nbsp;of&nbsp;embedded&nbsp;words&nbsp;in&nbsp;the&nbsp;title&nbsp;into&nbsp;a&nbsp;single&nbsp;128-dimensional&nbsp;vectortitle_features&nbsp;=&nbsp;layers.LSTM(128)(title_features)#&nbsp;Reduce&nbsp;sequence&nbsp;of&nbsp;embedded&nbsp;words&nbsp;in&nbsp;the&nbsp;body&nbsp;into&nbsp;a&nbsp;single&nbsp;32-dimensional&nbsp;vectorbody_features&nbsp;=&nbsp;layers.LSTM(32)(body_features)<br>#&nbsp;Merge&nbsp;all&nbsp;available&nbsp;features&nbsp;into&nbsp;a&nbsp;single&nbsp;large&nbsp;vector&nbsp;via&nbsp;concatenationx&nbsp;=&nbsp;layers.concatenate([title_features,&nbsp;body_features,&nbsp;tags_input])<br>#&nbsp;Stick&nbsp;a&nbsp;logistic&nbsp;regression&nbsp;for&nbsp;priority&nbsp;prediction&nbsp;on&nbsp;top&nbsp;of&nbsp;the&nbsp;featurespriority_pred&nbsp;=&nbsp;layers.Dense(1,&nbsp;name=&#8221;priority&#8221;)(x)#&nbsp;Stick&nbsp;a&nbsp;department&nbsp;classifier&nbsp;on&nbsp;top&nbsp;of&nbsp;the&nbsp;featuresdepartment_pred&nbsp;=&nbsp;layers.Dense(num_departments,&nbsp;name=&#8221;department&#8221;)(x)<br>#&nbsp;Instantiate&nbsp;an&nbsp;end-to-end&nbsp;model&nbsp;predicting&nbsp;both&nbsp;priority&nbsp;and&nbsp;departmentmodel&nbsp;=&nbsp;keras.Model(&nbsp;&nbsp;&nbsp;&nbsp;inputs=[title_input,&nbsp;body_input,&nbsp;tags_input],&nbsp;&nbsp;&nbsp;&nbsp;outputs=[priority_pred,&nbsp;department_pred],)\n\n<\/p>\n\n\n\n<p>You can build this model in a few lines with the functional API:<\/p>\n","protected":false},"excerpt":{"rendered":"<p>https:\/\/colab.research.google.com\/drive\/1ZNLC7QMG0Y2Zf4evf6JG_L0rybzmscoj#scrollTo=lwm64LEEt5NX Models with multiple inputs and outputs The functional API makes it easy to manipulate multiple inputs and outputs. This cannot be handled with the&nbsp;Sequential&nbsp;API. For example, if you&#8217;re building a system for ranking customer issue tickets by priority and routing them to the correct department, then the model will have three inputs: the title [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/laserphotonics.uk\/index.php?rest_route=\/wp\/v2\/posts\/398"}],"collection":[{"href":"https:\/\/laserphotonics.uk\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/laserphotonics.uk\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/laserphotonics.uk\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/laserphotonics.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=398"}],"version-history":[{"count":1,"href":"https:\/\/laserphotonics.uk\/index.php?rest_route=\/wp\/v2\/posts\/398\/revisions"}],"predecessor-version":[{"id":399,"href":"https:\/\/laserphotonics.uk\/index.php?rest_route=\/wp\/v2\/posts\/398\/revisions\/399"}],"wp:attachment":[{"href":"https:\/\/laserphotonics.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=398"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/laserphotonics.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=398"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/laserphotonics.uk\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=398"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}