# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + [markdown] id="view-in-github" colab_type="text"
# <a href="https://colab.research.google.com/github/VincentK16/15-minute-apps/blob/master/Working_with_the_Basic_Building_Blocks_of_the_Keras_API.ipynb" target="_parent"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/></a>

# + id="NOJei8t7O0AN" colab_type="code" colab={}
from sklearn.model_selection import train_test_split 
from sklearn.preprocessing import LabelBinarizer 
from tensorflow.keras import Input 
from tensorflow.keras.datasets import mnist 
from tensorflow.keras.layers import Dense 
from tensorflow.keras.models import Model 
from tensorflow.keras.models import Sequential

# + id="heB1liYuPOsp" colab_type="code" colab={}
layers = [Dense(256, input_shape=(28 * 28 * 1,), activation='sigmoid'), Dense(128, activation='sigmoid'), Dense(10, activation='softmax')] 
sequential_model_list = Sequential(layers)

# + id="HqqnZQChPZRw" colab_type="code" colab={}
sequential_model = Sequential() 
sequential_model.add(Dense(256, input_shape=(28 * 28 * 1,), activation='sigmoid')) 
sequential_model.add(Dense(128, activation='sigmoid')) 
sequential_model.add(Dense(10, activation='softmax'))

# + id="cMNXw68APjPa" colab_type="code" colab={}
input_layer = Input(shape=(28 * 28 * 1,)) 
dense_1 = Dense(256, activation='sigmoid')(input_layer) 
dense_2 = Dense(128, activation='sigmoid')(dense_1) 
predictions = Dense(10, activation='softmax')(dense_2) 
functional_model = Model(inputs=input_layer, outputs=predictions)


# + id="5MmNOcVhPxVX" colab_type="code" colab={}
class ClassModel(Model):
  def __init__(self):
    super(ClassModel, self).__init__() 
    self.dense_1 = Dense(256, activation='sigmoid') 
    self.dense_2 = Dense(256, activation='sigmoid') 
    self.predictions = Dense(10, activation='softmax') 
    
  def call(self, inputs, **kwargs): 
    x = self.dense_1(inputs) 
    x = self.dense_2(x) 
    return self.predictions(x) 
class_model = ClassModel()

# + id="TYzFcWvVQfDq" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 51} outputId="843119c0-51fd-4f26-cc5b-56380c1c8a2d"
(X_train, y_train), (X_test, y_test) = mnist.load_data() 
X_train = X_train.reshape((X_train.shape[0], 28 * 28 * 1)) 
X_test = X_test.reshape((X_test.shape[0], 28 * 28 * 1)) 
X_train = X_train.astype('float32') / 255.0 
X_test = X_test.astype('float32') / 255.0

# + id="_29veag5Qsd_" colab_type="code" colab={}
label_binarizer = LabelBinarizer() 
y_train = label_binarizer.fit_transform(y_train) 
y_test = label_binarizer.fit_transform(y_test)

# + id="VEJuso1XQxvs" colab_type="code" colab={}
X_train, X_valid, y_train, y_valid = train_test_split(X_train, y_train, train_size=0.8)

# + id="BeOorUL4Q61R" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 357} outputId="33fc3e46-4274-4370-ef21-5b97d4bab902"
models = { 
    'sequential_model': sequential_model, 
    'sequential_model_list': sequential_model_list, 
    'functional_model': functional_model, 
    'class_model': class_model 
    } 

for name, model in models.items(): 
  print(f'Compiling model: {name}') 
  model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) 
  print(f'Training model: {name}') 
  model.fit(X_train, y_train, validation_data=(X_valid, y_valid), epochs=50, batch_size=256, verbose=0) 
  accuracy = model.evaluate(X_test, y_test, verbose=0) 
  print(f'Testing model: {name}. \nAccuracy: {accuracy}') 
  print('---')

StarcoderdataJupyterScriptsDedupFiltered