<jupyter_start><jupyter_text>Keras Recap<jupyter_code>from keras.datasets import cifar10
from keras.models import Sequential
from keras.layers.core import Dense, Activation
from keras.optimizers import SGD
from keras.utils import np_utils

import numpy as np
%matplotlib inline
import matplotlib.pyplot as plt
(X_train_t, y_train), (X_test_t, y_test) = cifar10.load_data()
X_train_t.shape
32*32*3
X_train = X_train_t.reshape(len(X_train_t), 32*32*3).astype('float32') / 255
X_test = X_test_t.reshape(len(X_test_t), 32*32*3).astype('float32') / 255

y_train_1hot = np_utils.to_categorical(y_train, 10)
y_test_1hot = np_utils.to_categorical(y_test, 10)

print(X_train.shape)
print(X_test.shape)
print(y_train_1hot.shape)
print(y_test_1hot.shape)
for i in range(1, 5):
    plt.subplot(1, 4, i)
    plt.imshow(X_train[i].reshape(32, 32, 3))
    plt.title(np.argmax(y_train_1hot[i]))
model = Sequential()
model.add(Dense(1024, input_dim=3072, activation='relu'))
model.add(Dense(512, activation='relu'))
model.add(Dense(10, activation='softmax'))
model.compile(loss='categorical_crossentropy',
              optimizer='rmsprop',
              metrics=['accuracy'])

model.fit(X_train, y_train_1hot, batch_size=128,
          epochs=10, verbose=1,
          validation_split=0.1)

# Final test evaluation
score = model.evaluate(X_test, y_test_1hot, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])<jupyter_output><empty_output><jupyter_text>Exercise 1: Keras functional APIWe'e built a model using the `Sequential API` from Keras. Keras also offers a [functional API](https://keras.io/getting-started/functional-api-guide/). This API is the way to go for defining complex models, such as multi-output models, directed acyclic graphs, or models with shared layers.Can you rewrite the model above using the functional API?  Exercise 2: Convolutional RecapThe above model is a very simple fully connected deep neural network. As we have seen, Convolutional Neural Networks are much more powerful when dealing with images. The original data has shape:    (N_images, Height, Width, Channels)    1. Can you write a convolutional model using the sequential API?2. Can you rewrite it using the functional API?<jupyter_code>X_train_t.shape
X_train_t = X_train_t.astype('float32') / 255
X_test_t = X_test_t.astype('float32') / 255<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10970464135021098, 'fraction_numerical': 0.03670886075949367, 'mean_word_length': 5.660112359550562, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 1, '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': '641992', 'license': 'MIT', 'n_tokens_mistral': 900, 'n_tokens_neox': 833, 'n_words': 215, 'path': 'labs/Lab_04_DL Keras Recap.ipynb', 'repo_name': 'Dataweekends/global_AI_conference_Jan_2018', 'size': '2370'}