0

Before I continue, please excuse my ignorance. I have some experience programming before this, but my previous intuition has failed me presently.

Essentially, I need to expand a 1-D vector (size M x 1) of numbers ranging from 0...K, to a 2-D matrix (or Tensor, size M x K) where each row is a 1-D vector (size 1 x K), and each element is a 0 except for the index of the initial value being 1.

Yes, this is a multiclass classification problem for a ML class.

I had the idea of creating a zeros matrix of the correct shape, and then assigning the index of the element I need manually to a 1, but cannot seem to change the values of the already created Variable. I get the error:

TypeError: 'Tensor' object does not support item assignment

Can anyone assist with this? If you feel as though my way of going about creating this final Tensor could use a different approach, any advice would be appreciated.

j3private
  • 13
  • 5
  • As a side note try to put a reproducible example with some data in your post, explaining what exactly you are doing. Is not clear what you are trying to do in Python. Can you post Python reproducible code? – Umberto Jan 09 '18 at 13:53

1 Answers1

0

In tensorflow, the function tf.one_hot() is what you seek. One hot encoding is the term describing the operation you are looking to implement. See https://www.tensorflow.org/api_docs/python/tf/one_hot .

muskrat
  • 1,519
  • 11
  • 18