# Training of classifier breaks when casting uint8 to float

Hello guys,
I was just playing around with a toy example on MNIST, but i was using a conv1D network. Achieved around 96% after 5 epochs. Then i wanted to implement my own normalization function, but had to cast the data to float() first because you cant use mean() on a bytetensor. Just calling .float() on the data caused the network to suddenly drop to 50% for some odd reason. Does anyone know what might be happening here? I have the 2 notebooks on collab:

``````
EPOCHS = 5
BS = 128
device = 'cuda:0' if torch.cuda.is_available() else 'cpu'
transform = transforms.Compose([transforms.ToTensor()])
#####
#mnist_train.data = mnist_train.data.float()
#mnist_test.data = mnist_test.data.float()

class conv_net(nn.Module):
def __init__(self, lr):
super(conv_net, self).__init__()
self.layer_1 = nn.Conv1d(1, 32, kernel_size=7, stride=5)
self.layer_2 = nn.Conv1d(32, 32, kernel_size=7, stride=5)
self.out = nn.Linear(960, 10)
self.optimizer = optim.Adam(self.parameters(), lr = lr)
def forward(self, x):
#print(x.dtype)
x = x.to(device)
x = x.view((x.size(), 1, -1))
x = F.leaky_relu(self.layer_1(x), negative_slope=0.1)
x = F.relu(self.layer_2(x)).view(x.size(0),-1)
x = self.out(x)
return x

model = conv_net(1e-3).to(device)
criterion = torch.nn.CrossEntropyLoss()

for epoch in range(EPOCHS):
model.train()
y_train = y_train.to(device)
pred = model(x_train)
loss = criterion(pred, y_train)
loss.backward()
model.optimizer.step()

counter = 0
y_test = y_test.to(device)
model.eval()
pred = model(x_test)
pred = torch.argmax(F.log_softmax(pred), dim = 1)
correct = (pred==y_test).sum().item()
print(f'Test - Accuracy: {(correct/len(y_test)):.4f}') for epoch in range(EPOCHS):
model.train()
y_train = y_train.to(device)
pred = model(x_train)
loss = criterion(pred, y_train)
loss.backward()
model.optimizer.step()

counter = 0
y_test = y_test.to(device)
model.eval()
pred = model(x_test)
pred = torch.argmax(F.log_softmax(pred), dim = 1)
correct = (pred==y_test).sum().item()
print(f'Test - Accuracy: {(correct/len(y_test)):.4f}')
``````

Seems like the dataloader is gonna cast it to float anyways before serving the model. So basically the model gets the exact same data for both runs? Cant seem to be related to initalization, i tried both runs more than 20 times. Same result every time

The `Image.fromarray` operation fails, if you manipulate the underlying data in this line of code.

Example:

``````dataset = datasets.MNIST(
root='PATH',
transform=transforms.ToTensor(),
{"ft_lang_label":"__label__en","ft_lang_prob":0.56884354,"math_prob":0.99040055,"size":2774,"snap":"2022-05-2022-21","text_gpt3_token_len":786,"char_repetition_ratio":0.118411556,"word_repetition_ratio":0.3402778,"special_character_ratio":0.2999279,"punctuation_ratio":0.25618374,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9994537,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-05-19T19:20:56Z\",\"WARC-Record-ID\":\"<urn:uuid:f8f06d56-a6ac-4961-bcfc-838651879032>\",\"Content-Length\":\"20879\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:eafd797f-e92b-4b25-95bd-11503ebc346e>\",\"WARC-Concurrent-To\":\"<urn:uuid:b2b57829-9596-4b0d-868a-2e4329be7eda>\",\"WARC-IP-Address\":\"159.203.145.104\",\"WARC-Target-URI\":\"https://discuss.pytorch.org/t/training-of-classifier-breaks-when-casting-uint8-to-float/73279\",\"WARC-Payload-Digest\":\"sha1:XEGJAHTYUOUTCG6ZDUD5XMZVGPEJVGMY\",\"WARC-Block-Digest\":\"sha1:T5I4NPH224GP7GDSOXAENP4E7YB7TLF2\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-21/CC-MAIN-2022-21_segments_1652662529658.48_warc_CC-MAIN-20220519172853-20220519202853-00560.warc.gz\"}"}