Académique Documents
Professionnel Documents
Culture Documents
Below are few simple examples of the API to get you started with
TensorFlow Learn. For more examples, please see examples.
General tips
It's useful to re-scale dataset before passing to estimator to 0 mean
and unit standard deviation. Stochastic Gradient Descent doesn't
always do the right thing when variable are very different scale.
Linear Classifier
Simple linear classification:
iris = datasets.load_iris()
classifier = learn.TensorFlowLinearClassifier(n_classes=3)
classifier.fit(iris.data, iris.target)
score = metrics.accuracy_score(iris.target, classifier.predict(iris.data))
print("Accuracy: %f" % score)
Linear Regressor
Simple linear regression:
boston = datasets.load_boston()
X = preprocessing.StandardScaler().fit_transform(boston.data)
regressor = learn.TensorFlowLinearRegressor()
regressor.fit(X, boston.target)
score = metrics.mean_squared_error(regressor.predict(X), boston.target)
print ("MSE: %f" % score)
Custom model
Example of how to pass a custom model to the TensorFlowEstimator:
iris = datasets.load_iris()
classifier = learn.TensorFlowLinearRegression()
classifier.fit(...)
classifier.save('/tmp/tf_examples/my_model_1/')
new_classifier = TensorFlowEstimator.restore('/tmp/tf_examples/my_model_2')
new_classifier.predict(...)
Summaries
To get nice visualizations and summaries you can use logdir parameter
on fit. It will start writing summaries for loss and histograms for variables in
your model. You can also add custom summaries in your custom model
function by calling tf.summary and passing Tensors to report.
classifier = learn.TensorFlowLinearRegression()
classifier.fit(X, y, logdir='/tmp/tf_examples/my_model_1/')
Then run next command in command line:
tensorboard --logdir=/tmp/tf_examples/my_model_1
and follow reported url.
More examples
See examples folder for: