<jupyter_start><jupyter_text>Multiple Inputs and OutputsIn order to explore multiple inputs and outputs in the Keras Functional API let's try to predict how many retweets and likes a news headline will receive on Twitter. The main input to the model will be the headline itself, as a sequence of words. Our model will also have an additional input, receiving data such as the time of day when the headline was posted, etc.<jupyter_code>import keras
from keras.layers import Input, Embedding, LSTM, Dense
from keras.models import Model
import numpy as np

np.random.seed(23)

# Note that we can name any layer by passing it a "name" argument.
main_input = Input(shape=(100,), dtype='int32', name='main_input')

# This embedding layer will encode the input sequence into a sequence of dense 512-dimensional vectors.
x = Embedding(output_dim=512, input_dim=10000, input_length=100)(main_input)

lstm_out = LSTM(32)(x)
#Let's define the additional inputs and outputs
addtl_output = Dense(1, activation='sigmoid', name='addtl_output')(lstm_out)
addtl_input = Input(shape=(5,), name='addtl_input')
x = keras.layers.concatenate([lstm_out, addtl_input])

# Let's add a couple of layers
x = Dense(64, activation='relu')(x)
x = Dense(64, activation='relu')(x)

# And finally we add the main logistic regression layer
main_output = Dense(1, activation='sigmoid', name='main_output')(x)
model = Model(inputs=[main_input, addtl_input], outputs=[main_output, addtl_output])
model.compile(optimizer='rmsprop', loss='binary_crossentropy',
              loss_weights=[1., 0.2])<jupyter_output>W1110 19:01:44.499846   860 deprecation_wrapper.py:119] From C:\Users\jdeha\Anaconda3\envs\keras\lib\site-packages\keras\optimizers.py:790: The name tf.train.Optimizer is deprecated. Please use tf.compat.v1.train.Optimizer instead.

W1110 19:01:44.522175   860 deprecation_wrapper.py:119] From C:\Users\jdeha\Anaconda3\envs\keras\lib\site-packages\keras\backend\tensorflow_backend.py:3376: The name tf.log is deprecated. Please use tf.math.log instead.

W1110 19:01:44.526175   860 deprecation.py:323] From C:\Users\jdeha\Anaconda3\envs\keras\lib\site-packages\tensorflow\python\ops\nn_impl.py:180: add_dispatch_support.<locals>.wrapper (from tensorflow.python.ops.array_ops) is deprecated and will be removed in a future version.
Instructions for updating:
Use tf.where in 2.0, which has the same broadcast rule as np.where<jupyter_text>Let's simulate some data to test this out<jupyter_code>headline_data = np.round(np.abs(np.random.rand(12, 100) * 100))
additional_data = np.random.randn(12, 5)
headline_labels = np.random.randn(12, 1)
additional_labels = np.random.randn(12, 1)
model.fit([headline_data, additional_data], [headline_labels, additional_labels], epochs=10, batch_size=32)
model.predict({'main_input': headline_data, 'addtl_input': additional_data})<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10967966573816156, 'fraction_numerical': 0.04735376044568245, 'mean_word_length': 6.0763546798029555, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '279341', 'license': 'MIT', 'n_tokens_mistral': 1049, 'n_tokens_neox': 944, 'n_words': 268, 'path': 'Section 3/CODE/section3_video4.ipynb', 'repo_name': 'PacktPublishing/Keras-Tips-Tricks-and-Techniques', 'size': '2872'}