Scaling

import numpy as np
import matplotlib.pyplot as plt

# Creating an 8-element numpy vector with random gaussian values
# vector = np.random.randn(8)
vector = np.array([0.17148779, 0.04137234, 0.07904297, 0.06154607, 0.19238579,
       0.22156468, 0.11528585, 0.1173145 ])
vector = vector * 100

# Softmax function
def softmax(x):
    e_x = np.exp(x - np.max(x)) # Stability improvement by subtracting the max
    return e_x / e_x.sum()

# Applying softmax to the vector
softmax_vector = softmax(vector)
softmax_vector

array([6.30317583e-03, 1.40837150e-08, 6.09160734e-07, 1.05889083e-07,
       5.09504507e-02, 9.42694822e-01, 2.28423184e-05, 2.79797178e-05])
# Plotting both the original vector and the softmax-transformed vector
plt.figure(figsize=(12, 6))

# Plot for the original vector
plt.subplot(1, 2, 1)
plt.bar(range(len(vector)), vector, color='blue')
plt.title("Original Vector")
plt.xlabel("Index")
plt.ylabel("Value")

# Plot for the softmax-transformed vector
plt.subplot(1, 2, 2)
plt.bar(range(len(softmax_vector)), softmax_vector, color='green')
plt.title("Softmax-Transformed Vector")
plt.xlabel("Index")
plt.ylabel("Probability")

plt.tight_layout()
plt.show()

Back to top