Introduction
The softmax output layer helps a model pick one choice from many by turning numbers into probabilities that add up to 1.
Jump into concepts and practice - no test required
model.add(tf.keras.layers.Dense(number_of_classes, activation='softmax'))model.add(tf.keras.layers.Dense(3, activation='softmax'))
model = tf.keras.Sequential([
tf.keras.layers.Dense(10, activation='softmax')
])import tensorflow as tf import numpy as np # Create a simple model with softmax output for 3 classes model = tf.keras.Sequential([ tf.keras.layers.Dense(5, activation='relu', input_shape=(4,)), tf.keras.layers.Dense(3, activation='softmax') ]) # Compile the model with loss and optimizer model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy']) # Create dummy data: 6 samples, 4 features each x_train = np.array([[1, 2, 3, 4], [4, 3, 2, 1], [1, 0, 1, 0], [0, 1, 0, 1], [2, 2, 2, 2], [3, 3, 3, 3]], dtype=np.float32) # Labels for 3 classes (0, 1, or 2) y_train = np.array([0, 1, 2, 1, 0, 2], dtype=np.int32) # Train the model for 5 epochs history = model.fit(x_train, y_train, epochs=5, verbose=0) # Predict probabilities for a new sample sample = np.array([[1, 2, 3, 4]], dtype=np.float32) prediction = model.predict(sample) print('Predicted probabilities:', prediction) print('Sum of probabilities:', prediction.sum())
softmax output layer in a TensorFlow model?import tensorflow as tf import numpy as np logits = tf.constant([[2.0, 1.0, 0.1]]) softmax_output = tf.nn.softmax(logits) print(np.round(softmax_output.numpy(), 3))
model = tf.keras.Sequential([ tf.keras.layers.Dense(10, activation='relu'), tf.keras.layers.Dense(1, activation='softmax') ])
[0.1, 0.7, 0.1, 0.1] for a sample. Which class will the model predict and why?