import tensorflow as tf from tensorflow.keras import layers # Define the model architecture model = tf.keras.Sequential([ layers.Flatten(input_shape=(28, 28)), layers.Dense(128, activation='relu'), layers.Dense(10) ]) # Compile the model model.compile(optimizer='adam', loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True), metrics=['accuracy']) # Train the model model.fit(x_train, y_train, epochs=10, validation_data=(x_test, y_test)) # Evaluate the model test_loss, test_acc = model.evaluate(x_test, y_test, verbose=2) print("Test accuracy:", test_acc)
import tensorflow as tf from tensorflow.keras import layers # Define the model architecture model = tf.keras.Sequential([ layers.Dense(64, activation='relu', input_shape=[13]), layers.Dense(1) ]) # Compile the model model.compile(loss='mean_absolute_error', optimizer=tf.keras.optimizers.Adam(0.001)) # Train the model model.fit(X_train, y_train, epochs=100, validation_split=0.2) # Evaluate the model test_loss = model.evaluate(X_test, y_test, verbose=2) print("Test loss:", test_loss)In this code, we define a model with one `Dense` layer and an output layer. We compile the model with `mean_absolute_error` loss and the `Adam` optimizer, and train the model for 100 epochs. Finally, we evaluate the model on the test data and print the test loss. Package library: TensorFlow