def deep_model(self, wide_inputs, n_inputs, n_nodes=[100, 50], use_dropout=False):
        '''
        Model - deep, i.e. two-layer fully connected network model
        '''
        cc_input_var = {}
        cc_embed_var = {}
        flat_vars = []
        if self.verbose:
            print ("--> deep model: %s categories, %d continuous" % (len(self.categorical_columns), n_inputs))
        for cc, cc_size in self.categorical_columns.items():
            cc_input_var[cc] = zqtflearn.input_data(shape=[None, 1], name="%s_in" % cc, dtype=tf.int32)
            # embedding layers only work on CPU!  No GPU implementation in tensorflow, yet!
            cc_embed_var[cc] = zqtflearn.layers.embedding_ops.embedding(cc_input_var[cc], cc_size, 8, name="deep_%s_embed" % cc)
            if self.verbose:
                print ("    %s_embed = %s" % (cc, cc_embed_var[cc]))
            flat_vars.append(tf.squeeze(cc_embed_var[cc], squeeze_dims=[1], name="%s_squeeze" % cc))

        network = tf.concat([wide_inputs] + flat_vars, 1, name="deep_concat")
        for k in range(len(n_nodes)):
            network = zqtflearn.fully_connected(network, n_nodes[k], activation="relu", name="deep_fc%d" % (k + 1))
            if use_dropout:
                network = zqtflearn.dropout(network, 0.5, name="deep_dropout%d" % (k + 1))
        if self.verbose:
            print ("Deep model network before output %s" % network)
        network = zqtflearn.fully_connected(network, 1, activation="linear", name="deep_fc_output", bias=False)
        network = tf.reshape(network, [-1, 1])	# so that accuracy is binary_accuracy
        if self.verbose:
            print ("Deep model network %s" % network)
        return network
def vgg16(input, num_class):

    x = zqtflearn.conv_2d(input, 64, 3, activation='relu', scope='conv1_1')
    x = zqtflearn.conv_2d(x, 64, 3, activation='relu', scope='conv1_2')
    x = zqtflearn.max_pool_2d(x, 2, strides=2, name='maxpool1')

    x = zqtflearn.conv_2d(x, 128, 3, activation='relu', scope='conv2_1')
    x = zqtflearn.conv_2d(x, 128, 3, activation='relu', scope='conv2_2')
    x = zqtflearn.max_pool_2d(x, 2, strides=2, name='maxpool2')

    x = zqtflearn.conv_2d(x, 256, 3, activation='relu', scope='conv3_1')
    x = zqtflearn.conv_2d(x, 256, 3, activation='relu', scope='conv3_2')
    x = zqtflearn.conv_2d(x, 256, 3, activation='relu', scope='conv3_3')
    x = zqtflearn.max_pool_2d(x, 2, strides=2, name='maxpool3')

    x = zqtflearn.conv_2d(x, 512, 3, activation='relu', scope='conv4_1')
    x = zqtflearn.conv_2d(x, 512, 3, activation='relu', scope='conv4_2')
    x = zqtflearn.conv_2d(x, 512, 3, activation='relu', scope='conv4_3')
    x = zqtflearn.max_pool_2d(x, 2, strides=2, name='maxpool4')

    x = zqtflearn.conv_2d(x, 512, 3, activation='relu', scope='conv5_1')
    x = zqtflearn.conv_2d(x, 512, 3, activation='relu', scope='conv5_2')
    x = zqtflearn.conv_2d(x, 512, 3, activation='relu', scope='conv5_3')
    x = zqtflearn.max_pool_2d(x, 2, strides=2, name='maxpool5')

    x = zqtflearn.fully_connected(x, 4096, activation='relu', scope='fc6')
    x = zqtflearn.dropout(x, 0.5, name='dropout1')

    x = zqtflearn.fully_connected(x, 4096, activation='relu', scope='fc7')
    x = zqtflearn.dropout(x, 0.5, name='dropout2')

    x = zqtflearn.fully_connected(x, num_class, activation='softmax', scope='fc8',
                                  restore=False)

    return x
    def deep_model(self, wide_inputs, n_inputs, n_nodes=[100, 50], use_dropout=False):
        '''
        Model - deep, i.e. two-layer fully connected network model
        '''
        cc_input_var = {}
        cc_embed_var = {}
        flat_vars = []
        if self.verbose:
            print ("--> deep model: %s categories, %d continuous" % (len(self.categorical_columns), n_inputs))
        for cc, cc_size in self.categorical_columns.items():
            cc_input_var[cc] = zqtflearn.input_data(shape=[None, 1], name="%s_in" % cc, dtype=tf.int32)#[?,1]
            # embedding layers only work on CPU!  No GPU implementation in tensorflow, yet!
            cc_embed_var[cc] = zqtflearn.layers.embedding_ops.embedding(cc_input_var[cc], cc_size, 8, name="deep_%s_embed" % cc) #[?,1,embedding_size = 8]
            if self.verbose:
                print ("    %s_embed = %s" % (cc, cc_embed_var[cc]))
            flat_vars.append(tf.squeeze(cc_embed_var[cc], squeeze_dims=[1], name="%s_squeeze" % cc)) #[?,8]


        network = tf.concat([wide_inputs] + flat_vars, axis = 1, name="deep_concat") #x=xigma(dim of each element in flat_vars) + wide_inputs.size(1) [?,x]
        #这里是合并的步骤,合并采用的是前后拼接的方式。


        #在这里是合并之后的逻辑,对于合并之后的输入共同处理。
        for k in range(len(n_nodes)):#连续的两个全连接。
            network = zqtflearn.fully_connected(network, n_nodes[k], activation="relu", name="deep_fc%d" % (k + 1)) #默认应该是用bais的。要不然下面为什么要写bias=False
            if use_dropout:
                network = zqtflearn.dropout(network, 0.5, name="deep_dropout%d" % (k + 1))
        if self.verbose:
            print ("Deep model network before output %s" % network)
        network = zqtflearn.fully_connected(network, 1, activation="linear", name="deep_fc_output", bias=False) #[?,1]
        network = tf.reshape(network, [-1, 1])	# so that accuracy is binary_accuracy added by zhengquan ,不reshape不也是[?,1]的吗?可能如果最后的输出维度是1的话,结果是[?]的尺寸
        if self.verbose:
            print ("Deep model network %s" % network)
        return network
 def make_core_network(network):
     dense1 = zqtflearn.fully_connected(network, 64, activation='tanh',
                                        regularizer='L2', weight_decay=0.001, name="dense1")
     dropout1 = zqtflearn.dropout(dense1, 0.8)
     dense2 = zqtflearn.fully_connected(dropout1, 64, activation='tanh',
                                        regularizer='L2', weight_decay=0.001, name="dense2")
     dropout2 = zqtflearn.dropout(dense2, 0.8)
     softmax = zqtflearn.fully_connected(dropout2, 10, activation='softmax', name="softmax")
     return softmax
