Created
January 10, 2019 10:52
-
-
Save pemagrg1/43b4a4ff0be12361ed0b0305dbd86dd9 to your computer and use it in GitHub Desktop.
one hot encoding using Tensorflow
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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