<jupyter_start><jupyter_text>![title](network_flowchart.png)<jupyter_code>#Layer 1
filter_size1 = 5
num_filter1  =16
# Layer 2
filter_size2 = 5
num_filter2  =36
#Fully connected
num_neuron =  128
num_channels = 1
# We know that MNIST images are 28 pixels in each dimension.
img_size = 28

# Images are stored in one-dimensional arrays of this length.
img_size_flat = img_size * img_size

# Tuple with height and width of images used to reshape arrays.
img_shape = (img_size, img_size)

# Number of colour channels for the images: 1 channel for gray-scale.
num_channels = 1

# Number of classes, one class for each of 10 digits.
num_classes = 10
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
def plot_images(images, cls_true, cls_pred=None):
    assert len(images) == len(cls_true) == 9
    
    # Create figure with 3x3 sub-plots.
    fig, axes = plt.subplots(3, 3)
    fig.subplots_adjust(hspace=0.3, wspace=0.3)

    for i, ax in enumerate(axes.flat):
        # Plot image.
        ax.imshow(images[i].reshape(img_shape), cmap='binary')

        # Show true and predicted classes.
        if cls_pred is None:
            xlabel = "True: {0}".format(cls_true[i])
        else:
            xlabel = "True: {0}, Pred: {1}".format(cls_true[i], cls_pred[i])

        # Show the classes as the label on the x-axis.
        ax.set_xlabel(xlabel)
        
        # Remove ticks from the plot.
        ax.set_xticks([])
        ax.set_yticks([])
    
    # Ensure the plot is shown correctly with multiple plots
    # in a single Notebook cell.
    plt.show()
def plot_kernels(tensor, num_cols=6):
    if not tensor.ndim==4:
        raise Exception("assumes a 4D tensor")
    if not tensor.shape[-1]==3:
        raise Exception("last dim needs to be 3 to plot")
    num_kernels = tensor.shape[0]
    num_rows = 1+ num_kernels // num_cols
    fig = plt.figure(figsize=(num_cols,num_rows))
    for i in range(tensor.shape[0]):
        ax1 = fig.add_subplot(num_rows,num_cols,i+1)
        ax1.imshow(tensor[i])
        ax1.axis('off')
        ax1.set_xticklabels([])
        ax1.set_yticklabels([])

    plt.subplots_adjust(wspace=0.1, hspace=0.1)
    plt.show()
examples = enumerate(test_loader)
batch_idx, (example_data, example_targets) = next(examples)
example_data.shape
plot_images(example_data[0:9].permute(0,2,3,1).cpu().numpy(),example_targets[0:9].cpu().numpy())
example_data.permute(0,2,3,1).shape
class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.conv1 = nn.Conv2d(1, num_filter1, kernel_size=filter_size1,padding=2)
        self.conv2 = nn.Conv2d(num_filter1, num_filter2, kernel_size=filter_size2,padding=2)
        self.relu  = nn.ReLU()
        self.fc1 = nn.Linear(1764, num_neuron)
        self.fc2 = nn.Linear(num_neuron, 10)

    def forward(self, x):
        x=self.conv1(x)
        x= F.max_pool2d(x,2)
        w1=self.relu(x)
        x = self.conv2(w1)
        x=self.relu(x)
        w2=F.max_pool2d(x,2)
        #pdb.set_trace()
        x = w2.view(-1, 1764)
        x = F.relu(self.fc1(x))
        x = self.fc2(x)
        return (x,w1,w2)
momentum=0.9
model = Net().to(device)
optimizer = optim.SGD(model.parameters(), lr=learning_rate,
                      momentum=momentum)
criterion = nn.CrossEntropyLoss()
def plot_image(image):
    plt.imshow(image.reshape(img_shape),
               interpolation='nearest',
               cmap='binary')

    plt.show()
