22.02.2024 Views

Daniel Voigt Godoy - Deep Learning with PyTorch Step-by-Step A Beginner’s Guide-leanpub

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

StepByStep Method

@staticmethod

def _visualize_tensors(axs, x, y=None, yhat=None,

layer_name='', title=None):

# The number of images is the number of subplots in a row

n_images = len(axs)

# Gets max and min values for scaling the grayscale

minv, maxv = np.min(x[:n_images]), np.max(x[:n_images])

# For each image

for j, image in enumerate(x[:n_images]):

ax = axs[j]

# Sets title, labels, and removes ticks

if title is not None:

ax.set_title(f'{title} #{j}', fontsize=12)

shp = np.atleast_2d(image).shape

ax.set_ylabel(

f'{layer_name}\n{shp[0]}x{shp[1]}',

rotation=0, labelpad=40

)

xlabel1 = '' if y is None else f'\nLabel: {y[j]}'

xlabel2 = '' if yhat is None else f'\nPredicted: {yhat[j]}'

xlabel = f'{xlabel1}{xlabel2}'

if len(xlabel):

ax.set_xlabel(xlabel, fontsize=12)

ax.set_xticks([])

ax.set_yticks([])

# Plots weight as an image

ax.imshow(

np.atleast_2d(image.squeeze()),

cmap='gray',

vmin=minv,

vmax=maxv

)

return

setattr(StepByStep, '_visualize_tensors', _visualize_tensors)

"What is that @staticmethod thingy above the method’s definition?"

Visualizing Filters and More! | 389

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!