Exemple #5
0
def discriminator(x, reuse=False):
    with tf.variable_scope('Discriminator', reuse=reuse):
        x = zqtflearn.conv_2d(x, 64, 5, activation='tanh')
        x = zqtflearn.avg_pool_2d(x, 2)
        x = zqtflearn.conv_2d(x, 128, 5, activation='tanh')
        x = zqtflearn.avg_pool_2d(x, 2)
        x = zqtflearn.fully_connected(x, 1024, activation='tanh')
        x = zqtflearn.fully_connected(x, 2)
        x = tf.nn.softmax(x)
        return x
def build_dqn(num_actions, action_repeat):
    """
    Building a DQN.
    """
    inputs = tf.placeholder(tf.float32, [None, action_repeat, 84, 84])
    # Inputs shape: [batch, channel, height, width] need to be changed into
    # shape [batch, height, width, channel]
    net = tf.transpose(inputs, [0, 2, 3, 1])
    net = zqtflearn.conv_2d(net, 32, 8, strides=4, activation='relu')
    net = zqtflearn.conv_2d(net, 64, 4, strides=2, activation='relu')
    net = zqtflearn.fully_connected(net, 256, activation='relu')
    q_values = zqtflearn.fully_connected(net, num_actions)
    return inputs, q_values
    def deep_model(self,
                   wide_inputs,
                   deep_inpus,
                   n_inputs,
                   n_nodes=[100, 50],
                   use_dropout=False):
        '''
        Model - deep, i.e. two-layer fully connected network model
        '''
        self.vocab, self.embedding = self.read_embedding(
            embedding_path="I haven't deal with it")
        self.embedding = np.array(self.embedding, dtype=np.float32)

        net = zqtflearn.layers.embedding_ops.embedding(
            deep_inpus,
            len(self.vocab),
            self.embedding.shape[1],
            trainable=False,
            name="deep_video_ids_embed" % cc)

        network = tf.concat(
            [wide_inputs, net], axis=1, name="deep_concat"
        )  #x=xigma(dim of each element in flat_vars) + wide_inputs.size(1) [?,x]
        #这里是合并的步骤,合并采用的是前后拼接的方式。

        #在这里是合并之后的逻辑,对于合并之后的输入共同处理。
        for k in range(len(n_nodes)):  #连续的两个全连接。
            network = zqtflearn.fully_connected(
                network,
                n_nodes[k],
                activation="relu",
                name="deep_fc%d" % (k + 1))  #默认应该是用bais的。要不然下面为什么要写bias=False
            if use_dropout:
                network = zqtflearn.dropout(network,
                                            0.5,
                                            name="deep_dropout%d" % (k + 1))
        if self.verbose:
            print("Deep model network before output %s" % network)
        network = zqtflearn.fully_connected(network,
                                            1,
                                            activation="linear",
                                            name="deep_fc_output",
                                            bias=False)  #[?,1]
        network = tf.reshape(
            network, [-1, 1]
        )  # so that accuracy is binary_accuracy added by zhengquan ,不reshape不也是[?,1]的吗?可能如果最后的输出维度是1的话,结果是[?]的尺寸
        if self.verbose:
            print("Deep model network %s" % network)
        return network
Exemple #8
0
    def test_sequencegenerator(self):

        with tf.Graph().as_default():
            text = "123456789101234567891012345678910123456789101234567891012345678910"
            maxlen = 5

            X, Y, char_idx = \
                zqtflearn.data_utils.string_to_semi_redundant_sequences(text, seq_maxlen=maxlen, redun_step=3)

            g = zqtflearn.input_data(shape=[None, maxlen, len(char_idx)])
            g = zqtflearn.lstm(g, 32)
            g = zqtflearn.dropout(g, 0.5)
            g = zqtflearn.fully_connected(g, len(char_idx), activation='softmax')
            g = zqtflearn.regression(g, optimizer='adam', loss='categorical_crossentropy',
                                     learning_rate=0.1)

            m = zqtflearn.SequenceGenerator(g, dictionary=char_idx,
                                            seq_maxlen=maxlen,
                                            clip_gradients=5.0)
            m.fit(X, Y, validation_set=0.1, n_epoch=100, snapshot_epoch=False)
            res = m.generate(10, temperature=.5, seq_seed="12345")
            #self.assertEqual(res, "123456789101234", "SequenceGenerator test failed! Generated sequence: " + res + " expected '123456789101234'")

            # Testing save method
            m.save("test_seqgen.zqtflearn")
            self.assertTrue(os.path.exists("test_seqgen.zqtflearn.index"))

            # Testing load method
            m.load("test_seqgen.zqtflearn")
            res = m.generate(10, temperature=.5, seq_seed="12345")
Exemple #9
0
    def test_regression_placeholder(self):
        '''
        Check that regression does not duplicate placeholders
        '''

        with tf.Graph().as_default():

            g = zqtflearn.input_data(shape=[None, 2])
            g_nand = zqtflearn.fully_connected(g, 1, activation='linear')
            with tf.name_scope("Y"):
                Y_in = tf.placeholder(shape=[None, 1], dtype=tf.float32, name="Y")
            zqtflearn.regression(g_nand, optimizer='sgd',
                                 placeholder=Y_in,
                                 learning_rate=2.,
                                 loss='binary_crossentropy',
                                 op_name="regression1",
                                 name="Y")
            # for this test, just use the same default trainable_vars
            # in practice, this should be different for the two regressions
            zqtflearn.regression(g_nand, optimizer='adam',
                                 placeholder=Y_in,
                                 learning_rate=2.,
                                 loss='binary_crossentropy',
                                 op_name="regression2",
                                 name="Y")

            self.assertEqual(len(tf.get_collection(tf.GraphKeys.TARGETS)), 1)
Exemple #10
0
    def test_conv_layers(self):

        X = [[0., 0., 0., 0.], [1., 1., 1., 1.], [0., 0., 1., 0.], [1., 1., 1., 0.]]
        Y = [[1., 0.], [0., 1.], [1., 0.], [0., 1.]]

        with tf.Graph().as_default():
            g = zqtflearn.input_data(shape=[None, 4])
            g = zqtflearn.reshape(g, new_shape=[-1, 2, 2, 1])
            g = zqtflearn.conv_2d(g, 4, 2, activation='relu')
            g = zqtflearn.max_pool_2d(g, 2)
            g = zqtflearn.fully_connected(g, 2, activation='softmax')
            g = zqtflearn.regression(g, optimizer='sgd', learning_rate=1.)

            m = zqtflearn.DNN(g)
            m.fit(X, Y, n_epoch=100, snapshot_epoch=False)
            # TODO: Fix test
            #self.assertGreater(m.predict([[1., 0., 0., 0.]])[0][0], 0.5)

        # Bulk Tests
        with tf.Graph().as_default():
            g = zqtflearn.input_data(shape=[None, 4])
            g = zqtflearn.reshape(g, new_shape=[-1, 2, 2, 1])
            g = zqtflearn.conv_2d(g, 4, 2)
            g = zqtflearn.conv_2d(g, 4, 1)
            g = zqtflearn.conv_2d_transpose(g, 4, 2, [2, 2])
            g = zqtflearn.max_pool_2d(g, 2)
Exemple #11
0
def generator(x, reuse=False):
    with tf.variable_scope('Generator', reuse=reuse):
        x = zqtflearn.fully_connected(x, n_units=7 * 7 * 128)
        x = zqtflearn.batch_normalization(x)
        x = tf.nn.tanh(x)
        x = tf.reshape(x, shape=[-1, 7, 7, 128])
        x = zqtflearn.upsample_2d(x, 2)
        x = zqtflearn.conv_2d(x, 64, 5, activation='tanh')
        x = zqtflearn.upsample_2d(x, 2)
        x = zqtflearn.conv_2d(x, 1, 5, activation='sigmoid')
        return x
 def wide_model(self, inputs, n_inputs):
     '''
     Model - wide, i.e. normal linear model (for logistic regression)
     '''
     network = inputs
     # use fully_connected (instad of single_unit) because fc works properly with batches, whereas single_unit is 1D only
     network = zqtflearn.fully_connected(network, n_inputs, activation="linear", name="wide_linear", bias=False)	# x*W (no bias)
     network = tf.reduce_sum(network, 1, name="reduce_sum")	# batched sum, to produce logits
     network = tf.reshape(network, [-1, 1])	# so that accuracy is binary_accuracy
     if self.verbose:
         print ("Wide model network %s" % network)
     return network
    def __init__(self):
        inputs = zqtflearn.input_data(shape=[None, 784], name="input")

        with tf.variable_scope("scope1") as scope:
            net_conv = Model1.make_core_network(inputs)	# shape (?, 10)
        with tf.variable_scope("scope2") as scope:
            net_dnn = Model2.make_core_network(inputs)	# shape (?, 10)

        network = tf.concat([net_conv, net_dnn], 1, name="concat")	# shape (?, 20)
        network = zqtflearn.fully_connected(network, 10, activation="softmax")
        network = regression(network, optimizer='adam', learning_rate=0.01,
                             loss='categorical_crossentropy', name='target')

        self.model = zqtflearn.DNN(network, tensorboard_verbose=0)
Exemple #14
0
    def test_recurrent_layers(self):

        X = [[1, 3, 5, 7], [2, 4, 8, 10], [1, 5, 9, 11], [2, 6, 8, 0]]
        Y = [[0., 1.], [1., 0.], [0., 1.], [1., 0.]]

        with tf.Graph().as_default():
            g = zqtflearn.input_data(shape=[None, 4])
            g = zqtflearn.embedding(g, input_dim=12, output_dim=4)
            g = zqtflearn.lstm(g, 6)
            g = zqtflearn.fully_connected(g, 2, activation='softmax')
            g = zqtflearn.regression(g, optimizer='sgd', learning_rate=1.)

            m = zqtflearn.DNN(g)
            m.fit(X, Y, n_epoch=300, snapshot_epoch=False)
            self.assertGreater(m.predict([[5, 9, 11, 1]])[0][1], 0.9)
Exemple #15
0
    def test_sequencegenerator_words(self):

        with tf.Graph().as_default():
            text = ["hello","world"]*100
            word_idx = {"hello": 0, "world": 1}
            maxlen = 2

            vec = [x for x in map(word_idx.get, text) if x is not None]

            sequences = []
            next_words = []
            for i in range(0, len(vec) - maxlen, 3):
                sequences.append(vec[i: i + maxlen])
                next_words.append(vec[i + maxlen])

            X = np.zeros((len(sequences), maxlen, len(word_idx)), dtype=np.bool)
            Y = np.zeros((len(sequences), len(word_idx)), dtype=np.bool)
            for i, seq in enumerate(sequences):
                for t, idx in enumerate(seq):
                    X[i, t, idx] = True
                    Y[i, next_words[i]] = True

            g = zqtflearn.input_data(shape=[None, maxlen, len(word_idx)])
            g = zqtflearn.lstm(g, 32)
            g = zqtflearn.dropout(g, 0.5)
            g = zqtflearn.fully_connected(g, len(word_idx), activation='softmax')
            g = zqtflearn.regression(g, optimizer='adam', loss='categorical_crossentropy',
                                     learning_rate=0.1)

            m = zqtflearn.SequenceGenerator(g, dictionary=word_idx,
                                            seq_maxlen=maxlen,
                                            clip_gradients=5.0)
            m.fit(X, Y, validation_set=0.1, n_epoch=100, snapshot_epoch=False)
            res = m.generate(4, temperature=.5, seq_seed=["hello","world"])
            res_str = " ".join(res[-2:])
            self.assertEqual(res_str, "hello world", "SequenceGenerator (word level) test failed! Generated sequence: " + res_str + " expected 'hello world'")

            # Testing save method
            m.save("test_seqgen_word.zqtflearn")
            self.assertTrue(os.path.exists("test_seqgen_word.zqtflearn.index"))

            # Testing load method
            m.load("test_seqgen_word.zqtflearn")
            res = m.generate(4, temperature=.5, seq_seed=["hello","world"])
            res_str = " ".join(res[-2:])
            self.assertEqual(res_str, "hello world", "Reloaded SequenceGenerator (word level) test failed! Generated sequence: " + res_str + " expected 'hello world'")
