def train_with_trainobject(): from tensorgraphx.trainobject import train as mytrain config = tf.ConfigProto() config.gpu_options.allow_growth = True with tf.Session(config=config) as sess: X_train, y_train, X_test, y_test = Cifar10(contrast_normalize=False, whiten=False) _, h, w, c = X_train.shape _, nclass = y_train.shape seq = model(nclass=nclass, h=h, w=w, c=c) X_ph = tf.placeholder('float32', [None, h, w, c]) y_ph = tf.placeholder('float32', [None, nclass]) y_train_sb = seq.train_fprop(X_ph) y_test_sb = seq.test_fprop(X_ph) train_cost_sb = entropy(y_ph, y_train_sb) optimizer = tf.train.AdamOptimizer(0.001) test_accu_sb = accuracy(y_ph, y_test_sb) mytrain(session=sess, feed_dict={ X_ph: X_train, y_ph: y_train }, train_cost_sb=train_cost_sb, valid_cost_sb=-test_accu_sb, optimizer=optimizer, epoch_look_back=5, max_epoch=100, percent_decrease=0, train_valid_ratio=[5, 1], batchsize=64, randomize_split=False)
def train_with_trainobject(): from tensorgraphx.trainobject import train as mytrain with tf.Session() as sess: seq = model() X_train, y_train, X_test, y_test = Mnist(flatten=False, onehot=True, binary=True, datadir='.') X_ph = tf.placeholder('float32', [None, 28, 28, 1]) y_ph = tf.placeholder('float32', [None, 10]) y_train_sb = seq.train_fprop(X_ph) y_test_sb = seq.test_fprop(X_ph) train_cost_sb = entropy(y_ph, y_train_sb) optimizer = tf.train.AdamOptimizer(0.001) test_accu_sb = accuracy(y_ph, y_test_sb) mytrain(session=sess, feed_dict={ X_ph: X_train, y_ph: y_train }, train_cost_sb=train_cost_sb, valid_cost_sb=-test_accu_sb, optimizer=optimizer, epoch_look_back=5, max_epoch=100, percent_decrease=0, train_valid_ratio=[5, 1], batchsize=64, randomize_split=False)
def train(): from tensorgraphx.trainobject import train as mytrain with tf.Session() as sess: word_len = 20 sent_len = 50 # load data X_train, y_train, nclass = tweets(word_len, sent_len) # build model X_ph, y_train_sb, y_test_sb = model(word_len, sent_len, nclass) y_ph = tf.placeholder('float32', [None, nclass]) # set cost and optimizer train_cost_sb = entropy(y_ph, y_train_sb) optimizer = tf.train.AdamOptimizer(0.001) test_accu_sb = accuracy(y_ph, y_test_sb) # train model mytrain(session=sess, feed_dict={ X_ph: X_train, y_ph: y_train }, train_cost_sb=train_cost_sb, valid_cost_sb=-test_accu_sb, optimizer=optimizer, epoch_look_back=5, max_epoch=100, percent_decrease=0, train_valid_ratio=[5, 1], batchsize=64, randomize_split=False)
def train_with_Resnet(): from tensorgraphx.trainobject import train as mytrain config = tf.ConfigProto() config.gpu_options.allow_growth = True with tf.Session(config=config) as sess: X_train, y_train, X_test, y_test = Cifar10(contrast_normalize=False, whiten=False) _, h, w, c = X_train.shape _, nclass = y_train.shape print('X max', np.max(X_train)) print('X min', np.min(X_train)) seq = tg.Sequential() id1 = IdentityBlock(input_channels=c, input_shape=(h, w), nlayers=4, filters=[32, 64]) seq.add(id1) trans1 = TransitionLayer(input_channels=id1.output_channels, input_shape=id1.output_shape) seq.add(trans1) id2 = IdentityBlock(input_channels=trans1.output_channels, input_shape=trans1.output_shape, nlayers=4, filters=[64, 128]) seq.add(id2) trans2 = TransitionLayer(input_channels=id2.output_channels, input_shape=id2.output_shape) seq.add(trans2) seq.add(Flatten()) ldim = trans2.output_channels * np.prod(trans2.output_shape) seq.add(Linear(ldim, nclass)) seq.add(Softmax()) X_ph = tf.placeholder('float32', [None, h, w, c]) y_ph = tf.placeholder('float32', [None, nclass]) y_train_sb = seq.train_fprop(X_ph) y_test_sb = seq.test_fprop(X_ph) train_cost_sb = entropy(y_ph, y_train_sb) optimizer = tf.train.AdamOptimizer(0.001) test_accu_sb = accuracy(y_ph, y_test_sb) mytrain(session=sess, feed_dict={ X_ph: X_train, y_ph: y_train }, train_cost_sb=train_cost_sb, valid_cost_sb=-test_accu_sb, optimizer=optimizer, epoch_look_back=5, max_epoch=100, percent_decrease=0, train_valid_ratio=[5, 1], batchsize=64, randomize_split=False)
def train_with_Densenet(): from tensorgraphx.trainobject import train as mytrain config = tf.ConfigProto() config.gpu_options.allow_growth = True with tf.Session(config=config) as sess: X_train, y_train, X_test, y_test = Cifar10(contrast_normalize=False, whiten=False) _, h, w, c = X_train.shape _, nclass = y_train.shape print('X max', np.max(X_train)) print('X min', np.min(X_train)) seq = tg.Sequential() dense = DenseNet(input_channels=c, input_shape=(h, w), ndense=3, growth_rate=4, nlayer1blk=4) seq.add(dense) seq.add(Flatten()) ldim = dense.output_channels seq.add(Linear(ldim, nclass)) seq.add(Softmax()) X_ph = tf.placeholder('float32', [None, h, w, c]) y_ph = tf.placeholder('float32', [None, nclass]) y_train_sb = seq.train_fprop(X_ph) y_test_sb = seq.test_fprop(X_ph) train_cost_sb = entropy(y_ph, y_train_sb) optimizer = tf.train.AdamOptimizer(0.001) test_accu_sb = accuracy(y_ph, y_test_sb) print(tf.global_variables()) print('..total number of global variables: {}'.format( len(tf.global_variables()))) count = 0 for var in tf.global_variables(): count += int(np.prod(var.get_shape())) print('..total number of global parameters: {}'.format(count)) mytrain(session=sess, feed_dict={ X_ph: X_train, y_ph: y_train }, train_cost_sb=train_cost_sb, valid_cost_sb=-test_accu_sb, optimizer=optimizer, epoch_look_back=5, max_epoch=100, percent_decrease=0, train_valid_ratio=[5, 1], batchsize=64, randomize_split=False)
def train_with_VGG(): from tensorgraphx.trainobject import train as mytrain config = tf.ConfigProto() config.gpu_options.allow_growth = True with tf.Session(config=config) as sess: X_train, y_train, X_test, y_test = Cifar10(contrast_normalize=False, whiten=False) _, h, w, c = X_train.shape _, nclass = y_train.shape print('X max', np.max(X_train)) print('X min', np.min(X_train)) from tensorgraphx.layers import VGG19 seq = tg.Sequential() layer = VGG19(input_channels=c, input_shape=(h, w)) seq.add(layer) seq.add(Flatten()) seq.add(Linear(512, nclass)) seq.add(Softmax()) X_ph = tf.placeholder('float32', [None, h, w, c]) y_ph = tf.placeholder('float32', [None, nclass]) y_train_sb = seq.train_fprop(X_ph) y_test_sb = seq.test_fprop(X_ph) train_cost_sb = entropy(y_ph, y_train_sb) optimizer = tf.train.AdamOptimizer(0.001) test_accu_sb = accuracy(y_ph, y_test_sb) mytrain(session=sess, feed_dict={ X_ph: X_train, y_ph: y_train }, train_cost_sb=train_cost_sb, valid_cost_sb=-test_accu_sb, optimizer=optimizer, epoch_look_back=5, max_epoch=100, percent_decrease=0, train_valid_ratio=[5, 1], batchsize=64, randomize_split=False)
def train(seq): y_train_sb = seq.train_fprop(X_ph) y_test_sb = seq.test_fprop(X_ph) train_cost_sb = tg.cost.entropy(y_ph, y_train_sb) optimizer = tf.train.AdamOptimizer(0.0001) test_accu_sb = tg.cost.accuracy(y_ph, y_test_sb) with tf.Session() as sess: mytrain(session=sess, feed_dict={ X_ph: X_train, y_ph: y_train }, train_cost_sb=train_cost_sb, valid_cost_sb=-test_accu_sb, optimizer=optimizer, epoch_look_back=5, max_epoch=1, percent_decrease=0, train_valid_ratio=[5, 1], batchsize=1, randomize_split=False)