官术网_书友最值得收藏!

How to do it...

The strategy defined above is coded as follows (please refer to Issue_with_image translation.ipynb file in GitHub while implementing the code)

  1. Download the dataset and extract the train and test MNIST datasets:
from keras.datasets import mnist
from keras.layers import Flatten, Dense
from keras.models import Sequential
import matplotlib.pyplot as plt
%matplotlib inline
(X_train, y_train), (X_test, y_test) = mnist.load_data()
  1. Fetch the training set corresponding to label 1 only:
X_train1 = X_train[y_train==1]
  1. Reshape and normalize the original training dataset:
num_pixels = X_train.shape[1] * X_train.shape[2]
X_train = X_train.reshape(X_train.shape[0],num_pixels).astype('float32')
X_test = X_test.reshape(X_test.shape[0],num_pixels).astype('float32')
X_train = X_train / 255
X_test = X_test / 255
  1. One-hot-encode the output labels:
y_train = np_utils.to_categorical(y_train)
y_test = np_utils.to_categorical(y_test)
num_classes = y_train.shape[1]
  1. Build a model and fit it:
model = Sequential()
model.add(Dense(1000, input_dim=num_pixels, activation='relu'))
model.add(Dense(num_classes, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam',metrics=['accuracy'])
model.fit(X_train, y_train, validation_data=(X_test, y_test),epochs=5, batch_size=1024, verbose=1)
  1. Let's plot the average 1 image that we obtained in step 2:
pic=np.zeros((28,28))
pic2=np.copy(pic)
for i in range(X_train1.shape[0]):
pic2=X_train1[i,:,:]
pic=pic+pic2
pic=(pic/X_train1.shape[0])
plt.imshow(pic)

In the preceding code, we initialized an empty picture that is 28 x 28 in dimension and took an average pixel value at the various pixel locations of images that have a label of 1 (the X_train1 object) by looping through all the values in the X_train1 object.

The plot of the average 1 image appears as follows:

It is to be noted that the more yellow (thick) the pixel is, the more often people have written on top of the pixel, and the less yellow (more blue/less thick) the pixel, the less often people have written on top of the pixel. Also, it is to be noted that the pixel in the middle is the yellowest/thickest (this is because most people would be writing over the middle pixels, irrespective of whether the whole digit is written in a vertical line or is slanted toward the left or right).

主站蜘蛛池模板: 陇川县| 图片| 荥阳市| 北海市| 城市| 吴旗县| 玉屏| 霞浦县| 南投市| 中西区| 松桃| 金平| 古丈县| 土默特右旗| 建德市| 陕西省| 巧家县| 屏边| 石景山区| 泰州市| 广德县| 高阳县| 大理市| 上林县| 南宁市| 神池县| 沛县| 定安县| 嫩江县| 望谟县| 哈巴河县| 瑞昌市| 闽清县| 景宁| 富阳市| 翼城县| 牟定县| 定南县| 京山县| 汕头市| 牙克石市|