Skip to content

Instantly share code, notes, and snippets.

@nbro
Created January 31, 2020 12:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nbro/30832c777b6ae6c67b980abb1bd74ea4 to your computer and use it in GitHub Desktop.
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.
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