Created
January 14, 2022 10:23
-
-
Save rameshKrSah/da4e08ab962dae6d46b349859f5cef56 to your computer and use it in GitHub Desktop.
Multi-Input or Model Learning with TensorFlow
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tensorflow as tf | |
from tensorflow import keras | |
# Two inputs - One Output | |
# Image | |
input_1 = tf.keras.layers.Input(shape=(28, 28, 1)) | |
conv2d_1 = tf.keras.layers.Conv2D(64, kernel_size=3, | |
activation=tf.keras.activations.relu)(input_1) | |
# Second conv layer : | |
conv2d_2 = tf.keras.layers.Conv2D(32, kernel_size=3, | |
activation=tf.keras.activations.relu)(conv2d_1) | |
# Flatten layer : | |
flatten = tf.keras.layers.Flatten()(conv2d_2) | |
# The other input | |
input_2 = tf.keras.layers.Input(shape=(1,)) | |
dense_2 = tf.keras.layers.Dense(5, activation=tf.keras.activations.relu)(input_2) | |
# Concatenate | |
concat = tf.keras.layers.Concatenate()([flatten, dense_2]) | |
n_classes = 4 | |
# output layer | |
output = tf.keras.layers.Dense(units=n_classes, | |
activation=tf.keras.activations.softmax)(concat) | |
full_model = tf.keras.Model(inputs=[input_1, input_2], outputs=[output]) | |
print(full_model.summary()) | |
# Merging Two Models | |
from keras.layers import Input, Dense | |
from keras.models import Model | |
from keras.utils import plot_model | |
A1 = Input(shape=(30,),name='A1') | |
A2 = Dense(8, activation='relu',name='A2')(A1) | |
A3 = Dense(30, activation='relu',name='A3')(A2) | |
B2 = Dense(40, activation='relu',name='B2')(A2) | |
B3 = Dense(30, activation='relu',name='B3')(B2) | |
merged = Model(inputs=[A1],outputs=[A3,B3]) | |
merged.summary() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment