def train(src, tar, pivot_num, pivot_min_times, dim): get_data_start = time.time() x_non_pivot, x_pivot, D = data_helper.get_data(src, tar, pivot_num, pivot_min_times) pivot_matrix = np.zeros((pivot_num, D)) sgd_start = time.time() print('get data ok ---------------- time = %.3fs' % (sgd_start - get_data_start)) # SVD decomposition based on pivot features and non-pivot features for i in range(pivot_num): iteration_start = time.time() # clf = linear_model.SGDClassifier(loss = 'modified_huber', max_iter = 20, tol = 1e-3) clf = linear_model.SGDRegressor(loss='huber', max_iter=20, tol=1e-3) clf.fit(x_non_pivot, x_pivot[:, i]) pivot_matrix[i] = clf.coef_ # pivot_matrix[i] = clf.coef_[:, -1] if i > 0 and i % 20 == 0: print('SGD iteration i = %d per iteration time = %.3fs' % (i, time.time() - iteration_start)) pivot_matrix = pivot_matrix.transpose() sgd_end = time.time() print('SGD optimization ok -------- time = %.3fs' % (sgd_end - sgd_start)) svd = TruncatedSVD(n_components=dim) weight = svd.fit_transform(pivot_matrix) data_helper.save_svd(src, tar, dim, weight)
def main(): parser = argparse.ArgumentParser() parser.add_argument('--dataset', type=str, choices=['ffhq', 'cifar', 'mnist', 'mnist_fashion', 'emnist']) parser.add_argument('--image_size', type=int, choices=[32, 64, 128]) args = parser.parse_args() latent_dim = 32 image_size = args.image_size image_shape = [3, image_size, image_size] batch_size = 512 train_loader, _ = data_helper.get_data(args.dataset, batch_size, image_size) for each_distribution in ['standard_normal', 'uniform', 'gamma', 'beta', 'chi', 'laplace']: encoder = model.Encoder(latent_dim, image_shape).cuda() encoder.load_state_dict(torch.load(f'model/image_size_128_epoch_500_test_1/encoder_{args.dataset}_{args.image_size}_{each_distribution}')) z_array = None for each_batch in tqdm.tqdm(train_loader): each_batch = Variable(each_batch[0]).cuda() each_z_batch = encoder(each_batch) if z_array is None: z_array = each_z_batch.cpu().detach().numpy() else: z_array = np.concatenate((z_array, (each_z_batch.cpu().detach().numpy()))) print_percentage_of_data_that_is_difficult_to_generate(z_array, each_distribution)
def run_cnn(learning_rate, batch_size): """ Args: - learning_rate: learning rate used to train the network. - batch_size: batch_size to train the network. """ X_train, y_train, X_test, y_test = get_data() model = CNN(n_classes=10, learning_rate=learning_rate, batch_size=batch_size) print("Epoch 1...") model.train(X_train[:50], y_train[:50]) test_network(model, X_test[:5], y_test[:5])
def __init__(self): cf = configparser.ConfigParser() cf.read('conf.ini', encoding="utf-8-sig") self.mnist = data_helper.get_data() self.num_class = int(cf.get('parm', 'num_class')) self.height = int(cf.get('parm', 'height')) self.dim_hidden = int(cf.get('parm', 'dim_hidden')) self.width = int(cf.get('parm', 'width')) self.learning_rate = float(cf.get('parm', 'learning_rate')) self.epochs = int(cf.get('parm', 'epochs')) self.batch_size = int(cf.get('parm', 'batch_size')) self.display_step = int(cf.get('parm', 'display_step')) self.model_path = os.path.join(os.getcwd(), cf.get('parm', 'model_path')) self.summaries_path = os.path.join(os.getcwd(), cf.get('parm', 'summaries_path'))
def run_feedforward_network(hidden_shape, learning_rate, is_agent_mode_enabled): """ Args: - hidden shape: array of integers, the array ith element is the number of neurons for the ith layer. - learning_rate: learning rate used to train the network. - is_agent_mode_enabled: if true, each neuron is an instance of the class neuron. Used to give particular behavior to neuron. """ X_train, y_train, X_test, y_test = get_data(reshape=True) model = FullyConnectedNeuralNetwork( input_shape=784, hidden_shape=hidden_shape, output_shape=10, learning_rate=learning_rate, is_agent_mode_enabled=is_agent_mode_enabled) print("Epoch 1...") model.train(X_train, y_train) test_network(model, X_test, y_test)
def latent_plot(args, dataset, encoder, mapper, num=2048): # gather x sample, E(x) train_loader, _ = data_helper.get_data(dataset, args.batch_size, args.image_size, args.environment) ex_list = [] x_list = [] for each_batch in train_loader: with torch.no_grad(): ex_list.append(encoder(each_batch[0].to(args.device)).cpu()) x_list.append(each_batch[0]) if args.batch_size * len(ex_list) >= num: break ex_tensor = torch.cat(ex_list) plt_list = [] #case aae. plot{E(x), Gaussian}. E(x) imitate Gaussian. if args.model_name in ['aae', 'mask_aae']: z = torch.randn(*ex_tensor.shape) pca_plt = aae_latent_pca_plt(z, ex_tensor) for i in range(z.size(1)): plt_list.append(aae_latent_plt(z[:, i], ex_tensor[:, i], i)) #case non-prior. plot{Gaussian, M(z), E(x)} M(z) imitate E(x). elif args.model_name in ['non-prior']: z = torch.randn(*ex_tensor.shape) M_z = mapper(z.to(args.device)).detach().cpu() pca_plt = nonprior_latent_pca_plt(z, ex_tensor, M_z) for i in range(z.size(1)): plt_list.append( nonprior_latent_plt(z[:, i], ex_tensor[:, i], M_z[:, i], i)) #case learning-prior. plot{Gaussian, M(z), E(x)} E(x) imitate M(z) elif args.model_name in ['learning-prior']: z = torch.randn(*ex_tensor.shape) M_z = mapper(z.to(args.device)).detach().cpu() pca_plt = learningprior_latent_pca_plt(z, ex_tensor, M_z) for i in range(z.size(1)): plt_list.append( learningprior_latent_plt(z[:, i], ex_tensor[:, i], M_z[:, i], i)) return plt_list, pca_plt
def main(args): train_loader, _ = data_helper.get_data(args.dataset, args.batch_size, args.image_size, args.environment) if args.wandb: wandb_name = "%s[%d]_%s" % (args.dataset, args.image_size, args.model_name) wandb.login() wandb.init(project="AAE", config=args, name=wandb_name) inception_model_score = load_inception_model(train_loader, args.dataset, args.image_size, args.environment) ae_optimizer, d_optimizer, decoder, discriminator, encoder, g_optimizer, mapper = \ model.get_aae_model_and_optimizer(args) if args.model_name == 'mimic': mapper = model.Mimic(args.latent_dim, args.latent_dim, args.mapper_inter_nz, args.mapper_inter_layer).to(args.device) decoder, encoder = pretrain_autoencoder(ae_optimizer, args, decoder, encoder, train_loader) if args.model_name == 'non-prior': mapper, m_optimizer = model.get_nonprior_model_and_optimizer(args) if args.model_name == 'learning-prior': mapper, m_optimizer, discriminator_forpl, dpl_optimizer = \ model.get_learning_prior_model_and_optimizer(args) decoder_optimizer = torch.optim.Adam(decoder.parameters(), lr=1e-4) global start_time start_time = time.time() if args.pretrain_epoch > 0: pretrain_autoencoder(ae_optimizer, args, decoder, encoder, train_loader) log_dict, log, log2 = {}, {}, {} for i in range(0, args.epochs): log_dict, log, log2 = {}, {}, {} if args.time_limit and timeout(args.time_limit, start_time): break encoded_feature_list = [] for each_batch in tqdm.tqdm(train_loader, desc="train[%d/%d]" % (i, args.epochs)): each_batch = each_batch[0].to(args.device) if args.model_name in ['aae', 'mask_aae']: log = model.update_aae(ae_optimizer, args, d_optimizer, decoder, discriminator, each_batch, encoder, g_optimizer, args.latent_dim) elif args.model_name == 'mimic': log, encoded_feature = \ model.update_autoencoder(ae_optimizer, each_batch, encoder, decoder, return_encoded_feature=True) encoded_feature_list.append(encoded_feature) elif args.model_name == 'non-prior': log, encoded_feature = model.update_autoencoder( ae_optimizer, each_batch, encoder, decoder, return_encoded_feature_gpu=True, flag_retain_graph=False) log2 = model.update_posterior_part(args, mapper, discriminator, m_optimizer, d_optimizer, encoded_feature) elif args.model_name == 'learning-prior': log = model.update_aae_with_mappedz(args, ae_optimizer, d_optimizer, decoder, discriminator, mapper, each_batch, encoder, g_optimizer) log2 = model.update_mapper_with_discriminator_forpl( args, dpl_optimizer, decoder_optimizer, m_optimizer, discriminator_forpl, decoder, mapper, each_batch) if args.model_name == 'mimic': g_loss = model.train_mapper(args, encoder, mapper, args.device, args.lr, args.batch_size, encoded_feature_list) log_dict.update(log) log_dict.update(log2) # wandb log를 남기고, time_check와 time_limit 옵션이 둘다 없을때만, log interval마다 기록을 남김 if args.wandb and not args.time_check and not args.time_limit: decoder, discriminator, encoder, mapper = log_and_write_pca( args, decoder, discriminator, encoder, i, inception_model_score, mapper, log_dict) # wandb log를 남기고, time_check 또는 time_limit 옵션 둘 중 하나라도 있으면, 최후에 기록을 남김 if args.wandb and (args.time_check or args.time_limit): decoder, discriminator, encoder, mapper = log_and_write_pca( args, decoder, discriminator, encoder, i, inception_model_score, mapper, log_dict) save_models(args, decoder, encoder, mapper) if args.wandb: wandb.finish()
def get_word2vec(content): word2vec = Word2Vec.load('predictor/model/wiki.zh.seg_200d.model') res = np.zeros([200]) count = 0 # word_list = content.split() for word in content: if word in word2vec: res += word2vec[word] count += 1 return pd.Series(res / count) if __name__ == '__main__': print('reading...') all_text, y, label, label_to_int, int_tolabel = get_data(file='./data/train_data.csv') print('cut text...') train_data = cut_text(all_text) # train_data = [line.split() for line in train_data] print('get tfidf...') tfidf = train_tfidf(train_data) print('saving tfidf model') joblib.dump(tfidf, 'model/tfidf_5000.model') X_train, X_dev, y_train, y_dev = train_test_split(train_data, y, random_state=10, test_size=0.1) train_vec = tfidf.transform(X_train) test_vec = tfidf.transform(X_dev) print('training SVC') svm = train_SVC(train_vec, y_train) y_pre = svm.predict(test_vec) print(classification_report(y_dev, y_pre))
# Model parameters EMBED_HIDDEN_SIZE = 100 BATCH_SIZE = 32 EPOCHS = 200 NUM_BLOCKS = 20 PATIENCE = 200 LRPATIENCE = 30 # Cyclic Learning Rate # clr = clr.CyclicLR(base_lr=2e-4, max_lr=1e-2, step_size=3000., mode='triangular') # clr = clr.CyclicLR(base_lr=0.001, max_lr=0.01, step_size=3000., mode='triangular') # options: [triangular, triangular2, exp_range] # --------------------------------- PREPARE DATA --------------------------------------------------- train, test, params = data_helper.get_data(FLAGS, batch_size=BATCH_SIZE) x, xq, y = train[0], np.expand_dims(train[1], 1), train[2] tx, txq, ty = test[0], np.expand_dims(test[1], 1), test[2] max_sentence_length = params["max_sentence_length"] story_maxlen = params["story_maxlen"] query_maxlen = params["query_maxlen"] vocab_size = params["vocab_size"] vocab_size += NUM_BLOCKS # ========================================== BUILD KERAS MODEL ====================================== print('Build model...') # ------------------------------------------ STORY INIT --------------------------------------------- # Story # Define input
if __name__ == '__main__': parser = argparse.ArgumentParser( description='This is the TextCNN test project.') parser.add_argument( 'results_dir', type=str, help='The results dir including log, model, vocabulary and some images.' ) parser.add_argument('-p', '--padding_size', default=200, type=int, help='Padding size of sentences.(default=200)') parser.add_argument('-c', '--num_classes', default=95, type=int, help='Number of target classes.(default=95)') args = parser.parse_args() print('Parameters:', args) # x_test, y_test = preprocess("./data/test_data.csv", os.path.join(args.results_dir, "vocab.json"), # args.padding_size, test=True) x_test, y_test = get_data('./data/baidu_95_test_x.npy', './data/baidu_95_test_y.npy') print("Loading model...") model = load_model(os.path.join(args.results_dir, 'TextCNN.h5')) test(model, x_test, y_test)
input_x = tf.placeholder(tf.float32, [None, self.height, self.width]) rnn = MnistRnn(input_x, self.height, self.width, self.dim_hidden, self.num_class, self.learning_rate) saver = tf.train.Saver() with tf.Session() as sess: saver.restore(sess, self.model_path + 'model.ckpt') predict = tf.argmax(rnn.pre, 1) vector_list = sess.run(predict, feed_dict={input_x: batch_x_test}) vector_list = vector_list.tolist() return vector_list if __name__ == '__main__': rnn = RNN() if os.path.exists(os.path.join(os.getcwd(), 'ckpt', 'checkpoints')): if not os.path.getsize(os.path.join(os.getcwd(), 'ckpt', 'checkpoints')) > 0: print("正在训练模型.....") rnn.train() else: print("正在训练模型.....") rnn.train() mnist = data_helper.get_data() batch_x_test = mnist.test.images batch_x_test = batch_x_test.reshape([-1, rnn.height, rnn.width]) batch_y_test = mnist.test.labels batch_y_test = list(np.argmax(batch_y_test, 1)) pre_y = rnn.predict(batch_x_test) for text in batch_y_test: print('Label:', text, ' Predict:', pre_y[batch_y_test.index(text)])
def lazy_forward(self, dataset, image_size=32, batch_size=16, decoder=None, distribution=None, latent_dim=None, real_forward=True, device='cpu', model_name='aae', mapper=None, gen_image_in_gpu=False, environment='yhs'): assert self.lazy, "lazy_forward only run in lazy mode. call lazy_mode() first." train_loader, _ = data_helper.get_data(dataset, batch_size, image_size, environment) if real_forward: print("generate real images info") for each_batch in tqdm.tqdm(train_loader): self.real_forward(each_batch[0].to(device)) else: print("generate fake images info") if gen_image_in_gpu: fake_images_list = [] decoder = decoder.to(device) if isinstance(mapper, torch.nn.Module): mapper = mapper.to(device) for each_batch in tqdm.tqdm(train_loader, desc='gen_fake'): with torch.no_grad(): if model_name == "mimic": z = torch.rand( batch_size, latent_dim, device=device) * 2 - 1 fake_images = decoder(mapper(z)) elif model_name in ['non-prior', 'learning-prior']: z = torch.randn(batch_size, latent_dim, device=device) fake_images = decoder(mapper(z)) else: z = torch.FloatTensor( prior_factory.get_sample( distribution, batch_size, latent_dim)).to(device) if decoder.has_mask_layer: z = torch.mul(z, decoder.mask_vector) fake_images = decoder(z) fake_images_list.append(fake_images.cpu()) decoder = decoder.to('cpu') if isinstance(mapper, torch.nn.Module): mapper = mapper.to('cpu') fake_predict_softmax_list, fake_feature_list = [], [] for index in tqdm.tqdm(range(len(fake_images_list)), desc='gen_feature'): fake_images_gpu = fake_images_list[index].to(device) with torch.no_grad(): fake_predict_softmax, fake_feature = self.analysis_softmax_and_feature( fake_images) fake_predict_softmax_list.append( fake_predict_softmax.cpu()) fake_feature_list.append(fake_feature.cpu()) fake_images_list[index] = None self.fake_predict_softmax = torch.cat( fake_predict_softmax_list) self.fake_feature = torch.cat(fake_feature_list) else: for each_batch in tqdm.tqdm(train_loader): if model_name == "mimic": z = torch.rand(batch_size, latent_dim) * 2 - 1 fake_images = decoder(mapper(z)).to(device) elif model_name == 'non-prior': z = torch.randn(batch_size, latent_dim) fake_images = decoder(mapper(z)).to(device) else: z = torch.FloatTensor( prior_factory.get_sample(distribution, batch_size, latent_dim)) if decoder.has_mask_layer: z = torch.mul(z, decoder.mask_vector.cpu()) fake_images = decoder(z).to(device) self.fake_forward(fake_images)
from keras.callbacks import LambdaCallback from keras.models import Sequential from keras.layers import Dense, Activation from keras.layers import GRU, LSTM from keras.optimizers import RMSprop from keras.utils.data_utils import get_file from sklearn.externals import joblib import numpy as np import random import os import sys import re from data_helper import get_data text, chars, char_indices, indices_char = get_data() # cut the text in semi-redundant sequences of maxlen characters maxlen = 30 step = 3 sentences = [] next_chars = [] for i in range(0, len(text) - maxlen, step): sentences.append(text[i:i + maxlen]) next_chars.append(text[i + maxlen]) print('nb sequences:', len(sentences)) print('Vectorization...') x = np.zeros((len(sentences), maxlen, len(chars)), dtype=np.bool) y = np.zeros((len(sentences), len(chars)), dtype=np.bool) for i, sentence in enumerate(sentences):
import random import os import json from actor import ActorNetwork from self_att_rnn import SelfAttRNN from model_api import ModelAPI from integration_func import generate_embedding_mat from data_helper import get_data, rand_batch_gen os.environ["CUDA_VISIBLE_DEVICES"] = "2" metadata_pkl = './data/post_metadata.pkl' max_length = 200 word_to_int, int_to_word, sentence_int, label, length = get_data( metadata_pkl, max_length) rand_batch = rand_batch_gen(sentence_int, label, length, 64) vocab_size = len(word_to_int) embed_size = 128 options = {"context_lstm_dims": 100, "highway_layer_num": 1} class_list = [ u"投递质量", u"延误", u"信息质量", u"破损", u"丢失", u"服务态度", u"收寄质量", u"需求建议", u"售后服务" ] config = { "options": options, "vocab_size": vocab_size, "max_length": max_length, "emb_size": embed_size,
Neural network regression example for JDOT """ # Author: Remi Flamary <*****@*****.**> # Nicolas Courty <*****@*****.**> # # License: MIT License import numpy as np import data_helper import jdot from keras.utils import np_utils import keras import time X, y = data_helper.get_data('supernova-src') Xtest, ytest = data_helper.get_data('supernova-tgt') nclasses = len(np.unique(np.hstack((y, ytest)))) + 1 n_inputs = X.shape[1] ytest = np_utils.to_categorical(ytest, num_classes=nclasses) y = np_utils.to_categorical(y, num_classes=nclasses) def get_model(): # simple 1D nn model = keras.models.Sequential() model.add(keras.layers.Dense(80, activation='tanh', input_dim=n_inputs)) model.add(keras.layers.Dense(80, activation='tanh')) model.add(keras.layers.Dense(y.shape[1], activation='softmax')) model.compile(optimizer='sgd',
'--results_dir', default='./results/', type=str, help= 'The results dir including log, model, vocabulary and some images.(default=./results/)' ) args = parser.parse_args() print('Parameters:', args, '\n') if not os.path.exists(args.results_dir): os.mkdir(args.results_dir) timestamp = time.strftime("%Y-%m-%d-%H-%M", time.localtime(time.time())) os.mkdir(os.path.join(args.results_dir, timestamp)) os.mkdir(os.path.join(args.results_dir, timestamp, 'log/')) # if not os.path.exists("./data/train_data.csv") or not os.path.exists("./data/test_data.csv"): # data_helper.load_data_and_write_to_file("./data/fenduan_clean.xlsx", "./data/train_data.csv", # "./data/test_data.csv", args.test_sample_percentage) # x_train, y_train = data_helper.preprocess('./data/baidu_95.csv', # './data/vocab.txt', # './data/label_95.txt') # data_helper.preprocess('./data/baidu_95.csv', padding_size=args.padding_size) x_train, y_train = data_helper.get_data('./data/baidu_95_train_x.npy', './data/baidu_95_train_y.npy') print(x_train.shape) print(y_train.shape) train(x_train, y_train, args.vocab_size, args.padding_size, os.path.join(args.results_dir, timestamp, 'TextCNN.h5'))
# Model parameters EMBED_HIDDEN_SIZE = 100 BATCH_SIZE = 32 EPOCHS = 200 NUM_BLOCKS = 20 PATIENCE = 200 LRPATIENCE = 30 # Cyclic Learning Rate # clr = clr.CyclicLR(base_lr=2e-4, max_lr=1e-2, step_size=3000., mode='triangular') # clr = clr.CyclicLR(base_lr=0.001, max_lr=0.01, step_size=3000., mode='triangular') # options: [triangular, triangular2, exp_range] # --------------------------------- PREPARE DATA --------------------------------------------------- train, test, params = data_helper.get_data(FLAGS, batch_size=BATCH_SIZE) x, xq, y = train[0], np.expand_dims(train[1], 1), train[2] tx, txq, ty = test[0], np.expand_dims(test[1], 1), test[2] max_sentence_length = params["max_sentence_length"] story_maxlen = params["story_maxlen"] query_maxlen = params["query_maxlen"] vocab_size = params["vocab_size"] vocab_size += NUM_BLOCKS # ========================================== BUILD KERAS MODEL ====================================== print('Build model...') # ------------------------------------------ STORY INIT --------------------------------------------- # Story # Define input