<jupyter_start><jupyter_text>Table of Contents1&nbsp;&nbsp;Dataset2&nbsp;&nbsp;Numerical Model3&nbsp;&nbsp;LSTM Model4&nbsp;&nbsp;Merge5&nbsp;&nbsp;Plot  MergeHow to merge multiple models *Note: Merge function has been deprecated since Keras Vers 2.2.0*- https://github.com/keras-team/keras/issues/7417<jupyter_code>import keras 
# from numpy import array
import numpy as np 
from keras.preprocessing.sequence import pad_sequences
from keras.models import Sequential
from keras.layers import Flatten
from keras.layers.embeddings import Embedding
from keras.preprocessing.text import Tokenizer
from keras.layers import LSTM
from keras.layers import Dense, Dropout, BatchNormalization
from keras.layers.merge import Concatenate, Add
print(keras.__version__)

print('done')<jupyter_output>2.2.0
done<jupyter_text>Dataset<jupyter_code># define documents
docs = ['Well done!',
        'Good work',
        'Great effort',
        'nice work',
        'Excellent!',
        'Weak',
        'Poor effort!',
        'not good',
        'poor work',
        'Could have done better.']

# numerical feature 
feature = [45,332,35,325,36,-34,5,-344,-11,-644]

# define class labels
labels = np.array([1,1,1,1,1,0,0,0,0,0])

print(len(labels))<jupyter_output>10<jupyter_text>Numerical Model<jupyter_code>model = Sequential()
model.add(Dense(16, activation='relu', input_shape=(1,)))
model.add(Dense(16, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
model.summary()
model.fit(feature, labels,
            epochs=5,
            verbose=1
           )
score = model.evaluate(feature, labels, verbose=1)
print('Accuracy:', score[1])<jupyter_output>_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense_1 (Dense)              (None, 16)                32        
_________________________________________________________________
dense_2 (Dense)              (None, 16)                272       
_________________________________________________________________
dense_3 (Dense)              (None, 1)                 17        
=================================================================
Total params: 321
Trainable params: 321
Non-trainable params: 0
_________________________________________________________________
Epoch 1/5
10/10 [==============================] - 0s 20ms/step - loss: 3.6234 - acc: 0.5000
Epoch 2/5
10/10 [==============================] - 0s 138us/step - loss: 3.3093 - acc: 0.5000
Epoch 3/5
10/10 [==============================] - 0s 157us/step - loss: 2.9960 - acc: 0.[...]<jupyter_text>LSTM Model<jupyter_code>t = Tokenizer()
t.fit_on_texts(docs)
vocab_size = len(t.word_index) + 1

# integer encode the documents
encoded_docs = t.texts_to_sequences(docs)
print(encoded_docs[:10])
# pad documents to a max length of 4 words
max_length = 4
padded_docs = pad_sequences(encoded_docs, maxlen=max_length, padding='post')
print(padded_docs)
train_lstm = padded_docs.reshape(-1,4,1)
model2 = Sequential()
model2.add(LSTM(16, input_shape=(4,1), return_sequences=False))

model2.add(Dense(1, activation='sigmoid'))
model2.compile(loss='binary_crossentropy', 
              optimizer='adam', 
              metrics=['accuracy'])

print(model2.summary())

model2.fit(train_lstm, labels, 
            epochs=5,
            verbose=1)

loss, accuracy = model2.evaluate(train_lstm, labels, verbose=1)
print('Accuracy: %f' % (accuracy*100))<jupyter_output>_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
lstm_1 (LSTM)                (None, 16)                1152      
_________________________________________________________________
dense_4 (Dense)              (None, 1)                 17        
=================================================================
Total params: 1,169
Trainable params: 1,169
Non-trainable params: 0
_________________________________________________________________
None
Epoch 1/5
10/10 [==============================] - 1s 53ms/step - loss: 0.6824 - acc: 0.5000
Epoch 2/5
10/10 [==============================] - 0s 467us/step - loss: 0.6807 - acc: 0.5000
Epoch 3/5
10/10 [==============================] - 0s 469us/step - loss: 0.6785 - acc: 0.5000
Epoch 4/5
10/10 [==============================] - 0s 522us/step - loss: 0.6764 - acc: 0.5000
Epoch 5/5
10/10 [=======[...]<jupyter_text>Merge<jupyter_code>conc = Concatenate(axis=1)([model.output, model2.output])
merged = BatchNormalization()(conc)
merged = Dense(1, activation='sigmoid')(merged)

big_model = Model(inputs=[model.input,model2.input], outputs=merged)
big_model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
big_model.summary()
big_model.fit([feature, train_lstm], labels,epochs =5,verbose=1) 

loss, accuracy = big_model.evaluate([feature,train_lstm], labels, verbose=1)
print('Accuracy: %f' % (accuracy*100))<jupyter_output>__________________________________________________________________________________________________
Layer (type)                    Output Shape         Param #     Connected to                     
==================================================================================================
dense_1_input (InputLayer)      (None, 1)            0                                            
__________________________________________________________________________________________________
dense_1 (Dense)                 (None, 16)           32          dense_1_input[0][0]              
__________________________________________________________________________________________________
lstm_1_input (InputLayer)       (None, 4, 1)         0                                            
__________________________________________________________________________________________________
dense_2 (Dense)                 (None, 16)           272         dense_1[0][0]                    
__________[...]<jupyter_text>Plot<jupyter_code>from keras.utils import plot_model
# plot_model(big_model, to_file='model.png')

from IPython.display import SVG
from keras.utils.vis_utils import model_to_dot
SVG(model_to_dot(big_model).create(prog='dot', format='svg'))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.19022498060512025, 'fraction_numerical': 0.04685802948021722, 'mean_word_length': 4.063629222309505, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 20, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '66606', 'license': 'MIT', 'n_tokens_mistral': 2000, 'n_tokens_neox': 1752, 'n_words': 371, 'path': '*Deep_Learning/*Keras/Merge_Models/Merge.ipynb', 'repo_name': 'jasjung/Machine_Learnin', 'size': '6445'}