In [1]:
import org.platanios.tensorflow.api._

import scala.collection.mutable.ArrayBuffer
import scala.util.Random

val random = new Random()


Out[1]:
import org.platanios.tensorflow.api._


import scala.collection.mutable.ArrayBuffer

import scala.util.Random


random: Random = scala.util.Random@77cb9c1e

In [2]:
val weight = random.nextFloat()

def batch(batchSize: Int): (Tensor[Float], Tensor[Float]) = {
val inputs = ArrayBuffer.empty[Float]
val outputs = ArrayBuffer.empty[Float]
var i = 0
while (i < batchSize) {
  val input = random.nextFloat()
  inputs += input
  outputs += weight * input
  i += 1
}
(Tensor(inputs).reshape(Shape(-1, 1)), Tensor(outputs).reshape(Shape(-1, 1)))
}


Out[2]:
weight: Float = 0.74208564F
defined function batch

In [3]:
print("Building linear regression model.")
val inputs = tf.placeholder[Float](Shape(-1, 1))
val outputs = tf.placeholder[Float](Shape(-1, 1))
val weights = tf.variable[Float]("weights", Shape(1, 1), tf.ZerosInitializer)
val predictions = tf.matmul(inputs, weights)
val loss = tf.sum(tf.square(predictions - outputs))
val trainOp = tf.train.AdaGrad(1.0f).minimize(loss)

println("Training the linear regression model.")
val session = Session()
session.run(targets = tf.globalVariablesInitializer())
for (i <- 0 to 25) {
val trainBatch = batch(10000)
val feeds = Map(inputs -> trainBatch._1, outputs -> trainBatch._2)
val trainLoss = session.run(feeds = feeds, fetches = loss, targets = trainOp)
if (i % 1 == 0)
  println(s"Train loss at iteration $i = ${trainLoss.scalar} " +
    s"(weight = ${session.run(fetches = weights.value).scalar})")
}

println(s"Trained weight value: ${session.run(fetches = weights.value).scalar}")
println(s"True weight value: $weight")


Building linear regression model.
SLF4J: Class path contains multiple SLF4J providers.
SLF4J: Found provider [org.slf4j.log4j12.Log4j12ServiceProvider@4e09428c]
SLF4J: Found provider [org.slf4j.helpers.NOPServiceProvider@6366d536]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual provider is of type [org.slf4j.log4j12.Log4j12ServiceProvider@4e09428c]
Training the linear regression model.
Train loss at iteration 0 = 1831.6814 (weight = 1.0)
Train loss at iteration 1 = 222.29132 (weight = 0.6703382)
Train loss at iteration 2 = 17.351349 (weight = 0.76244646)
Train loss at iteration 3 = 1.3862652 (weight = 0.736524)
Train loss at iteration 4 = 0.10259153 (weight = 0.74354696)
Train loss at iteration 5 = 0.0070248283 (weight = 0.74171674)
Train loss at iteration 6 = 4.5134925E-4 (weight = 0.74218255)
Train loss at iteration 7 = 3.1532058E-5 (weight = 0.7420587)
Train loss at iteration 8 = 2.427319E-6 (weight = 0.74209297)
Train loss at iteration 9 = 1.7987655E-7 (weight = 0.7420836)
Train loss at iteration 10 = 1.3720174E-8 (weight = 0.7420862)
Train loss at iteration 11 = 9.51544E-10 (weight = 0.7420855)
Train loss at iteration 12 = 4.9631847E-11 (weight = 0.7420857)
Train loss at iteration 13 = 1.3894611E-11 (weight = 0.74208564)
Train loss at iteration 14 = 0.0 (weight = 0.74208564)
Train loss at iteration 15 = 0.0 (weight = 0.74208564)
Train loss at iteration 16 = 0.0 (weight = 0.74208564)
Train loss at iteration 17 = 0.0 (weight = 0.74208564)
Train loss at iteration 18 = 0.0 (weight = 0.74208564)
Train loss at iteration 19 = 0.0 (weight = 0.74208564)
Train loss at iteration 20 = 0.0 (weight = 0.74208564)
Train loss at iteration 21 = 0.0 (weight = 0.74208564)
Train loss at iteration 22 = 0.0 (weight = 0.74208564)
Train loss at iteration 23 = 0.0 (weight = 0.74208564)
Train loss at iteration 24 = 0.0 (weight = 0.74208564)
Train loss at iteration 25 = 0.0 (weight = 0.74208564)
Trained weight value: 0.74208564
True weight value: 0.74208564
Out[3]:
inputs: Output[Float] = Output(Placeholder, 0)
outputs: Output[Float] = Output(Placeholder_1, 0)
weights: Variable[Float] = Variable(
  DataType("Float", 1, Some(4), DT_FLOAT),
  Output(weights, 0),
  weights/InitializationAssign,
  null,
  Output(weights/Read/ReadVariable, 0)
)
predictions: Output[Float] = Output(MatMul, 0)
loss: Output[Float] = Output(Sum, 0)
trainOp: UntypedOp = Minimize/Finish
session: Session = org.platanios.tensorflow.api.core.client.Session@6d50edbb
res2_9: implicits.helpers.OutputToTensor.<refinement>.this.type.V = List()

In [ ]: