Thanks, sure here it is:

import numpy as np
import torch
import torchvision
from torchvision import transforms, datasets
from torch.utils.data import DataLoader
import torch.nn as nn
def softmax(x):
exp_x = torch.exp(x)
sum_x = torch.sum(exp_x, dim=1, keepdim=True)
return exp_x/sum_x

def log_softmax(x):
return x - torch.logsumexp(x,dim=1, keepdim=True)
def CrossEntropyLoss(outputs, targets):
num_examples = targets.shape[0]
batch_size = outputs.shape[0]
outputs = log_softmax(outputs)
outputs = outputs[range(batch_size), targets]
return - torch.sum(outputs)/num_examples

def my_model(x):
bs = x.shape[0]
return x.reshape(bs, input_dim) @ w_init + b
#FashionMNIST Datasets for training/test
trans = transforms.Compose([transforms.ToTensor()])
trn_ds = datasets.FashionMNIST(’./training’, download=True, transform=trans)
test_ds = datasets.FashionMNIST(’./test’, train=False, download=True, transform=trans)
#Dataloader for training/test
trn_dl = DataLoader(trn_ds, batch_size=64, shuffle=True)
test_dl = DataLoader(test_ds, batch_size=64)
#paramaters initialization
input_dim = 784 # 28x28 FashionMNIST data
output_dim = 10
w_init = np.random.normal(scale=0.05, size=(input_dim,output_dim))
w_init = torch.tensor(w_init, requires_grad=True).float()
b = torch.zeros(output_dim)
#pytorch CrossEntropyLoss
criterion = nn.CrossEntropyLoss()
for X, y in trn_dl:
outputs = my_model(X)
my_outputs = softmax(outputs)
my_ce = CrossEntropyLoss(my_outputs, y)
pytorch_ce = criterion(outputs, y)

print (f'my custom cross entropy: {my_ce.item()}\npytorch cross entroopy: {pytorch_ce.item()}')

break