def main(args): is_train = not args['nottrain'] is_save = args['savemodel'] model_path = args['modelpath'] epoch = args['epoch'] config = load_conf(args) config['nepoch'] = epoch #Load Dataset train_data, test_data = load_tt_datas(args, config) # setup randomer Randomer.set_stddev(config['stddev']) with tf.Graph().as_default(): # build model model = Seq2SeqAttNN(config) model.build_model() if is_save or not is_train: saver = tf.train.Saver(max_to_keep=30) else: saver = None # run with tf.Session() as sess: sess.run(tf.global_variables_initializer()) if is_train: t1 = time.time() model.train(sess, train_data, test_data, saver) t2 = time.time() print('Training+Evaluation Time = ', t2 - t1) else: t1 = time.time() saver.restore(sess, model_path) model.test(sess, test_data) t2 = time.time() print('Testing Time = ', t2 - t1)
def __init__(self, hidden_size, stddev=None): ''' hidden_size: int, ''' self.stddev = stddev self.hidden_size = hidden_size self.W = tf.Variable(Randomer.random_normal( [self.hidden_size, self.hidden_size]), trainable=True) self.U = tf.Variable(Randomer.random_normal( [self.hidden_size, self.hidden_size]), trainable=True) self.b = tf.Variable(tf.zeros([1]), trainable=True)
def __init__(self, w_shape, stddev=None, params=None): ''' :param w_shape: [input_dim, output_dim] :param stddev: 用于初始化 :param params: 从外界制定参数 ''' if params is None: self.w = tf.Variable(Randomer.random_normal(w_shape), trainable=True) else: self.w = params['w']
def __init__(self, edim, class_num, stddev=None, params=None): ''' class_num: class type num. edim: the input embedding dim. params = {'wline': wline, 'bline': bline} ''' self.edim = edim self.class_num = class_num # the linear basic_layer for softmax. if params is None: self.wline_softmax = tf.Variable(Randomer.random_normal( [self.edim, self.class_num]), trainable=True) self.bline_softmax = tf.Variable(tf.zeros([1, 1]), trainable=True) else: self.wline_softmax = params['wline'] self.bline_softmax = params['bline']
def __init__(self, w_shape=None, stddev=None, params=None, active='tanh'): ''' the initialize function. w_shape is the shape of the w param. if params is None, need. staddev is the stddev of the tf.random_normal. if params is None, need. params = {'wline':wline}, is use to assign the params. active is the active function. ''' self.w_shape = w_shape self.stddev = stddev if params is None: self.wline = tf.Variable( Randomer.random_normal(self.w_shape), # tf.random_uniform(self.w_shape, -0.0015, 0.035), trainable=True) else: self.wline = params['wline'] self.active = active
def main(options, modelconf="config/model.conf"): ''' model: 需要加载的模型 dataset: 需要加载的数据集 reload: 是否需要重新加载数据,yes or no modelconf: model config文件所在的路径 class_num: 分类的类别 use_term: 是否是对aspect term 进行分类 ''' model = options.model dataset = options.dataset reload = options.reload class_num = options.classnum is_train = not options.not_train is_save = not options.not_save_model model_path = options.model_path input_data = options.input_data epoch = options.epoch module, obj, config = load_conf(model, modelconf) config['model'] = model print(model) config['dataset'] = dataset config['class_num'] = class_num config['nepoch'] = epoch train_data, test_data = load_tt_datas(config, reload) module = __import__(module, fromlist=True) # setup randomer Randomer.set_stddev(config['stddev']) with tf.Graph().as_default(): # build model model = getattr(module, obj)(config) model.build_model() if is_save or not is_train: saver = tf.train.Saver(max_to_keep=30) else: saver = None # run with tf.Session() as sess: sess.run(tf.global_variables_initializer()) if is_train: print(dataset) if dataset == "cikm16": model.train(sess, train_data, test_data, saver, threshold_acc=config['cikm_threshold_acc']) elif dataset == "rsc15": model.train(sess, train_data, test_data, saver, threshold_acc=config['recsys_threshold_acc']) else: model.train(sess, train_data, test_data, saver) # if dataset == "rsc15": # model.train(sess, train_data, test_data, saver, threshold_acc=config['recsys_threshold_acc']) else: if input_data is "test": sent_data = test_data elif input_data is "train": sent_data = train_data else: sent_data = test_data saver.restore(sess, model_path) model.test(sess, sent_data)