Exemple #16
0
    def test_feed_dict_no_None(self):

        X = [[0., 0., 0., 0.], [1., 1., 1., 1.], [0., 0., 1., 0.], [1., 1., 1., 0.]]
        Y = [[1., 0.], [0., 1.], [1., 0.], [0., 1.]]

        with tf.Graph().as_default():
            g = zqtflearn.input_data(shape=[None, 4], name="X_in")
            g = zqtflearn.reshape(g, new_shape=[-1, 2, 2, 1])
            g = zqtflearn.conv_2d(g, 4, 2)
            g = zqtflearn.conv_2d(g, 4, 1)
            g = zqtflearn.max_pool_2d(g, 2)
            g = zqtflearn.fully_connected(g, 2, activation='softmax')
            g = zqtflearn.regression(g, optimizer='sgd', learning_rate=1.)

            m = zqtflearn.DNN(g)

            def do_fit():
                m.fit({"X_in": X, 'non_existent': X}, Y, n_epoch=30, snapshot_epoch=False)
            self.assertRaisesRegexp(Exception, "Feed dict asks for variable named 'non_existent' but no such variable is known to exist", do_fit)
 def build_simple_model(self):
     """Build a simple model for test
     Returns:
         DNN, [ (input layer name, input placeholder, input data) ], Target data
     """
     inputPlaceholder1, inputPlaceholder2 = \
         tf.placeholder(tf.float32, (1, 1), name = "input1"), tf.placeholder(tf.float32, (1, 1), name = "input2")
     input1 = zqtflearn.input_data(placeholder=inputPlaceholder1)
     input2 = zqtflearn.input_data(placeholder=inputPlaceholder2)
     network = zqtflearn.merge([input1, input2], "sum")
     network = zqtflearn.reshape(network, (1, 1))
     network = zqtflearn.fully_connected(network, 1)
     network = zqtflearn.regression(network)
     return (
         zqtflearn.DNN(network),
         [("input1:0", inputPlaceholder1, self.INPUT_DATA_1),
          ("input2:0", inputPlaceholder2, self.INPUT_DATA_2)],
         self.TARGET,
     )
Exemple #18
0
    def test_core_layers(self):

        X = [[0., 0.], [0., 1.], [1., 0.], [1., 1.]]
        Y_nand = [[1.], [1.], [1.], [0.]]
        Y_or = [[0.], [1.], [1.], [1.]]

        # Graph definition
        with tf.Graph().as_default():
            # Building a network with 2 optimizers
            g = zqtflearn.input_data(shape=[None, 2])

            # Nand operator definition
            g_nand = zqtflearn.fully_connected(g, 32, activation='linear')
            g_nand = zqtflearn.fully_connected(g_nand, 32, activation='linear')
            g_nand = zqtflearn.fully_connected(g_nand, 1, activation='sigmoid')
            g_nand = zqtflearn.regression(g_nand, optimizer='sgd',
                                          learning_rate=2.,
                                          loss='binary_crossentropy')
            # Or operator definition
            g_or = zqtflearn.fully_connected(g, 32, activation='linear')
            g_or = zqtflearn.fully_connected(g_or, 32, activation='linear')
            g_or = zqtflearn.fully_connected(g_or, 1, activation='sigmoid')
            g_or = zqtflearn.regression(g_or, optimizer='sgd',
                                        learning_rate=2.,
                                        loss='binary_crossentropy')
            # XOR merging Nand and Or operators
            g_xor = zqtflearn.merge([g_nand, g_or], mode='elemwise_mul')

            # Training
            m = zqtflearn.DNN(g_xor)
            m.fit(X, [Y_nand, Y_or], n_epoch=400, snapshot_epoch=False)

            # Testing
            self.assertLess(m.predict([[0., 0.]])[0][0], 0.01)
            self.assertGreater(m.predict([[0., 1.]])[0][0], 0.9)
            self.assertGreater(m.predict([[1., 0.]])[0][0], 0.9)
            self.assertLess(m.predict([[1., 1.]])[0][0], 0.01)

        # Bulk Tests
        with tf.Graph().as_default():
            net = zqtflearn.input_data(shape=[None, 2])
            net = zqtflearn.flatten(net)
            net = zqtflearn.reshape(net, new_shape=[-1])
            net = zqtflearn.activation(net, 'relu')
            net = zqtflearn.dropout(net, 0.5)
            net = zqtflearn.single_unit(net)
Exemple #19
0
Simple Example to train logical operators
"""

from __future__ import absolute_import, division, print_function

import tensorflow as tf
import zqtflearn

# Logical NOT operator
X = [[0.], [1.]]
Y = [[1.], [0.]]

# Graph definition
with tf.Graph().as_default():
    g = zqtflearn.input_data(shape=[None, 1])
    g = zqtflearn.fully_connected(g, 128, activation='linear')
    g = zqtflearn.fully_connected(g, 128, activation='linear')
    g = zqtflearn.fully_connected(g, 1, activation='sigmoid')
    g = zqtflearn.regression(g,
                             optimizer='sgd',
                             learning_rate=2.,
                             loss='mean_square')

    # Model training
    m = zqtflearn.DNN(g)
    m.fit(X, Y, n_epoch=100, snapshot_epoch=False)

    # Test model
    print("Testing NOT operator")
    print("NOT 0:", m.predict([[0.]]))
    print("NOT 1:", m.predict([[1.]]))
Exemple #20
0
if not os.path.isfile(path):
    context = ssl._create_unverified_context()
    moves.urllib.request.urlretrieve("https://raw.githubusercontent.com/tflearn/tflearn.github.io/master/resources/US_Cities.txt", path, context=context)

maxlen = 20

string_utf8 = open(path, "r").read().decode('utf-8')
X, Y, char_idx = \
    string_to_semi_redundant_sequences(string_utf8, seq_maxlen=maxlen, redun_step=3)

g = zqtflearn.input_data(shape=[None, maxlen, len(char_idx)])
g = zqtflearn.lstm(g, 512, return_seq=True)
g = zqtflearn.dropout(g, 0.5)
g = zqtflearn.lstm(g, 512)
g = zqtflearn.dropout(g, 0.5)
g = zqtflearn.fully_connected(g, len(char_idx), activation='softmax')
g = zqtflearn.regression(g, optimizer='adam', loss='categorical_crossentropy',
                         learning_rate=0.001)

m = zqtflearn.SequenceGenerator(g, dictionary=char_idx,
                                seq_maxlen=maxlen,
                                clip_gradients=5.0,
                                checkpoint_path='model_us_cities')

for i in range(40):
    seed = random_sequence_from_string(string_utf8, maxlen)
    m.fit(X, Y, validation_set=0.1, batch_size=128,
          n_epoch=1, run_id='us_cities')
    print("-- TESTING...")
    print("-- Test with temperature of 1.2 --")
    print(m.generate(30, temperature=1.2, seq_seed=seed).encode('utf-8'))
Exemple #21
0
""" An example showing how to save/restore models and retrieve weights. """

from __future__ import absolute_import, division, print_function

import zqtflearn

import zqtflearn.datasets.mnist as mnist

# MNIST Data
X, Y, testX, testY = mnist.load_data(one_hot=True)

# Model
input_layer = zqtflearn.input_data(shape=[None, 784], name='input')
dense1 = zqtflearn.fully_connected(input_layer, 128, name='dense1')
dense2 = zqtflearn.fully_connected(dense1, 256, name='dense2')
softmax = zqtflearn.fully_connected(dense2, 10, activation='softmax')
regression = zqtflearn.regression(softmax,
                                  optimizer='adam',
                                  learning_rate=0.001,
                                  loss='categorical_crossentropy')

# Define classifier, with model checkpoint (autosave)
model = zqtflearn.DNN(regression, checkpoint_path='model.tfl.ckpt')

# Train model, with model checkpoint every epoch and every 200 training steps.
model.fit(
    X,
    Y,
    n_epoch=1,
    validation_set=(testX, testY),
    show_metric=True,
Exemple #22
0
def discriminator(x, reuse=False):
    with tf.variable_scope('Discriminator', reuse=reuse):
        x = zqtflearn.fully_connected(x, 256, activation='relu')
        x = zqtflearn.fully_connected(x, 1, activation='sigmoid')
        return x
Exemple #23
0
"""
from __future__ import division, print_function, absolute_import

import zqtflearn

# Data loading and preprocessing
import zqtflearn.datasets.mnist as mnist

X, Y, testX, testY = mnist.load_data(one_hot=True)

# Building deep neural network
input_layer = zqtflearn.input_data(shape=[None, 784])
dense1 = zqtflearn.fully_connected(input_layer,
                                   64,
                                   activation='elu',
                                   regularizer='L2',
                                   weight_decay=0.001)

#install a deep network of highway layers
highway = dense1
for i in range(10):
    highway = zqtflearn.highway(highway,
                                64,
                                activation='elu',
                                regularizer='L2',
                                weight_decay=0.001,
                                transform_dropout=0.8)

softmax = zqtflearn.fully_connected(highway, 10, activation='softmax')
    def deep_model(self,
                   wide_inputs,
                   deep_inputs,
                   n_inputs,
                   n_nodes=[100, 50],
                   use_dropout=False):
        '''
        Model - deep, i.e. two-layer fully connected network model
        '''
        vocab = pickle.load(open("video_ids.pkl", "rb"))
        vocab = list(vocab)

        self.vocab = {}
        for idx, wd in enumerate(vocab):
            self.vocab[wd] = idx
        self.vocab["[UNK]"] = len(self.vocab)
        if os.path.exists('self_embedding.pkl'):
            self.embedding = pickle.load(open("self_embedding.pkl", "rb"))
            print("making new self.embedding!!!")
        else:
            self.embedding = self.read_embedding(
                embedding_path='videoID_vector.pkl', vocab=self.vocab)
            pickle.dump(self.embedding, open("self_embedding.pkl", "wb"))

        net = zqtflearn.layers.embedding_ops.embedding(
            deep_inputs,
            len(self.vocab),
            self.embedding.shape[1],
            trainable=False,
            name="deep_video_ids_embed")
        net = tf.squeeze(net, squeeze_dims=[1], name="video_ids_squeeze")
        # net = zqtflearn.fully_connected(net, 108 , activation="relu",name="deep_fc_108" )
        # net = zqtflearn.fully_connected(net, 54, activation="relu", name="deep_fc_54")
        network = tf.concat(
            [wide_inputs, net], axis=1, name="deep_concat"
        )  # x=xigma(dim of each element in flat_vars) + wide_inputs.size(1) [?,x]

        print("n_nodes = ", n_nodes)
        for k in range(len(n_nodes)):
            network = zqtflearn.fully_connected(
                network,
                n_nodes[k],
                activation="relu",
                name="deep_fc%d" % (k + 1))  # 默认应该是用bais的。要不然下面为什么要写bias=False
            if use_dropout:
                network = zqtflearn.dropout(network,
                                            0.5,
                                            name="deep_dropout%d" % (k + 1))
        if self.verbose:
            print("Deep model network before output %s" % network)
        network = zqtflearn.fully_connected(network,
                                            1,
                                            activation="linear",
                                            name="deep_fc_output",
                                            bias=False)  # [?,1]

        network = tf.reshape(
            network, [-1, 1]
        )  # so that accuracy is binary_accuracy added by zhengquan ,不reshape不也是[?,1]的吗?可能如果最后的输出维度是1的话,结果是[?]的尺寸
        if self.verbose:
            print("Deep model network %s" % network)
        return network
    [MNIST Dataset] http://yann.lecun.com/exdb/mnist/

"""
from __future__ import division, print_function, absolute_import

import numpy as np
import matplotlib.pyplot as plt
import zqtflearn

# Data loading and preprocessing
import zqtflearn.datasets.mnist as mnist
X, Y, testX, testY = mnist.load_data(one_hot=True)

# Building the encoder
encoder = zqtflearn.input_data(shape=[None, 784])
encoder = zqtflearn.fully_connected(encoder, 256)
encoder = zqtflearn.fully_connected(encoder, 64)

# Building the decoder
decoder = zqtflearn.fully_connected(encoder, 256)
decoder = zqtflearn.fully_connected(decoder, 784, activation='sigmoid')

# Regression, with mean square error
net = zqtflearn.regression(decoder,
                           optimizer='adam',
                           learning_rate=0.001,
                           loss='mean_square',
                           metric=None)

# Training the auto encoder
model = zqtflearn.DNN(net, tensorboard_verbose=0)
import tensorflow as tf

import zqtflearn

# Data loading and preprocessing
import zqtflearn.datasets.mnist as mnist
X, Y, testX, testY = mnist.load_data(one_hot=True)

# Params
original_dim = 784 # MNIST images are 28x28 pixels
hidden_dim = 256
latent_dim = 2

# Building the encoder
encoder = zqtflearn.input_data(shape=[None, 784], name='input_images')
encoder = zqtflearn.fully_connected(encoder, hidden_dim, activation='relu')
z_mean = zqtflearn.fully_connected(encoder, latent_dim)
z_std = zqtflearn.fully_connected(encoder, latent_dim)

# Sampler: Normal (gaussian) random distribution
eps = tf.random_normal(tf.shape(z_std), dtype=tf.float32, mean=0., stddev=1.0,
                       name='epsilon')
z = z_mean + tf.exp(z_std / 2) * eps

# Building the decoder (with scope to re-use these layers later)
decoder = zqtflearn.fully_connected(z, hidden_dim, activation='relu',
                                    scope='decoder_h')
decoder = zqtflearn.fully_connected(decoder, original_dim, activation='sigmoid',
                                    scope='decoder_out')

# Define VAE Loss
Exemple #27
0
    # Placeholders for data and labels
    X = tf.placeholder(shape=(None, 784), dtype=tf.float32)
    Y = tf.placeholder(shape=(None, 10), dtype=tf.float32)

    net = tf.reshape(X, [-1, 28, 28, 1])

    # Using TFLearn wrappers for network building
    net = zqtflearn.conv_2d(net, 32, 3, activation='relu')
    net = zqtflearn.max_pool_2d(net, 2)
    net = zqtflearn.local_response_normalization(net)
    net = zqtflearn.dropout(net, 0.8)
    net = zqtflearn.conv_2d(net, 64, 3, activation='relu')
    net = zqtflearn.max_pool_2d(net, 2)
    net = zqtflearn.local_response_normalization(net)
    net = zqtflearn.dropout(net, 0.8)
    net = zqtflearn.fully_connected(net, 128, activation='tanh')
    net = zqtflearn.dropout(net, 0.8)
    net = zqtflearn.fully_connected(net, 256, activation='tanh')
    net = zqtflearn.dropout(net, 0.8)
    net = zqtflearn.fully_connected(net, 10, activation='linear')

    # Defining other ops using Tensorflow
    loss = tf.reduce_mean(
        tf.nn.softmax_cross_entropy_with_logits_v2(logits=net, labels=Y))
    optimizer = tf.train.AdamOptimizer(learning_rate=0.01).minimize(loss)

    # Initializing the variables
    init = tf.global_variables_initializer()

    # Launch the graph
    with tf.Session() as sess:
"""

