简体   繁体   中英

Functional API Linking Feed-Forward Networks and Convolutional neural network

Right now I have two networks f and g, the first trained on task 1 and the second on task 2. I labelled my data as either beloning to task 1 or to task 2. How can I build the following (trainable) custom architecture:

x -> decide if 1 or 2 -> pass to f or g accordingly?

I've never used such a branched architecture before...

I tried to demonstrate what you need with a Sample Code shown below. Please let me know if this is not what you are looking for and give more details, and I will be Happy to help you.

As per the question, we are trying to achieve 2 Tasks, Task 1 --> Regression (Feedforward Neural Networks) and Task 2 --> CNN . We shall form 2 Datasets from the existing Dataset based on the Label, whether it belongs to Task 1 --> Data_T1 and Task 2 --> Data_T2 .

Then using Functional API, we can pass Multiple Inputs and we can get Multiple Outputs .

Code is shown below:

from tensorflow.keras.models import Model
from tensorflow.keras.layers import Input, Conv2D, Dense, Flatten
import pandas as pd

F1 = [1,2,3,4,5,6,7,8,9,10]
F2 = [1,2,3,4,5,6,7,8,9,10]
F3 = [1,2,3,4,5,6,7,8,9,10]
Task = ['t1', 't1', 't2', 't1', 't2', 't2', 't2', 't1', 't1', 't2']

Dict = {'F1': F1, 'F2':F2, 'F3':F3, 'Task':Task} # Column Task tells us whether the Data belongs to Task1 or Task2

Data = pd.DataFrame(Dict) #Create a Dummy Data Frame

Data_T1 = Data[Data['Task']=='t1']
Data_T1 = Data_T1.drop(columns = ['Task'])

Data_T2 = Data[Data['Task']=='t2']
Data_T2 = Data_T2.drop(columns = ['Task'])

Input1 = ...
Input2 = ...

Number_Of_Classes = 3
# Regression Model
D1 = Dense(10, activation = 'relu')(Input1)
Out_Task1 = Dense(1, activation = 'linear') 
# CNN Model
Conv1 = Conv2D(16, (3,3), activation = 'relu')(Input2)
Conv2 = Conv2D(32, (3,3, activation = 'relu'))(Conv1)
Flatten = Flatten()(Conv2)
D2_1 = Dense(10, activation = 'relu')
Out_Task2 = Dense(Number_Of_Classes, activation = 'softmax')

model = Model(inputs = [Input1, Input2], outputs = [Out_Task1, Out_Task2])

model.compile....

model.fit([Data_T1, Data_T2], .....)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM