data = pd.read_csv('\\digit-recognizer\\train.csv')
data = np.array(data)
test_data = data[:1000]
train_data = data[1000:10000]
X_batch, y_batch = train_data[:, 1:], train_data[:,0]
X_batch = X_batch.reshape(X_batch.shape[0], 1 , 28, 28)
X_batch = X_batch / 255
y_batch = y_batch.reshape(y_batch.shape[0], 1)
y_batch = np.int_((np.arange(0,10) == y_batch))
y_batch = torch.from_numpy(y_batch).float()
X_batch = torch.from_numpy(X_batch).float()
X_batch = X_batch.long()
y_batch = y_batch.long()
class ConvNet(nn.Module):
def __init__(self):
super().__init__()
# размер исходной картинки 28x28
# conv 1
self.conv1 = nn.Conv2d(in_channels=1, out_channels=6, kernel_size=(3,3)) #26x26
# pool
self.pool1 = nn.MaxPool2d(kernel_size=(2,2)) #13x13
# conv 2
self.conv2 = nn.Conv2d(in_channels=6, out_channels=9, kernel_size=(4,4)) #10x10
# flatten
self.flatten = nn.Flatten()
# linear 1
self.fc1 = nn.Linear(900, 100)
# linear 2
self.fc2 = nn.Linear(100, 10)
def forward(self, x):
# forward pass сети
x = F.relu(self.conv1(x))
x = self.pool1(x)
x = F.relu(self.conv2(x))
x = self.flatten(x)
x = F.relu(self.fc1(x))
x = self.fc2(x)
return x
conv_net = ConvNet()
device = torch.device("cpu")
conv_net = conv_net.to(device)
loss_fn = torch.nn.CrossEntropyLoss()
learning_rate = 1e-3
optimizer = torch.optim.Adam(conv_net.parameters(), lr=learning_rate)
model.train(True)
model(X_batch.to(device))
Получаю
RuntimeError: expected scalar type Long but found Float