3

I am trying to fit a model with string labels in form ["aa", "aa", "bb", "bb", "bb", "cc"] and wanted to use tf.one_hot(labels, depth=3) to get the one hot vectors but I get the error:

NotFoundError: Could not find valid device for node.
Node:{{node OneHot}}
All kernels registered for op OneHot :
  device='XLA_GPU'; TI in [DT_INT32, DT_UINT8, DT_INT64]; T in [DT_FLOAT, DT_DOUBLE, DT_INT32, DT_UINT8, DT_INT16, ..., DT_UINT16, DT_COMPLEX128, DT_HALF, DT_UINT32, DT_UINT64]
  device='XLA_CPU'; TI in [DT_INT32, DT_UINT8, DT_INT64]; T in [DT_FLOAT, DT_DOUBLE, DT_INT32, DT_UINT8, DT_INT16, ..., DT_UINT16, DT_COMPLEX128, DT_HALF, DT_UINT32, DT_UINT64]

my questions:

1- is it even possible to transform these kind of label to one hot directly with tensorflow 2 or should the input be INT?

2- if yes what could cause this problem

3-is it possible to fit a model directly with string labels?

extra information:

I am using a cluster to train and using GPU and defined my device using os.environ["CUDA_VISIBLE_DEVICES"]="2" based on this

I will post more of my code if it's necessary.

hamid.khb
  • 432
  • 2
  • 11

1 Answers1

2

1- is it even possible to transform these kind of label to one hot directly with tensorflow 2 or should the input be INT?

2- if yes what could cause this problem

TensorFlow only supports numeric indices. If you look at the documentation for tf.one_hot, it is in fact an index you are passing as the labels. I found this info via this Github issue

3-is it possible to fit a model directly with string labels?

I don't see an easy way out for this. But if you can fit your data in-memory, I'd suggest using pandas/sklearn and convert these to onehot encoded. If it doesn't you might need to create a series of feature columns for your data. For example this feature column supports what you need to achieve.

thushv89
  • 10,865
  • 1
  • 26
  • 39
  • Thanks for the reply I got that through further reading in documentations. I will leave this for future matters: 1-one hot vector is being used for categorical crossentropy and integer labels is being used for sparse categorical crossentropy. I changes my labels to integers and matched with my loss function and it worked. – hamid.khb Apr 24 '20 at 08:19