API - Utility¶
fit (sess, network, train_op, cost, X_train, …) |
Traing a given non time-series network by the given cost function, training data, batch_size, n_epoch etc. |
test (sess, network, acc, X_test, y_test, x, …) |
Test a given non time-series network by the given test data and metric. |
predict (sess, network, X, x, y_op) |
Return the predict results of given non time-series network. |
evaluation ([y_test, y_predict, n_classes]) |
Input the predicted results, targets results and the number of class, return the confusion matrix, F1-score of each class, accuracy and macro F1-score. |
class_balancing_oversample ([X_train, …]) |
Input the features and labels, return the features and labels after oversampling. |
dict_to_one ([dp_dict]) |
Input a dictionary, return a dictionary that all items are set to one, use for disable dropout, dropconnect layer and so on. |
flatten_list ([list_of_list]) |
Input a list of list, return a list that all items are in a list |
Training, testing and predicting¶
-
tensorlayer.utils.
fit
(sess, network, train_op, cost, X_train, y_train, x, y_, acc=None, batch_size=100, n_epoch=100, print_freq=5, X_val=None, y_val=None, eval_train=True)[source]¶ Traing a given non time-series network by the given cost function, training data, batch_size, n_epoch etc.
Parameters: - sess : TensorFlow session
sess = tf.InteractiveSession()
- network : a TensorLayer layer
the network will be trained
- train_op : a TensorFlow optimizer
like tf.train.AdamOptimizer
- X_train : numpy array
the input of training data
- y_train : numpy array
the target of training data
- x : placeholder
for inputs
- y_ : placeholder
for targets
- acc : the TensorFlow expression of accuracy (or other metric) or None
if None, would not display the metric
- batch_size : int
batch size for training and evaluating
- n_epoch : int
the number of training epochs
- print_freq : int
display the training information every
print_freq
epochs- X_val : numpy array or None
the input of validation data
- y_val : numpy array or None
the target of validation data
- eval_train : boolen
if X_val and y_val are not None, it refects whether to evaluate the training data
Examples
>>> see tutorial_mnist_simple.py >>> tl.utils.fit(sess, network, train_op, cost, X_train, y_train, x, y_, ... acc=acc, batch_size=500, n_epoch=200, print_freq=5, ... X_val=X_val, y_val=y_val, eval_train=False)
-
tensorlayer.utils.
test
(sess, network, acc, X_test, y_test, x, y_, batch_size, cost=None)[source]¶ Test a given non time-series network by the given test data and metric.
Parameters: - sess : TensorFlow session
sess = tf.InteractiveSession()
- network : a TensorLayer layer
the network will be trained
- acc : the TensorFlow expression of accuracy (or other metric) or None
if None, would not display the metric
- X_test : numpy array
the input of test data
- y_test : numpy array
the target of test data
- x : placeholder
for inputs
- y_ : placeholder
for targets
- batch_size : int or None
batch size for testing, when dataset is large, we should use minibatche for testing. when dataset is small, we can set it to None.
- cost : the TensorFlow expression of cost or None
if None, would not display the cost
Examples
>>> see tutorial_mnist_simple.py >>> tl.utils.test(sess, network, acc, X_test, y_test, x, y_, batch_size=None, cost=cost)
-
tensorlayer.utils.
predict
(sess, network, X, x, y_op)[source]¶ Return the predict results of given non time-series network.
Parameters: - sess : TensorFlow session
sess = tf.InteractiveSession()
- network : a TensorLayer layer
the network will be trained
- X : numpy array
the input
- y_op : placeholder
the argmax expression of softmax outputs
Examples
>>> see tutorial_mnist_simple.py >>> y = network.outputs >>> y_op = tf.argmax(tf.nn.softmax(y), 1) >>> print(tl.utils.predict(sess, network, X_test, x, y_op))
Evaluation functions¶
-
tensorlayer.utils.
evaluation
(y_test=None, y_predict=None, n_classes=None)[source]¶ Input the predicted results, targets results and the number of class, return the confusion matrix, F1-score of each class, accuracy and macro F1-score.
Parameters: - y_test : numpy.array or list
target results
- y_predict : numpy.array or list
predicted results
- n_classes : int
number of classes
Examples
>>> c_mat, f1, acc, f1_macro = evaluation(y_test, y_predict, n_classes)
Class balancing functions¶
-
tensorlayer.utils.
class_balancing_oversample
(X_train=None, y_train=None, printable=True)[source]¶ Input the features and labels, return the features and labels after oversampling.
Parameters: - X_train : numpy.array
Features, each row is an example
- y_train : numpy.array
Labels
Examples
>>> X_train, y_train = class_balancing_oversample(X_train, y_train, printable=True)
Helper functions¶
-
tensorlayer.utils.
dict_to_one
(dp_dict={})[source]¶ Input a dictionary, return a dictionary that all items are set to one, use for disable dropout, dropconnect layer and so on.
Parameters: - dp_dict : dictionary
keeping probabilities
Examples
>>> dp_dict = dict_to_one( network.all_drop ) >>> dp_dict = dict_to_one( network.all_drop ) >>> feed_dict.update(dp_dict)