Created
January 31, 2020 12:31
-
-
Save nbro/30832c777b6ae6c67b980abb1bd74ea4 to your computer and use it in GitHub Desktop.
In TF 2.1 (at least), you can create a model by passing to the outputs parameter a dictionary.
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
def example1(): | |
# TensorFlow 2.1 | |
import tensorflow as tf | |
import numpy as np | |
Input_1 = tf.keras.layers.Input(shape=(1,)) | |
x = tf.keras.layers.Dense(100, activation='relu')(Input_1) | |
targets = ["out1", "out2", "out3"] | |
outputs = {} | |
for target in targets: | |
out1 = tf.keras.layers.Dense(1, activation='linear', name=target)(x) | |
outputs[target] = out1 | |
model = tf.keras.models.Model(inputs=Input_1, outputs=outputs) | |
model.compile(optimizer="rmsprop", loss={target: "mse" for target in targets}, | |
metrics={target: ["mse", "accuracy"] for target in targets}) | |
data = np.array([[1], [2], [3], [4], [5]]) | |
predictions = model.predict(data) | |
if __name__ == '__main__': | |
example1() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment