Skip to content

Instantly share code, notes, and snippets.

@pemagrg1
Created January 10, 2019 10:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pemagrg1/43b4a4ff0be12361ed0b0305dbd86dd9 to your computer and use it in GitHub Desktop.
Save pemagrg1/43b4a4ff0be12361ed0b0305dbd86dd9 to your computer and use it in GitHub Desktop.
one hot encoding using Tensorflow
import tensorflow as tf
import pandas as pd
text = 'My cat is a great cat'
tokens = text.lower().split()
vocab = set(tokens)
vocab = pd.Series(range(len(vocab)), index=vocab)
word_ids = vocab.loc[tokens].values
inputs = tf.placeholder(tf.int32, [None])
# TensorFlow has an operation for one-hot encoding
one_hot_inputs = tf.one_hot(inputs, len(vocab))
transformed = tf.Session().run(one_hot_inputs, {inputs: word_ids})
print (transformed)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment