Exemple #1
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 #2
0
    def test_dnn(self):

        with tf.Graph().as_default():
            X = [3.3,4.4,5.5,6.71,6.93,4.168,9.779,6.182,7.59,2.167,7.042,10.791,5.313,7.997,5.654,9.27,3.1]
            Y = [1.7,2.76,2.09,3.19,1.694,1.573,3.366,2.596,2.53,1.221,2.827,3.465,1.65,2.904,2.42,2.94,1.3]
            input = zqtflearn.input_data(shape=[None])
            linear = zqtflearn.single_unit(input)
            regression = zqtflearn.regression(linear, optimizer='sgd', loss='mean_square',
                                              metric='R2', learning_rate=0.01)
            m = zqtflearn.DNN(regression)
            # Testing fit and predict
            m.fit(X, Y, n_epoch=1000, show_metric=True, snapshot_epoch=False)
            res = m.predict([3.2])[0]
            self.assertGreater(res, 1.3, "DNN test (linear regression) failed! with score: " + str(res) + " expected > 1.3")
            self.assertLess(res, 1.8, "DNN test (linear regression) failed! with score: " + str(res) + " expected < 1.8")

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

        with tf.Graph().as_default():
            input = zqtflearn.input_data(shape=[None])
            linear = zqtflearn.single_unit(input)
            regression = zqtflearn.regression(linear, optimizer='sgd', loss='mean_square',
                                              metric='R2', learning_rate=0.01)
            m = zqtflearn.DNN(regression)

            # Testing load method
            m.load("test_dnn.zqtflearn")
            res = m.predict([3.2])[0]
            self.assertGreater(res, 1.3, "DNN test (linear regression) failed after loading model! score: " + str(res) + " expected > 1.3")
            self.assertLess(res, 1.8, "DNN test (linear regression) failed after loading model! score: " + str(res) + " expected < 1.8")
    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
Exemple #4
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")
    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
Exemple #6
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)
 def __init__(self):
     network = zqtflearn.input_data(shape=[None, 784], name="input")
     network = self.make_core_network(network)
     network = regression(network, optimizer='adam', learning_rate=0.01,
                          loss='categorical_crossentropy', name='target')
     
     model = zqtflearn.DNN(network, tensorboard_verbose=0)
     self.model = model
Exemple #8
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)
 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,
     )
    def __init__(self):
        # Building deep neural network
        network = zqtflearn.input_data(shape=[None, 784], name="input")
        network = self.make_core_network(network)

        # 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)

        network = zqtflearn.regression(network, optimizer=sgd, metric=top_k,
                                       loss='categorical_crossentropy', name="target")
        model = zqtflearn.DNN(network, tensorboard_verbose=0)
        self.model = model
    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 #12
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 #13
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 #14
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)
    # Sort by descending id and delete columns
    for column_to_delete in sorted(columns_to_delete, reverse=True):
        [passenger.pop(column_to_delete) for passenger in passengers]
    for i in range(len(passengers)):
        # Converting 'sex' field to float (id is 1 after removing labels column)
        passengers[i][1] = 1. if passengers[i][1] == 'female' else 0.
    return np.array(passengers, dtype=np.float32)

# Ignore 'name' and 'ticket' columns (id 1 & 6 of data array)
to_ignore=[1, 6]

# Preprocess data
data = preprocess(data, to_ignore)

# Build neural network
net = zqtflearn.input_data(shape=[None, 6])
net = zqtflearn.fully_connected(net, 32)
net = zqtflearn.fully_connected(net, 32)
net = zqtflearn.fully_connected(net, 2, activation='softmax')
net = zqtflearn.regression(net)

# Define model
model = zqtflearn.DNN(net)
# Start training (apply gradient descent algorithm)
model.fit(data, labels, n_epoch=10, batch_size=16, show_metric=True)

# Let's create some data for DiCaprio and Winslet
dicaprio = [3, 'Jack Dawson', 'male', 19, 0, 0, 'N/A', 5.0000]
winslet = [1, 'Rose DeWitt Bukater', 'female', 17, 1, 2, 'N/A', 100.0000]
# Preprocess data
dicaprio, winslet = preprocess([dicaprio, winslet], to_ignore)
Exemple #16
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.]]))
Exemple #17
0
# Discriminator
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


# Input Data
gen_input = zqtflearn.input_data(shape=[None, z_dim], name='input_gen_noise')
input_disc_noise = zqtflearn.input_data(shape=[None, z_dim],
                                        name='input_disc_noise')
input_disc_real = zqtflearn.input_data(shape=[None, 28, 28, 1],
                                       name='input_disc_real')

# Build Discriminator
disc_fake = discriminator(generator(input_disc_noise))
disc_real = discriminator(input_disc_real, reuse=True)
disc_net = tf.concat([disc_fake, disc_real], axis=0)
# Build Stacked Generator/Discriminator
gen_net = generator(gen_input, reuse=True)
stacked_gan_net = discriminator(gen_net, reuse=True)

# Build Training Ops for both Generator and Discriminator.
# Each network optimization should only update its own variable, thus we need
    def build_model(self, learning_rate=[0.001, 0.01]):
        '''
        Model - wide and deep - built using zqtflearn
        '''
        n_cc = len(self.continuous_columns)
        n_cc = 108

        input_shape = [None, n_cc]
        if self.verbose:
            print("=" * 77 + " Model %s (type=%s)" %
                  (self.name, self.model_type))
            print("  Input placeholder shape=%s" % str(input_shape))
        wide_inputs = zqtflearn.input_data(shape=input_shape, name="wide_X")
        deep_inputs = zqtflearn.input_data(shape=[None, 1], name="deep_X")
        if not isinstance(learning_rate, list):
            learning_rate = [learning_rate, learning_rate]  # wide, deep
        if self.verbose:
            print("  Learning rates (wide, deep)=%s" % learning_rate)

        with tf.name_scope(
                "Y"):  # placeholder for target variable (i.e. trainY input)
            Y_in = tf.placeholder(shape=[None, 1], dtype=tf.float32, name="Y")

        with tf.variable_op_scope([wide_inputs], None, "cb_unit",
                                  reuse=False) as scope:
            central_bias = zqtflearn.variables.variable(
                'central_bias',
                shape=[1],
                initializer=tf.constant_initializer(np.random.randn()),
                trainable=True,
                restore=True)
            tf.add_to_collection(tf.GraphKeys.LAYER_VARIABLES + '/cb_unit',
                                 central_bias)

        if 'wide' in self.model_type:
            wide_network = self.wide_model(wide_inputs, n_cc)
            network = wide_network
            wide_network_with_bias = tf.add(wide_network,
                                            central_bias,
                                            name="wide_with_bias")

        if 'deep' in self.model_type:
            deep_network = self.deep_model(
                wide_inputs, deep_inputs, n_cc
            )  # 这里面是wide inputs,在这个函数内部wide_inputs,会和deep_model制造的输入相合并。
            deep_network_with_bias = tf.add(deep_network,
                                            central_bias,
                                            name="deep_with_bias")
            if 'wide' in self.model_type:
                network = tf.add(wide_network, deep_network)
                if self.verbose:
                    print("Wide + deep model network %s" % network)
            else:
                network = deep_network

        network = tf.add(network, central_bias, name="add_central_bias")

        # add validation monitor summaries giving confusion matrix entries
        with tf.name_scope('Monitors'):
            predictions = tf.cast(tf.greater(network, 0), tf.int64)
            print("predictions=%s" % predictions)
            Ybool = tf.cast(Y_in, tf.bool)
            print("Ybool=%s" % Ybool)
            pos = tf.boolean_mask(predictions, Ybool)
            neg = tf.boolean_mask(predictions, ~Ybool)
            psize = tf.cast(tf.shape(pos)[0], tf.int64)
            nsize = tf.cast(tf.shape(neg)[0], tf.int64)
            true_positive = tf.reduce_sum(pos, name="true_positive")
            false_negative = tf.subtract(psize,
                                         true_positive,
                                         name="false_negative")
            false_positive = tf.reduce_sum(neg, name="false_positive")
            true_negative = tf.subtract(nsize,
                                        false_positive,
                                        name="true_negative")
            overall_accuracy = tf.truediv(tf.add(true_positive, true_negative),
                                          tf.add(nsize, psize),
                                          name="overall_accuracy")
        vmset = [
            true_positive, true_negative, false_positive, false_negative,
            overall_accuracy
        ]

        trainable_vars = tf.trainable_variables()
        tv_deep = [v for v in trainable_vars if v.name.startswith('deep_')]
        tv_wide = [v for v in trainable_vars if v.name.startswith('wide_')]

        if self.verbose:
            print("DEEP trainable_vars")
            for v in tv_deep:
                print("  Variable %s: %s" % (v.name, v))
            print("WIDE trainable_vars")
            for v in tv_wide:
                print("  Variable %s: %s" % (v.name, v))

        # if 'wide' in self.model_type:
        #     if not 'deep' in self.model_type:
        #         tv_wide.append(central_bias)
        #     zqtflearn.regression(wide_network_with_bias,
        #                          placeholder=Y_in,
        #                          optimizer='sgd',
        #                          loss='roc_auc_score',
        #                          #loss='binary_crossentropy',
        #                          metric="accuracy",
        #                          learning_rate=learning_rate[0],
        #                          validation_monitors=vmset,
        #                          trainable_vars=tv_wide,
        #                          op_name="wide_regression",
        #                          name="Y")
        #
        # if 'deep' in self.model_type:
        #     if not 'wide' in self.model_type:
        #         tv_wide.append(central_bias)
        #     zqtflearn.regression(deep_network_with_bias,
        #                          placeholder=Y_in,
        #                          optimizer='adam',
        #                          loss='roc_auc_score',
        #                          #loss='binary_crossentropy',
        #                          metric="accuracy",
        #                          learning_rate=learning_rate[1],
        #                          validation_monitors=vmset if not 'wide' in self.model_type else None,
        #                          trainable_vars=tv_deep,
        #                          op_name="deep_regression",
        #                          name="Y")

        if self.model_type == 'wide+deep':  # learn central bias separately for wide+deep
            zqtflearn.regression(
                network,
                placeholder=Y_in,
                optimizer='adam',
                #loss="roc_auc_score",
                loss='binary_crossentropy',
                metric="accuracy",
                validation_monitors=vmset,
                learning_rate=learning_rate[0],  # use wide learning rate
                #trainable_vars=[central_bias], #[tv_deep,tv_wide,central_bias] # None
                op_name="central_bias_regression",
                name="Y")

        self.model = zqtflearn.DNN(
            network,
            tensorboard_verbose=self.tensorboard_verbose,
            max_checkpoints=self.max_checkpoints,
            checkpoint_path="%s/%s.tfl" % (self.checkpoints_dir, self.name),
            tensorboard_dir=self.tensorboard_dir)
        # tensorboard_dir="/tmp/tflearn_logs/" zqtflearn.DNN 我把他改为当前目录下的了,这样也比较好规范
        if 'deep' in self.model_type:
            embeddingWeights = zqtflearn.get_layer_variables_by_name(
                'deep_video_ids_embed')[0]
            # CUSTOM_WEIGHT = pickle.load("Haven't deal")
            # emb = np.array(CUSTOM_WEIGHT, dtype=np.float32)
            # emb = self.embedding
            new_emb_t = tf.convert_to_tensor(self.embedding)
            self.model.set_weights(embeddingWeights, new_emb_t)

        if self.verbose:
            print("Target variables:")
            for v in tf.get_collection(tf.GraphKeys.TARGETS):
                print("  variable %s: %s" % (v.name, v))

            print("=" * 77)

        print("model build finish")