image1 = example_data[0].permute(1,2,0).cpu().numpy()
plot_image(image1)
outputs = model(example_data[0].view(1,1,28,28))
outputs[1].shape
def plot_conv_weights(weights, input_channel=0):
    w=weights
    w_min = np.min(w)
    w_max = np.max(w)
    #shape =[filter_size,filter_size,num_channels,num_filters]
    # Number of filters used in the conv. layer.
    num_filters = w.shape[3]
    print(num_filters)
    # Number of grids to plot.
    # Create figure with a grid of sub-plots.
    num_grids = math.ceil(math.sqrt(num_filters))
    fig, axes = plt.subplots(num_grids, num_grids)

    # Plot all the filter-weights.
    for i, ax in enumerate(axes.flat):
        # Only plot the valid filter-weights.
        if i<num_filters:
            # Get the weights for the i'th filter of the input channel.
            # See new_conv_layer() for details on the format
            # of this 4-dim tensor.
            img = w[:, :, input_channel, i]

            # Plot image.
            ax.imshow(img, vmin=w_min, vmax=w_max,
                      interpolation='nearest', cmap='seismic')
        
        # Remove ticks from the plot.
        ax.set_xticks([])
        ax.set_yticks([])
    
    # Ensure the plot is shown correctly with multiple plots
    # in a single Notebook cell.
    plt.show()
plot_conv_weights((outputs[1].permute(2,3,0,1).detach().cpu().numpy()))
mm = model.float()
filters = mm.modules
body_model = [i for i in mm.children()]

layer1 = body_model[1]
tensor = layer1.weight.permute(2,3,1,0).data.cpu().numpy()
plot_conv_weights(tensor)
model.train()

total_step = len(train_loader)
for epoch in range(num_epochs):
    for i, (images, labels) in enumerate(train_loader):
        images = images.to(device)
        labels = labels.to(device)
        
        # Forward pass
        outputs = model(images)
        loss = criterion(outputs[0], labels)
        
        # Backward and optimize
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
        
        if (i+1) % 100 == 0:
            print ('Epoch [{}/{}], Step [{}/{}], Loss: {:.4f}' 
                   .format(epoch+1, num_epochs, i+1, total_step, loss.item()))
# Split the test-set into smaller batches of this size.
test_batch_size = batch_size
model.eval() 
def print_test_accuracy(show_example_errors=False,
                        show_confusion_matrix=False):

    num_test = test_dataset.data.cpu().numpy().shape[0]
    cls_pred = np.zeros(shape=num_test, dtype=np.int)
    cls_true = np.zeros(shape=num_test, dtype=np.int)
    i = 0
    with torch.no_grad():
        correct = 0
        total = 0
        for images, labels in test_loader:
            images = images.to(device)
            labels = labels.to(device)
            outputs = model(images)
            _, predicted = torch.max(outputs[0].data, 1)
            total += labels.size(0)
            #pdb.set_trace()
            correct += (predicted == labels).sum().item()
            # The ending index for the next batch is denoted j.
            j = min(i + test_batch_size, num_test)

            # Calculate the predicted class using TensorFlow.
            cls_pred[i:j] = predicted.detach().cpu().numpy()
            cls_true[i:j] =labels.detach().cpu().numpy()
            # Set the start-index for the next batch to the
            # end-index of the current batch.
            i = j

    # Convenience variable for the true class-numbers of the test-set.


    # Create a boolean array whether each image is correctly classified.
    correct = (cls_true == cls_pred)

    # Calculate the number of correctly classified images.
    # When summing a boolean array, False means 0 and True means 1.
    correct_sum = correct.sum()

    # Classification accuracy is the number of correctly classified
    # images divided by the total number of images in the test-set.
    acc = float(correct_sum) / num_test

    # Print the accuracy.
    msg = "Accuracy on Test-Set: {0:.1%} ({1} / {2})"
    print(msg.format(acc, correct_sum, num_test))

    # Plot some examples of mis-classifications, if desired.
    if show_example_errors:
        print("Example errors:")
        plot_example_errors(cls_pred=cls_pred, correct=correct)

    # Plot the confusion matrix, if desired.
    if show_confusion_matrix:
        print("Confusion Matrix:")
        plot_confusion_matrix(cls_pred=cls_pred, cls_true=cls_true)
def plot_example_errors(cls_pred, correct):
    incorrect = (correct == False)
    images = test_dataset.data.cpu().numpy()[incorrect]
    cls_pred = cls_pred[incorrect]
    cls_true = test_dataset.targets.cpu().numpy()[incorrect]
    plot_images(images=images[0:9],
                cls_true=cls_true[0:9],
                cls_pred=cls_pred[0:9])
test_dataset.targets.cpu().numpy().shape
def plot_confusion_matrix(cls_pred,cls_true):
    cm = confusion_matrix(y_true=cls_true,
                          y_pred=cls_pred)
    print(cm)
print_test_accuracy(show_example_errors=True,show_confusion_matrix=True)


# Save the model checkpoint
torch.save(model.state_dict(), 'model.ckpt')<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup