Trains for num_epochs epochs with mini-bachtes of size equal to batch_size.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(network_type), | intent(inout) | :: | self | |||
| real(kind=rk), | intent(in) | :: | x(:,:) | |||
| real(kind=rk), | intent(in) | :: | y(:,:) | |||
| real(kind=rk), | intent(in) | :: | eta | |||
| integer(kind=ik), | intent(in) | :: | num_epochs | |||
| integer(kind=ik), | intent(in) | :: | batch_size |
subroutine train_epochs(self, x, y, eta, num_epochs, batch_size)
!! Trains for num_epochs epochs with mini-bachtes of size equal to batch_size.
class(network_type), intent(in out) :: self
integer(ik), intent(in) :: num_epochs, batch_size
real(rk), intent(in) :: x(:,:), y(:,:), eta
integer(ik) :: i, n, nsamples, nbatch
integer(ik) :: batch_start, batch_end
real(rk) :: pos
nsamples = size(y, dim=2)
nbatch = nsamples / batch_size
epochs: do n = 1, num_epochs
batches: do i = 1, nbatch
!pull a random mini-batch from the dataset
call random_number(pos)
batch_start = int(pos * (nsamples - batch_size + 1))
if (batch_start == 0) batch_start = 1
batch_end = batch_start + batch_size - 1
call self % train(x(:,batch_start:batch_end), y(:,batch_start:batch_end), eta)
end do batches
end do epochs
end subroutine train_epochs