Advertisement
Guest User

Untitled

a guest
Feb 21st, 2020
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.02 KB | None | 0 0
  1. def train(net, X, Y, criterion, batch_size=2, n_epoch=5):
  2.     loss_history = []
  3.  
  4.     for i in range(n_epoch):
  5.         for x_batch, y_batch in get_batches((X, Y), batch_size):
  6.  
  7.             net.zeroGradParameters()
  8.  
  9.             # Forward
  10.             predictions = net.forward(x_batch)
  11.             loss = criterion.forward(predictions, y_batch)
  12.  
  13.             # Backward
  14.             dp = criterion.backward(predictions, y_batch)
  15.             net.backward(x_batch, dp)
  16.  
  17.             # Update weights
  18.             sgd_momentum(net.getParameters(),
  19.                          net.getGradParameters(),
  20.                          optimizer_config,
  21.                          optimizer_state)      
  22.  
  23.             loss_history.append(loss)
  24.  
  25.         # Visualize
  26.         display.clear_output(wait=True)
  27.         plt.figure(figsize=(8, 6))
  28.  
  29.         plt.title("Training loss")
  30.         plt.xlabel("#iteration")
  31.         plt.ylabel("loss")
  32.         plt.plot(loss_history, 'b')
  33.         plt.show()
  34.  
  35.         print('Current loss: %f' % loss)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement