@@ -13,30 +13,30 @@ Plotting Samples and Filters
1313.. _here: http://deeplearning.net/tutorial/code/utils.py
1414
1515
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
1717are a flattened image (there is no 2D structure to the visible units,
1818just 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
2020order in which the inital MNIST images where converted into a 1D array.
2121Lucky for us this is just a call of the ``numpy.reshape`` function.
2222
2323Plotting 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
2626to the connection with visible unit `j` is at position `j`. Therefore,
2727if 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
2929the input image.
3030
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
33332D 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
3636python libarary to deal with images.
3737
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.
4040
4141.. code-block:: python
4242
@@ -49,18 +49,18 @@ Tiling minibatches together is done for us by the
4949 return ndar
5050
5151
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),
5353 scale_rows_to_unit_interval=True,
5454 output_pixel_vals=True):
5555 """
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
5757 which images are reshaped and layed out like tiles on a floor.
5858
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
6161 (such as the first layer of a neural net).
6262
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
6464 be 2-D ndarrays or None;
6565 :param X: a 2-D array in which every row is a flattened image.
6666
@@ -69,38 +69,38 @@ Tiling minibatches together is done for us by the
6969
7070 :type tile_shape: tuple; (rows, cols)
7171 :param tile_shape: the number of images to tile (rows, cols)
72-
72+
7373 :param output_pixel_vals: if output should be pixel values (i.e. int8
7474 values) or floats
7575
7676 :param scale_rows_to_unit_interval: if the values need to be scaled before
7777 being plotted to [0,1] or not
7878
7979
80- :returns: array suitable for viewing as an image.
80+ :returns: array suitable for viewing as an image.
8181 (See:`PIL.Image.fromarray`.)
8282 :rtype: a 2-d array with same dtype as X.
8383
8484 """
85-
85+
8686 assert len(img_shape) == 2
8787 assert len(tile_shape) == 2
8888 assert len(tile_spacing) == 2
8989
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 :
9292 #
9393 # out_shape = [0,0]
9494 # out_shape[0] = (img_shape[0] + tile_spacing[0]) * tile_shape[0] -
9595 # tile_spacing[0]
9696 # out_shape[1] = (img_shape[1] + tile_spacing[1]) * tile_shape[1] -
9797 # 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
9999 in zip(img_shape, tile_shape, tile_spacing)]
100100
101101 if isinstance(X, tuple):
102102 assert len(X) == 4
103- # Create an output numpy ndarray to store the image
103+ # Create an output numpy ndarray to store the image
104104 if output_pixel_vals:
105105 out_array = numpy.zeros((out_shape[0], out_shape[1], 4), dtype='uint8')
106106 else:
@@ -114,19 +114,19 @@ Tiling minibatches together is done for us by the
114114
115115 for i in xrange(4):
116116 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
118118 # dtype
119119 out_array[:, :, i] = numpy.zeros(out_shape,
120120 dtype='uint8' if output_pixel_vals else out_array.dtype
121121 ) + channel_defaults[i]
122122 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
124124 # in the output
125125 out_array[:, :, i] = tile_raster_images(X[i], img_shape, tile_shape, tile_spacing, scale_rows_to_unit_interval, output_pixel_vals)
126126 return out_array
127127
128128 else:
129- # if we are dealing with only one channel
129+ # if we are dealing with only one channel
130130 H, W = img_shape
131131 Hs, Ws = tile_spacing
132132
@@ -138,13 +138,13 @@ Tiling minibatches together is done for us by the
138138 for tile_col in xrange(tile_shape[1]):
139139 if tile_row * tile_shape[1] + tile_col < X.shape[0]:
140140 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
142142 # do this by calling the `scale_to_unit_interval`
143143 # function
144144 this_img = scale_to_unit_interval(X[tile_row * tile_shape[1] + tile_col].reshape(img_shape))
145145 else:
146146 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
148148 # output array
149149 out_array[
150150 tile_row * (H+Hs): tile_row * (H + Hs) + H,
0 commit comments