(value, param_name, save_dir)
| 1620 | from matplotlib.colors import LinearSegmentedColormap |
| 1621 | |
| 1622 | def plot_distribution(value, param_name, save_dir): |
| 1623 | tensor_np = value.flatten().detach().cpu().numpy() |
| 1624 | min_val, max_val = tensor_np.min(), tensor_np.max() |
| 1625 | |
| 1626 | nice_blue = '#4878CF' # Brighter blue |
| 1627 | |
| 1628 | plt.figure(figsize=(6, 4.5), dpi=100) |
| 1629 | |
| 1630 | # Use more bins for a smoother histogram |
| 1631 | n, bins, patches = plt.hist(tensor_np, bins=50, density=False, alpha=0.85, |
| 1632 | color=nice_blue, edgecolor='none') |
| 1633 | |
| 1634 | # Add grid lines but place them behind the chart |
| 1635 | plt.grid(alpha=0.3, linestyle='--', axis='y') |
| 1636 | plt.gca().set_axisbelow(True) |
| 1637 | |
| 1638 | # Use scientific notation for y-axis ticks |
| 1639 | plt.gca().yaxis.set_major_formatter(ticker.ScalarFormatter(useMathText=True)) |
| 1640 | plt.gca().ticklabel_format(style='sci', axis='y', scilimits=(0,0)) |
| 1641 | |
| 1642 | # Improved annotations for minimum and maximum values, smaller size |
| 1643 | plt.annotate(f'Min: {min_val:.2f}', xy=(min_val, 0), xytext=(min_val, max(n) * 0.1), |
| 1644 | arrowprops=dict(facecolor='green', width=1.5, headwidth=6, headlength=6, shrink=0.05), |
| 1645 | fontsize=8, color='darkgreen', weight='bold', |
| 1646 | bbox=dict(boxstyle="round,pad=0.1", fc="white", ec="green", alpha=0.7)) |
| 1647 | |
| 1648 | plt.annotate(f'Max: {max_val:.2f}', xy=(max_val, 0), xytext=(max_val, max(n) * 0.1), |
| 1649 | arrowprops=dict(facecolor='red', width=1.5, headwidth=6, headlength=6, shrink=0.05), |
| 1650 | fontsize=8, color='darkred', weight='bold', |
| 1651 | bbox=dict(boxstyle="round,pad=0.1", fc="white", ec="red", alpha=0.7)) |
| 1652 | |
| 1653 | # Beautify title and labels |
| 1654 | plt.title(f'{param_name} Distribution') |
| 1655 | plt.xlabel('Value') |
| 1656 | plt.ylabel('Frequency') |
| 1657 | |
| 1658 | # Adjust x and y axis ranges to leave enough space for annotations |
| 1659 | plt.xlim(min_val - (max_val - min_val) * 0.05, max_val + (max_val - min_val) * 0.05) |
| 1660 | plt.ylim(0, max(n) * 1.2) |
| 1661 | |
| 1662 | plt.tight_layout() |
| 1663 | plt.savefig(os.path.join(save_dir, f'{param_name}.png'), dpi=120, bbox_inches='tight') |
| 1664 | plt.close() |
| 1665 | |
| 1666 | os.makedirs(save_dir, exist_ok=True) |
| 1667 | for param_name, value in param_dict.items(): |
nothing calls this directly
no outgoing calls
no test coverage detected