Created
September 16, 2021 11:01
-
-
Save arshren/46af84b9fe7e15bd4fe0e030f9d3da42 to your computer and use it in GitHub Desktop.
Code snippet for building model using Keras Functional API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Import required libraries | |
import tensorflow as tf | |
from keras.datasets import mnist | |
#Creating the MNIST Dataset | |
(x_train, y_train), (x_test, y_test) = mnist.load_data() | |
x_train, x_test= x_train/255.0, x_test/255.0 | |
x_train=x_train[...,tf.newaxis].astype('float32') | |
x_test=x_test[...,tf.newaxis].astype('float32') | |
#Building the Keras model using Functional API | |
inputs = tf.keras.Input(shape=(28,28,1)) | |
x=tf.keras.layers.Conv2D(32,3, activation='relu')(inputs) | |
x=tf.keras.layers.Flatten()(x) | |
x=tf.keras.layers.Dense(128, activation='relu')(x) | |
output=tf.keras.layers.Dense(10, activation='softmax')(x) | |
model_fapi= tf.keras.Model(inputs=inputs, outputs=output, name="mnist_model") | |
# compile the mode; | |
model_fapi.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy']) | |
# Fit the model on MNIST dataset | |
model_fapi.fit(x_train, y_train, epochs=10) | |
#Evaluate the model on the test data | |
test_loss, test_acc=model_fapi.evaluate(x_test, y_test) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment