def __init__(self, *args, **kwargs): """""" super(Network, self).__init__(*args, **kwargs) # hacky! #hacky_train_files = op.join(self.save_dir, op.basename(self.get("train_files"))) #self._config.set('Configurable', 'train_files', hacky_train_files) # TODO make this more flexible, maybe specify it in config? temp_nlp_model = self.nlp_model.from_configurable(self) if temp_nlp_model.input_vocabs == ['tags']: word_vocab = WordVocab.from_configurable(self) word_multivocab = Multivocab.from_configurable(self, [word_vocab], name=word_vocab.name) tag_vocab = TagVocab.from_configurable(self, initialize_zero=False) else: word_vocab = WordVocab.from_configurable(self) pretrained_vocab = PretrainedVocab.from_vocab(word_vocab) subtoken_vocab = self.subtoken_vocab.from_vocab(word_vocab) word_multivocab = Multivocab.from_configurable(self, [word_vocab, pretrained_vocab, subtoken_vocab], name=word_vocab.name) #word_multivocab = Multivocab.from_configurable(self, [word_vocab, pretrained_vocab], name=word_vocab.name) tag_vocab = TagVocab.from_configurable(self) dep_vocab = DepVocab.from_configurable(self) lemma_vocab = LemmaVocab.from_configurable(self) xtag_vocab = XTagVocab.from_configurable(self) head_vocab = HeadVocab.from_configurable(self) rel_vocab = RelVocab.from_configurable(self) self._vocabs = [dep_vocab, word_multivocab, lemma_vocab, tag_vocab, xtag_vocab, head_vocab, rel_vocab] self._global_step = tf.Variable(0., trainable=False, name='global_step') self._global_epoch = tf.Variable(0., trainable=False, name='global_epoch') self._optimizer = RadamOptimizer.from_configurable(self, global_step=self.global_step) return
def __call__(self): """""" #création d'un optimizer RAdam radam_optimizer = RadamOptimizer.from_configurable(self, learning_rate=1e-1, decay_steps=500) x = tf.placeholder(tf.float32, shape=(None, 1), name='x') y = tf.placeholder(tf.float32, shape=(None, 1), name='y') def affine(a, b): return a * tf.log(x) + b a = tf.get_variable('a', shape=self.n_zipfs, dtype=tf.float32, initializer=tf.random_normal_initializer()) b = tf.get_variable('b', shape=self.n_zipfs, dtype=tf.float32, initializer=tf.random_normal_initializer()) s = tf.get_variable('s', shape=self.n_zipfs, dtype=tf.float32, initializer=tf.random_uniform_initializer(-2, -.5)) t = tf.get_variable('t', shape=self.n_zipfs, dtype=tf.float32, initializer=tf.random_normal_initializer()) w = tf.expand_dims(tf.nn.softmax(affine(a, b)), axis=1, name='w') z = tf.expand_dims(affine(s, t), axis=2, name='z') yhat = tf.squeeze(tf.matmul(w, z), axis=2, name='yhat') ell = tf.reduce_mean((tf.log(y) - yhat)**2 / 2, name='ell') ell += tf.reduce_mean((tf.reduce_max(w, axis=0) - 1)**2 / 2) minimize = radam_optimizer.minimize(ell, name='minimize') return x, y, ell, minimize
def __init__(self, train=False, *args, **kwargs): """""" super(Network, self).__init__(*args, **kwargs) # hacky! #hacky_train_files = op.join(self.save_dir, op.basename(self.get("train_files"))) #self._config.set('Configurable', 'train_files', hacky_train_files) #todo: essayer de reserver tt le gpu avant toute chose, si ça n'est pas possible on passe au prochain gpu gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=1) sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options)) temp_nlp_model = self.nlp_model.from_configurable(self) print("\nloading word vocab") word_vocab = WordVocab.from_configurable(self) print("\nloading pretrained vocab") pretrained_vocab = PretrainedVocab.from_vocab(word_vocab) print("\nloading tag representation vocab") tagrep_vocab = None if 'tagrep' in temp_nlp_model.input_vocabs: tagrep_vocab = TagRepVocab.from_vocab(word_vocab) print("\nloading subtoken vocab") subtoken_vocab = self.subtoken_vocab.from_vocab(word_vocab) print("\ncreating multivocab") vocabs_of_multivocab = [word_vocab, pretrained_vocab] word_multivocab = Multivocab.from_configurable(self, vocabs_of_multivocab, name=word_vocab.name) if ('chars' in temp_nlp_model.input_vocabs or word_multivocab.merge_strategy != "only_pretrained"): vocabs_of_multivocab.append(subtoken_vocab) word_multivocab = Multivocab.from_configurable( self, vocabs_of_multivocab, name=word_vocab.name) print(word_multivocab.merge_strategy) print('chars' in temp_nlp_model.input_vocabs) print(word_multivocab.merge_strategy != "only_pretrained") print("\nloading tag vocab") tag_vocab = None if 'tags' in temp_nlp_model.input_vocabs or 'tags' in temp_nlp_model.output_vocabs: tag_vocab = TagVocab.from_configurable(self) print("\nloading dep vocab") dep_vocab = DepVocab.from_configurable(self) print("\nloading lemma vocab") lemma_vocab = LemmaVocab.from_configurable(self) print("\nloading xtag vocab") xtag_vocab = None if 'xtags' in temp_nlp_model.input_vocabs or 'xtags' in temp_nlp_model.output_vocabs or 'subxtags' in temp_nlp_model.input_vocabs or 'subxtags' in temp_nlp_model.output_vocabs: xtag_vocab = XTagVocab.from_configurable(self) print("\nloading feat vocab") feat_vocab = None if 'feats' in temp_nlp_model.input_vocabs or 'feats' in temp_nlp_model.output_vocabs or 'subfeats' in temp_nlp_model.input_vocabs or 'subfeats' in temp_nlp_model.output_vocabs: feat_vocab = FeatVocab.from_configurable(self) print("\nloading subfeat vocab") subfeat_vocabs = None if 'subfeats' in temp_nlp_model.input_vocabs or 'subfeats' in temp_nlp_model.output_vocabs: subfeat_vocabs = self.prepare_subpos_vocabs( feat_vocab, temp_nlp_model) print("\nloading subxtag vocab") subxtag_vocabs = None if 'subxtags' in temp_nlp_model.input_vocabs or 'subxtags' in temp_nlp_model.output_vocabs: subxtag_vocabs = self.prepare_subpos_vocabs( xtag_vocab, temp_nlp_model) head_vocab = HeadVocab.from_configurable(self) rel_vocab = RelVocab.from_configurable(self) self._vocabs = [ dep_vocab, word_multivocab, lemma_vocab, tag_vocab, xtag_vocab, feat_vocab, head_vocab, rel_vocab, tagrep_vocab ] if subfeat_vocabs != None: self._vocabs.extend(subfeat_vocabs) if subxtag_vocabs != None: self._vocabs.extend(subxtag_vocabs) self._vocabs = filter(None, self._vocabs) print(self._vocabs) #print('subfeat_vocabs:',len(subfeat_vocabs)) #print('subxtag_vocabs:',len(subxtag_vocabs)) self._global_step = tf.Variable(0., trainable=False, name='global_step') self._global_epoch = tf.Variable(0., trainable=False, name='global_epoch') self._optimizer = RadamOptimizer.from_configurable( self, global_step=self.global_step) if train: self._global_time = tf.Variable(0., trainable=False, name='global_time') return