Created
November 22, 2017 05:44
-
-
Save akshaychawla/02849170e190fbd7fa9d431450e8d6ef to your computer and use it in GitHub Desktop.
Lambda layer with multiple inputs in Keras.
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 numpy as np | |
from keras.models import Model | |
from keras.layers import Dense, Activation, Lambda, Input | |
import keras.backend as K | |
from keras.utils import to_categorical | |
# Model definition | |
def foo(ip): | |
a = ip[1] | |
x = ip[0] | |
b = ip[2] | |
return a*x + b | |
a = Input(shape=(1,)) | |
b = Input(shape=(1,)) | |
ip = Input(shape=(784,)) | |
x = Dense(32, activation="relu", input_dim=784)(ip) | |
x = Lambda(foo)([x, a, b]) # Important: You can give list of inputs to Lambda layer | |
x = Dense(10, activation="softmax")(x) | |
model = Model(inputs=[ip, a, b], outputs=x) | |
model.compile(optimizer="adam", loss="categorical_crossentropy", metrics=["accuracy"]) | |
# Generating Some data | |
x_s = np.random.randn(1000, 784) | |
a_s = np.ones((1000,1)) | |
b_s = np.zeros((1000,1)) | |
y_s = np.random.randint(0, 10, size=(1000,)) | |
y_s = to_categorical(y_s, num_classes=10) | |
# Train | |
model.fit( [x_s, a_s, b_s] , y_s, batch_size=64 , epochs=5) | |
import ipdb; ipdb.set_trace() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment