@@ -13,30 +13,30 @@ Plotting Samples and Filters |
13 | 13 | .. _here: http://deeplearning.net/tutorial/code/utils.py |
14 | 14 |
|
15 | 15 |
|
16 | | -To plot a sample, what we need to do is to take the visible units, which |
| 16 | +To plot a sample, what we need to do is to take the visible units, which |
17 | 17 | are a flattened image (there is no 2D structure to the visible units, |
18 | 18 | just a 1D string of unit activations) and reshape it into a 2D image. The order in |
19 | | -which the points from the 1D array go into the 2D image is given by the |
| 19 | +which the points from the 1D array go into the 2D image is given by the |
20 | 20 | order in which the inital MNIST images where converted into a 1D array. |
21 | 21 | Lucky for us this is just a call of the ``numpy.reshape`` function. |
22 | 22 |
|
23 | 23 | Plotting the weights is a bit more tricky. We have ``n_hidden`` hidden |
24 | | -units, each of them corresponding to a column of the weight matrix. A |
25 | | -column has the same shape as the visible, where the weight corresponding |
| 24 | +units, each of them corresponding to a column of the weight matrix. A |
| 25 | +column has the same shape as the visible, where the weight corresponding |
26 | 26 | to the connection with visible unit `j` is at position `j`. Therefore, |
27 | 27 | if we reshape every such column, using ``numpy.reshape``, we get a |
28 | | -filter image that tells us how this hidden unit is influenced by |
| 28 | +filter image that tells us how this hidden unit is influenced by |
29 | 29 | the input image. |
30 | 30 |
|
31 | | -We need a utility function that takes a minibatch, or the weight matrix, |
32 | | -and converts each row ( for the weight matrix we do a transpose ) into a |
| 31 | +We need a utility function that takes a minibatch, or the weight matrix, |
| 32 | +and converts each row ( for the weight matrix we do a transpose ) into a |
33 | 33 | 2D image and then tile these images together. Once we converted the |
34 | | -minibatch or the weights in this image of tiles, we can use PIL to plot |
35 | | -and save. `PIL <http://www.pythonware.com/products/pil/>`_ is a standard |
| 34 | +minibatch or the weights in this image of tiles, we can use PIL to plot |
| 35 | +and save. `PIL <http://www.pythonware.com/products/pil/>`_ is a standard |
36 | 36 | python libarary to deal with images. |
37 | 37 |
|
38 | | -Tiling minibatches together is done for us by the |
39 | | -``tile_raster_image`` function which we provide here. |
| 38 | +Tiling minibatches together is done for us by the |
| 39 | +``tile_raster_image`` function which we provide here. |
40 | 40 |
|
41 | 41 | .. code-block:: python |
42 | 42 |
|
@@ -49,18 +49,18 @@ Tiling minibatches together is done for us by the |
49 | 49 | return ndar |
50 | 50 |
|
51 | 51 |
|
52 | | - def tile_raster_images(X, img_shape, tile_shape, tile_spacing=(0, 0), |
| 52 | + def tile_raster_images(X, img_shape, tile_shape, tile_spacing=(0, 0), |
53 | 53 | scale_rows_to_unit_interval=True, |
54 | 54 | output_pixel_vals=True): |
55 | 55 | """ |
56 | | - Transform an array with one flattened image per row, into an array in |
| 56 | + Transform an array with one flattened image per row, into an array in |
57 | 57 | which images are reshaped and layed out like tiles on a floor. |
58 | 58 |
|
59 | | - This function is useful for visualizing datasets whose rows are images, |
60 | | - and also columns of matrices for transforming those rows |
| 59 | + This function is useful for visualizing datasets whose rows are images, |
| 60 | + and also columns of matrices for transforming those rows |
61 | 61 | (such as the first layer of a neural net). |
62 | 62 |
|
63 | | - :type X: a 2-D ndarray or a tuple of 4 channels, elements of which can |
| 63 | + :type X: a 2-D ndarray or a tuple of 4 channels, elements of which can |
64 | 64 | be 2-D ndarrays or None; |
65 | 65 | :param X: a 2-D array in which every row is a flattened image. |
66 | 66 |
|
@@ -69,38 +69,38 @@ Tiling minibatches together is done for us by the |
69 | 69 |
|
70 | 70 | :type tile_shape: tuple; (rows, cols) |
71 | 71 | :param tile_shape: the number of images to tile (rows, cols) |
72 | | - |
| 72 | + |
73 | 73 | :param output_pixel_vals: if output should be pixel values (i.e. int8 |
74 | 74 | values) or floats |
75 | 75 |
|
76 | 76 | :param scale_rows_to_unit_interval: if the values need to be scaled before |
77 | 77 | being plotted to [0,1] or not |
78 | 78 |
|
79 | 79 |
|
80 | | - :returns: array suitable for viewing as an image. |
| 80 | + :returns: array suitable for viewing as an image. |
81 | 81 | (See:`PIL.Image.fromarray`.) |
82 | 82 | :rtype: a 2-d array with same dtype as X. |
83 | 83 |
|
84 | 84 | """ |
85 | | - |
| 85 | + |
86 | 86 | assert len(img_shape) == 2 |
87 | 87 | assert len(tile_shape) == 2 |
88 | 88 | assert len(tile_spacing) == 2 |
89 | 89 |
|
90 | | - # The expression below can be re-written in a more C style as |
91 | | - # follows : |
| 90 | + # The expression below can be re-written in a more C style as |
| 91 | + # follows : |
92 | 92 | # |
93 | 93 | # out_shape = [0,0] |
94 | 94 | # out_shape[0] = (img_shape[0] + tile_spacing[0]) * tile_shape[0] - |
95 | 95 | # tile_spacing[0] |
96 | 96 | # out_shape[1] = (img_shape[1] + tile_spacing[1]) * tile_shape[1] - |
97 | 97 | # tile_spacing[1] |
98 | | - out_shape = [(ishp + tsp) * tshp - tsp for ishp, tshp, tsp |
| 98 | + out_shape = [(ishp + tsp) * tshp - tsp for ishp, tshp, tsp |
99 | 99 | in zip(img_shape, tile_shape, tile_spacing)] |
100 | 100 |
|
101 | 101 | if isinstance(X, tuple): |
102 | 102 | assert len(X) == 4 |
103 | | - # Create an output numpy ndarray to store the image |
| 103 | + # Create an output numpy ndarray to store the image |
104 | 104 | if output_pixel_vals: |
105 | 105 | out_array = numpy.zeros((out_shape[0], out_shape[1], 4), dtype='uint8') |
106 | 106 | else: |
@@ -114,19 +114,19 @@ Tiling minibatches together is done for us by the |
114 | 114 |
|
115 | 115 | for i in xrange(4): |
116 | 116 | if X[i] is None: |
117 | | - # if channel is None, fill it with zeros of the correct |
| 117 | + # if channel is None, fill it with zeros of the correct |
118 | 118 | # dtype |
119 | 119 | out_array[:, :, i] = numpy.zeros(out_shape, |
120 | 120 | dtype='uint8' if output_pixel_vals else out_array.dtype |
121 | 121 | ) + channel_defaults[i] |
122 | 122 | else: |
123 | | - # use a recurrent call to compute the channel and store it |
| 123 | + # use a recurrent call to compute the channel and store it |
124 | 124 | # in the output |
125 | 125 | out_array[:, :, i] = tile_raster_images(X[i], img_shape, tile_shape, tile_spacing, scale_rows_to_unit_interval, output_pixel_vals) |
126 | 126 | return out_array |
127 | 127 |
|
128 | 128 | else: |
129 | | - # if we are dealing with only one channel |
| 129 | + # if we are dealing with only one channel |
130 | 130 | H, W = img_shape |
131 | 131 | Hs, Ws = tile_spacing |
132 | 132 |
|
@@ -138,13 +138,13 @@ Tiling minibatches together is done for us by the |
138 | 138 | for tile_col in xrange(tile_shape[1]): |
139 | 139 | if tile_row * tile_shape[1] + tile_col < X.shape[0]: |
140 | 140 | if scale_rows_to_unit_interval: |
141 | | - # if we should scale values to be between 0 and 1 |
| 141 | + # if we should scale values to be between 0 and 1 |
142 | 142 | # do this by calling the `scale_to_unit_interval` |
143 | 143 | # function |
144 | 144 | this_img = scale_to_unit_interval(X[tile_row * tile_shape[1] + tile_col].reshape(img_shape)) |
145 | 145 | else: |
146 | 146 | this_img = X[tile_row * tile_shape[1] + tile_col].reshape(img_shape) |
147 | | - # add the slice to the corresponding position in the |
| 147 | + # add the slice to the corresponding position in the |
148 | 148 | # output array |
149 | 149 | out_array[ |
150 | 150 | tile_row * (H+Hs): tile_row * (H + Hs) + H, |
|