2017-11-07 4 views
-1

trainieren Ich trainierte ein Modell mit CNN, shuffle_batch verwendet, um mit großen Dateien umzugehen, und ich legte eine Batchgröße 64 vor dem Training. Es scheint, dass die Stapelgröße während oder nach dem Training nicht geändert werden konnte. Wie kann dann das trainierte Modell verwendet werden, um nur einen Datensatz mit fester Stapelgröße vorauszusagen?Wie auf einen Datensatz vorherzusagen, wenn shuffle_batch verwendet wird, um das Modell in Tensorflow

Gebrauchte Platzhalter für batch_size, Code ist wie folgt:

def train(target_path, vocab_processor): 
    with tf.Graph().as_default(): 
     **batch_size = tf.placeholder(tf.int32, name='batch_size')** 
     data_batch, label_batch = read_data_from_tfrecords(target_path, batch_size) 
     session_conf = tf.ConfigProto(
      allow_soft_placement=FLAGS.allow_soft_placement, 
      log_device_placement=FLAGS.log_device_placement) 
     sess = tf.Session(config=session_conf) 
     with sess.as_default(): 
      cnn = TextCNN(
       sequence_length=data_batch.shape[1], 
       num_classes=label_batch.shape[1], 
       vocab_size=len(vocab_processor.vocabulary_), 
       embedding_size=FLAGS.embedding_dim, 
       filter_sizes=list(map(int, FLAGS.filter_sizes.split(","))), 
       num_filters=FLAGS.num_filters, 
       input_x=data_batch, 
       input_y=label_batch, 
       l2_reg_lambda=FLAGS.l2_reg_lambda 
      ) 
      # Define Training procedure 
      global_step = tf.Variable(0, name="global_step", trainable=False) 
      optimizer = tf.train.AdamOptimizer(1e-3) 
      grads_and_vars = optimizer.compute_gradients(cnn.loss) 
      train_op = optimizer.apply_gradients(grads_and_vars, global_step=global_step) 
      # Keep track of gradient values and sparsity (optional) 
      grad_summaries = [] 
      for g, v in grads_and_vars: 
       if g is not None: 
        grad_hist_summary = tf.summary.histogram("{}/grad/hist".format(v.name), g) 
        sparsity_summary = tf.summary.scalar("{}/grad/sparsity".format(v.name), tf.nn.zero_fraction(g)) 
        grad_summaries.append(grad_hist_summary) 
        grad_summaries.append(sparsity_summary) 
      grad_summaries_merged = tf.summary.merge(grad_summaries) 
      # Output directory for models and summaries 
      timestamp = str(int(time.time())) 
      out_dir = os.path.abspath(os.path.join(os.path.curdir, "runs", timestamp)) 
      print("Writing to {}\n".format(out_dir)) 
      # Summaries for loss and accuracy 
      loss_summary = tf.summary.scalar("loss", cnn.loss) 
      acc_summary = tf.summary.scalar("accuracy", cnn.accuracy) 
      # Train Summaries 
      train_summary_op = tf.summary.merge([loss_summary, acc_summary, grad_summaries_merged]) 
      train_summary_dir = os.path.join(out_dir, "summaries", "train") 
      train_summary_writer = tf.summary.FileWriter(train_summary_dir, sess.graph) 
      # Checkpoint directory. Tensorflow assumes this directory already exists so we need to create it 
      checkpoint_dir = os.path.abspath(os.path.join(out_dir, "checkpoints")) 
      checkpoint_prefix = os.path.join(checkpoint_dir, "model") 
      if not os.path.exists(checkpoint_dir): 
       os.makedirs(checkpoint_dir) 
      saver = tf.train.Saver(tf.global_variables(), max_to_keep=FLAGS.num_checkpoints) 
      init = tf.group(tf.global_variables_initializer(), 
          tf.local_variables_initializer()) 
      sess.run(init) 
      # sess = tf_debug.LocalCLIDebugWrapperSession(sess) 
      coord = tf.train.Coordinator() 
      threads = tf.train.start_queue_runners(sess=sess, coord=coord) 
      try: 
       while not coord.should_stop(): 
        **feed_dict = { 
         cnn.dropout_keep_prob: FLAGS.dropout_keep_prob, 
         batch_size: 64 
        }** 
        _, step, summaries, loss, accuracy = sess.run(
         [train_op, global_step, train_summary_op, cnn.loss, cnn.accuracy], feed_dict) 
        time_str = datetime.datetime.now().isoformat() 
        print("{}: step {}, loss {:g}, acc {:g}".format(time_str, step, loss, accuracy)) 
        train_summary_writer.add_summary(summaries, step) 
        current_step = tf.train.global_step(sess, global_step) 
        if current_step % FLAGS.checkpoint_every == 0: 
         path = saver.save(sess, checkpoint_prefix, global_step=current_step) 
         print("Saved model checkpoint to {}\n".format(path)) 
      except tf.errors.OutOfRangeError: 
       print("done training") 
      finally: 
       coord.request_stop() 
      coord.join(threads) 
      sess.close() 

Fehler:

Traceback (most recent call last): 
     File "/home/ubuntu/Documents/code/error-classify/cnn_classify/test_train.py", line 247, in <module> 
     train(tfRecorder_path, vocab_processor) 
     File "/home/ubuntu/Documents/code/error-classify/cnn_classify/test_train.py", line 82, in train 
     num_threads=2) 
     File "/home/ubuntu/.pyenv/versions/3.5.3/lib/python3.5/site-packages/tensorflow/python/training/input.py", line 1220, in shuffle_batch 
     name=name) 
     File "/home/ubuntu/.pyenv/versions/3.5.3/lib/python3.5/site-packages/tensorflow/python/training/input.py", line 765, in _shuffle_batch 
     if capacity <= min_after_dequeue: 
     File "/home/ubuntu/.pyenv/versions/3.5.3/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 499, in __bool__ 
     raise TypeError("Using a `tf.Tensor` as a Python `bool` is not allowed. " 
    TypeError: Using a `tf.Tensor` as a Python `bool` is not allowed. Use `if t is not None:` instead of `if t:` to test if a tensor is defined, and use TensorFlow ops such as tf.cond to execute subgraphs conditioned on the value of a tensor. 

Antwort

0

Sie können Ihre feste Losgröße durch einen Platzhalter ersetzen, dass Sie bis zu 64 für die Ausbildung gesetzt und zu allem, was du brauchst, wenn du ableitest.

batch_size = tf.placeholder(tf.int32,(), name="batch_size") 
tf.train.shuffle_batch (..., batch_size = batch_size, ...) 
+0

Vielen Dank für die Anwendung. Aber wo sollte ich 'feed_dict = {'batch_size': 32}' setzen? Ich habe es vor sess.run() platziert und einen Fehler bekommen, der besagt, dass TypeError ausgelöst wurde ("Verwenden eines tf.Tensors" als Python-Bool ist nicht erlaubt.) ' –

+0

Könnten Sie einen vollständigeren Teil Ihres Codes als Editierung veröffentlichen? Ihre Frage oder als neue Frage, damit wir uns Ihr Problem ansehen können? – Pop

+0

Ich habe den Code nach der Frage hinzugefügt. Vielen Dank nochmal für Ihre Hilfe –

0

Das Problem ist mit diesem Set allow_smaller_final_batch=True gelöst. Generell sollte beim Testen anstelle von shuffle_batch train.batch verwendet werden.

Fehler bei der Verwendung von Platzhalter, habe nicht herausgefunden, warum

Verwandte Themen