예제 #1
0
def create_model(session, forward_only):
    """Create translation model and initialize or load parameters in session."""
    model = seq2seq_model.Seq2SeqModel(FLAGS.en_vocab_size,
                                       FLAGS.fr_vocab_size,
                                       _buckets,
                                       FLAGS.size,
                                       FLAGS.num_layers,
                                       FLAGS.max_gradient_norm,
                                       FLAGS.batch_size,
                                       FLAGS.learning_rate,
                                       FLAGS.learning_rate_decay_factor,
                                       use_lstm=True,
                                       num_samples=FLAGS.num_samples,
                                       forward_only=forward_only,
                                       bidirectional=True,
                                       lstm_type=FLAGS.lstm_type)

    # print the trainable variables
    for v in tf.trainable_variables():
        print(v.name)

    ckpt = tf.train.get_checkpoint_state(FLAGS.model_dir)
    print(ckpt)

    # one-off <ignore>
    # tmp_model_path = os.path.join(FLAGS.data_dir, "my-model-00418500")
    # tmp_model_path = os.path.join("..\..\..\RQnA\PhillyExperiments\FirstIteration_2_layer_lstm_advance\Checkpoints", "my-model-00418500")
    # model.saver.restore(session, ckpt.tmp_model_path)
    # try:
    #   model.saver.restore(session, tmp_model_path)
    #   print("Restored Model ! ")
    # except Exception as e:
    #   print("Not able to Restore !")

    try:
        print("Master, Trying to read model parameters from %s" %
              ckpt.model_checkpoint_path)
        model.saver.restore(session, ckpt.model_checkpoint_path)
        print("Master, Parameters Read !!")
    except Exception as e:
        print("Master, Creating model with fresh parameters.")
        session.run(tf.initialize_all_variables())
        print("Master, Created model with fresh parameters.")

    return model
예제 #2
0
def create_model(session, forward_only):

  """Create model and initialize or load parameters"""
  model = seq2seq_model.Seq2SeqModel( gConfig['enc_vocab_size'], gConfig['dec_vocab_size'], _buckets, gConfig['layer_size'], gConfig['num_layers'], gConfig['max_gradient_norm'], gConfig['batch_size'], gConfig['learning_rate'], gConfig['learning_rate_decay_factor'], forward_only=forward_only)

  if 'pretrained_model' in gConfig:
      model.saver.restore(session,gConfig['pretrained_model'])
      return model

  ckpt = tf.train.get_checkpoint_state(gConfig['working_directory'])
  # if ckpt and tf.gfile.Exists(ckpt.model_checkpoint_path):
  if ckpt:
    print("Reading model parameters from %s" % ckpt.model_checkpoint_path)
    model.saver.restore(session, ckpt.model_checkpoint_path)
  else:
    print("Created model with fresh parameters.")
    session.run(tf.global_variables_initializer())
  return model
예제 #3
0
def self_test():
  """Test the translation model."""
  with tf.Session() as sess:
    print("Self-test for neural translation model.")
    # Create model with vocabularies of 10, 2 small buckets, 2 layers of 32.
    model = seq2seq_model.Seq2SeqModel(10, 10, [(3, 3), (6, 6)], 32, 2,
                                       5.0, 32, 0.3, 0.99, num_samples=8)
    sess.run(tf.initialize_all_variables())

    # Fake data set for both the (3, 3) and (6, 6) bucket.
    data_set = ([([1, 1], [2, 2]), ([3, 3], [4]), ([5], [6])],
                [([1, 1, 1, 1, 1], [2, 2, 2, 2, 2]), ([3, 3, 3], [5, 6])])
    for _ in xrange(5):  # Train the fake model for 5 steps.
      bucket_id = random.choice([0, 1])
      encoder_inputs, decoder_inputs, target_weights = model.get_batch(
          data_set, bucket_id)
      model.step(sess, encoder_inputs, decoder_inputs, target_weights,
                 bucket_id, False)
예제 #4
0
def self_decode():
    """Test the translation model."""
    with tf.Session() as sess:

        print("Self-test for neural translation model.")
        # Create model with vocabulariecds of 10, 2 small buckets, 2 layers of 32.
        model = seq2seq_model.Seq2SeqModel(20,
                                           20, [(13, 13), (13, 13)],
                                           128,
                                           2,
                                           5.0,
                                           32,
                                           0.3,
                                           0.99,
                                           5,
                                           num_samples=8)
        sess.run(tf.initialize_all_variables())

        saver = tf.train.Saver()
        saver.restore(sess, "train/translate.ckpt")

        res_logits = []
        einputs = []
        dinputs = []
        for j in range(1):
            data_set = my_data()
            #print('----------------------------------------')
            bucket_id = random.choice([0, 1])
            encoder_inputs, decoder_inputs, target_weights = model.get_batch(
                data_set, bucket_id)
            _a, _b, output_logits = model.step(sess, encoder_inputs,
                                               decoder_inputs, target_weights,
                                               bucket_id, True)

            #print(len(output_logits))
            #print(len(output_logits[0]))
            #print(len(output_logits[0][0]))
            #print(_b)

            einputs.append(encoder_inputs)
            dinputs.append(decoder_inputs)
            res_logits.append(output_logits)
        return einputs, dinputs, res_logits  #(13,32,128)
예제 #5
0
파일: chars.py 프로젝트: teinvdlugt/PWS
def create_model(session, forward_only):
    """Create translation model and initialize or load parameters in session."""
    model = seq2seq_model.Seq2SeqModel(buckets,
                                       size,
                                       num_layers,
                                       max_gradient_norm,
                                       batch_size,
                                       learning_rate,
                                       learning_rate_decay_factor,
                                       forward_only=forward_only,
                                       dtype=dtype)
    ckpt = tf.train.get_checkpoint_state(train_dir)
    if ckpt and tf.gfile.Exists(ckpt.model_checkpoint_path):
        print("Reading model parameters from %s" % ckpt.model_checkpoint_path)
        model.saver.restore(session, ckpt.model_checkpoint_path)
    else:
        print("Creating model with fresh parameters.")
        session.run(tf.initialize_all_variables())
    return model
예제 #6
0
def create_model(session, predict_only):
    model = seq2seq_model.Seq2SeqModel(EN_VOCAB_SIZE,
                                       EN_VOCAB_SIZE,
                                       SIZE,
                                       NUM_LAYERS,
                                       MAX_GRADIENT_NORM,
                                       MODEL_LENGTH,
                                       BATCH_SIZE,
                                       LEARNING_RATE,
                                       LEARNING_RATE_DECAY_FACTOR,
                                       predict_only=predict_only)
    ckpt = tf.train.get_checkpoint_state(DATA_PATH)
    if ckpt and tf.gfile.Exists(ckpt.model_checkpoint_path):
        print("Reading model parameters from %s" % ckpt.model_checkpoint_path)
        model.saver.restore(session, ckpt.model_checkpoint_path)
    else:
        print "created model with fresh parameters"
        session.run(tf.initialize_all_variables())
    return model
예제 #7
0
def self_test():
  """Test the translation model."""
  with tf.Session(config=tf.ConfigProto(intra_op_parallelism_threads=NUM_THREADS)) as sess:
    print("Self-test for modified neural translation model.")
    # Create model with vocabularies of 10, 2 small buckets, 2 layers of 32.
    model = seq2seq_model.Seq2SeqModel(10, 10, [(3, 3), (6, 6)], 2, 2, 10,
                                       5.0, 2, 0.3, 0.99, num_samples=8)
    sess.run(tf.initialize_all_variables())

    # Fake data set for both the (3, 3) and (6, 6) bucket.
    data_set = ([([1, 1], [2, 2]), ([3, 3], [4]), ([5], [6])],
                [([1, 1, 1, 1, 1], [2, 2, 2, 2, 2]), ([3, 3, 3], [5, 6])])
    for _ in xrange(5):  # Train the fake model for 5 steps.
      for bucket_id in (0, 1):
        for bucket_offset in (0,2):
          encoder_inputs, decoder_inputs, target_weights, seq_len = model.get_ordered_batch(
                  data_set, bucket_id, bucket_offset)
          model.step(sess, encoder_inputs, decoder_inputs, target_weights, seq_len, bucket_id, False)
          print(len(encoder_inputs), len(decoder_inputs))
def create_model(session, forward_only):
    model = seq2seq_model.Seq2SeqModel(FLAGS.reactant_vocab_size,
                                       FLAGS.product_vocab_size,
                                       _buckets,
                                       FLAGS.size,
                                       FLAGS.num_layers,
                                       FLAGS.max_gradient_norm,
                                       FLAGS.batch_size,
                                       FLAGS.learning_rate,
                                       FLAGS.learning_rate_decay_factor,
                                       forward_only=forward_only)
    ckpt = tf.train.get_checkpoint_state(FLAGS.train_dir)
    if ckpt and tf.gfile.Exists(ckpt.model_checkpoint_path):
        print("Reading model parameters from %s" % ckpt.model_checkpoint_path)
        model.saver.restore(session, ckpt.model_checkpoint_path)
    else:
        print("Created model with fresh parameters.")
        session.run(tf.initialize_all_variables())
    return model
예제 #9
0
def createModel(session, forward_only, from_vocab_size, to_vocab_size):
    model = seq2seq_model.Seq2SeqModel(from_vocab_size,
                                       to_vocab_size,
                                       _buckets,
                                       hidden_size,
                                       num_layers,
                                       dropout,
                                       grad_clip,
                                       batch_size,
                                       learning_rate,
                                       lr_decay_factor,
                                       forward_only=forward_only,
                                       dtype=tf.float32)
    ckpt = tf.train.latest_checkpoint(checkpoint_dir)
    if ckpt != None:
        model.saver.restore(session, ckpt)
    else:
        session.run(tf.global_variables_initializer())
    return model
예제 #10
0
 def create_model(self, session, forward_only):
     """Create dialogue model and initialize or load parameters in session."""
     dtype = tf.float32
     model = seq2seq_model.Seq2SeqModel(
         Configuration.VOCAB_SIZE,
         _buckets,
         Configuration.SIZE,
         Configuration.NUM_LAYERS,
         Configuration.MAX_GRADIENT_NORM,
         Configuration.BATCH_SIZE,
         Configuration.LEARNING_RATE,
         Configuration.LEARNING_RATE_DECAY_FACTOR,
         forward_only=forward_only,
         dtype=dtype)
     # use existing model & checkpoint
     ckpt = tf.train.get_checkpoint_state(Configuration.TRAIN_DIR)
     print("Reading model parameters from %s" % ckpt.model_checkpoint_path)
     model.saver.restore(session, ckpt.model_checkpoint_path)
     return model
예제 #11
0
def create_model(actions, sampling=False):

    policy_net = seq2seq_model.Seq2SeqModel(
        FLAGS.architecture,
        FLAGS.seq_length_in if not sampling else 50,
        FLAGS.seq_length_out if not sampling else 25,
        FLAGS.size, # hidden layer size
        FLAGS.num_layers,
        FLAGS.batch_size,
        summaries_dir,
        FLAGS.loss_to_use if not sampling else "sampling_based",
        len( actions ),
        device,
        not FLAGS.omit_one_hot,
        FLAGS.residual_velocities,
        stochastic = True,
        dtype=torch.float32)

    discrim_net = discriminator.Discriminator(
        not FLAGS.omit_one_hot,
        FLAGS.discrim_hidden_size,
        FLAGS.batch_size,
        FLAGS.discrim_num_layers,
        len( actions )
    )

    #initalize a new model
    if FLAGS.load <= 0 and FLAGS.discrim_load <=0:
        print("Creating model with fresh parameters.")
        #TODO: Initial parameter here
        return policy_net, discrim_net
    #Load model from iteration
    if os.path.isfile(os.path.join(train_dir, 'pretrain-policy-checkpoint-{0}.pt'.format(FLAGS.load))):
        policy_net.load_state_dict(torch.load(os.path.join(train_dir, 'pretrain-policy-checkpoint-{0}.pt'.format(FLAGS.load))))
    elif FLAGS.load > 0:
        raise ValueError("Asked to load pretrain policy checkpoint {0}, but it does not seem to exist".format(FLAGS.load))

    if os.path.isfile(os.path.join(train_dir, 'pretrain-discrim-checkpoint-{0}.pt'.format(FLAGS.discrim_load))):
        policy_net.load_state_dict(torch.load(os.path.join(train_dir, 'discrim-checkpoint-{0}.pt'.format(FLAGS.load))))
    elif FLAGS.discrim_load > 0:
        raise ValueError("Asked to load pretrain discrim checkpoint {0}, but it does not seem to exist".format(FLAGS.discrim_load))

    return policy_net, discrim_net
예제 #12
0
def create_model(session, forward_only):
    """Create translation model and initialize or load parameters in session."""
    dtype = tf.float16 if FLAGS.use_fp16 else tf.float32
    '''
      source_vocab_size,
      target_vocab_size,
      buckets,
      size,
      num_layers,
      max_gradient_norm,
      batch_size,
      learning_rate,
      learning_rate_decay_factor,
      use_lstm=False,
      num_samples=512,
      forward_only=False,
      dtype=tf.float32):
    '''
    model = seq2seq_model.Seq2SeqModel(FLAGS.from_vocab_size,
                                       FLAGS.to_vocab_size,
                                       _buckets,
                                       FLAGS.size,
                                       FLAGS.num_layers,
                                       FLAGS.max_gradient_norm,
                                       FLAGS.batch_size,
                                       FLAGS.learning_rate,
                                       FLAGS.learning_rate_decay_factor,
                                       forward_only=forward_only,
                                       dtype=dtype)

    # ckpt = tf.train.get_checkpoint_state(FLAGS.train_dir)
    ckpt = tf.train.get_checkpoint_state(MODEL_DIR)
    if ckpt and tf.train.checkpoint_exists(ckpt.model_checkpoint_path):
        print("Reading model parameters from %s" % ckpt.model_checkpoint_path)
        # 恢复模型
        model.saver.restore(session, ckpt.model_checkpoint_path)
    else:
        print("Created model with fresh parameters.")
        # Returns an Op that initializes global variables.
        session.run(tf.global_variables_initializer())
    # 返回模型
    return model
예제 #13
0
def create_model(session, forward_only):
  """Create translation model and initialize or load parameters in session."""
  model = seq2seq_model.Seq2SeqModel(source_vocab_size=FLAGS.en_vocab_size,
                                     target_vocab_size=FLAGS.fr_vocab_size,
                                     buckets=_buckets,
                                     size=FLAGS.size,
                                     num_layers=FLAGS.num_layers,
                                     max_gradient_norm=FLAGS.max_gradient_norm,
                                     batch_size=FLAGS.batch_size,
                                     learning_rate=FLAGS.learning_rate,
                                     learning_rate_decay_factor=FLAGS.learning_rate_decay_factor,
                                     forward_only=forward_only)
  ckpt = tf.train.get_checkpoint_state(FLAGS.train_dir)
  if ckpt and tf.train.checkpoint_exists(ckpt.model_checkpoint_path):
    print('Reading model parameters from %s...' % ckpt.model_checkpoint_path)
    model.saver.restore(session, ckpt.model_checkpoint_path)
  else:
    print('Created model with fresh parameters.')
    session.run(tf.global_variables_initializer())
  return model
예제 #14
0
def create_model(session, forward_only):
  """Create translation model and initialize or load parameters in session."""
  dtype = tf.float16 if FLAGS.use_fp16 else tf.float32
  model = seq2seq_model.Seq2SeqModel(
      FLAGS.en_vocab_size,
      FLAGS.fr_vocab_size,
      _buckets,
      FLAGS.size,
      FLAGS.num_layers,
      FLAGS.max_gradient_norm,
      FLAGS.batch_size,
      FLAGS.learning_rate,
      FLAGS.learning_rate_decay_factor,
      forward_only=forward_only,
      dtype=dtype)
  ckpt = tf.train.get_checkpoint_state(FLAGS.train_dir)
  if ckpt and tf.train.checkpoint_exists(ckpt.model_checkpoint_path):
    print("Reading model parameters from %s" % ckpt.model_checkpoint_path)
    model.saver.restore(session, ckpt.model_checkpoint_path)
  return model
def self_test():
  """Test the translation model."""
  config_ = tf.ConfigProto()
  config_.gpu_options.allow_growth = True
  config_.allow_soft_placement = True
  with tf.Session(config=config_) as sess:
    logging.info("Self-test for neural translation model.")
    # Create model with vocabularies of 10, 2 small buckets, 2 layers of 32.
    model = seq2seq_model.Seq2SeqModel(10, 10, [(3, 3), (6, 6)], 32, 2,
                                       5.0, 32, 0.3, 0.99, num_samples=8)
    sess.run(tf.global_variables_initializer())

    # Fake data set for both the (3, 3) and (6, 6) bucket.
    data_set = ([([1, 1], [2, 2]), ([3, 3], [4]), ([5], [6])],
                [([1, 1, 1, 1, 1], [2, 2, 2, 2, 2]), ([3, 3, 3], [5, 6])])
    for _ in xrange(5):  # Train the fake model for 5 steps.
      bucket_id = random.choice([0, 1])
      encoder_inputs, decoder_inputs, target_weights = model.get_batch(
          data_set, bucket_id)
      model.step(sess, encoder_inputs, decoder_inputs, target_weights,
                 bucket_id, False)
예제 #16
0
def create_model(session, forward_only):
  #1024表示隐藏层神经元的个数,3表示网络共三层
  numberhidd=1024
  numlayer=3
  max_gradient_norm=5.#RNN防止梯度爆炸,所以需要在训练的时候,加入梯度裁剪
  batch_size=5
  learning_rate=0.5
  learning_rate_decay_factor=0.99




  model = seq2seq_model.Seq2SeqModel(en_vocab_size,ch_vocab_size , _buckets,numberhidd,numlayer,
                                     max_gradient_norm, batch_size,learning_rate, learning_rate_decay_factor,forward_only=forward_only)
  #如果已经有训练好的模型,那么直接加载参数,否则就初始化全部的参数
  '''ckpt = tf.train.get_checkpoint_state(FLAGS.train_dir)
  if ckpt and tf.gfile.Exists(ckpt.model_checkpoint_path):
    model.saver.restore(session, ckpt.model_checkpoint_path)
  else:'''
  session.run(tf.initialize_all_variables())
  return model
예제 #17
0
def copy_params(session, batch_size=None):
    bw_model = seq2seq_model.Seq2SeqModel(
        vocab_size=bw_config.vocab_size,
        embedding_dim=bw_config.embedding_dim,
        buckets=bw_config.BUCKETS,
        size=bw_config.size,
        num_layers=bw_config.num_layers,
        max_gradient_norm=bw_config.max_gradient_norm,
        batch_size=bw_config.batch_size if not batch_size else batch_size,
        learning_rate=bw_config.learning_rate,
        learning_rate_decay_factor=bw_config.learning_rate_decay_factor,
        use_lstm=True,
        forward_only=True,
        name='bw_model')
    session.run(tf.global_variables_initializer())
    for ele in bw_model.variables:
        f = open('bw_model/tmp/%s.pkl' % ele.name[9:].replace('/', '_'))
        value = pickle.load(f)
        f.close()
        session.run(tf.assign(ele, value))
    bw_model.saver.save(session, 'bw/model.ckpt')
예제 #18
0
def create_model(session, forward_only):
    """Create speechT model and initialize or load parameters in session."""
    dtype = tf.float16 if FLAGS.use_fp16 else tf.float32
    model = seq2seq_model.Seq2SeqModel(FLAGS.vocab_size,
                                       _buckets,
                                       FLAGS.size,
                                       FLAGS.num_layers,
                                       FLAGS.max_gradient_norm,
                                       FLAGS.batch_size,
                                       FLAGS.learning_rate,
                                       FLAGS.learning_rate_decay_factor,
                                       forward_only=forward_only,
                                       dtype=dtype)
    ckpt = tf.train.get_checkpoint_state(FLAGS.train_dir)
    if ckpt and tf.gfile.Exists(ckpt.model_checkpoint_path):
        print("Reading model parameters from %s" % ckpt.model_checkpoint_path)
        model.saver.restore(session, ckpt.model_checkpoint_path)
    else:
        print("Created model with fresh parameters.")
        session.run(tf.initialize_all_variables())
    return model
예제 #19
0
def create_model(session, forward_only):
    model = seq2seq_model.Seq2SeqModel(FLAGS.enc_vocab_size,
                                       FLAGS.dec_vocab_size,
                                       _buckets,
                                       FLAGS.layer_size,
                                       FLAGS.num_layers,
                                       FLAGS.max_gradient_norm,
                                       FLAGS.batch_size,
                                       FLAGS.learning_rate,
                                       FLAGS.learning_rate_decay_factor,
                                       forward_only=forward_only)

    ckpt = tf.train.get_checkpoint_state(FLAGS.working_directory)

    if ckpt and ckpt.model_checkpoint_path:
        print("Reading model parameters from %s" % ckpt.model_checkpoint_path)
        model.saver.restore(session, ckpt.model_checkpoint_path)
    else:
        print("Created model with fresh parameters.")
        session.run(tf.global_variables_initializer())
    return model
예제 #20
0
def create_model(session, forward_only):

  """Create model and initialize or load parameters"""
  model = seq2seq_model.Seq2SeqModel( gConfig['enc_vocab_size'], gConfig['dec_vocab_size'], _buckets, gConfig['layer_size'], gConfig['num_layers'], gConfig['max_gradient_norm'], gConfig['batch_size'], gConfig['learning_rate'], gConfig['learning_rate_decay_factor'], forward_only=forward_only)

  if 'pretrained_model' in gConfig:
      model.saver.restore(session,gConfig['pretrained_model'])
      return model

  ckpt = tf.train.get_checkpoint_state(gConfig['working_directory'])
  # the checkpoint filename has changed in recent versions of tensorflow
  checkpoint_suffix = ""
  if tf.__version__ > "0.12":
      checkpoint_suffix = ".index"
  if ckpt and tf.gfile.Exists(ckpt.model_checkpoint_path + checkpoint_suffix):
    print("Reading model parameters from %s" % ckpt.model_checkpoint_path)
    model.saver.restore(session, ckpt.model_checkpoint_path)
  else:
    print("Created model with fresh parameters.")
    session.run(tf.initialize_all_variables())
  return model
예제 #21
0
def self_test():
    """Test the translation model."""
    with tf.Session() as sess:
        print("Self-test for neural translation model.")
        # Create model with vocabularies of 10, 2 small buckets, 2 layers of 32.
        '''
           source_vocab_size,
           target_vocab_size,
           buckets,
           size,
           num_layers,
           max_gradient_norm,
           batch_size,
           learning_rate,
           learning_rate_decay_factor,
           use_lstm=False,
           num_samples=512,
           forward_only=False,
        '''
        model = seq2seq_model.Seq2SeqModel(10,
                                           10, [(3, 3), (6, 6)],
                                           32,
                                           2,
                                           5.0,
                                           32,
                                           0.3,
                                           0.99,
                                           num_samples=8)
        sess.run(tf.global_variables_initializer())

        # Fake data set for both the (3, 3) and (6, 6) bucket.
        data_set = ([([1, 1], [2, 2]), ([3, 3], [4]),
                     ([5], [6])], [([1, 1, 1, 1, 1], [2, 2, 2, 2, 2]),
                                   ([3, 3, 3], [5, 6])])
        for _ in xrange(5):  # Train the fake model for 5 steps.
            bucket_id = random.choice([0, 1])  # 随机选取0 1
            encoder_inputs, decoder_inputs, target_weights = model.get_batch(
                data_set, bucket_id)
            model.step(sess, encoder_inputs, decoder_inputs, target_weights,
                       bucket_id, False)
예제 #22
0
def create_model(sess, forward_only):
    dtype = tf.float16 if FLAGS.use_fp16 else tf.float32
    model = seq2seq_model.Seq2SeqModel(
        FLAGS.form_vocab_size,
        FLAGS.to_vocab_size,
        _buckets,
        FLAGS.size,
        FLAGS.num_layers,
        FALGS.max_gradinet_norm,
        FLAGS.batch_size,
        FALGS.learning_rate,
        FALGS.learning_rate_decay_factor,
        forward_only=forward_only,
        dtype=dtype)
    ckpt = tf.train.get_checkpoint_state(FLAGS.train_dir)
    if ckpt and tf.train.checkpoint_exits(ckpt.model_checkpoint_path):
        print("Reading model params from %s" % ckpt.model_checkpoint_path)
        model.saver.restore(sess, ckpt.model_checkpoint_path)
    else:
        print("Created model with fresh params")
        sess.run(tf.global_variables_initializer())
    return model
예제 #23
0
파일: test.py 프로젝트: S4NdeeP/char-lstm
def create_model(session, forward_only):
    """Create translation model and initialize or load parameters in session."""
    dtype = tf.float16 if use_fp16 else tf.float32
    model = seq2seq_model.Seq2SeqModel(from_vocab_size,
                                       to_vocab_size,
                                       _buckets,
                                       size,
                                       num_layers,
                                       max_gradient_norm,
                                       batch_size,
                                       lr,
                                       learning_rate_decay_factor,
                                       forward_only=forward_only,
                                       dtype=dtype)
    ckpt = tf.train.get_checkpoint_state(train_dir)
    if ckpt and tf.train.checkpoint_exists(ckpt.model_checkpoint_path):
        print("Reading model parameters from %s" % ckpt.model_checkpoint_path)
        model.saver.restore(session, ckpt.model_checkpoint_path)
    else:
        print("Created model with fresh parameters.")
        session.run(tf.global_variables_initializer())
    return model
예제 #24
0
def create_model(session, forward_only):
    """Create transliteration model and initialize or load parameters in session."""
    model = seq2seq_model.Seq2SeqModel(FLAGS.en_vocab_size,
                                       FLAGS.hn_vocab_size,
                                       _buckets,
                                       FLAGS.size,
                                       FLAGS.num_layers,
                                       FLAGS.max_gradient_norm,
                                       FLAGS.batch_size,
                                       FLAGS.learning_rate,
                                       FLAGS.learning_rate_decay_factor,
                                       forward_only=forward_only,
                                       use_lstm=True,
                                       drop_out=FLAGS.drop_out)
    ckpt = tf.train.get_checkpoint_state(FLAGS.train_dir)
    if ckpt and ckpt.model_checkpoint_path:
        print("Reading model parameters from %s" % ckpt.model_checkpoint_path)
        model.saver.restore(session, ckpt.model_checkpoint_path)
    else:
        print("Created model with fresh parameters.")
        session.run(tf.global_variables_initializer())
    return model
예제 #25
0
def create_model(session, forward_only):
    """
    创建text_sum模型
    :param session:tf的session
    :param forward_only:是否更新参数
    :return:模型model
    """
    dtype = tf.float16 if FLAGS.use_fp16 else tf.float32
    # 参数初始化函数
    initializer = tf.random_uniform_initializer(-config.init_scale,
                                                config.init_scale)
    with tf.variable_scope(FLAGS.headline_scope_name,
                           reuse=None,
                           initializer=initializer,
                           dtype=dtype):
        model = seq2seq_model.Seq2SeqModel(
            FLAGS.vocab_size,
            FLAGS.vocab_size,
            buckets,
            FLAGS.size,
            FLAGS.num_layers,
            FLAGS.max_gradient_norm,
            FLAGS.batch_size,
            FLAGS.learning_rate,
            FLAGS.learning_rate_decay_factor,
            use_lstm=True,  # LSTM instend of GRU
            num_samples=FLAGS.num_samples,
            forward_only=forward_only)
        # 先检测模型文件是否存在
        ckpt = tf.train.get_checkpoint_state(FLAGS.train_dir)
        if ckpt:
            model_checkpoint_path = ckpt.model_checkpoint_path
            print("Reading model parameters from %s" % model_checkpoint_path)
            saver = tf.train.Saver()
            saver.restore(session, tf.train.latest_checkpoint(FLAGS.train_dir))
        else:
            print("Created model with fresh parameters.")
            session.run(tf.global_variables_initializer())
        return model
예제 #26
0
def create_model(session, forward_only):
    """Create translation model and initialize or load parameters in session."""
    model = seq2seq_model.Seq2SeqModel(FLAGS.src_vocab_size,
                                       FLAGS.dst_vocab_size,
                                       _buckets,
                                       FLAGS.size,
                                       FLAGS.num_layers,
                                       FLAGS.max_gradient_norm,
                                       FLAGS.batch_size,
                                       FLAGS.learning_rate,
                                       FLAGS.learning_rate_decay_factor,
                                       forward_only=forward_only,
                                       use_lstm=FLAGS.use_lstm,
                                       output_keep_prob=FLAGS.output_keep_prob)
    ckpt = tf.train.get_checkpoint_state(FLAGS.train_dir)
    if ckpt and tf.gfile.Exists(ckpt.model_checkpoint_path):
        print("Reading model parameters from %s" % ckpt.model_checkpoint_path)
        model.saver.restore(session, ckpt.model_checkpoint_path)
    else:
        print("Created model with fresh parameters.")
        session.run(tf.initialize_all_variables())
    return model
예제 #27
0
파일: execute.py 프로젝트: WilliamTseCh/nmt
def create_model(session, forward_only):
    model = seq2seq_model.Seq2SeqModel(session,
                                       outfile,
                                       gConfig['enc_vocab_size'],
                                       gConfig['dec_vocab_size'],
                                       _buckets,
                                       gConfig['layer_size'],
                                       gConfig['num_layers'],
                                       gConfig['max_gradient_norm'],
                                       gConfig['batch_size'],
                                       gConfig['learning_rate'],
                                       gConfig['learning_rate_decay_factor'],
                                       forward_only=forward_only)
    ckpt = tf.train.get_checkpoint_state(gConfig['working_directory'])
    if ckpt and ckpt.model_checkpoint_path:
        outfile.write("Reading model parameters from %s" %
                      ckpt.model_checkpoint_path + "\n")
        model.saver.restore(session, ckpt.model_checkpoint_path)
    else:
        outfile.write("Created model with fresh parameters.\n")
        session.run(tf.global_variables_initializer())
    return model
