import tensorflow as tf from tensorflow.keras.models import Model from tensorflow.keras.layers import Input, Dense # Creating a dummy model input_layer = Input(shape=(784,)) hidden_layer = Dense(256, activation='relu')(input_layer) output_layer = Dense(10, activation='softmax')(hidden_layer) model = Model(inputs=input_layer, outputs=output_layer) # Compiling the model model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
import tensorflow as tf from tensorflow.keras.models import Model from tensorflow.keras.layers import Input, Conv2D, MaxPooling2D, Flatten, Dense # Creating a CNN model input_layer = Input(shape=(28,28,1)) conv1 = Conv2D(32, (3,3), activation='relu')(input_layer) pool1 = MaxPooling2D((2,2))(conv1) flatten = Flatten()(pool1) dense1 = Dense(64, activation='relu')(flatten) output_layer = Dense(10, activation='softmax')(dense1) model = Model(inputs=input_layer, outputs=output_layer) # Compiling the model model.compile(optimizer=tf.keras.optimizers.SGD(), loss=tf.keras.losses.SparseCategoricalCrossentropy(), metrics=tf.keras.metrics.SparseCategoricalAccuracy())In this example, we create a convolutional neural network (CNN) with one convolutional layer followed by max pooling, a fully connected layer, and an output layer that predicts probabilities of 10 classes. We then compile the model with the stochastic gradient descent (SGD) optimizer, sparse categorical cross-entropy loss function, and sparse categorical accuracy as a performance metric. The package library being used in these examples is `tensorflow`.