Hands-On Neural Networks with Keras
上QQ阅读APP看书,第一时间看更新

Checking the dimensions

Next, we need to check out what our data looks like. We will do this by checking its type, then shape, and finally by plotting our individual observations using matplotlib.pyplot, like so:

type(x_train[0]),x_train.shape,y_train.shape

You will get the following result:

(numpy.ndarray, (60000, 28, 28), (60000,))

Plotting the points:

import matplotlib.-pyplot as plt
%matplotlib inline
plt.show(x_train[0], cmap= plt.cm.binary)
<matplotlib.image.AxesImage at 0x24b7f0fa3c8>

This will plot a figure similar to what's shown in the following screenshot:

As we can see, our training set has 60,000 images, with each image represented by a 28 x 28 matrix. When we represent our whole dataset, we are just representing a tensor of three dimensions (60,000 x 28 x 28). Now, let's rescale our pixel values, which usually lie between 0 and 225. Rescaling these values to values between 0 and 1 makes it a lot easier for our network to perform computations and learn predictive features. We encourage you to carry out experiments with and without normalization so that you can assess the difference in predictive power:

x_train=keras.utils.normalize(x_train, axis=1)
x_test=keras.utils.normalize(x_test, axis=1)
plt.imshow(x_train[0], cmap=plt.cm.binary)

The preceding code generates the following output:

<matplotlib.image.AxesImage at 0x24b00003e48>

The following plot is acquired: