2626import tensorflow as tf
2727import time
2828
29+ # TODO(mmoroz): Use replacements for Tensorflow 2.x
2930from tensorflow .contrib import layers
3031from tensorflow .contrib import rnn
3132
@@ -102,24 +103,26 @@ def main(args):
102103
103104 # Set graph-level random seed, so any random sequence generated in this
104105 # graph is repeatable. It could also be removed.
105- tf .set_random_seed (0 )
106+ tf .compat . v1 . set_random_seed (0 )
106107
107108 # Define placeholder for learning rate, dropout and batch size.
108- lr = tf .placeholder (tf .float32 , name = 'lr' )
109- pkeep = tf .placeholder (tf .float32 , name = 'pkeep' )
110- batchsize = tf .placeholder (tf .int32 , name = 'batchsize' )
109+ lr = tf .compat . v1 . placeholder (tf .float32 , name = 'lr' )
110+ pkeep = tf .compat . v1 . placeholder (tf .float32 , name = 'pkeep' )
111+ batchsize = tf .compat . v1 . placeholder (tf .int32 , name = 'batchsize' )
111112
112113 # Input data.
113- input_bytes = tf .placeholder (tf .uint8 , [None , None ], name = 'input_bytes' )
114+ input_bytes = tf .compat .v1 .placeholder (
115+ tf .uint8 , [None , None ], name = 'input_bytes' )
114116 input_onehot = tf .one_hot (input_bytes , constants .ALPHA_SIZE , 1.0 , 0.0 )
115117
116118 # Expected outputs = same sequence shifted by 1, since we are trying to
117119 # predict the next character.
118- expected_bytes = tf .placeholder (tf .uint8 , [None , None ], name = 'expected_bytes' )
120+ expected_bytes = tf .compat .v1 .placeholder (
121+ tf .uint8 , [None , None ], name = 'expected_bytes' )
119122 expected_onehot = tf .one_hot (expected_bytes , constants .ALPHA_SIZE , 1.0 , 0.0 )
120123
121124 # Input state.
122- hidden_state = tf .placeholder (
125+ hidden_state = tf .compat . v1 . placeholder (
123126 tf .float32 , [None , hidden_state_size * hidden_layer_size ],
124127 name = 'hidden_state' )
125128
@@ -131,7 +134,7 @@ def main(args):
131134 multicell = rnn .MultiRNNCell (dropcells , state_is_tuple = False )
132135 multicell = rnn .DropoutWrapper (multicell , output_keep_prob = pkeep )
133136
134- output_raw , next_state = tf .nn .dynamic_rnn (
137+ output_raw , next_state = tf .compat . v1 . nn .dynamic_rnn (
135138 multicell , input_onehot , dtype = tf .float32 , initial_state = hidden_state )
136139 next_state = tf .identity (next_state , name = 'next_state' )
137140
@@ -143,44 +146,44 @@ def main(args):
143146 expected_flat = tf .reshape (expected_onehot , [- 1 , constants .ALPHA_SIZE ])
144147
145148 # Compute training loss.
146- loss = tf .nn .softmax_cross_entropy_with_logits_v2 (
149+ loss = tf .nn .softmax_cross_entropy_with_logits (
147150 logits = output_logits , labels = expected_flat )
148151 loss = tf .reshape (loss , [batchsize , - 1 ])
149152
150153 # Use softmax to normalize training outputs.
151154 output_onehot = tf .nn .softmax (output_logits , name = 'output_onehot' )
152155
153156 # Use argmax to get the max value, which is the predicted bytes.
154- output_bytes = tf .argmax (output_onehot , 1 )
157+ output_bytes = tf .argmax (input = output_onehot , axis = 1 )
155158 output_bytes = tf .reshape (output_bytes , [batchsize , - 1 ], name = 'output_bytes' )
156159
157160 # Choose Adam optimizer to compute gradients.
158- optimizer = tf .train .AdamOptimizer (lr ).minimize (loss )
161+ optimizer = tf .compat . v1 . train .AdamOptimizer (lr ).minimize (loss )
159162
160163 # Stats for display.
161- seqloss = tf .reduce_mean (loss , 1 )
162- batchloss = tf .reduce_mean (seqloss )
164+ seqloss = tf .reduce_mean (input_tensor = loss , axis = 1 )
165+ batchloss = tf .reduce_mean (input_tensor = seqloss )
163166 accuracy = tf .reduce_mean (
164- tf .cast (
167+ input_tensor = tf .cast (
165168 tf .equal (expected_bytes , tf .cast (output_bytes , tf .uint8 )),
166169 tf .float32 ))
167- loss_summary = tf .summary .scalar ('batch_loss' , batchloss )
168- acc_summary = tf .summary .scalar ('batch_accuracy' , accuracy )
169- summaries = tf .summary .merge ([loss_summary , acc_summary ])
170+ loss_summary = tf .compat . v1 . summary .scalar ('batch_loss' , batchloss )
171+ acc_summary = tf .compat . v1 . summary .scalar ('batch_accuracy' , accuracy )
172+ summaries = tf .compat . v1 . summary .merge ([loss_summary , acc_summary ])
170173
171174 # Init Tensorboard stuff.
172175 # This will save Tensorboard information in folder specified in command line.
173176 # Two sets of data are saved so that you can compare training and
174177 # validation curves visually in Tensorboard.
175178 timestamp = str (math .trunc (time .time ()))
176- summary_writer = tf .summary .FileWriter (
179+ summary_writer = tf .compat . v1 . summary .FileWriter (
177180 os .path .join (log_dir , timestamp + '-training' ))
178- validation_writer = tf .summary .FileWriter (
181+ validation_writer = tf .compat . v1 . summary .FileWriter (
179182 os .path .join (log_dir , timestamp + '-validation' ))
180183
181184 # Init for saving models.
182185 # They will be saved into a directory specified in command line.
183- saver = tf .train .Saver (max_to_keep = constants .MAX_TO_KEEP )
186+ saver = tf .compat . v1 . train .Saver (max_to_keep = constants .MAX_TO_KEEP )
184187
185188 # For display: init the progress bar.
186189 step_size = batch_size * constants .TRAINING_SEQLEN
@@ -192,7 +195,7 @@ def main(args):
192195
193196 # Set initial state.
194197 state = np .zeros ([batch_size , hidden_state_size * hidden_layer_size ])
195- session = tf .Session ()
198+ session = tf .compat . v1 . Session ()
196199
197200 # We continue training on exsiting model, or start with a new model.
198201 if existing_model :
@@ -207,7 +210,7 @@ def main(args):
207210 return constants .ExitCode .TENSORFLOW_ERROR
208211 else :
209212 print ('No existing model provided. Start training with a new model.' )
210- session .run (tf .global_variables_initializer ())
213+ session .run (tf .compat . v1 . global_variables_initializer ())
211214
212215 # Num of bytes we have trained so far.
213216 steps = 0
0 commit comments