Advertisement
Guest User

Untitled

a guest
Nov 15th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.87 KB | None | 0 0
  1. subroutine mlrmodel(fvec,n,d,m,c,cgrad)
  2. implicit none
  3. integer, intent(in) :: n,d,m !training data sizes and number of classes
  4. real(kind=8), dimension((m-1)*(n+1)), intent(in) :: fvec !fitting parameters
  5. real(kind=8), intent(out) :: c !cost
  6. real(kind=8), dimension((m-1)*(n+1)), intent(out) :: cgrad !gradient of cost
  7. integer :: i1,j1,i,j,k,y, bias_column
  8. real(kind=8), dimension(m-1,n) :: w
  9. real(kind=8), dimension(m-1) :: b
  10. !Declare other variables as needed
  11.  
  12. real(kind=8), dimension(n) :: x
  13. real(kind=8), dimension(m-1) :: z, a
  14.  
  15. bias_column = (m-1) * (n)
  16.  
  17. !unpack fitting parameters (use if needed)
  18. do i1=1,n
  19. j1 = (i1-1)*(m-1)+1
  20. w(:,i1) = fvec(j1:j1+m-2) !weight matrix
  21. end do
  22. b = fvec((m-1)*n+1:(m-1)*(n+1)) !bias vector
  23.  
  24. ! Add cost and gradient contributions from each training data
  25. do k = 1, d
  26. ! Fetch required training input and label
  27. x = lr_x(:, k)
  28. y = lr_y(k)
  29.  
  30. a = compute_mlr_a(w, b, x, n, m)
  31.  
  32. ! Compute gradient and cost
  33. do i = 1, m - 1
  34. if (y == i) then
  35. ! Cost log term
  36. c = c - safe_log(a(i))
  37.  
  38. ! Bias gradient with y == i
  39. cgrad(bias_column + i) = cgrad(bias_column + i) - (1 - a(i))
  40.  
  41. ! Weight gradient y == i
  42. do j = 1, n
  43. cgrad(j * (m - 1) + i) = cgrad(j * (m - 1) + i) - (1 - a(i)) * x(j)
  44. end do
  45. else
  46. ! Bias gradient i /= j, y == j
  47. cgrad(i + bias_column) = cgrad(i + bias_column) + a(y)
  48.  
  49. ! Weight gradient i /= j, y == j
  50. do j = 1, n
  51. cgrad(j * (m - 1) + i) = cgrad(j * (m - 1) + i) + a(i) * x(j)
  52. end do
  53. end if
  54. end do
  55. end do
  56.  
  57. do i = 1, n
  58. do j = 1, m - 1
  59. ! Add penalty terms
  60. cgrad(i * (m - 1) + j) = cgrad(i * (m-1) + j) + w(j, i)
  61. c = c + lr_lambda * w(j,i) * w(j,i)
  62. end do
  63. end do
  64. end subroutine mlrmodel
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement