Clustermap

Neural Network Weights

Layer weight visualization with activation clustering

Output
Neural Network Weights
Python
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np
from matplotlib.colors import LinearSegmentedColormap

np.random.seed(42)

# Simulated neural network layer weights
input_neurons = [f'In_{i}' for i in range(16)]
hidden_neurons = [f'H_{i}' for i in range(12)]

# Xavier-like initialization with some structure
weights = np.random.randn(16, 12) * np.sqrt(2.0 / 16)
# Add learned patterns
weights[:4, :3] += 0.5
weights[4:8, 3:6] -= 0.5
weights[8:12, 6:9] += 0.3
weights[12:, 9:] -= 0.3

df = pd.DataFrame(weights, index=input_neurons, columns=hidden_neurons)

# NEON diverging colormap
neon_weights = LinearSegmentedColormap.from_list('neon_w', ['#27D3F5', '#0a0a0f', '#F5276C'])

g = sns.clustermap(df, cmap=neon_weights, center=0, 
                   method='ward', metric='euclidean',
                   linewidths=0.2, linecolor='#1a1a2e',
                   figsize=(8, 7), dendrogram_ratio=(0.12, 0.12),
                   cbar_pos=(0.01, 0.08, 0.008, 0.12),
                   tree_kws={'linewidths': 1.5, 'colors': '#27D3F5'})

g.fig.patch.set_facecolor('#0a0a0f')
g.ax_heatmap.set_facecolor('#0a0a0f')
g.ax_heatmap.tick_params(colors='white', labelsize=8)
g.ax_heatmap.set_xlabel('Hidden Neurons', color='white', fontsize=10)
g.ax_heatmap.set_ylabel('Input Neurons', color='white', fontsize=10)

for ax in [g.ax_row_dendrogram, g.ax_col_dendrogram]:
    ax.set_facecolor('#0a0a0f')

g.fig.suptitle('Neural Network Weight Matrix', color='white', fontsize=14, fontweight='bold', y=1.02)


plt.show()
Library

Matplotlib

Category

Heatmaps & Density

Did this help you?

Support PyLucid to keep it free & growing

Support