# Number of Training Samplesprint(data_train.__len__())print(label_train.__len__())# Number of Test Samplesprint(data_test.__len__())print(label_test.__len__())
Matrix(data_train[0])
plt.figure()plt.subplot(121)plt.imshow(data_train[0])print("The label for image[0] is",label_train[0])plt.subplot(122)plt.imshow(data_train[59999])print("The label for image[59999] is",label_train[59999])plt.show()
# Convert numpy.ndarry data to tensor type in Pytorchdata_train=Variable(torch.FloatTensor(data_train))/255.0data_test=Variable(torch.FloatTensor(data_test))/255.0label_train=Variable(torch.LongTensor(label_train))label_test=Variable(torch.LongTensor(label_test))
# Convert a 28 × 28 Matrix into a 784 × 1 Vector data_train[0].reshape(1,-1).size()
forepochinrange(epochs):forbatchinrange(batches):# Get the input data matrix: dim = 100 × 784input_data=Variable(torch.FloatTensor(batch_size,col))forjinrange(batch_size):input_data[j]=data_train[j+batch_size*batch].reshape(1,-1)# Forward propagation: output_data has dim = 100 × 10output_data=DNN_Model.forward(input_data)# Compute cross entropy lossloss=loss_function(output_data,label_train[batch_size*batch:batch_size*(batch+1)])# backward propagationloss.backward()# update parametersoptimizer.step()# Reset grad to 0optimizer.zero_grad()# Save loss for this batchloss_list.append(loss)# Print details for the gradient descentif(epoch)%10==0and(batch+1)%1==0:print("epoch =",epoch+1,"; ","batch =",batch+1,":")print(" -> Now loss =",loss.item())print("-------------------------------------------------------------")adjust_learning_rate(optimizer,epoch)if(epoch)%10==0:print("*********************** Epoch",epoch+1,"Over **********************")print(" ")print(" ")ifloss<0.74:break
5. Visualization of the Cross Entropy Loss Function¶
plt.figure(figsize=(14,6))length=loss_list.__len__()print("The length of loss_list is:",length)plt.plot(np.arange(1,length+1,1),loss_list,"black")plt.xlabel("epoch")plt.ylabel("loss")plt.show()
6. Prediction on the Test Set and Model Evaluation¶
data_test.__len__()
plt.imshow(data_test[0])print("The label of this image is:",label_test[0])
pred_vec=DNN_Model.forward(data_test[0].reshape(1,-1))print("Prediction for data_test[0]:")Matrix(pred_vec.detach().numpy())
proba_distribution=F.softmax(DNN_Model.forward(data_test[0].reshape(1,-1)),dim=1)print("Probability distribution for the prediction of data_test[0]:")print(" ->The argmax of the probability distribution vector is:",torch.argmax(proba_distribution).detach().numpy())print(" ->Sum of the probability distribution vector is:",torch.sum(proba_distribution).detach().numpy())Matrix(proba_distribution.detach().numpy())