From labels to one-hot vectors

Recall that neural networks built in Gorgonia only take tensor.Tensors as inputs. Therefore, the labels will also have to be converted into tensor.Tensor. The function is quite similar to prepareX:

func prepareY(N []Label) (retVal tensor.Tensor) {
rows := len(N)
cols := 10

b := make([]float64, 0, rows*cols)
for i := 0; i < rows; i++ {
for j := 0; j < 10; j++ {
if j == int(N[i]) {
b = append(b, 1)
} else {
b = append(b, 0)
}
}
}
return tensor.New(tensor.WithShape(rows, cols), tensor.WithBacking(b))
}

What we're building here is a matrix with N rows and ten columns. The specifics of why we build a matrix of (N,10) will be explored in the next chapter, but for now let's zoom into an imaginary row. Imagine the first label, (int(N[i]))is 7. The row will look like this:

[0, 0, 0, 0, 0, 0, 0, 1, 0, 0]

This is called a one-hot vector encoding. It will be useful to us later, and will expanded upon in the next chapter.

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

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