Scala SGD
Scala SGD
DataFrames and
Datasets
Annoncements and plan for
today
• Class and labsession of Feb24 moved to, respectively,
March 6, 9am, and March 7, 2pm.
• Collections
• Case classes
scala> 1 to 10
res0: scala.collection.immutable.Range.Inclusive = Range 1 to 10
scala>
Scala types
https://docs.scala-lang.org/tour/uni ed-types.html
fi
Focus on range collections
scala> 1 to 10
res1: scala.collection.immutable.Range.Inclusive = Range 1 to 10
scala> 1 until 10
res2: scala.collection.immutable.Range = Range 1 until 10
scala> 1 to 10 by 2
res3: scala.collection.immutable.Range = inexact Range 1 to 10 by 2
scala>
Range for populating
sequences
scala>
Values and Variables
scala> val answer = 8 * 5 + 2
answer: Int = 42 • Names can be either of the val
scala> 0.5 * answer
or variable kind
res59: Double = 21.0
scala> answer = 0
• Val is used for names
<console>:13: error: reassignment to val associated to constants
answer = 0
^
• For instance
scala> if (y > 0) 1
• () denotes ‘no value’ and its
res5: AnyVal = 1 type is Unit.
scala>
Complex branches
scala> if (n > 0) { r = r * n; n -= 1 } • Many Scala programmers
scala> if (n > 0) { prefer the second style
| r = r * n
| n -= 1
| }
scala>
Block expressions and assignements
scala> { r = r * n; n -= 1 }
• The value/type of a block
expression is the value/type of
scala> var c = { r = r * n; n -= 1 }
c: Unit = ()
its last expression
scala>
scala>
Input and output
scala> val name = scala.io.StdIn.readLine("Your name: ")
Your name: name: String = toto
scala> printf("Hello, %s! Next year, you will be %d.\n", name, age + 1)
scala>
Functions
scala> def abs(x: Double) = if (x >= 0) x else -x
abs: (x: Double)Double
• Scala has functions in addition to
scala> def fac(n : Int) = { methods.
| var r = 1
| for (i <- 1 to n) r = r * i
| r
• A method operates on an object,
| } but a function doesn’t
fac: (n: Int)Int
• While types of input parameters
scala>
must be declared, the output
type could not
scala> def fac(n: Int):Int =
| if (n <= 0) 1 else n * fac(n - 1) • With the exception of recursive
fac: (n: Int)Int functions
scala>
Arrays
scala> val nums = new Array[Int](10)
nums: Array[Int] = Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
scala>
fi
fi
Variable-Length Arrays
scala> import scala.collection.mutable.ArrayBuffer
scala> b += 1
res17: b.type = ArrayBuffer(1)
scala> b += (1, 2, 3, 5)
res18: b.type = ArrayBuffer(1, 1, 2, 3, 5)
scala> b.trimEnd(5)
scala> b
res27: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 1, 2)
scala>
Traversing Arrays
scala> b ++= Array(8, 13, 21)
res19: b.type = ArrayBuffer(1, 1, 2, 3, 5, 8, 13, 21)
scala> b.trimEnd(5)
scala>
Transforming Arrays
scala> b.trimEnd(5)
scala>
Tuples - immutable
scala> val t = (1, 3.14, "Fred")
t: (Int, Double, String) = (1,3.14,Fred)
scala> t._1
res49: Int = 1
scala> println(message1.sender)
scala>
Pattern matching on case classes
case class Email(sender: String, title: String, body: String) extends Noti cation
https://docs.scala-lang.org/tour/pattern-matching.html
fi
fi
fi
fi
Pattern matching on case classes
case class Email(sender: String, title: String, body: String) extends Noti cation
println(showNoti cation(someSms)) // prints You got an SMS from 12345! Message: Are you there?
println(showNoti cation(someVoiceRecording)) // prints You received a Voice Recording from Tom! Click the link to hear it:
voicerecording.org/id/123
https://docs.scala-lang.org/tour/pattern-matching.html
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
What to use in place of
numpy?
https://www.cpuheater.com/scala/machine-learning-scala-linear-regression/
Much more in the following
references
Based on material of Stanford CS221 course Arti cial Intelligence: Principles and Techniques
fi
Linear regression
fw (x) = w · (x)
3
( (x), y)
w · (x)
2 residual w · (x) y
0
0 1 2 3 4
Example:
Losssquared (x, y, w) = 25
Loss minimization framework
So far: one example, Loss(x, y, w) is easy to minimize.
min TrainLoss(w)
w2Rd
Key: need to set w to make global tradeo↵s — not every example can
be happy.
Definition: gradient
Initialize w = [0, . . . , 0]
For t = 1, . . . , T :
w w ⌘ rw TrainLoss(w)
|{z} | {z }
step size gradient
Gradient descent:
w w ⌘rw TrainLoss(w)
Gradient desce
1
TrainLoss(w) =
|Dtrain |
(x
Least squares regression
Objective function:
1 X
TrainLoss(w) = (w · (x) y)2
|Dtrain |
(x,y)2Dtrain
[semi-live solution]
Array[Double,
Array[Double]]