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 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)
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'")
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)
# NOTE: Padding is required for dimension consistency. This will pad sequences # with 0 at the end, until it reaches the max sequence length. 0 is used as a # masking value by dynamic RNNs in TFLearn; a sequence length will be # retrieved by counting non zero elements in a sequence. Then dynamic RNN step # computation is performed according to that length. 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]) # Masking is not required for embedding, sequence length is computed prior to # the embedding op and assigned as 'seq_length' attribute to the returned Tensor. net = zqtflearn.embedding(net, input_dim=10000, output_dim=128) net = zqtflearn.lstm(net, 128, dropout=0.8, dynamic=True) 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)
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) m.fit(X, Y, validation_set=0.1, batch_size=128,
# -*- coding: utf-8 -*- """ MNIST Classification using RNN over images pixels. A picture is representated as a sequence of pixels, coresponding to an image's width (timestep) and height (number of sequences). """ from __future__ import division, print_function, absolute_import import numpy as np import zqtflearn import zqtflearn.datasets.mnist as mnist X, Y, testX, testY = mnist.load_data(one_hot=True) X = np.reshape(X, (-1, 28, 28)) testX = np.reshape(testX, (-1, 28, 28)) net = zqtflearn.input_data(shape=[None, 28, 28]) net = zqtflearn.lstm(net, 128, return_seq=True) net = zqtflearn.lstm(net, 128) net = zqtflearn.fully_connected(net, 10, activation='softmax') net = zqtflearn.regression(net, optimizer='adam', loss='categorical_crossentropy', name="output1") model = zqtflearn.DNN(net, tensorboard_verbose=2) model.fit(X, Y, n_epoch=1, validation_set=0.1, show_metric=True, snapshot_step=100)