python - Tensorflow onehot encode -
i'm new @ using tensorflow , have questions tensorflows 1 hot encoding.
i want read cvs file in in last column represents labels. labels integer values 1 7.
i want classification using softmax model.
therefore need labels in onehot tensor format?
is there simple way/tensorflow built-in convert labels 1 hot?
according tutorial file parsing have following code parsing csv part of 1 hot encoding missing.
def read_from_cvs(filename_queue): reader = tf.textlinereader() key, value = reader.read(filename_queue) record_defaults = [[] col in range((num_attributes))] # no defaults, values must given attributes = tf.decode_csv(value, record_defaults=record_defaults) features = tf.stack(attributes[1:-1]) labels = tf.stack(attributes[-1]) return features, labels def input_pipeline(filename = 'dataset.csv', batch_size = 30, num_epochs=none): filename_queue = tf.train.string_input_producer(filename, num_epochs=num_epochs, shuffle=true) features, labels = read_from_cvs(filename_queue) min_after_dequeue = 10000 capacity = min_after_dequeue + 3 * batch_size feature_batch, label_batch = tf.train.shuffle_batch( [features, labels], batch_size=batch_size, capacity=capacity, min_after_dequeue=min_after_dequeue) return feature_batch, label_batch
you can use tf.one_hot
# depth = num_clasess in general problem labels = tf.one_hot(tf.stack(attributes[-1]), depth)
Comments
Post a Comment