예제 #28
0
def create_model(session, forward_only, ckpt_file=None):
    """Create translation model and initialize or load parameters in session."""
    emb_en_file = file(FLAGS.constant_emb_en_dir, "rb")
    emb_fr_file = file(FLAGS.constant_emb_fr_dir, "rb")
    constant_emb_en = pkl.load(emb_en_file)  # added by al
    constant_emb_fr = pkl.load(emb_fr_file)  # added by al
    model = seq2seq_model.Seq2SeqModel(
        FLAGS.en_vocab_size_1,
        FLAGS.en_vocab_size_2,
        FLAGS.fr_vocab_size,
        _buckets,
        FLAGS.hidden_edim,
        FLAGS.hidden_units,  # by yfeng
        FLAGS.num_layers,
        FLAGS.max_gradient_norm,
        FLAGS.batch_size,
        FLAGS.learning_rate,
        FLAGS.learning_rate_decay_factor,
        FLAGS.beam_size,  # added by shiyue
        constant_emb_en=constant_emb_en,  # added by al
        constant_emb_fr=constant_emb_fr,  # added by al
        forward_only=forward_only)
    if ckpt_file:
        model_path = os.path.join(FLAGS.train_dir, ckpt_file)
        if tf.gfile.Exists(model_path):
            sys.stderr.write("Reading model parameters from %s\n" % model_path)
            sys.stderr.flush()
            model.saver.restore(session, model_path)
    else:
        ckpt = tf.train.get_checkpoint_state(FLAGS.train_dir)
        if ckpt and tf.gfile.Exists(ckpt.model_checkpoint_path):
            print("Reading model parameters from %s" %
                  ckpt.model_checkpoint_path)
            model.saver.restore(session, ckpt.model_checkpoint_path)
        else:
            print("Created model with fresh parameters.")
            session.run(tf.initialize_all_variables())
    return model
예제 #29
0
    def load_decode_model(self):
        """Load G2P model and initialize or load parameters in session."""
        if not os.path.exists(os.path.join(self.model_dir, 'checkpoint')):
            raise RuntimeError("Model not found in %s" % self.model_dir)

        self.batch_size = 1  # We decode one word at a time.
        #Load model parameters.
        num_layers, size = data_utils.load_params(self.model_dir)
        # Load vocabularies
        print("Loading vocabularies from %s" % self.model_dir)
        self.gr_vocab = data_utils.load_vocabulary(
            os.path.join(self.model_dir, "vocab.grapheme"))
        self.ph_vocab = data_utils.load_vocabulary(
            os.path.join(self.model_dir, "vocab.phoneme"))

        self.rev_ph_vocab =\
          data_utils.load_vocabulary(os.path.join(self.model_dir, "vocab.phoneme"),
                                     reverse=True)

        self.session = tf.Session()

        # Restore model.
        print("Creating %d layers of %d units." % (num_layers, size))
        self.model = seq2seq_model.Seq2SeqModel(len(self.gr_vocab),
                                                len(self.ph_vocab),
                                                self._BUCKETS,
                                                size,
                                                num_layers,
                                                0,
                                                self.batch_size,
                                                0,
                                                0,
                                                forward_only=True)
        self.model.saver = tf.train.Saver(tf.global_variables(), max_to_keep=1)
        # Check for saved models and restore them.
        print("Reading model parameters from %s" % self.model_dir)
        self.model.saver.restore(self.session,
                                 os.path.join(self.model_dir, "model"))
예제 #30
0
파일: main.py 프로젝트: tttwwy/tensorflow
def create_model(session, forward_only):
    """Create translation model and initialize or load parameters in session."""
    model = seq2seq_model.Seq2SeqModel(
        FLAGS.en_vocab_size,
        FLAGS.fr_vocab_size,
        _buckets,
        FLAGS.size,
        FLAGS.num_layers,
        FLAGS.max_gradient_norm,
        FLAGS.batch_size,
        FLAGS.learning_rate,
        FLAGS.learning_rate_decay_factor,
        forward_only=forward_only,
        cell_initializer=tf.constant_initializer(
            np.array(word2vec_model.syn0, dtype=np.float32)))
    ckpt = tf.train.get_checkpoint_state(FLAGS.train_dir)
    if ckpt and tf.gfile.Exists(ckpt.model_checkpoint_path):
        print("Reading model parameters from %s" % ckpt.model_checkpoint_path)
        model.saver.restore(session, ckpt.model_checkpoint_path)
    else:
        print("Created model with fresh parameters.")
        session.run(tf.initialize_all_variables())
    return model