Last active
June 23, 2018 19:10
-
-
Save DavidRdgz/cde4adfb68cf6cc176a389e7f9eba633 to your computer and use it in GitHub Desktop.
Really small example (multi-class) mixture of experts model, almost. Technically, belief_per_model function needs to assign probabilities based on a function.
This file contains 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
from keras.models import Model | |
from keras.layers import Input, Lambda, Dense | |
from keras.utils import to_categorical | |
from numpy.random import randint | |
import numpy as np | |
def belief_per_model(x): | |
x1, x2, x3, x4 = x | |
return x1 * .2 + x2 * .3 + x3 * .4 + x4 * .1 | |
def my_model(): | |
inputs = Input(shape=(20,)) | |
m1 = Dense(64)(inputs) | |
m2 = Dense(64)(inputs) | |
m3 = Dense(64)(inputs) | |
m4 = Dense(64)(inputs) | |
mixture = Lambda( | |
belief_per_model, output_shape=(64,) | |
)([m1, m2, m3, m4]) | |
output = Dense( | |
10, activation='softmax' | |
)(mixture) | |
model = Model([inputs], output) | |
model.compile( | |
loss='categorical_crossentropy', | |
optimizer="sgd" | |
) | |
return model | |
def test_data(): | |
x = np.random.random((1000, 20)) | |
y = to_categorical( | |
randint(10, size=(1000, 1)), | |
num_classes=10 | |
) | |
return y, x | |
if __name__ == '__main__': | |
y_train, x_train = test_data() | |
model = my_model() | |
model.fit([x_train], y_train) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment