Skip to content

Commit 61dc340

Browse files
Updates to README for TF Learn to remove references to "SKFlow" and update
code/resources. Change: 133738295
1 parent 7e437d5 commit 61dc340

File tree

1 file changed

+122
-71
lines changed
  • tensorflow/contrib/learn/python/learn

1 file changed

+122
-71
lines changed
Lines changed: 122 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,44 @@
1-
# TF Learn (aka Scikit Flow)
1+
# TF Learn
22

3-
[![Join the chat at https://gitter.im/tensorflow/skflow](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/tensorflow/skflow?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
4-
5-
This is a simplified interface for TensorFlow, to get people started on predictive analytics and data mining.
6-
7-
Library covers variety of needs from linear models to *Deep Learning* applications like text and image understanding.
3+
TF Learn is a simplified interface for TensorFlow, to get people started on predictive analytics and data mining. The library covers a variety of needs: from linear models to *Deep Learning* applications like text and image understanding.
84

95
### Why *TensorFlow*?
106

11-
- TensorFlow provides a good backbone for building different shapes of machine learning applications.
12-
- It will continue to evolve both in the distributed direction and as general pipelinining machinery.
7+
* TensorFlow provides a good backbone for building different shapes of machine learning applications.
8+
* It will continue to evolve both in the distributed direction and as general pipelinining machinery.
139

14-
### Why *TensorFlow Learn* (Scikit Flow)?
10+
### Why *TensorFlow Learn*?
1511

16-
- To smooth the transition from the Scikit Learn world of one-liner machine learning into the more open world of building different shapes of ML models. You can start by using fit/predict and slide into TensorFlow APIs as you are getting comfortable.
17-
- To provide a set of reference models that would be easy to integrate with existing code.
12+
- To smooth the transition from the [scikit-learn](http://scikit-learn.org/stable/) world of one-liner machine learning into the more open world of building different shapes of ML models. You can start by using [fit](../../../../g3doc/api_docs/python/contrib.learn.md#Estimator.fit)/[predict](../../../../g3doc/api_docs/python/contrib.learn.md#Estimator.predict) and slide into TensorFlow APIs as you are getting comfortable.
13+
- To provide a set of reference models that will be easy to integrate with existing code.
1814

1915
## Installation
2016

21-
Optionally you can install Scikit Learn and Pandas for additional functionality.
22-
23-
Then you can simply import `learn` via `from tensorflow.contrib.learn` or use `tf.contrib.learn`.
17+
[Install TensorFlow](../../../../g3doc/get_started/os_setup.md), and then simply import `learn` via `from tensorflow.contrib.learn` or use `tf.contrib.learn`.
2418

19+
Optionally you can install [scikit-learn](http://scikit-learn.org/stable/) and [pandas](http://pandas.pydata.org/) for additional functionality.
2520

26-
### Tutorial
21+
### Tutorials
2722

28-
- [Introduction to Scikit Flow and Why You Want to Start Learning
29-
TensorFlow](https://medium.com/@ilblackdragon/tensorflow-tutorial-part-1-c559c63c0cb1)
30-
- [DNNs, Custom model and Digit Recognition
31-
examples](https://medium.com/@ilblackdragon/tensorflow-tutorial-part-2-9ffe47049c92)
32-
- [Categorical Variables: One Hot vs Distributed
33-
representation](https://medium.com/@ilblackdragon/tensorflow-tutorial-part-3-c5fc0662bc08)
34-
- [Scikit Flow Key Features Illustrated](http://terrytangyuan.github.io/2016/03/14/scikit-flow-intro/)
23+
- [TF Learn Quickstart](../../../../g3doc/tutorials/tflearn/index.md). Build, train, and evaluate a neural network with just a few lines of code.
24+
- [Linear Model](../../../../g3doc/tutorials/wide/index.md). Learn the basics of building linear models.
25+
- [Logging and Monitoring](../../../../g3doc/tutorials/monitors/index.md). Use the Monitor API to audit training of a neural network.
26+
- [Wide and Deep Learning](../../../../g3doc/tutorials/wide_and_deep/index.md). Jointly train a linear model and a deep neural network.
3527
- More coming soon.
3628

3729
### Community
3830

39-
- Twitter [#skflow](https://twitter.com/search?q=skflow&src=typd).
40-
- StackOverflow with [skflow tag](http://stackoverflow.com/questions/tagged/skflow) for questions and struggles.
41-
- Github [issues](https://github.com/tensorflow/tensorflow/issues) for technical discussions and feature requests.
42-
- [Gitter channel](https://gitter.im/tensorflow/skflow) for non-trivial discussions.
31+
- Twitter [#tensorflow](https://twitter.com/search?q=tensorflow&src=typd).
32+
- StackOverflow with [tensorflow tag](http://stackoverflow.com/questions/tagged/tensorflow) for questions and struggles.
33+
- GitHub [issues](https://github.com/tensorflow/tensorflow/issues) for technical discussions and feature requests.
4334

4435
### Usage
4536

46-
Below are few simple examples of the API. For more examples, please see [examples](https://github.com/tensorflow/tensorflow/tree/master/tensorflow/examples/skflow).
37+
Below are a few simple examples of the API. For more examples, please see [examples](https://www.tensorflow.org/code/tensorflow/examples/skflow).
4738

48-
## General tips
39+
General tips:
4940

50-
- 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.
41+
- It's useful to rescale a dataset to 0 mean and unit standard deviation before passing it to an [`Estimator`](../../../../g3doc/api_docs/python/contrib.learn.md#estimators). [Stochastic Gradient Descent](https://en.wikipedia.org/wiki/Stochastic_gradient_descent) doesn't always do the right thing when variable are at very different scales.
5142

5243
- Categorical variables should be managed before passing input to the estimator.
5344

@@ -60,9 +51,11 @@ import tensorflow.contrib.learn.python.learn as learn
6051
from sklearn import datasets, metrics
6152

6253
iris = datasets.load_iris()
63-
classifier = learn.LinearClassifier(n_classes=3)
54+
feature_columns = learn.infer_real_valued_columns_from_input(iris.data)
55+
classifier = learn.LinearClassifier(n_classes=3, feature_columns=feature_columns)
6456
classifier.fit(iris.data, iris.target, steps=200, batch_size=32)
65-
score = metrics.accuracy_score(iris.target, classifier.predict(iris.data))
57+
iris_predictions = list(classifier.predict(iris.data, as_iterable=True))
58+
score = metrics.accuracy_score(iris.target, iris_predictions)
6659
print("Accuracy: %f" % score)
6760
```
6861

@@ -76,9 +69,11 @@ from sklearn import datasets, metrics, preprocessing
7669

7770
boston = datasets.load_boston()
7871
x = preprocessing.StandardScaler().fit_transform(boston.data)
79-
regressor = learn.LinearRegressor()
72+
feature_columns = learn.infer_real_valued_columns_from_input(x)
73+
regressor = learn.LinearRegressor(feature_columns=feature_columns)
8074
regressor.fit(x, boston.target, steps=200, batch_size=32)
81-
score = metrics.mean_squared_error(regressor.predict(x), boston.target)
75+
boston_predictions = list(regressor.predict(x, as_iterable=True))
76+
score = metrics.mean_squared_error(boston_predictions, boston.target)
8277
print ("MSE: %f" % score)
8378
```
8479

@@ -91,9 +86,11 @@ import tensorflow.contrib.learn.python.learn as learn
9186
from sklearn import datasets, metrics
9287

9388
iris = datasets.load_iris()
94-
classifier = learn.DNNClassifier(hidden_units=[10, 20, 10], n_classes=3)
89+
feature_columns = learn.infer_real_valued_columns_from_input(iris.data)
90+
classifier = learn.DNNClassifier(hidden_units=[10, 20, 10], n_classes=3, feature_columns=feature_columns)
9591
classifier.fit(iris.data, iris.target, steps=200, batch_size=32)
96-
score = metrics.accuracy_score(iris.target, classifier.predict(iris.data))
92+
iris_predictions = list(classifier.predict(iris.data, as_iterable=True))
93+
score = metrics.accuracy_score(iris.target, iris_predictions)
9794
print("Accuracy: %f" % score)
9895
```
9996

@@ -102,68 +99,122 @@ print("Accuracy: %f" % score)
10299
Example of how to pass a custom model to the Estimator:
103100

104101
```python
102+
from sklearn import datasets
103+
from sklearn import metrics
104+
import tensorflow as tf
105+
import tensorflow.contrib.layers.python.layers as layers
105106
import tensorflow.contrib.learn.python.learn as learn
106-
from sklearn import datasets, metrics
107107

108108
iris = datasets.load_iris()
109109

110-
def my_model(x, y):
111-
"""This is DNN with 10, 20, 10 hidden layers, and dropout of 0.5 probability."""
112-
layers = learn.ops.dnn(x, [10, 20, 10], dropout=0.5)
113-
return learn.models.logistic_regression(layers, y)
110+
def my_model(features, target):
111+
"""DNN with three hidden layers, and dropout of 0.1 probability."""
112+
# Convert the target to a one-hot tensor of shape (length of features, 3) and
113+
# with a on-value of 1 for each one-hot vector of length 3.
114+
target = tf.one_hot(target, 3, 1, 0)
114115

115-
classifier = learn.Estimator(model_fn=my_model, n_classes=3)
116-
classifier.fit(iris.data, iris.target)
117-
score = metrics.accuracy_score(iris.target, classifier.predict(iris.data))
118-
print("Accuracy: %f" % score)
119-
```
116+
# Create three fully connected layers respectively of size 10, 20, and 10 with
117+
# each layer having a dropout probability of 0.1.
118+
features = layers.stack(features, layers.fully_connected, [10, 20, 10])
120119

121-
## Saving / Restoring models
120+
# Create two tensors respectively for prediction and loss.
121+
prediction, loss = (
122+
tf.contrib.learn.models.logistic_regression(features, target)
123+
)
122124

123-
Each estimator has a ``save`` method which takes folder path where all model information will be saved. For restoring you can just call ``learn.Estimator.restore(path)`` and it will return object of your class.
125+
# Create a tensor for training op.
126+
train_op = tf.contrib.layers.optimize_loss(
127+
loss, tf.contrib.framework.get_global_step(), optimizer='Adagrad',
128+
learning_rate=0.1)
124129

125-
Some example code:
130+
return {'class': tf.argmax(prediction, 1), 'prob': prediction}, loss, train_op
126131

127-
```python
128-
classifier = learn.LinearRegressor()
129-
classifier.fit(...)
130-
classifier.save('/tmp/tf_examples/my_model_1/')
132+
classifier = learn.Estimator(model_fn=my_model)
133+
classifier.fit(iris.data, iris.target, steps=1000)
131134

132-
new_classifier = Estimator.restore('/tmp/tf_examples/my_model_2')
133-
new_classifier.predict(...)
135+
y_predicted = [
136+
p['class'] for p in classifier.predict(iris.data, as_iterable=True)]
137+
score = metrics.accuracy_score(iris.target, y_predicted)
138+
print('Accuracy: {0:f}'.format(score))
134139
```
135140

136-
## Summaries
141+
## Saving / Restoring models
137142

138-
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.
143+
Each estimator supports a `model_dir` argument, which takes a folder path where all model information will be saved:
139144

140145
```python
141-
classifier = learn.LinearRegressor()
142-
classifier.fit(x, y, logdir='/tmp/tf_examples/my_model_1/')
146+
classifier = learn.DNNClassifier(..., model_dir="/tmp/my_model")
143147
```
144148

145-
Then run next command in command line:
149+
If you run multiple `fit` operations on the same `Estimator`, training will resume where the last operation left off, e.g.:
150+
151+
<pre><strong>classifier = learn.DNNClassifier(..., model_dir="/tmp/my_model")
152+
classifier.fit(..., steps=300)</strong>
153+
INFO:tensorflow:Create CheckpointSaverHook
154+
INFO:tensorflow:loss = 2.40115, step = 1
155+
INFO:tensorflow:Saving checkpoints for 1 into /tmp/leftoff/model.ckpt.
156+
INFO:tensorflow:loss = 0.338706, step = 101
157+
INFO:tensorflow:loss = 0.159414, step = 201
158+
INFO:tensorflow:Saving checkpoints for 300 into /tmp/leftoff/model.ckpt.
159+
INFO:tensorflow:Loss for final step: 0.0953846.
160+
161+
<strong>classifier.fit(..., steps=300)</strong>
162+
INFO:tensorflow:Create CheckpointSaverHook
163+
INFO:tensorflow:loss = 0.113173, step = 301
164+
INFO:tensorflow:Saving checkpoints for 301 into /tmp/leftoff/model.ckpt.
165+
INFO:tensorflow:loss = 0.175782, step = 401
166+
INFO:tensorflow:loss = 0.119735, step = 501
167+
INFO:tensorflow:Saving checkpoints for 600 into /tmp/leftoff/model.ckpt.
168+
INFO:tensorflow:Loss for final step: 0.0518137.</pre>
169+
170+
To restore checkpoints to a new `Estimator`, just pass it the same `model_dir` argument, e.g.:
171+
172+
<pre><strong>classifier = learn.DNNClassifier(..., model_dir="/tmp/my_model")
173+
classifier.fit(..., steps=300)</strong>
174+
INFO:tensorflow:Create CheckpointSaverHook
175+
INFO:tensorflow:loss = 1.16335, step = 1
176+
INFO:tensorflow:Saving checkpoints for 1 into /tmp/leftoff/model.ckpt.
177+
INFO:tensorflow:loss = 0.176995, step = 101
178+
INFO:tensorflow:loss = 0.184573, step = 201
179+
INFO:tensorflow:Saving checkpoints for 300 into /tmp/leftoff/model.ckpt.
180+
INFO:tensorflow:Loss for final step: 0.0512496.
181+
182+
<strong>classifier2 = learn.DNNClassifier(..., model_dir="/tmp/my_model")
183+
classifier2.fit(..., steps=300)</strong>
184+
INFO:tensorflow:Create CheckpointSaverHook
185+
INFO:tensorflow:loss = 0.0543797, step = 301
186+
INFO:tensorflow:Saving checkpoints for 301 into /tmp/leftoff/model.ckpt.
187+
INFO:tensorflow:loss = 0.101036, step = 401
188+
INFO:tensorflow:loss = 0.137956, step = 501
189+
INFO:tensorflow:Saving checkpoints for 600 into /tmp/leftoff/model.ckpt.
190+
INFO:tensorflow:Loss for final step: 0.0162506.</pre>
191+
192+
## Summaries
193+
194+
If you supply a `model_dir` argument to your `Estimator`s, TensorFlow will write summaries for ``loss`` and histograms for variables in this directory. (You can also add custom summaries in your custom model function by calling [Summary](../../../../g3doc/api_docs/python/train.md#summary-operations) operations.)
195+
196+
To view the summaries in TensorBoard, run the following command, where `logdir` is the `model_dir` for your `Estimator`:
146197

147198
```shell
148199
tensorboard --logdir=/tmp/tf_examples/my_model_1
149200
```
150201

151-
and follow reported url.
202+
and then load the reported URL.
152203

153-
Graph visualization: |Text classification RNN Graph|
204+
**Graph visualization**
154205

155-
Loss visualization: |Text classification RNN Loss|
206+
![Text classification RNN Graph](https://raw.githubusercontent.com/tensorflow/skflow/master/g3doc/images/text_classification_rnn_graph.png)
156207

157-
## More examples
208+
**Loss visualization**
158209

159-
See [examples folder](https://github.com/tensorflow/tensorflow/tree/master/tensorflow/examples/skflow) for:
210+
![Text classification RNN Loss](https://raw.githubusercontent.com/tensorflow/skflow/master/g3doc/images/text_classification_rnn_loss.png)
160211

161-
- Easy way to handle categorical variables - words are just an example of categorical variable.
162-
- Text Classification - see examples for RNN, CNN on word and characters.
163-
- Language modeling and text sequence to sequence.
164-
- Images (CNNs) - see example for digit recognition.
165-
- More & deeper - different examples showing DNNs and CNNs
212+
## More examples
166213

167-
![Text classification RNN Graph](https://raw.githubusercontent.com/tensorflow/skflow/master/g3doc/images/text_classification_rnn_graph.png)
214+
See the [examples folder](https://www.tensorflow.org/code/tensorflow/examples/skflow) for:
168215

169-
![Text classification RNN Loss](https://raw.githubusercontent.com/tensorflow/skflow/master/g3doc/images/text_classification_rnn_loss.png)
216+
- An easy way to handle [categorical variables](https://www.tensorflow.org/code/tensorflow/examples/skflow/text_classification.py) (words are just an example of a categorical variable)
217+
- Text Classification: see examples for [RNN](https://www.tensorflow.org/code/tensorflow/examples/skflow/text_classification_character_rnn.py) and [CNN](https://www.tensorflow.org/code/tensorflow/examples/skflow/text_classification_character_cnn.py) on characters
218+
- [Language modeling and text sequence to sequence](https://www.tensorflow.org/code/tensorflow/examples/skflow/language_model.py)
219+
- [Digit recognition using a CNN](https://www.tensorflow.org/code/tensorflow/examples/skflow/digits.py)
220+
- And much more!

0 commit comments

Comments
 (0)