from scipy.stats import norm
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')
Exemple #20
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,
X, Y = image_preloader(files_list, image_shape=(224, 224), mode='file',
                       categorical_labels=True, normalize=False,
                       files_extension=['.jpg', '.png'], filter_channel=True)
# or use the mode 'floder'
# X, Y = image_preloader(data_dir, image_shape=(224, 224), mode='folder',
#                        categorical_labels=True, normalize=True,
#                        files_extension=['.jpg', '.png'], filter_channel=True)

num_classes = 10 # num of your dataset

# VGG preprocessing
img_prep = ImagePreprocessing()
img_prep.add_featurewise_zero_center(mean=[123.68, 116.779, 103.939],
                                     per_channel=True)
# VGG Network
x = zqtflearn.input_data(shape=[None, 224, 224, 3], name='input',
                         data_preprocessing=img_prep)
softmax = vgg16(x, num_classes)
regression = zqtflearn.regression(softmax, optimizer='adam',
                                  loss='categorical_crossentropy',
                                  learning_rate=0.001, restore=False)

model = zqtflearn.DNN(regression, checkpoint_path='vgg-finetuning',
                      max_checkpoints=3, tensorboard_verbose=2,
                      tensorboard_dir="./logs")

model_file = os.path.join(model_path, "vgg16.zqtflearn")
model.load(model_file, weights_only=True)

