In Keras there is a helpful way to define a model: using the functional API. With functional API you can define a directed acyclic graphs of layers, which lets you build completely arbitrary architectures. Considering your example:
#A_data = np.zeros((1,30))
#A_labels = np.zeros((1,30))
#B_labels =np.zeros((1,30))
A1 = layers.Input(shape=(30,), name='A_input')
A2 = layers.Dense(8, activation='???')(A1)
A3 = layers.Dense(30, activation='???', name='A_output')(A2)
B2 = layers.Dense(40, activation='???')(A2)
B3 = layers.Dense(30, activation='???', name='B_output')(B2)
## define A
A = models.Model(inputs=A1, outputs=A3)
## define B
B = models.Model(inputs=A1, outputs=B3)
B.compile(optimizer='??',
loss={'B_output': '??'}
)
B.fit({'A_input': A_data},
{'B_output': B_labels},
epochs=??, batch_size=??)
So, that's it! You can see the result by: B.summary()
:
Layer (type) Output Shape Param
A_input (InputLayer) (None, 30) 0
_________________________________________________________________
dense_8 (Dense) (None, 8) 248
______________________________________________________________
dense_9 (Dense) (None, 40) 360
_________________________________________________________________
B_output (Dense) (None, 30) 1230