Guest User

Untitled

a guest
Jun 23rd, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.81 KB | None | 0 0
  1. struct Example
  2. featureVector::Array{Array{Float64,1},1}
  3. outputVector::Array{Array{Float64,1},1}
  4. end
  5.  
  6. Example(x1::Float64, x2::Float64, y::String) = Example(
  7. [[1.0], [(x1 - 50.0) / 50.0], [(x2 - 50.0) / 50.0], [((x1 - 50.0) / 50.0)^2], [((x2 - 50.0) / 50.0)^2]],
  8. if y == "red"
  9. [[1.0], [0.0], [0.0]]
  10. elseif y == "green"
  11. [[0.0], [1.0], [0.0]]
  12. elseif y == "blue"
  13. [[0.0], [0.0], [1.0]]
  14. end
  15. )
  16.  
  17. Example(x1::Int64, x2::Int64, y::String) = Example(Float64(x1), Float64(x2), y)
  18.  
  19. function Model(theta, example::Example)
  20. [1.0 ./ s for s in 1 .+ [e .^ p for p in theta' * example.featureVector]]
  21. end
  22.  
  23. function Cost(theta, examples::Array{Example, 1})
  24. function CostOfExample(theta, example::Example)
  25. m = Model(theta, example)
  26. results = []
  27. for (x_i, y_i) in zip(m, example.outputVector)
  28. if y_i[1] == 1.0
  29. result = log(x_i[1])
  30. else
  31. result = log(1 - x_i[1])
  32. end
  33. if result == -Inf
  34. result = -1e20
  35. end
  36. push!(results, result)
  37. end
  38. results
  39. end
  40.  
  41. function SumCostOverExamples(theta, examples::Array{Example, 1})
  42. sum([CostOfExample(theta, ex) for ex in examples])
  43. end
  44.  
  45. -1.0 / length(examples) * SumCostOverExamples(theta, examples)
  46. end
  47.  
  48. function DCostDTheta(theta, featureIndex::Int64, classIndex::Int64, examples::Array{Example, 1})
  49. terms = [(Model(theta, ex)[classIndex] - ex.outputVector[classIndex])[1] * ex.featureVector[featureIndex][1] for ex in examples]
  50. sum(terms)[1]
  51. end
  52.  
  53. training_examples = [
  54. Example(6, 48, "red"),
  55. Example(8, 11, "red"),
  56. Example(15, 1, "red"),
  57. Example(16, 19, "red"),
  58. Example(22, 40, "red"),
  59. Example(25, 33, "red"),
  60. Example(30, 49, "red"),
  61. Example(31, 44, "red"),
  62. Example(32, 29, "red"),
  63. Example(38, 2, "red"),
  64. Example(53, 63, "green"),
  65. Example(50, 55, "green"),
  66. Example(90, 70, "green"),
  67. Example(69, 59, "green"),
  68. Example(76, 28, "green"),
  69. Example(86, 33, "green"),
  70. Example(51, 55, "green"),
  71. Example(88, 57, "green"),
  72. Example(85, 54, "green"),
  73. Example(60, 62, "green"),
  74. Example(14, 66, "blue"),
  75. Example(13, 76, "blue"),
  76. Example(40, 82, "blue"),
  77. Example(10, 98, "blue"),
  78. Example( 6, 51, "blue"),
  79. Example(47, 62, "blue"),
  80. Example(40, 85, "blue"),
  81. Example(11, 59, "blue"),
  82. Example(26, 95, "blue"),
  83. Example(20, 88, "blue")
  84. ]
  85.  
  86. # theta has a row for each feature and a column for each class
  87. theta_shape = (
  88. length(training_examples[1].featureVector),
  89. length(training_examples[1].outputVector)
  90. )
  91. theta = zeros(theta_shape)
  92.  
  93. # currentCost is a row vector with a column for each class
  94. currentCost = Cost(theta, training_examples)
  95.  
  96. println("Starting cost: $(sum(currentCost))")
  97. learningRate = 0.5
  98.  
  99. adjustments = ones(theta_shape)
  100. iterCount = 0
  101.  
  102. while sum(sum(abs.(adjustments))) > 1e-2
  103. for featureIndex = 1:length(training_examples[1].featureVector)
  104. for classIndex = 1:length(training_examples[1].outputVector)
  105. gradient = DCostDTheta(theta, featureIndex, classIndex, training_examples)
  106. adjustments[(classIndex - 1) * length(training_examples[1].featureVector) + featureIndex] = learningRate * gradient
  107. end
  108. end
  109. theta += adjustments
  110. iterCount += 1
  111. currentCost = Cost(theta, training_examples)
  112. if (0 == iterCount % 1000)
  113. println("Cost after $(iterCount) iterations: $(sum(currentCost))")
  114. end
  115. end
  116. println("Red: $(join(["theta[$(i-1)] = $(@sprintf("%.2f", theta[i]))" for i = 1:length(training_examples[1].featureVector)], ", "))")
  117. println("Green: $(join(["theta[$(i-1)] = $(@sprintf("%.2f", theta[5 + i]))" for i = 1:length(training_examples[1].featureVector)], ", "))")
  118. println("Blue: $(join(["theta[$(i-1)] = $(@sprintf("%.2f", theta[10 + i]))" for i = 1:length(training_examples[1].featureVector)], ", "))")
  119. println("in $(iterCount) iterations")
  120. println("Final cost = $(sum(Cost(theta, training_examples)))")
Add Comment
Please, Sign In to add comment