• 15

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191


File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

Multiple outputs in Keras

I have a problem which deals with predicting two outputs when given a vector of predictors. Assume that a predictor vector looks like x1, y1, att1, att2, ..., attn, which says x1, y1 are coordinates and att's are the other attributes attached to the occurrence of x1, y1 coordinates. Based on this predictor set I want to predict x2, y2. This is a time series problem, which I am trying to solve using multiple regresssion. My question is how do I setup keras, which can give me 2 outputs in the final layer. I have solved simple regression problem in keras and the code is avaialable in my github.

from keras.models import Model
from keras.layers import *    

#inp is a "tensor", that can be passed when calling other layers to produce an output 
inp = Input((10,)) #supposing you have ten numeric values as input 

#here, SomeLayer() is defining a layer, 
#and calling it with (inp) produces the output tensor x
x = SomeLayer(blablabla)(inp) 
x = SomeOtherLayer(blablabla)(x) #here, I just replace x, because this intermediate output is not interesting to keep

#here, I want to keep the two different outputs for defining the model
#notice that both left and right are called with the same input x, creating a fork
out1 = LeftSideLastLayer(balbalba)(x)    
out2 = RightSideLastLayer(banblabala)(x)

#here, you define which path you will follow in the graph you've drawn with layers
#notice the two outputs passed in a list, telling the model I want it to have two outputs.
model = Model(inp, [out1,out2])
model.compile(optimizer = ...., loss = ....) #loss can be one for both sides or a list with different loss functions for out1 and out2    

model.fit(inputData,[outputYLeft, outputYRight], epochs=..., batch_size=...)
  • 42
Reply Report
    • Added comments to my answer :) -- You cannot create branches with a sequential model, it's simply not possible.
      • 1
    • @Daniel Hi Daniel, could you expand on that? What I'm looking for is to have a network that attempts to predict two different things and so I was picturing a branch happening at my penultimate layer which feeds into two different softmax layers, I then concatenate the results of those two layers and then backpropogate with respect to that. Is this not possible in keras?
      • 2
    • If you know the true values for both sides, you don't need to concatenate them. The model will do everything automatically. (The only reasons I can think of to concatenate both branches are: 1 - your true data is already concatenated; 2 - you want to add further layers taking that as input).
    • So if I understand you correctly then what you mean is: InputShape = (10, ) model_1 = Sequential() model_1.add(Dense(250, activation='tanh', input_shape=(InputShape))) model_1.add(Dense(2, activation='relu')) model_1.compile(optimizer='adam', loss='mse', metrics=['accuracy']) model_1.fit(predictors, targets, epochs=whatever, ....) . My question is how is this different than yours, where you are specifying two outputs exclusively.

Trending Tags