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 = tflearn.input_data(shape=[None]) linear = tflearn.single_unit(input) regression = tflearn.regression(linear, optimizer='sgd', loss='mean_square', metric='R2', learning_rate=0.01) m = tflearn.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.tflearn") self.assertTrue(os.path.exists("test_dnn.tflearn")) with tf.Graph().as_default(): input = tflearn.input_data(shape=[None]) linear = tflearn.single_unit(input) regression = tflearn.regression(linear, optimizer='sgd', loss='mean_square', metric='R2', learning_rate=0.01) m = tflearn.DNN(regression) # Testing load method m.load("test_dnn.tflearn") 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 __init__(self, s_date, n_frame): self.n_epoch = 20 prev_bd = int(s_date[:6])-1 prev_ed = int(s_date[9:15])-1 if prev_bd%100 == 0: prev_bd -= 98 if prev_ed%100 == 0: prev_ed -= 98 pred_s_date = "%d01_%d01" % (prev_bd, prev_ed) prev_model = '../model/tflearn/reg_l3_bn/big/%s' % pred_s_date self.model_dir = '../model/tflearn/reg_l3_bn/big/%s' % s_date tf.reset_default_graph() tflearn.init_graph(gpu_memory_fraction=0.1) input_layer = tflearn.input_data(shape=[None, 23*n_frame], name='input') dense1 = tflearn.fully_connected(input_layer, 400, name='dense1', activation='relu') dense1n = tflearn.batch_normalization(dense1, name='BN1') dense2 = tflearn.fully_connected(dense1n, 100, name='dense2', activation='relu') dense2n = tflearn.batch_normalization(dense2, name='BN2') dense3 = tflearn.fully_connected(dense2n, 1, name='dense3') output = tflearn.single_unit(dense3) regression = tflearn.regression(output, optimizer='adam', loss='mean_square', metric='R2', learning_rate=0.001) self.estimators = tflearn.DNN(regression) if os.path.exists('%s/model.tfl' % prev_model): self.estimators.load('%s/model.tfl' % prev_model) self.n_epoch = 10 if not os.path.exists(self.model_dir): os.makedirs(self.model_dir)
def linear_regression(): # 回归数据 X = [ 3.38, 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_ = tflearn.input_data(shape=[None]) linear = tflearn.single_unit(input_) # 优化器,目标(`optimizer`)和评价指标(`metric`) regression = tflearn.regression(linear, optimizer='sgd', loss='mean_square', metric='R2', learning_rate=0.01) # 使用`DNN`(深度神经网络)模型类训练模型 m = tflearn.DNN(regression) m.fit(X, Y, n_epoch=1000, show_metric=True, snapshot_epoch=False) print("回归结果:") # `get_weights`方法获取模型的权重值 print("Y = " + str(m.get_weights(linear.W)) + "*X + " + str(m.get_weights(linear.b))) print("x的测试预测 = 3.2, 3.3, 3.4:") print(m.predict([3.2, 3.3, 3.4]))
def __init__(self): if not available: raise ImportError("You don't have tensorflow") input_ = tflearn.input_data(shape=[None]) linear = tflearn.single_unit(input_) regression = tflearn.regression(linear) self.m = tflearn.DNN(regression)
def __init__(self): self.len_past = 30 #self.s_date = "20120101_20160330" #self.model_dir = '../model/tflearn/reg_l3_bn/big/%s/' % self.s_date tf.reset_default_graph() tflearn.init_graph(gpu_memory_fraction=0.05) input_layer = tflearn.input_data(shape=[None, 690], name='input') dense1 = tflearn.fully_connected(input_layer, 400, name='dense1', activation='relu') dense1n = tflearn.batch_normalization(dense1, name='BN1') dense2 = tflearn.fully_connected(dense1n, 100, name='dense2', activation='relu') dense2n = tflearn.batch_normalization(dense2, name='BN2') dense3 = tflearn.fully_connected(dense2n, 1, name='dense3') output = tflearn.single_unit(dense3) regression = tflearn.regression(output, optimizer='adam', loss='mean_square', metric='R2', learning_rate=0.001) self.estimators = tflearn.DNN(regression) self.qty = {} self.day_last = {} self.currency = 100000000
def get_network_spec(): network = tflearn.input_data(shape=[None]) network = tflearn.single_unit(network) network = tflearn.regression(network, optimizer='sgd', loss='mean_square', metric='R2', learning_rate=0.01) return network
def case_linear_regression(): x = [1,2,3,4,5] y = [2,4,6,8,10] net = tflearn.input_data([None]) linear = tflearn.single_unit(net) net = tflearn.regression(linear, optimizer='sgd', loss='mean_square', metric='R2', learning_rate=0.01) model = tflearn.DNN(net) model.fit(x, y, n_epoch=200, snapshot_epoch=False,show_metric=True,batch_size=1) print model.predict([8,9]) # [15.990408897399902, 17.988374710083008] print model.get_weights(linear.W) # [ 1.99796414] print model.get_weights(linear.b) # [ 0.00669619]
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 = tflearn.input_data(shape=[None]) linear = tflearn.single_unit(input) regression = tflearn.regression(linear, optimizer='sgd', loss='mean_square', metric='R2', learning_rate=0.01) m = tflearn.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.tflearn") self.assertTrue(os.path.exists("test_dnn.tflearn")) # 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 = tflearn.input_data(shape=[None]) linear = tflearn.single_unit(input) regression = tflearn.regression(linear, optimizer='sgd', loss='mean_square', metric='R2', learning_rate=0.01) m = tflearn.DNN(regression) def try_load(): m.load("test_dnn.tflearn") self.assertRaises(tf.errors.NotFoundError, try_load) # fails, since names in file don't have "scopeA" m.load("test_dnn.tflearn", 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")
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 = tflearn.input_data(shape=[None, 2]) # Nand operator definition g_nand = tflearn.fully_connected(g, 32, activation='linear') g_nand = tflearn.fully_connected(g_nand, 32, activation='linear') g_nand = tflearn.fully_connected(g_nand, 1, activation='sigmoid') g_nand = tflearn.regression(g_nand, optimizer='sgd', learning_rate=2., loss='binary_crossentropy') # Or operator definition g_or = tflearn.fully_connected(g, 32, activation='linear') g_or = tflearn.fully_connected(g_or, 32, activation='linear') g_or = tflearn.fully_connected(g_or, 1, activation='sigmoid') g_or = tflearn.regression(g_or, optimizer='sgd', learning_rate=2., loss='binary_crossentropy') # XOR merging Nand and Or operators g_xor = tflearn.merge([g_nand, g_or], mode='elemwise_mul') # Training m = tflearn.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 = tflearn.input_data(shape=[None, 2]) net = tflearn.flatten(net) net = tflearn.reshape(net, new_shape=[-1]) net = tflearn.activation(net, 'relu') net = tflearn.dropout(net, 0.5) net = tflearn.single_unit(net)
def __init__(self, s_date): prev_bd = int(s_date[:6])-1 prev_ed = int(s_date[9:15])-1 if prev_bd%100 == 0: prev_bd -= 98 if prev_ed%100 == 0: prev_ed -= 98 pred_s_date = "%d01_%d01" % (prev_bd, prev_ed) prev_model = '../model/tflearn/lstm/%s' % pred_s_date self.model_dir = '../model/tflearn/lstm/%s' % s_date tf.reset_default_graph() tflearn.init_graph(gpu_memory_fraction=0.1) input_layer = tflearn.input_data(shape=[None, 30, 23], name='input') lstm1 = tflearn.lstm(input_layer, 23, dynamic=True, name='lstm1') dense1 = tflearn.fully_connected(lstm1, 1, name='dense1') output = tflearn.single_unit(dense1) regression = tflearn.regression(output, optimizer='adam', loss='mean_square', metric='R2', learning_rate=0.001) self.estimators = tflearn.DNN(regression) if os.path.exists('%s/model.tfl' % prev_model): self.estimators.load('%s/model.tfl' % prev_model)
def __init__(self, s_date): prev_bd = int(s_date[:6]) - 1 prev_ed = int(s_date[9:15]) - 1 if prev_bd % 100 == 0: prev_bd -= 98 if prev_ed % 100 == 0: prev_ed -= 98 pred_s_date = "%d01_%d01" % (prev_bd, prev_ed) prev_model = '../model/tflearn/lstm/%s' % pred_s_date self.model_dir = '../model/tflearn/lstm/%s' % s_date tf.reset_default_graph() tflearn.init_graph(gpu_memory_fraction=0.1) input_layer = tflearn.input_data(shape=[None, 30, 23], name='input') lstm1 = tflearn.lstm(input_layer, 23, dynamic=True, name='lstm1') dense1 = tflearn.fully_connected(lstm1, 1, name='dense1') output = tflearn.single_unit(dense1) regression = tflearn.regression(output, optimizer='adam', loss='mean_square', metric='R2', learning_rate=0.001) self.estimators = tflearn.DNN(regression) if os.path.exists('%s/model.tfl' % prev_model): self.estimators.load('%s/model.tfl' % prev_model)
def __init__(self, s_date, n_frame): self.n_epoch = 20 prev_bd = int(s_date[:6]) - 1 prev_ed = int(s_date[9:15]) - 1 if prev_bd % 100 == 0: prev_bd -= 98 if prev_ed % 100 == 0: prev_ed -= 98 pred_s_date = "%d01_%d01" % (prev_bd, prev_ed) prev_model = '../model/tflearn/reg_l3_bn/big/%s' % pred_s_date self.model_dir = '../model/tflearn/reg_l3_bn/big/%s' % s_date tf.reset_default_graph() tflearn.init_graph(gpu_memory_fraction=0.1) input_layer = tflearn.input_data(shape=[None, 23 * n_frame], name='input') dense1 = tflearn.fully_connected(input_layer, 400, name='dense1', activation='relu') dense1n = tflearn.batch_normalization(dense1, name='BN1') dense2 = tflearn.fully_connected(dense1n, 100, name='dense2', activation='relu') dense2n = tflearn.batch_normalization(dense2, name='BN2') dense3 = tflearn.fully_connected(dense2n, 1, name='dense3') output = tflearn.single_unit(dense3) regression = tflearn.regression(output, optimizer='adam', loss='mean_square', metric='R2', learning_rate=0.001) self.estimators = tflearn.DNN(regression) if os.path.exists('%s/model.tfl' % prev_model): self.estimators.load('%s/model.tfl' % prev_model) self.n_epoch = 10 if not os.path.exists(self.model_dir): os.makedirs(self.model_dir)
import tflearn # 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] input_ = tflearn.input_data(shape=[None]) linear = tflearn.single_unit(input_) regression = tflearn.regression(linear, optimizer='sgd', loss='mean_square', metric='R2', learning_rate=0.01) m = tflearn.DNN(regression) m.fit(X, Y, n_epoch=1000, show_metric=True, snapshot_epoch=False) print("\n Regression result: ") print('Y = {} * X + {}'.format(m.get_weights(linear.W), 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]))
""" Linear Regression Example """ from __future__ import absolute_import, division, print_function import tflearn # 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_ = tflearn.input_data(shape=[None]) linear = tflearn.single_unit(input_) regression = tflearn.regression(linear, optimizer='sgd', loss='mean_square', metric='R2', learning_rate=0.01) m = tflearn.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])) # should output (close, not exact) y = [1.5315033197402954, 1.5585315227508545, 1.5855598449707031]
#!/usr/bin/python """ Linear Regression Example """ from __future__ import absolute_import, division, print_function import tflearn # 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_ = tflearn.input_data(shape=[None]) linear = tflearn.single_unit(input_) regression = tflearn.regression(linear, optimizer='sgd', loss='mean_square', metric='R2', learning_rate=0.01) m = tflearn.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])) # should output (close, not exact) y = [1.5315033197402954, 1.5585315227508545, 1.5855598449707031
import tflearn from tflearn.models.dnn import DNN from tflearn.models.generator import SequenceGenerator from tflearn.data_utils import VocabularyProcessor from tflearn.data_preprocessing import DataPreprocessing from tflearn.helpers.trainer import Trainer from tflearn.helpers.evaluator import Evaluator from tflearn.helpers.summarizer import summarize from tflearn.helpers.regularizer import add_weights_regularizer from tensorflow.contrib.slim import dataset from tensorflow.contrib.slim import dataset tflearn.input_data() tflearn.variable() tflearn.conv_2d() tflearn.single_unit() tflearn.lstm() tflearn.embedding() tflearn.batch_normalization() tflearn.merge() tflearn.regression() tflearn.tanh() tflearn.softmax_categorical_crossentropy() tflearn.SGD() tflearn.initializations.uniform() tflearn.losses.L1() tflearn.add_weights_regularizer() tflearn.metrics.Accuracy() tflearn.summaries() tflearn.ImagePreprocessing() tflearn.ImageAugmentation()
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 = tflearn.input_data(shape=[None]) linear = tflearn.single_unit(input) regression = tflearn.regression(linear, optimizer='sgd', loss='mean_square', metric='R2', learning_rate=0.01) m = tflearn.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.tflearn") self.assertTrue(os.path.exists("test_dnn.tflearn.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 = tflearn.input_data(shape=[None]) linear = tflearn.single_unit(input) regression = tflearn.regression(linear, optimizer='sgd', loss='mean_square', metric='R2', learning_rate=0.01) m = tflearn.DNN(regression) def try_load(): m.load("test_dnn.tflearn") self.assertRaises( tf.errors.NotFoundError, try_load) # fails, since names in file don't have "scopeA" m.load("test_dnn.tflearn", 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")
tratio = 0.1 #ratio of testing to training inputDayLength = 5 # Neural Network Model Setup (regression) inlayer = tflearn.layers.core.input_data(shape=[inputDayLength, len(tfields)]) layer1 = tflearn.layers.core.fully_connected( inlayer, ((inputDayLength % 2) + 1) * len(tfields), activation='linear') layer2 = tflearn.layers.core.fully_connected( layer1, ((inputDayLength % 4) + 1) * len(tfields), activation='linear') layer3 = tflearn.layers.core.fully_connected( layer2, ((inputDayLength % 16) + 1) * len(tfields), activation='linear') #layer4 = tflearn.layers.core.fully_connected(layer2,10, activation = 'linear') layer5 = tflearn.layers.core.fully_connected( layer3, 1, activation='linear' ) #final layer needs to be only one neuron wide for regression to work linear = tflearn.single_unit(layer5) nOptimizer = tflearn.optimizers.Momentum(learning_rate=0.003, decay_step=1000) regression = tflearn.layers.estimator.regression(linear, optimizer=nOptimizer, loss='mean_square') m = tflearn.DNN(regression) ''' # Neural Network Model Setup (categorical) net = tflearn.layers.core.input_data(shape=[90,len(tfields)]) net = tflearn.layers.core.fully_connected(net,45*len(tfields), activation = 'relu') net = tflearn.layers.core.fully_connected(net,5*len(tfields), activation = 'relu') #net = tflearn.layers.core.fully_connected(net,500, activation = 'relu') #net = tflearn.layers.core.fully_connected(net,50, activation = 'relu') net = tflearn.layers.core.dropout(net,0.8) net = tflearn.layers.core.fully_connected(net,5, activation = 'softmax') #net = tflearn.single_unit(net)
import tflearn import tensorflow as tf import numpy as np #training data x_train = [1, 2, 3, 4, 5] y_train = [2, 4, 6, 8, 10] #testing data x_test = [6, 7, 8, 9, 10] y_test = [12, 14, 16, 18, 20] tf.reset_default_graph() # build neural network net = tflearn.input_data(shape=[None]) net = tflearn.single_unit(net) # net = tflearn.fully_connected(net,1, activation='softmax') net = tflearn.regression(net, loss='mean_square', optimizer='adam', learning_rate=0.01) model = tflearn.DNN(net, tensorboard_dir='tflearn_logs') model.fit(x_train, y_train, n_epoch=1000, show_metric=True) classes = model.predict(x_test) print(classes)