Testing and validation

Training is all well and good, but we also need to know whether or not our model is actually doing what it claims to be doing. We can reuse our training code, but let's make a few changes.

First, let's remove the solver command. We're testing our model, not training it, so we shouldn't be updating weights:

solver.Step(m.learnables())

Second, let's actually get an image out of our dataset into a convenient file:

for j := 0; j < xVal.Shape()[0]; j++ {
rowT, _ := xVal.Slice(sli{j, j + 1})
row := rowT.Data().([]float64)

img := visualizeRow(row)

f, _ := os.OpenFile(fmt.Sprintf("images/%d - %d - %d - %d.jpg", b, j, rowLabel, rowGuess), os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
jpeg.Encode(f, img, &jpeg.Options{jpeg.DefaultQuality})
f.Close()
}

As you can see, the following points are true: 

  • b is our batch number
  • j is the item number in said batch
  • rowLabel is the MNIST-provided label
  • rowGuess is our model's guess or prediction

Now, let's add some ways for us to extract both our data labels and our predictions into more human-readable formats (that is, as integers from 0 to 9).

For our data labels, let's add the following:

yRowT, _ := yVal.Slice(sli{j, j + 1})
yRow := yRowT.Data().([]float64)
var rowLabel int
var yRowHigh float64

for k := 0; k < 10; k++ {
if k == 0 {
rowLabel = 0
yRowHigh = yRow[k]
} else if yRow[k] > yRowHigh {
rowLabel = k
yRowHigh = yRow[k]
}
}

For our predictions, we first need to extract them into a familiar format. In this case, let's put them into a tensor so we can reuse all of our earlier code:

arrayOutput := m.predVal.Data().([]float64)
yOutput := tensor.New(
tensor.WithShape(bs, 10), tensor.WithBacking(arrayOutput)
)

Notice that the output coming out of m.predVal, which contains our prediction values, is an array of float64. You can also retrieve the original shape of the object, which helps you to make a tensor of the correct shape. In this case, we know the shape already, so we'll just put those parameters straight in.

The prediction code is, of course, similar to extracting our labels from our preprocessed MNIST dataset:

// get prediction
predRowT, _ := yOutput.Slice(sli{j, j + 1})
predRow := predRowT.Data().([]float64)
var rowGuess int
var predRowHigh float64

// guess result
for k := 0; k < 10; k++ {
if k == 0 {
rowGuess = 0
predRowHigh = predRow[k]
} else if predRow[k] > predRowHigh {
rowGuess = k
predRowHigh = predRow[k]
}
}

For all that hard work, you'll be rewarded with a folder full of image files with the following labels and guesses:

You'll see that in its current form, our model struggles with some (potentially poor) handwriting.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.98.250