In section 4.4 of the book “Practical Machine Learning Projects with TensorFlow”, the author used skflow. When skflow first came out, it was quite popular, but the interface changed very frequently, leading to a decline in its usage, which also resulted in the code from section 4.4 no longer running.
However, in the recently released TensorFlow 1.4, we found that this module has been integrated into the core module, indicating that the interface has stabilized and is trending towards wider adoption. Therefore, I rewrote the program from section 4.4 using Estimator, keeping the variable names mostly unchanged. The code is as follows:
import tensorflow as tf
from sklearn import datasets, metrics, preprocessing
import numpy as np
import pandas as pd
import os
df = pd.read_csv("data/CHD.csv", header=0)
print( df.describe())
X = df['age'].astype(float)
feature_columns = [tf.contrib.layers.real_valued_column("X", dimension=1)]
classifier = tf.estimator.LinearClassifier(feature_columns=feature_columns,
model_dir=os.path.join(".","tmp","logistic"))
input_fn_train = tf.estimator.inputs.numpy_input_fn(
x={"X" : np.array(X)},
y=np.array(df['chd']),
batch_size=2,
num_epochs=None,
shuffle=True)
classifier.train(input_fn=input_fn_train, steps=2000)
Model Accuracy
score = classifier.evaluate(input_fn=input_fn_train, steps=50)["accuracy"]
print("Accuracy: %f" % score)
Note: This program can run on Ubuntu and MacOS, but not on Windows due to path issues. This seems to be a bug with Estimator, as it also doesn’t work under contrib.learn.
The most challenging part to write is the input_fn function, which is also the most important function. In this program, I directly used numpy_input_fn to construct it. Reference [1] provides another method from pandas that you can try yourself.
The advantage of input_fn is that it allows data to be read in a producer-consumer model. For a detailed explanation, you can refer to [2]. In simple terms, since IO is generally slow, we need to read data during the data processing phase to save time efficiently. This design uses multithreading to continuously fetch data in the background.
Building feature_columns requires some skill, mainly referenced from [3].
Another change is that model accuracy is no longer measured using the metric module, but rather the built-in module of Estimator.
If anyone has any questions, feel free to leave a message.
Practical Machine Learning Projects with TensorFlow
Author: [Argentina] Rodolfo Bonnin
Deep Learning Artificial Intelligence Reference Book
Second Generation Machine Learning Practical Guide
Provides practical projects on deep learning neural networks, etc.
Effectively improves project speed and efficiency
TensorFlow is a machine learning framework led by Google and is a popular subject of research and application in the field of machine learning. This book mainly introduces how to use the TensorFlow library to implement various models, aiming to lower the learning threshold and provide detailed methods and guidance for solving problems for readers. The book consists of 10 chapters, covering the basics of TensorFlow, clustering, linear regression, logistic regression, different neural networks, scaling models, and application techniques of the library. This book is suitable for readers who want to learn and understand TensorFlow and machine learning. If readers have some experience in C++ and Python, they will find it easier to read and learn from this book.
This article is sourced from the Asynchronous Community. Read the original text for more details, and the copyright belongs to the author.