Mastering TensorFlow 1.x
上QQ阅读APP看书,第一时间看更新

PrettyTensor

PrettyTensor provides a thin wrapper on top of TensorFlow. The objects provided by PrettyTensor support a chainable syntax to define neural networks. For example, a model could be created by chaining the layers as shown in the following code:

model = (X.
flatten().
fully_connected(10).
softmax_classifier(n_classes, labels=Y))

PrettyTensor can be installed in Python 3 with the following command:

pip3 install prettytensor

PrettyTensor offers a very lightweight and extensible interface in the form of a method named apply(). Any additional function can be chained to PrettyTensor objects using the .apply(function, arguments) method. PrettyTensor will call the function and supply the current tensor as the first argument to the function.

User-created functions can be added using the @prettytensor.register decorator. Details can be found at https://github.com/google/prettytensor.

The workflow to define and train models in PrettyTensor is as follows:

  1. Get the data.
  2. Define hyperparameters and parameters.
  3. Define the inputs and outputs.
  4. Define the model.
  5. Define the evaluator, optimizer, and trainer functions.
  6. Create the runner object.
  7. Within a TensorFlow session, train the model with the runner.train_model() method.
  8. Within the same session, evaluate the model with the runner.evaluate_model() method.

The complete code for the PrettyTensor MNIST classification example is provided in the notebook ch-02_TF_High_Level_LibrariesThe output from the PrettyTensor MNIST example is as follows:

[1] [2.5561881]
[600] [0.3553167]
Accuracy after 1 epochs 0.8799999952316284 

[601] [0.47775066]
[1200] [0.34739292]
Accuracy after 2 epochs 0.8999999761581421 

[1201] [0.19110668]
[1800] [0.17418651]
Accuracy after 3 epochs 0.8999999761581421 

[1801] [0.27229539]
[2400] [0.34908807]
Accuracy after 4 epochs 0.8700000047683716 

[2401] [0.40000191]
[3000] [0.30816519]
Accuracy after 5 epochs 0.8999999761581421 

[3001] [0.29905257]
[3600] [0.41590339]
Accuracy after 6 epochs 0.8899999856948853 

[3601] [0.32594997]
[4200] [0.36930788]
Accuracy after 7 epochs 0.8899999856948853 

[4201] [0.26780865]
[4800] [0.2911002]
Accuracy after 8 epochs 0.8899999856948853 

[4801] [0.36304188]
[5400] [0.39880857]
Accuracy after 9 epochs 0.8999999761581421 

[5401] [0.1339224]
[6000] [0.14993289]
Accuracy after 10 epochs 0.8899999856948853