How to use the tflearn.data_utils.to_categorical function in tflearn

To help you get started, we’ve selected a few tflearn examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Naresh1318 / DiagnosisPredictor / Predictor_word2vec / load_dense_fully_connected_1.py View on Github external
def model1():
    for c, d in enumerate(uniq_diag[:20]):
        # Display the training diagnosis
        print("--------------------Training {}--------------------".format(d))

        # Run each iteration in a graph
        with tf.Graph().as_default():
            y = Y[d].astype(np.float32)
            y = y.reshape(-1, 1)
            y = to_categorical(y, nb_classes=2)  # Convert label to categorical to train with tflearn

            # Train and test data
            X_train, X_test, Y_train, Y_test = train_test_split(X, y, test_size=0.1, random_state=0)

            # Standardize the data
            sc = StandardScaler()
            sc.fit(X_train)
            X_test_sd = sc.transform(X_test)

            # Model
            input_layer = tflearn.input_data(shape=[None, 100], name='input')
            dense1 = tflearn.fully_connected(input_layer, 128, activation='linear', name='dense1')
            dropout1 = tflearn.dropout(dense1, 0.8)
            dense2 = tflearn.fully_connected(dropout1, 128, activation='linear', name='dense2')
            dropout2 = tflearn.dropout(dense2, 0.8)
            output = tflearn.fully_connected(dropout2, 2, activation='softmax', name='output')
github dalmia / Quora-Question-Pairs / code / util.py View on Github external
#     if i % 10 == 0:
    #         train_accuracy = sess.run(accuracy, feed_dict={
    #         x: batch[0], y: batch[1]})
    #         print('Step %d: Training accuracy %g' % (i, train_accuracy))
    #         print("{} Saving checkpoint of model...".format(datetime.now()))
    #
    #         #save checkpoint of the model
    #         checkpoint_name = os.path.join(checkpoint_path, 'model_step'+str(i)+'.ckpt')
    #         save_path = saver.save(sess, checkpoint_name)
    #
    #         print("{} Model checkpoint saved at {}".format(datetime.now(), checkpoint_name))
    #     sess.run(train_step, feed_dict={x: batch[0], y: batch[1]})
    #
    # checkpoint_name = os.path.join(checkpoint_path, 'model_train.ckpt')
    # save_path = saver.save(sess, checkpoint_name)
    validY = to_categorical(validY, nb_classes=2)
    trainY = to_categorical(trainY, nb_classes=2)

    net = tflearn.input_data([None, 100])
    net = tflearn.fully_connected(net, 2, activation='softmax')
    net = tflearn.regression(net, optimizer='adam', learning_rate=0.0001,
                             loss='categorical_crossentropy')
    model = tflearn.DNN(net, tensorboard_verbose=0)
    model.fit(trainX, trainY, validation_set=(validX, validY), show_metric=True,
              batch_size=batch_size, snapshot_epoch=True, n_epoch=n_epoch)
github Naresh1318 / DiagnosisPredictor / Predictor_word2vec / load_dense_fully_connected_1.py View on Github external
def model3():
    for c, d in enumerate(uniq_diag[40:60]):
        # Display the training diagnosis
        print("--------------------Training {}--------------------".format(d))

        # Run each iteration in a graph
        with tf.Graph().as_default():
            y = Y[d].astype(np.float32)
            y = y.reshape(-1, 1)
            y = to_categorical(y, nb_classes=2)  # Convert label to categorical to train with tflearn

            # Train and test data
            X_train, X_test, Y_train, Y_test = train_test_split(X, y, test_size=0.1, random_state=0)

            # Standardize the data
            sc = StandardScaler()
            sc.fit(X_train)
            X_test_sd = sc.transform(X_test)

            # Model
            input_layer = tflearn.input_data(shape=[None, 100], name='input')
            dense1 = tflearn.fully_connected(input_layer, 128, activation='linear', name='dense1')
            dropout1 = tflearn.dropout(dense1, 0.8)
            dense2 = tflearn.fully_connected(dropout1, 128, activation='linear', name='dense2')
            dropout2 = tflearn.dropout(dense2, 0.8)
            output = tflearn.fully_connected(dropout2, 2, activation='softmax', name='output')
github tflearn / tflearn / examples / images / dcgan.py View on Github external
loss='categorical_crossentropy',
                               trainable_vars=gen_vars,
                               batch_size=64, name='target_gen',
                               op_name='GEN')

# Define GAN model, that output the generated images.
gan = tflearn.DNN(gan_model)

# Training
# Prepare input data to feed to the discriminator
disc_noise = np.random.uniform(-1., 1., size=[total_samples, z_dim])
# Prepare target data to feed to the discriminator (0: fake image, 1: real image)
y_disc_fake = np.zeros(shape=[total_samples])
y_disc_real = np.ones(shape=[total_samples])
y_disc_fake = tflearn.data_utils.to_categorical(y_disc_fake, 2)
y_disc_real = tflearn.data_utils.to_categorical(y_disc_real, 2)

# Prepare input data to feed to the stacked generator/discriminator
gen_noise = np.random.uniform(-1., 1., size=[total_samples, z_dim])
# Prepare target data to feed to the discriminator
# Generator tries to fool the discriminator, thus target is 1 (e.g. real images)
y_gen = np.ones(shape=[total_samples])
y_gen = tflearn.data_utils.to_categorical(y_gen, 2)

# Start training, feed both noise and real images.
gan.fit(X_inputs={'input_gen_noise': gen_noise,
                  'input_disc_noise': disc_noise,
                  'input_disc_real': X},
        Y_targets={'target_gen': y_gen,
                   'target_disc_fake': y_disc_fake,
                   'target_disc_real': y_disc_real},
        n_epoch=10)
github tflearn / tflearn / examples / images / residual_network_cifar10.py View on Github external
- [CIFAR-10 Dataset](https://www.cs.toronto.edu/~kriz/cifar.html)

"""

from __future__ import division, print_function, absolute_import

import tflearn

# Residual blocks
# 32 layers: n=5, 56 layers: n=9, 110 layers: n=18
n = 5

# Data loading
from tflearn.datasets import cifar10
(X, Y), (testX, testY) = cifar10.load_data()
Y = tflearn.data_utils.to_categorical(Y)
testY = tflearn.data_utils.to_categorical(testY)

# Real-time data preprocessing
img_prep = tflearn.ImagePreprocessing()
img_prep.add_featurewise_zero_center(per_channel=True)

# Real-time data augmentation
img_aug = tflearn.ImageAugmentation()
img_aug.add_random_flip_leftright()
img_aug.add_random_crop([32, 32], padding=4)

# Building Residual Network
net = tflearn.input_data(shape=[None, 32, 32, 3],
                         data_preprocessing=img_prep,
                         data_augmentation=img_aug)
net = tflearn.conv_2d(net, 16, 3, regularizer='L2', weight_decay=0.0001)
github mindgarage / Ovation / datasets / hotel_reviews.py View on Github external
helpful_votes.append(json_obj["num_helpful_votes"])
            titles.append(datasets.tokenize(json_obj["title"]))
        
        if rescale is not None and one_hot == False:
            ratings_service = datasets.rescale(ratings_service, rescale, [1.0, 5.0])
            ratings_cleanliness = datasets.rescale(ratings_cleanliness, rescale,
                                                   [1.0, 5.0])
            ratings_overall = datasets.rescale(ratings_overall, rescale, [1.0, 5.0])
            ratings_value = datasets.rescale(ratings_value, rescale, [1.0, 5.0])
            ratings_sleep_quality = datasets.rescale(ratings_sleep_quality, rescale,
                                                     [1.0, 5.0])
            ratings_rooms = datasets.rescale(ratings_rooms, rescale, [1.0, 5.0])
        elif rescale is None and one_hot == True:
            ratings_service = to_categorical([x - 1 for x in ratings_service],
                                             nb_classes=5)
            ratings_cleanliness = to_categorical([x - 1 for x in ratings_cleanliness],
                                             nb_classes=5)
            ratings_overall = to_categorical([x - 1 for x in ratings_overall],
                                             nb_classes=5)
            ratings_value = to_categorical([x - 1 for x in ratings_value],
                                             nb_classes=5)
            ratings_sleep_quality = to_categorical([x - 1 for x in ratings_sleep_quality],
                                                   nb_classes=5)
            ratings_rooms = to_categorical([x - 1 for x in ratings_rooms],
                                             nb_classes=5)
        elif rescale is None and one_hot == False:
            pass
        else:
            raise ValueError('rescale and one_hot cannot be set together')
        
        if mark_entities:
            text = datasets.mark_entities(text)
github tflearn / tflearn / examples / images / densenet.py View on Github external
from __future__ import division, print_function, absolute_import

import tflearn

# Growth Rate (12, 16, 32, ...)
k = 12

# Depth (40, 100, ...)
L = 40
nb_layers = int((L - 4) / 3)

# Data loading
from tflearn.datasets import cifar10
(X, Y), (testX, testY) = cifar10.load_data()
Y = tflearn.data_utils.to_categorical(Y)
testY = tflearn.data_utils.to_categorical(testY)

# Real-time data preprocessing
img_prep = tflearn.ImagePreprocessing()
img_prep.add_featurewise_zero_center(per_channel=True)

# Real-time data augmentation
img_aug = tflearn.ImageAugmentation()
img_aug.add_random_flip_leftright()
img_aug.add_random_crop([32, 32], padding=4)

# Building Residual Network
net = tflearn.input_data(shape=[None, 32, 32, 3],
                         data_preprocessing=img_prep,
                         data_augmentation=img_aug)
net = tflearn.conv_2d(net, 16, 3, regularizer='L2', weight_decay=0.0001)
github duoergun0729 / 1book / code / 16-5.py View on Github external
def do_rnn(trainX, testX, trainY, testY):
    global max_sequences_len
    global max_sys_call
    # Data preprocessing
    # Sequence padding

    trainX = pad_sequences(trainX, maxlen=max_sequences_len, value=0.)
    testX = pad_sequences(testX, maxlen=max_sequences_len, value=0.)
    # Converting labels to binary vectors
    trainY = to_categorical(trainY, nb_classes=2)
    testY_old=testY
    testY = to_categorical(testY, nb_classes=2)

    # Network building
    print "GET max_sequences_len embedding %d" % max_sequences_len
    print "GET max_sys_call embedding %d" % max_sys_call

    net = tflearn.input_data([None, max_sequences_len])
    net = tflearn.embedding(net, input_dim=max_sys_call+1, output_dim=128)
    net = tflearn.lstm(net, 128, dropout=0.3)
    net = tflearn.fully_connected(net, 2, activation='softmax')
    net = tflearn.regression(net, optimizer='adam', learning_rate=0.1,
                             loss='categorical_crossentropy')

    # Training
github brightmart / text_classification / aa3_CNNSentenceClassificationTflearn / p4_conv_classification_tflearn.py View on Github external
"""

import tflearn
from tflearn.data_utils import shuffle, to_categorical
from tflearn.layers.core import input_data, dropout, fully_connected
from tflearn.layers.conv import conv_2d, max_pool_2d
from tflearn.layers.estimator import regression
from tflearn.data_preprocessing import ImagePreprocessing
from tflearn.data_augmentation import ImageAugmentation

print("started...")
# Data loading and preprocessing
from tflearn.datasets import cifar10
(X, Y), (X_test, Y_test) = cifar10.load_data()
X, Y = shuffle(X, Y)
Y = to_categorical(Y, 10)
Y_test = to_categorical(Y_test, 10)

# Real-time data preprocessing
img_prep = ImagePreprocessing()
img_prep.add_featurewise_zero_center()
img_prep.add_featurewise_stdnorm()

# Real-time data augmentation
img_aug = ImageAugmentation()
img_aug.add_random_flip_leftright()
img_aug.add_random_rotation(max_angle=25.)

# Convolutional network building
#-------------------------------------------------------------------------------------------
network = input_data(shape=[None, 32, 32, 3],
                     data_preprocessing=img_prep,