代码拉取完成,页面将自动刷新
import torch
from torch import nn
from torch.utils.data import DataLoader
from torchvision import datasets
from torchvision.transforms import ToTensor
from sklearn.model_selection import train_test_split
import pandas as pd
import numpy as np
train_df = pd.read_csv('digit recognizor.csv')
train_df.info()
<class 'pandas.core.frame.DataFrame'> RangeIndex: 42000 entries, 0 to 41999 Columns: 785 entries, label to pixel783 dtypes: int64(785) memory usage: 251.5 MB
train_df.head()
label | pixel0 | pixel1 | pixel2 | pixel3 | pixel4 | pixel5 | pixel6 | pixel7 | pixel8 | ... | pixel774 | pixel775 | pixel776 | pixel777 | pixel778 | pixel779 | pixel780 | pixel781 | pixel782 | pixel783 | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | ... | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | ... | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
2 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | ... | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
3 | 4 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | ... | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
4 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | ... | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
5 rows × 785 columns
train_x = train_df.drop(['label'], axis=1)
train_y = train_df['label']
train_x.values.dtype
dtype('int64')
X_train, X_test, y_train, y_test = train_test_split( train_x, train_y, test_size=0.2, random_state=42)
X_train.shape
(33600, 784)
X_train = torch.tensor(X_train.values,dtype=torch.float)
y_train = torch.LongTensor(y_train.values)
X_test = torch.tensor(X_test.values,dtype=torch.float)
y_test = torch.LongTensor(y_test.values)
# X_train = torch.tensor(X_train.values)
# y_train = torch.tensor(y_train.values)
# X_test = torch.tensor(X_test.values)
# y_test = torch.tensor(y_test.values)
X_train.dtype
torch.float32
y_train.dtype
torch.int64
# Get cpu or gpu device for training.
device = "cpu"
print(f"Using {device} device")
# Define model
class NeuralNetwork(nn.Module):
def __init__(self):
super(NeuralNetwork, self).__init__()
self.flatten = nn.Flatten()
self.linear_relu_stack = nn.Sequential(
nn.Linear(28*28, 512),
nn.ReLU(),
nn.Linear(512, 512),
nn.ReLU(),
nn.Linear(512, 10)
)
def forward(self, x):
x = self.flatten(x)
logits = self.linear_relu_stack(x)
return logits
model = NeuralNetwork().to(device)
print(model)
Using cpu device NeuralNetwork( (flatten): Flatten(start_dim=1, end_dim=-1) (linear_relu_stack): Sequential( (0): Linear(in_features=784, out_features=512, bias=True) (1): ReLU() (2): Linear(in_features=512, out_features=512, bias=True) (3): ReLU() (4): Linear(in_features=512, out_features=10, bias=True) ) )
loss_fn = nn.CrossEntropyLoss()
optimizer = torch.optim.SGD(model.parameters(), lr=1e-3)
def train(X,y, model, loss_fn, optimizer):
batch = 64
size = len(X)
model.train()
X, y = X.to(device), y.to(device)
pred = model(X)
loss = loss_fn(pred, y)
# Backpropagation
optimizer.zero_grad()
loss.backward()
optimizer.step()
if batch % 100 == 0:
loss, current = loss.item(), batch * len(X)
print(f"loss: {loss:>7f} [{current:>5d}/{size:>5d}]")
def test(X,y, model, loss_fn):
size = len(X)
num_batches = 64
model.eval()
test_loss, correct = 0, 0
with torch.no_grad():
X, y = X.to(device), y.to(device)
pred = model(X)
test_loss += loss_fn(pred, y).item()
correct += (pred.argmax(1) == y).type(torch.float).sum().item()
test_loss /= num_batches
correct /= size
print(f"Test Error: \n Accuracy: {(100*correct):>0.1f}%, Avg loss: {test_loss:>8f} \n")
epochs = 10
for t in range(epochs):
print(f"Epoch {t+1}\n-------------------------------")
train(X_train,y_train, model, loss_fn, optimizer)
test(X_test,y_test, model, loss_fn)
print("Done!")
Epoch 1 ------------------------------- Test Error: Accuracy: 14.7%, Avg loss: 0.199190 Epoch 2 ------------------------------- Test Error: Accuracy: 24.8%, Avg loss: 0.177755 Epoch 3 ------------------------------- Test Error: Accuracy: 36.6%, Avg loss: 0.123590 Epoch 4 ------------------------------- Test Error: Accuracy: 36.3%, Avg loss: 0.080007 Epoch 5 ------------------------------- Test Error: Accuracy: 50.0%, Avg loss: 0.073817 Epoch 6 ------------------------------- Test Error: Accuracy: 54.7%, Avg loss: 0.043107 Epoch 7 ------------------------------- Test Error: Accuracy: 63.9%, Avg loss: 0.029944 Epoch 8 ------------------------------- Test Error: Accuracy: 66.4%, Avg loss: 0.028494 Epoch 9 ------------------------------- Test Error: Accuracy: 69.5%, Avg loss: 0.022046 Epoch 10 ------------------------------- Test Error: Accuracy: 70.1%, Avg loss: 0.021558 Done!
for name, param in model.named_parameters():
print(name,'-->',param.type(),'-->',param.dtype,'-->',param.shape)
linear_relu_stack.0.weight --> torch.FloatTensor --> torch.float32 --> torch.Size([512, 784]) linear_relu_stack.0.bias --> torch.FloatTensor --> torch.float32 --> torch.Size([512]) linear_relu_stack.2.weight --> torch.FloatTensor --> torch.float32 --> torch.Size([512, 512]) linear_relu_stack.2.bias --> torch.FloatTensor --> torch.float32 --> torch.Size([512]) linear_relu_stack.4.weight --> torch.FloatTensor --> torch.float32 --> torch.Size([10, 512]) linear_relu_stack.4.bias --> torch.FloatTensor --> torch.float32 --> torch.Size([10])
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。