# Start finetuning
model.fit(X, Y, n_epoch=10, validation_set=0.1, shuffle=True,
          show_metric=True, batch_size=64, snapshot_epoch=False,
    def test_dnn_loading_scope(self):

        with tf.Graph().as_default():
            X = [
                3.3, 4.4, 5.5, 6.71, 6.93, 4.168, 9.779, 6.182, 7.59, 2.167,
                7.042, 10.791, 5.313, 7.997, 5.654, 9.27, 3.1
            ]
            Y = [
                1.7, 2.76, 2.09, 3.19, 1.694, 1.573, 3.366, 2.596, 2.53, 1.221,
                2.827, 3.465, 1.65, 2.904, 2.42, 2.94, 1.3
            ]
            input = zqtflearn.input_data(shape=[None])
            linear = zqtflearn.single_unit(input)
            regression = zqtflearn.regression(linear,
                                              optimizer='sgd',
                                              loss='mean_square',
                                              metric='R2',
                                              learning_rate=0.01)
            m = zqtflearn.DNN(regression)
            # Testing fit and predict
            m.fit(X, Y, n_epoch=1000, show_metric=True, snapshot_epoch=False)
            res = m.predict([3.2])[0]
            self.assertGreater(
                res, 1.3, "DNN test (linear regression) failed! with score: " +
                str(res) + " expected > 1.3")
            self.assertLess(
                res, 1.8, "DNN test (linear regression) failed! with score: " +
                str(res) + " expected < 1.8")

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

        # Testing loading, with change of variable scope (saved with no scope, now loading into scopeA)
        with tf.Graph().as_default():  # start with clear graph
            with tf.variable_scope("scopeA") as scope:
                input = zqtflearn.input_data(shape=[None])
                linear = zqtflearn.single_unit(input)
                regression = zqtflearn.regression(linear,
                                                  optimizer='sgd',
                                                  loss='mean_square',
                                                  metric='R2',
                                                  learning_rate=0.01)
                m = zqtflearn.DNN(regression)

                def try_load():
                    m.load("test_dnn.zqtflearn")

                self.assertRaises(
                    tf.errors.NotFoundError,
                    try_load)  # fails, since names in file don't have "scopeA"

                m.load("test_dnn.zqtflearn", variable_name_map=(
                    "scopeA/",
                    ""))  # succeeds, because variable names are rewritten
                res = m.predict([3.2])[0]
                self.assertGreater(
                    res, 1.3,
                    "DNN test (linear regression) failed after loading model! score: "
                    + str(res) + " expected > 1.3")
                self.assertLess(
                    res, 1.8,
                    "DNN test (linear regression) failed after loading model! score: "
                    + str(res) + " expected < 1.8")
from __future__ import absolute_import, division, print_function

import zqtflearn

# Regression data
X = [
    3.3, 4.4, 5.5, 6.71, 6.93, 4.168, 9.779, 6.182, 7.59, 2.167, 7.042, 10.791,
    5.313, 7.997, 5.654, 9.27, 3.1
]
Y = [
    1.7, 2.76, 2.09, 3.19, 1.694, 1.573, 3.366, 2.596, 2.53, 1.221, 2.827,
    3.465, 1.65, 2.904, 2.42, 2.94, 1.3
]

# Linear Regression graph
input_ = zqtflearn.input_data(shape=[None])
linear = zqtflearn.single_unit(input_)
regression = zqtflearn.regression(linear,
                                  optimizer='sgd',
                                  loss='mean_square',
                                  metric='R2',
                                  learning_rate=0.01)
m = zqtflearn.DNN(regression)
m.fit(X, Y, n_epoch=1000, show_metric=True, snapshot_epoch=False)

print("\nRegression result:")
print("Y = " + str(m.get_weights(linear.W)) + "*X + " +
      str(m.get_weights(linear.b)))

print("\nTest prediction for x = 3.2, 3.3, 3.4:")
print(m.predict([3.2, 3.3, 3.4]))
Exemple #24
0
train, test, _ = imdb.load_data(path='imdb.pkl',
                                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 #25
0
from __future__ import division, print_function, absolute_import

import zqtflearn
import zqtflearn.data_utils as du

# Data loading and preprocessing
import zqtflearn.datasets.mnist as mnist
X, Y, testX, testY = mnist.load_data(one_hot=True)
X = X.reshape([-1, 28, 28, 1])
testX = testX.reshape([-1, 28, 28, 1])
X, mean = du.featurewise_zero_center(X)
testX = du.featurewise_zero_center(testX, mean)

# Building Residual Network
net = zqtflearn.input_data(shape=[None, 28, 28, 1])
net = zqtflearn.conv_2d(net, 64, 3, activation='relu', bias=False)
# Residual blocks
net = zqtflearn.residual_bottleneck(net, 3, 16, 64)
net = zqtflearn.residual_bottleneck(net, 1, 32, 128, downsample=True)
net = zqtflearn.residual_bottleneck(net, 2, 32, 128)
net = zqtflearn.residual_bottleneck(net, 1, 64, 256, downsample=True)
net = zqtflearn.residual_bottleneck(net, 2, 64, 256)
net = zqtflearn.batch_normalization(net)
net = zqtflearn.activation(net, 'relu')
net = zqtflearn.global_avg_pool(net)
# Regression
net = zqtflearn.fully_connected(net, 10, activation='softmax')
net = zqtflearn.regression(net,
                           optimizer='momentum',
                           loss='categorical_crossentropy',
    def build_model(self, learning_rate=[0.001, 0.01]):
        '''
        Model - wide and deep - built using tflearn
        '''
        n_cc = len(self.continuous_columns)
        n_cc = 108

        input_shape = [None, n_cc]
        if self.verbose:
            print("=" * 77 + " Model %s (type=%s)" %
                  (self.name, self.model_type))
            print("  Input placeholder shape=%s" % str(input_shape))
        wide_inputs = zqtflearn.input_data(shape=input_shape, name="wide_X")
        if not isinstance(learning_rate, list):
            learning_rate = [learning_rate, learning_rate]  # wide, deep
        if self.verbose:
            print("  Learning rates (wide, deep)=%s" % learning_rate)

        with tf.name_scope(
                "Y"):  # placeholder for target variable (i.e. trainY input)
            Y_in = tf.placeholder(shape=[None, 1], dtype=tf.float32, name="Y")

        with tf.variable_op_scope([wide_inputs], None, "cb_unit",
                                  reuse=False) as scope:
            central_bias = zqtflearn.variables.variable(
                'central_bias',
                shape=[1],
                initializer=tf.constant_initializer(np.random.randn()),
                trainable=True,
                restore=True)
            tf.add_to_collection(tf.GraphKeys.LAYER_VARIABLES + '/cb_unit',
                                 central_bias)

        wide_network = self.wide_model(wide_inputs, n_cc)
        network = wide_network
        network = tf.add(network, central_bias, name="add_central_bias")

        # add validation monitor summaries giving confusion matrix entries
        with tf.name_scope('Monitors'):
            predictions = tf.cast(tf.greater(network, 0), tf.int64)
            print("predictions=%s" % predictions)
            Ybool = tf.cast(Y_in, tf.bool)
            print("Ybool=%s" % Ybool)
            pos = tf.boolean_mask(predictions, Ybool)
            neg = tf.boolean_mask(predictions, ~Ybool)
            psize = tf.cast(tf.shape(pos)[0], tf.int64)
            nsize = tf.cast(tf.shape(neg)[0], tf.int64)
            true_positive = tf.reduce_sum(pos, name="true_positive")
            false_negative = tf.subtract(psize,
                                         true_positive,
                                         name="false_negative")
            false_positive = tf.reduce_sum(neg, name="false_positive")
            true_negative = tf.subtract(nsize,
                                        false_positive,
                                        name="true_negative")
            overall_accuracy = tf.truediv(tf.add(true_positive, true_negative),
                                          tf.add(nsize, psize),
                                          name="overall_accuracy")
        vmset = [
            true_positive, true_negative, false_positive, false_negative,
            overall_accuracy
        ]

        zqtflearn.regression(
            network,
            placeholder=Y_in,
            optimizer='adam',
            #loss="roc_auc_score",
            loss='binary_crossentropy',
            metric="accuracy",
            learning_rate=learning_rate[0],  # use wide learning rate
            # trainable_vars=[central_bias],
            validation_monitors=vmset,
            op_name="central_bias_regression",
            name="Y")

        self.model = zqtflearn.DNN(
            network,
            tensorboard_verbose=self.tensorboard_verbose,
            max_checkpoints=self.max_checkpoints,
            checkpoint_path="%s/%s.tfl" % (self.checkpoints_dir, self.name),
            tensorboard_dir=self.tensorboard_dir)

        if self.verbose:
            print("Target variables:")
            for v in tf.get_collection(tf.GraphKeys.TARGETS):
                print("  variable %s: %s" % (v.name, v))

            print("=" * 77)
Exemple #27
0
Links:
    [MNIST Dataset] http://yann.lecun.com/exdb/mnist/

"""
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
    def build_model(self, learning_rate=[0.001, 0.01]):
        '''
        Model - wide and deep - built using zqtflearn
        '''
        n_cc = len(self.continuous_columns)
        n_categories = 1			# two categories: is_idv and is_not_idv
        input_shape = [None, n_cc]
        if self.verbose:
            print ("="*77 + " Model %s (type=%s)" % (self.name, self.model_type))
            print ("  Input placeholder shape=%s" % str(input_shape))
        wide_inputs = zqtflearn.input_data(shape=input_shape, name="wide_X")
        if not isinstance(learning_rate, list):
            learning_rate = [learning_rate, learning_rate]	# wide, deep
        if self.verbose:
            print ("  Learning rates (wide, deep)=%s" % learning_rate)

        with tf.name_scope("Y"):			# placeholder for target variable (i.e. trainY input)
            Y_in = tf.placeholder(shape=[None, 1], dtype=tf.float32, name="Y")

        with tf.variable_scope(None, "cb_unit", [wide_inputs]) as scope:
            central_bias = zqtflearn.variables.variable('central_bias', shape=[1],
                                                        initializer=tf.constant_initializer(np.random.randn()),
                                                        trainable=True, restore=True)
            tf.add_to_collection(tf.GraphKeys.LAYER_VARIABLES + '/cb_unit', central_bias)

        if 'wide' in self.model_type:
            wide_network = self.wide_model(wide_inputs, n_cc)
            network = wide_network
            wide_network_with_bias = tf.add(wide_network, central_bias, name="wide_with_bias")

        if 'deep' in self.model_type:
            deep_network = self.deep_model(wide_inputs, n_cc)
            deep_network_with_bias = tf.add(deep_network, central_bias, name="deep_with_bias")
            if 'wide' in self.model_type:
                network = tf.add(wide_network, deep_network)
                if self.verbose:
                    print ("Wide + deep model network %s" % network)
            else:
                network = deep_network

        network = tf.add(network, central_bias, name="add_central_bias")

        # add validation monitor summaries giving confusion matrix entries
        with tf.name_scope('Monitors'):
            predictions = tf.cast(tf.greater(network, 0), tf.int64)
            print ("predictions=%s" % predictions)
            Ybool = tf.cast(Y_in, tf.bool)
            print ("Ybool=%s" % Ybool)
            pos = tf.boolean_mask(predictions, Ybool)
            neg = tf.boolean_mask(predictions, ~Ybool)
            psize = tf.cast(tf.shape(pos)[0], tf.int64)
            nsize = tf.cast(tf.shape(neg)[0], tf.int64)
            true_positive = tf.reduce_sum(pos, name="true_positive")
            false_negative = tf.subtract(psize, true_positive, name="false_negative")
            false_positive = tf.reduce_sum(neg, name="false_positive")
            true_negative = tf.subtract(nsize, false_positive, name="true_negative")
            overall_accuracy = tf.truediv(tf.add(true_positive, true_negative), tf.add(nsize, psize), name="overall_accuracy")
        vmset = [true_positive, true_negative, false_positive, false_negative, overall_accuracy]

        trainable_vars = tf.trainable_variables()
        tv_deep = [v for v in trainable_vars if v.name.startswith('deep_')]
        tv_wide = [v for v in trainable_vars if v.name.startswith('wide_')]

        if self.verbose:
            print ("DEEP trainable_vars")
            for v in tv_deep:
                print ("  Variable %s: %s" % (v.name, v))
            print ("WIDE trainable_vars")
            for v in tv_wide:
                print ("  Variable %s: %s" % (v.name, v))

        if 'wide' in self.model_type:
            if not 'deep' in self.model_type:
                tv_wide.append(central_bias)
            zqtflearn.regression(wide_network_with_bias,
                                 placeholder=Y_in,
                                 optimizer='sgd',
                                 #loss='roc_auc_score',
                                 loss='binary_crossentropy',
                                 metric="accuracy",
                                 learning_rate=learning_rate[0],
                                 validation_monitors=vmset,
                                 trainable_vars=tv_wide,
                                 op_name="wide_regression",
                                 name="Y")

        if 'deep' in self.model_type:
            if not 'wide' in self.model_type:
                tv_wide.append(central_bias)
            zqtflearn.regression(deep_network_with_bias,
                                 placeholder=Y_in,
                                 optimizer='adam',
                                 #loss='roc_auc_score',
                                 loss='binary_crossentropy',
                                 metric="accuracy",
                                 learning_rate=learning_rate[1],
                                 validation_monitors=vmset if not 'wide' in self.model_type else None,
                                 trainable_vars=tv_deep,
                                 op_name="deep_regression",
                                 name="Y")

        if self.model_type=='wide+deep':	# learn central bias separately for wide+deep
            zqtflearn.regression(network,
                                 placeholder=Y_in,
                                 optimizer='adam',
                                 loss='binary_crossentropy',
                                 metric="accuracy",
                                 learning_rate=learning_rate[0],  # use wide learning rate
                                 trainable_vars=[central_bias],
                                 op_name="central_bias_regression",
                                 name="Y")

        self.model = zqtflearn.DNN(network,
                                   tensorboard_verbose=self.tensorboard_verbose,
                                   max_checkpoints=5,
                                   checkpoint_path="%s/%s.tfl" % (self.checkpoints_dir, self.name),
                                   )

        if self.verbose:
            print ("Target variables:")
            for v in tf.get_collection(tf.GraphKeys.TARGETS):
                print ("  variable %s: %s" % (v.name, v))

            print ("="*77)
Links:
    [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
Exemple #30
0
import zqtflearn
from zqtflearn.data_utils import *

path = "US_Cities.txt"
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)