Advertisement
Guest User

Sparse vectors in Tensorflow

a guest
Dec 6th, 2016
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.73 KB | None | 0 0
  1. #############
  2. # READ DATA #
  3. #############
  4.  
  5. # Create filename_queue
  6. filename_queue = tf.train.string_input_producer(train_files, shuffle=True)
  7.  
  8. min_after_dequeue = 1024
  9. capacity          = min_after_dequeue + 3*batch_size
  10. examples_queue = tf.RandomShuffleQueue(
  11.         capacity=capacity,
  12.         min_after_dequeue=min_after_dequeue,
  13.         dtypes=[tf.string])
  14.  
  15. # Create multiple readers to populate the queue of examples
  16. enqueue_ops = []
  17. for i in range(n_readers):
  18.     reader = tf.TextLineReader()
  19.     _key, value = reader.read(filename_queue)
  20.     enqueue_ops.append(examples_queue.enqueue([value]))
  21.  
  22. tf.train.queue_runner.add_queue_runner(
  23.         tf.train.queue_runner.QueueRunner(examples_queue, enqueue_ops))
  24. example_string = examples_queue.dequeue()
  25.  
  26. # Default values, and type of the columns, first is sequence_length
  27. # +1 since first field is sequence length
  28. record_defaults = [[0]]*(max_sequence_length+1)
  29.  
  30. parsed_examples = []
  31. for thread_id in range(n_preprocess_threads):
  32.     example = tf.decode_csv(value, record_defaults=record_defaults)
  33.  
  34.     # Create 1-HOT vectors of the sequence
  35.     one_hots = example[1:]
  36.     one_hots = tf.reshape(one_hots, [-1])
  37.     one_hots = tf.one_hot(one_hots, depth=n_classes)
  38.  
  39.     # Split the row into input/target values
  40.     sequence_length = example[0]
  41.     features = one_hots[:-1]
  42.     targets  = one_hots[1:]
  43.  
  44.    
  45.     parsed_examples.append([sequence_length, features, targets])
  46.  
  47. # Batch together examples
  48. session_length, x, y = tf.train.batch_join(
  49.         parsed_examples,
  50.         batch_size=batch_size,
  51.         capacity=2*n_preprocess_threads*batch_size)
  52.  
  53. ###############
  54. # RNN NETWORK #
  55. ###############
  56.  
  57. output, state = tf.nn.dynamic_rnn(
  58.         tf.nn.rnn_cell.GRUCell(n_hidden),
  59.         x,
  60.         dtype=tf.float32,
  61.         sequence_length=session_length)
  62.            
  63. layer = {'weights':tf.Variable(tf.random_normal([n_hidden, n_classes])),
  64.          'biases':tf.Variable(tf.random_normal([n_classes]))}
  65.  
  66. # Flatten to apply same weights to all time steps.
  67. output = tf.reshape(output, [-1, n_hidden], name="flatOuput")
  68.  
  69. # Add dropout
  70. keep_prob = tf.placeholder(tf.float32)
  71. output = tf.nn.dropout(output, keep_prob)
  72.  
  73. # The models prediction
  74. prediction = tf.matmul(output, layer['weights'], name="prediction")# + layer['biases'] TODO
  75.  
  76. # Reduce sum, since average divides by max_length, which is often wrong
  77. error = tf.nn.softmax_cross_entropy_with_logits(prediction, y, name="crossEntropy")
  78. cost  = tf.reduce_sum(error, name="cost")
  79.  
  80. # The training 'function'
  81. optimizer = tf.train.AdamOptimizer(name="optimizer").minimize(cost)
  82.  
  83. # Top k predictions for testing
  84. top_pred_vals, top_pred_indices = tf.nn.top_k(prediction, k=k, sorted=True, name="topPredictions")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement