Advertisement
Guest User

Untitled

a guest
Nov 16th, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Julia 2.47 KB | None | 0 0
  1. # Load data into RAM.
  2. path = "C:/Users/dream/Desktop/4th Year/Introduction to Deep Learning/Assignment 1/"
  3.  
  4. trainxs = npzread(string(path, "Data/data/fashion-train-imgs.npz"))
  5. trainys = npzread(string(path, "Data/data/fashion-train-labels.npz"))
  6. devxs   = npzread(string(path, "Data/data/fashion-dev-imgs.npz"))
  7. devys   = npzread(string(path, "Data/data/fashion-dev-labels.npz"))
  8. testxs  = npzread(string(path, "Data/data/fashion-test-imgs.npz"))
  9. testys  = npzread(string(path, "Data/data/fashion-test-labels.npz"))
  10.  
  11. # Define logistic function:
  12. function exponentialResponse(image :: Array{Float64, 2}, weights :: Array{Float64, 2}, bias :: Float64)
  13.     pixels = size(image)
  14.  
  15.     linearCombination = 0
  16.  
  17.     if pixels == size(weights)
  18.         for i in 1:pixels[1]
  19.             for j in 1:pixels[2]
  20.                 linearCombination += image[i, j] * weights[i, j]
  21.             end
  22.         end
  23.     end
  24.  
  25.     linearCombination += bias
  26.  
  27.     # print(linearCombination)
  28.     # print("    linear   ")
  29.     return(BigFloat(Base.MathConstants.e^linearCombination))
  30. end
  31.  
  32.  
  33. # Define loss function:
  34. function loss(weights :: Array{Float64, 2}, bias :: Float64)
  35.     n = size(trainxs, 3)
  36.  
  37.     sum = 0
  38.     exponentialResp = 0
  39.  
  40.     for i in 1:n
  41.         exponentialResp = exponentialResponse(trainxs[:,:,i], weights, bias)
  42.         perImageLogisticTerm = exponentialResp/(1 + exponentialResp)
  43.         print(perImageLogisticTerm)
  44.         print("    ")
  45.         term = trainys[i]^2 - 2*trainys[i]*perImageLogisticTerm + perImageLogisticTerm^2
  46.         sum += term
  47.     end
  48.  
  49.     loss = sum /(2*n)
  50.  
  51.     return(loss)
  52. end
  53.  
  54. # Compute a specific derivative (for weight j):
  55. function derivate(weights :: Array{Float64, 2}, bias :: Float64, j :: Array{Int64, 1})
  56.     sumBag = 0
  57.     sumNotBag = 0
  58.     exponentialResp = 0
  59.  
  60.     if size(j)[1] == 2
  61.         for i in 1:size(trainys)[1]
  62.             if trainys[i] == 0
  63.                 exponentialResp = exponentialResponse(trainxs[:,:,i], weights, bias)
  64.                 sumNotBag += trainxs[j[1],j[2],i] * exponentialResp / ((1 + exponentialResp)^3)
  65.             else
  66.                 exponentialResp = exponentialResponse(trainxs[:,:,i], weights, bias)
  67.                 sumBag += trainxs[j[1],j[2],i] * (exponentialResp^2) / ((1 + exponentialResp)^3)
  68.             end
  69.         end
  70.     end
  71.  
  72.     return(sumNotBag - sumBag)
  73. end
  74.  
  75. function computeAllDerivatives(weights :: Array{Float64, 2}, bias :: Float64)
  76.     weightDerivatives = zeros(size(weights)[1], size(weights)[2])
  77.     for i in 1:size(weights)[1]
  78.         for j in 1:size(weights)[2]
  79.             weightDerivatives[i,j] = derivate(weights, bias, [i, j])
  80.         end
  81.     end
  82.  
  83.     return weightDerivatives
  84. end
  85.  
  86.  
  87. print(loss(fill(1.0, size(trainxs)[1:2]), 1.0))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement