<jupyter_start><jupyter_text>Stacking RNNs<jupyter_code># Only have to specify input_shape at the initial layer
# Single scalar output
timesteps = 100
features = 3
model = Sequential()
model.add(LSTM(64, input_shape=(timesteps, features), return_sequences=True))
model.add(LSTM(64))
model.add(Dense(1))

# Scalar output every timestep
model2 = Sequential()
model2.add(LSTM(64, input_shape=(timesteps, features), return_sequences=True))
model2.add(LSTM(64, return_sequences=True),)
model2.add(TimeDistributed(Dense(1)))
model.summary()
model2.summary()<jupyter_output>_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
lstm_11 (LSTM)               (None, 100, 64)           17408     
_________________________________________________________________
lstm_12 (LSTM)               (None, 100, 64)           33024     
_________________________________________________________________
time_distributed_2 (TimeDist (None, 100, 1)            65        
=================================================================
Total params: 50,497.0
Trainable params: 50,497
Non-trainable params: 0.0
_________________________________________________________________<jupyter_text>Unequal length sequences<jupyter_code># Pad sequences with zeros to have equal sizes
padded_sequences = pad_sequences(list_of_sequences)

# Add a masking layer
model = Sequential()
model.add(Masking(mask_value=0., input_shape=(timesteps, features)))
model.add(LSTM(32))<jupyter_output><empty_output><jupyter_text>Keras in Tensorflow* tf.contrib.keras  One-to-many<jupyter_code>from IPython.display import Image
Image(filename="/src/keras/Gerome/01_main/95_keras_practice/one_to_many.png", height=200, width=200) 
# This model will encode an image into a vector.
vision_model = Sequential()
vision_model.add(Conv2D(64, (3, 3), activation='relu', padding='same', input_shape=(224, 224, 3)))
vision_model.add(Conv2D(64, (3, 3), activation='relu'))
vision_model.add(MaxPooling2D((2, 2)))
vision_model.add(Conv2D(128, (3, 3), activation='relu', padding='same'))
vision_model.add(Conv2D(128, (3, 3), activation='relu'))
vision_model.add(MaxPooling2D((2, 2)))
vision_model.add(Conv2D(256, (3, 3), activation='relu', padding='same'))
vision_model.add(Conv2D(256, (3, 3), activation='relu'))
vision_model.add(Conv2D(256, (3, 3), activation='relu'))
vision_model.add(MaxPooling2D((2, 2)))
vision_model.add(Flatten())
vision_model.add(Dense(4096, activation='relu'))

# Now let's get a tensor with the output of our vision model:
image_input = Input(shape=(224, 224, 3))
encoded_image = vision_model(image_input)
repeat_encoded_img = RepeatVector(100)(encoded_image)
# Sentence encoding model
sentence_input = Input(shape=(100,), dtype='int32')
embedded_question = Embedding(input_dim=10000, output_dim=256, input_length=100)(sentence_input)
encoded_question = LSTM(256, return_sequences=True)(embedded_question)
# Merge both image features and sentences
merged = concatenate([encoded_question, repeat_encoded_img])
lstm_layer = LSTM(256, return_sequences=True)(merged)

# And let's train a logistic regression over 1000 words on top:
output = TimeDistributed(Dense(1000, activation='softmax'))(lstm_layer)

# This is our final model:
image_captioning_model = Model(inputs=[image_input, sentence_input], outputs=output)
model_img_path = '/src/keras/Gerome/01_main/95_keras_practice/RNN_image_captioning_model.png'
plot_model(image_captioning_model, to_file=model_img_path, show_shapes=True)
Image(model_img_path)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.151440329218107, 'fraction_numerical': 0.04938271604938271, 'mean_word_length': 7.518691588785047, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, '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': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '11434', 'license': 'MIT', 'n_tokens_mistral': 1272, 'n_tokens_neox': 1124, 'n_words': 234, 'path': 'RNN_Extras.ipynb', 'repo_name': 'gjvivar/deep_learning', 'size': '3645'}