#%% import numpy as np import tensorflow as tf from tensorflow import keras from tensorflow.keras import layers from uci_data import * #%% Load and preprocess some UCI datasetdataset (training_set, test_set) = read_uci1("../uci_datasets", "pendigits") (training_inputs, training_labels) = training_set (test_inputs, test_labels) = test_set max_value = np.max(np.abs(training_inputs)) training_inputs = training_inputs / max_value test_inputs = test_inputs/ max_value input_shape = training_inputs[0].shape number_of_classes = np.max([np.max(training_labels), np.max(test_labels)]) + 1 #%% Defining a fully connected model using the Sequential() method model = tf.keras.Sequential([keras.Input(shape = input_shape), layers.Dense(30, activation='tanh'), layers.Dense(50, activation='tanh'), layers.Dense(number_of_classes, activation='softmax')]) model.compile(optimizer='adam', loss=tf.keras.losses.SparseCategoricalCrossentropy(), metrics=['accuracy']) # Training the model model.fit(training_inputs, training_labels, epochs=10) # Testing the model test_loss, test_acc = model.evaluate(test_inputs, test_labels, verbose=0) print('\nTest accuracy: %.2f%%' % (test_acc * 100)) #%% Defining the same fully connected model using the Layers API inputs = keras.Input(shape=input_shape) x1 = layers.Dense(30, activation='tanh')(inputs) x2 = layers.Dense(50, activation='tanh')(x1) outputs = layers.Dense(number_of_classes, activation="softmax")(x2) model = keras.Model(inputs, outputs) model.compile(optimizer='adam', loss=tf.keras.losses.SparseCategoricalCrossentropy(), metrics=['accuracy']) model.summary() # Training the model model.fit(training_inputs, training_labels, epochs=10) # Testing the model test_loss, test_acc = model.evaluate(test_inputs, test_labels, verbose=0) print('\nTest accuracy: %.2f%%' % (test_acc * 100)) #%% This is code that will give an error, because it does not specify # how to compute the model output based on the model input. inputs1 = keras.Input(shape=input_shape) inputs2 = keras.Input(shape=input_shape) x1 = layers.Dense(30, activation='tanh')(inputs1) x2 = layers.Dense(50, activation='tanh')(inputs2) outputs = layers.Dense(number_of_classes, activation="softmax")(x2) model = keras.Model(inputs1, outputs) model.compile(optimizer='adam', loss=tf.keras.losses.SparseCategoricalCrossentropy(), metrics=['accuracy']) model.summary() #%% This is an example of how to use the Functional API to create a model # that is not sequential, and that cannot be defined using Keras.Sequential(). inputs = keras.Input(shape=input_shape) x1 = layers.Dense(30, activation='tanh')(inputs) x2 = layers.Dense(50, activation='tanh')(x1) x3 = layers.Concatenate()((x1,x2)) outputs = layers.Dense(number_of_classes, activation="softmax")(x3) model = keras.Model(inputs, outputs) model.compile(optimizer='adam', loss=tf.keras.losses.SparseCategoricalCrossentropy(), metrics=['accuracy']) model.summary() # Training the model model.fit(training_inputs, training_labels, epochs=10) # Testing the model test_loss, test_acc = model.evaluate(test_inputs, test_labels, verbose=0) print('\nTest accuracy: %.2f%%' % (test_acc * 100)) #%% This is an example of how to use the Functional API to create a model # that uses some operations not supported by pre-defined Keras layers. inputs = keras.Input(shape=input_shape) x1 = layers.Dense(30, activation='tanh')(inputs) x2 = tf.square(x1) outputs = layers.Dense(number_of_classes, activation="softmax")(x1) model = keras.Model(inputs, outputs) model.compile(optimizer='adam', loss=tf.keras.losses.SparseCategoricalCrossentropy(), metrics=['accuracy']) model.summary() # Training the model model.fit(training_inputs, training_labels, epochs=10) # Testing the model test_loss, test_acc = model.evaluate(test_inputs, test_labels, verbose=0) print('\nTest accuracy: %.2f%%' % (test_acc * 100)) #%% Load and preprocess the MNIST dataset dataset = keras.datasets.mnist (x_train, train_labels), (x_test, test_labels) = dataset.load_data() number_of_classes = np.max([np.max(train_labels), np.max(test_labels)]) + 1 # Scale images to the [0, 1] range x_train = x_train.astype("float32") / 255 x_test = x_test.astype("float32") / 255 # Make sure images have shape (28, 28, 1) x_train = np.expand_dims(x_train, -1) x_test = np.expand_dims(x_test, -1) input_shape = x_train[0].shape #%% Defining a CNN model using the Sequential() method model = keras.Sequential([ keras.Input(shape=input_shape), layers.Conv2D(32, kernel_size=(3, 3), activation="relu"), layers.MaxPooling2D(pool_size=(2, 2)), layers.Conv2D(64, kernel_size=(3, 3), activation="relu"), layers.MaxPooling2D(pool_size=(2, 2)), layers.Flatten(), layers.Dropout(0.5), layers.Dense(number_of_classes, activation="softmax"), ]) model.summary() batch_size = 128 epochs = 15 model.compile(loss=keras.losses.SparseCategoricalCrossentropy(), optimizer="adam", metrics=["accuracy"]) model.fit(x_train, train_labels, epochs=epochs, batch_size=batch_size) test_loss, test_acc = model.evaluate(x_test, test_labels, verbose=2) print('\nTest accuracy: %.2f%%' % (test_acc * 100)) #%% Defining the same CNN model using the Layers API inputs = keras.Input(shape=input_shape) x = layers.Conv2D(32, kernel_size=(3, 3), activation="relu")(inputs) x = layers.MaxPooling2D(pool_size=(2, 2))(x) x = layers.Conv2D(64, kernel_size=(3, 3), activation="relu")(x) x = layers.MaxPooling2D(pool_size=(2, 2))(x) x = layers.Flatten()(x) x = layers.Dropout(0.5)(x) outputs = layers.Dense(number_of_classes, activation="softmax")(x) model = keras.Model(inputs, outputs) model.summary() batch_size = 128 epochs = 15 model.compile(loss=keras.losses.SparseCategoricalCrossentropy(), optimizer="adam", metrics=["accuracy"]) model.fit(x_train, train_labels, epochs=epochs, batch_size=batch_size) test_loss, test_acc = model.evaluate(x_test, test_labels, verbose=2) print('\nTest accuracy: %.2f%%' % (test_acc * 100)) #%%