Python Forum

Full Version: Can someone explain this small snippet of code like I am a 5 year old?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello everyone,

I have been detached from Python for quite a while now, can someone explain this code to me as if I am a 5 year old?


print (output2.shape)    #Prints: "(1, 13, 13, 128)"
n_features = output2.shape[-1]    #Selects the last element of the list, 128 in this case.
size = output2.shape[1]    #Select the second element of the list, 13 in this case.
display_grid = np.zeros((size, size * n_features))
for i in range(n_features):    #Run the loop 128 times
    x  = output2[0, :, :, i]    #?
    x -= x.mean()    #Finds the mean on x and subtracts it from x
    x /= x.std ()    #?
    x *=  64    #Multiplies x by 64
    x += 128    #Adds 128 to x
    x  = np.clip(x, 0, 255).astype('uint8')    #?
    display_grid[:, i * size : (i + 1) * size] = x    #?
scale = 20. / n_features    #?
plt.figure( figsize=(scale * n_features, scale) )    #?
plt.title ( "conv2d" )    #?
plt.grid  ( False )    #?
#plt.imshow( display_grid, aspect='auto', cmap='viridis' )    #?
plt.imsave( "test.jpeg", display_grid, cmap='viridis' )    #?
I understood few of the simple lines, but I am not well versed with numpy and display_grid which is a part of matplotlib I guess.

Can someone kindly help me out?
Kind regards,
PythonNPC.
Did you write the comments? I should limit feedback to lines with #? ?

Is this sufficient (as an example):

x  = np.clip(x, 0, 255).astype('uint8')
clip(value, min, max) clips x to range 0, 255, If x > 255 it becomes 255. if x < 0 it becomes 0. clip() creates a new array which is returned.
array.astype(type) casts the contents of an array to the specified type. astype() creates a new array which is returned.
Used in combination this clips an array so all numbers are in the range 0..255 and converts their type to uint8 (8 bit unsigned integers.). The new results are assigned to x. The old x is no longer referenced, so it will be deleted.
x /= x.std ()
std(x) computes the standard deviation of x. x =/std(x) divides the x array by the standard deviation of x and assigns the result to x. The old x is no longer referenced, so it will be deleted.
The result is the new x is a standardized version of the old x. Each value in x is the standardized deviation of the old x value from the mean. The new x will have a mean of 0 and a standard deviation of 1.

My knowledge is pretty spotty on numpy indexing so I will leave this one alone:
display_grid[:, i * size : (i + 1) * size] = x
All of this stuff creates a plot using matplotlib and saves it to a file. Do you need a description of each of the lines? It might be more informative to just plot the image (uncomment imshow line or call show(plt.show()).
plt.figure( figsize=(scale * n_features, scale) )    #?
plt.title ( "conv2d" )    #?
plt.grid  ( False )    #?
#plt.imshow( display_grid, aspect='auto', cmap='viridis' )    #?
plt.imsave( "test.jpeg", display_grid, cmap='viridis' )    #?
Thanks! That was really helpful.

Any idea what this line means?

x  = output2[0, :, :, i]
I have no idea what those colons mean, sure, I get output2 is a list of x will get the value of the list but what do those colons represent?
This:
x  = output2[0, :, :, i]
is called slicing. It "slices" a 2D array out of a 4D array. I will try to explain with an example.

Here I make a 4D array with a shape(2, 3, 4, 5).
import numpy as np
x = np.array(range(120)).reshape(2, 3, 4, 5)
x[0] and x[1] are 3D arrays, each with a shape(3, 4, 5)
x[0][1] is a 2D arrays with a shape(4, 5).
x[1][2][3] is a 1D array with shape(5)
x[1][2][3][4] is a scalar that equals 119
Each of these are a slice, a part of the complete array that is sliced away. I can do the same thing with regular lists in Python, but Numpy has a super ginsu knife slicer. I can make arbitrary slices that don't line up with any of the dimensions in Numpy:
print(x[0].shape)
print(x[0, 1].shape)
print(x[1, 2, 3].shape)
print(x[1, 2, 3, 4].shape, x[1, 2, 3, 4])
Output:
(3, 4, 5) (4, 5) (5,) () 119
Numpy also lets me slice crosswise through multiple dimensions. This is where the ":" comes in. Think of ":" as a wildcard.
print(x[:, 0, 0, 0])
print(x[0, :, 0, 0])
print(x[0, 0, :, 0])
print(x[0, 0, 0, :])
Output:
[ 0 60] [ 0 20 40] [ 0 5 10 15] [0 1 2 3 4]
x[:, 0, 0, 0] returns a len 2 array because the first dimension is size 1. x[0, :, 0, 0] is len 3, x[0, 0, :, 0] len 4 and x[0, 0, 0, :] len 5.

Your example: x = output2[0, :, :, i] is making a 2D slice where the index of the first dimension is 0 and the index of the last dimension is i. I cannot say what kind of information this slice contains without knowing the contents of 4D array.