MCPcopy Create free account
hub / github.com/lazyprogrammer/machine_learning_examples / full_gd

Function full_gd

pytorch/ann_regression.py:39–61  ·  view source on GitHub ↗
(model, criterion, optimizer, X_train, y_train, epochs=1000)

Source from the content-addressed store, hash-verified

37
38# Train the model
39def full_gd(model, criterion, optimizer, X_train, y_train, epochs=1000):
40 # Stuff to store
41 train_losses = np.zeros(epochs)
42
43 for it in range(epochs):
44 # zero the parameter gradients
45 optimizer.zero_grad()
46
47 # Forward pass
48 outputs = model(X_train)
49 loss = criterion(outputs, y_train)
50
51 # Backward and optimize
52 loss.backward()
53 optimizer.step()
54
55 # Save losses
56 train_losses[it] = loss.item()
57
58 if (it + 1) % 50 == 0:
59 print(f'Epoch {it+1}/{epochs}, Train Loss: {loss.item():.4f}')
60
61 return train_losses
62
63X_train = torch.from_numpy(X.astype(np.float32))
64y_train = torch.from_numpy(Y.astype(np.float32).reshape(-1, 1))

Callers 1

ann_regression.pyFile · 0.85

Calls 1

stepMethod · 0.45

Tested by

no test coverage detected