from __future__ import absolute_import, division, print_function

import zqtflearn
import numpy as np

# Regression data- 10 training instances
#10 input features per instance.
X=np.random.rand(10,10).tolist()
#2 output features per instance
Y=np.random.rand(10,2).tolist()

# Multiple Regression graph, 10-d input layer
input_ = zqtflearn.input_data(shape=[None, 10])
#10-d fully connected layer
r1 = zqtflearn.fully_connected(input_, 10)
#2-d fully connected layer for output
r1 = zqtflearn.fully_connected(r1, 2)
r1 = zqtflearn.regression(r1, optimizer='sgd', loss='mean_square',
                          metric='R2', learning_rate=0.01)

m = zqtflearn.DNN(r1)
m.fit(X,Y, n_epoch=100, show_metric=True, snapshot_epoch=False)

#Predict for 1 instance
testinstance=np.random.rand(1,10).tolist()
print("\nInput features:  ",testinstance)
print("\n Predicted output: ")
print(m.predict(testinstance))
Exemple #29
0
                                n_words=10000,
                                valid_portion=0.1)
trainX, trainY = train
testX, testY = test

# Data preprocessing
# Sequence padding
trainX = pad_sequences(trainX, maxlen=100, value=0.)
testX = pad_sequences(testX, maxlen=100, value=0.)
# Converting labels to binary vectors
trainY = to_categorical(trainY)
testY = to_categorical(testY)

# Network building
net = zqtflearn.input_data([None, 100])
net = zqtflearn.embedding(net, input_dim=10000, output_dim=128)
net = zqtflearn.lstm(net, 128, dropout=0.8)
net = zqtflearn.fully_connected(net, 2, activation='softmax')
net = zqtflearn.regression(net,
                           optimizer='adam',
                           learning_rate=0.001,
                           loss='categorical_crossentropy')

# Training
model = zqtflearn.DNN(net, tensorboard_verbose=0)
model.fit(trainX,
          trainY,
          validation_set=(testX, testY),
          show_metric=True,
          batch_size=32)
Exemple #30
0
"""
from __future__ import division, print_function, absolute_import

import zqtflearn

# Data loading and preprocessing
import zqtflearn.datasets.mnist as mnist

X, Y, testX, testY = mnist.load_data(one_hot=True)

# Building deep neural network
input_layer = zqtflearn.input_data(shape=[None, 784])
dense1 = zqtflearn.fully_connected(input_layer,
                                   64,
                                   activation='tanh',
                                   regularizer='L2',
                                   weight_decay=0.001)
dropout1 = zqtflearn.dropout(dense1, 0.8)
dense2 = zqtflearn.fully_connected(dropout1,
                                   64,
                                   activation='tanh',
                                   regularizer='L2',
                                   weight_decay=0.001)
dropout2 = zqtflearn.dropout(dense2, 0.8)
softmax = zqtflearn.fully_connected(dropout2, 10, activation='softmax')

# Regression using SGD with learning rate decay and Top-3 accuracy
sgd = zqtflearn.SGD(learning_rate=0.1, lr_decay=0.96, decay_step=1000)
top_k = zqtflearn.metrics.Top_k(3)
net = zqtflearn.regression(softmax,