import tensorflow as tf from tensorflow import keras model = keras.Sequential([ keras.layers.Flatten(input_shape=(28, 28)), keras.layers.Dense(128, activation='relu'), keras.layers.Dense(10, activation='softmax') ])
import tensorflow as tf from tensorflow import keras mnist = keras.datasets.mnist (train_images, train_labels), (test_images, test_labels) = mnist.load_data() train_images = train_images / 255.0 test_images = test_images / 255.0 model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy']) model.fit(train_images, train_labels, epochs=10) test_loss, test_acc = model.evaluate(test_images, test_labels) print('Test accuracy:', test_acc)In this example, the sequential model is trained on the MNIST dataset for digit recognition. The dataset is preprocessed by scaling the pixel values to the range of [0, 1]. The model is compiled with Adam optimizer, sparse categorical crossentropy loss function, and accuracy metric. The model is trained for 10 epochs using the fit method. Finally, the test set is evaluated using the evaluate method, and the accuracy is printed out. The package library used in the above examples is tensorflow.keras.