Computer Vision News - August 2019
The Eager execution is an easy and intuitive way to train networks with TensorFlow. It allows the user flexibility and enables to debug the code faster. This execution scheme aims to make TensorFlow better resemble packages like Keras and Pytorch (with great success). With the code supplied above, you can implement any kind of network and see how simple it is to debug and understand what happens behind the scene in your network during your training session. Enjoy! 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 optimizer = tf . train . GradientDescentOptimizer(learning_rate = 0.0001 ) accuracy = tfe . metrics . Accuracy() train_loss_results = [] train_accuracy_results = [] global_step = tf . Variable( 0 ) num_epochs = 500 for epoch in range (num_epochs): epoch_loss_avg = tfe . metrics . Mean() epoch_accuracy = tfe . metrics . Accuracy() for (xb, yb) in tfe . Iterator(train_dataset . shuffle( 1000 ) . batch( 32 )): loss_value, grads = grad(model, xb, yb) optimizer . apply_gradients( zip (grads, model . trainable_variables), global_step) epoch_loss_avg(loss_value) epoch_accuracy(tf . argmax(model(X), axis = 1 , output_type = tf . int32), tf . argmax(y, axis = 1 , output_type = tf . int32)) train_loss_results . append(epoch_loss_avg . result()) train_accuracy_results . append(epoch_accuracy . result()) accuracy(tf . argmax(model(tf . constant(xb)), axis = 1 ), tf . argmax(tf . constant(yb), axis = 1 )) if epoch % 50 == 0 : print ( "Epoch {:03d}: Loss: {:.3f}, Accuracy: {:.3%}" . format(epoch, epoch_loss_avg . result(), epoch_accuracy . result())) We Tried for You: 24
Made with FlippingBook
RkJQdWJsaXNoZXIy NTc3NzU=