TensorFlow provides two levels of API, and the underlying TensorFlow core provides complete control for researchers. Higher-level use is simpler, such as Tf.contrib.learn, but contrib is still in the update
TensorFlow operation of the core layer
The data units in the TensorFlow are represented by tensor, and the tensor rank represents its dimension first needs import
Import TensorFlow as TF computational Graph
Computational graph is to put a series of tensorflow operations into a picture
The TensorFlow program can be seen as creating computational graph and running computational graph two part
The nodes in the diagram can enter 0 or more tensor and output 1 tensor.
The node can be either an operation or a tensor, and the constant type node has no input and outputs the value it stores
Node1 = tf.constant (3.0, tf.float32)
Node2 = tf.constant (4.0)
You need to create a session when running computational graph
Sess = tf. Session ()
A, B = sess.run ([Node1, Node2])
You can then create an action-based node
Node3 = Tf.add (Node1, Node2)
Sess.run (NODE3)
Computational graph can also receive external input
Define Placeholders
A = Tf.placeholder (tf.float32)
B = Tf.placeholder (tf.float32)
Adder_node = a + b
sess.run (Adder_node, {a: [1 , 3], B: [3,5]})
add_and_triple = Adder_node * 3
sess.run (add_and_triple, {a:3, b:5})
Using variables to adjust parameters in graph
W = tf. Variable ([. 3], tf.float32)
B = tf. Varialbe ([-.3], tf.float32)
x = Tf.placeholder (tf.float32)
Linear_model = W * x + b
Initialize the variable first when you use it.
init = Tf.global_variables_initializer ()
sess.run (init)
y = Tf.placeholder (tf.float32)
Squared_deltas = Tf.square (linear_model-y)
loss = Tf.reduce_sum (squared_ Deltas
To change the value of a variable, you need to use Tf.assign
Fixw = Tf.assign (W, [-1])
FIXB = tf.assign (b, [1])
sess.run ([Fixw, FIXB])
Training and optimization Tf.train API
Optimizer = Tf.train.GradientDescentOptimizer (0.01)
train = optimizer.minimize (loss) for
I in range (1000):
Sess.run (Train, {x: [1,2,3,4], U: [0,-1,-2,-3]})
Operation of the Tf.contrib.learn
Tf.contrib.learn can simplify the machine learning process in the following four ways:
Running training loops.
Running evaluation loops
Managing Data sets
Managing feeding
Define features first
features = [Tf.contrib.layers.real_valued_column ("x", dimension = 1)]
Dimension the dimension of the characteristic
Define Estimator
There are many well-defined models in TF, such as linear regression,logistic regression, linear classification, logistic classification, neural network, etc.
Estimator = Tf.contrib.learn.LinearRegressor (feature_columns=features)
Estimator = Tf.contrib.learn.DNNClassifier (feature_columns=feature,
hidden_units=[10, ten),
n_classes=3 ,
model_dir= "./temp"
Note that the training results of the model are saved, and the next time the program is run, it starts at the last result.
Defining training Data
x = Np.array ([1., 2., 3., 4.])
y = Np.array ([0., -1., -2., -3.])
INPUT_FN = TF.CONTRIB.LEARN.IO.NUMPY_INPUT_FN ({"X": x}, Y, batch_size = 4, Num_epochs = 1000)
Training and Evaluation
Estimator.fit (INPUT_FN = input_fn, steps = +)
estimator.evaluate (input_fn = INPUT_FN)
Custom Models
The previously used Tf.contrib.learn.LinearRegressor is actually a subclass of Tf.contrib.learn.Estimator , Estimatior also has a function model_fn to tell Tf.contrib.learn How to evaluate the forecast, the training step, the cost.
Import NumPy as NP import TensorFlow as TF def model (features, labels, mode): #build a linear model and predict values W = tf.get_variable ("W", [1], dtype = tf.float64) b = tf.get_variable ("B", [1], dtype = tf.float64) y = W * features[ ' X '] + b #Loss sub-graph Loss = Tf.reduce_sum (Tf.square (y-labels)) #Training sub-graph #get Global Step variable In the graph Global_step = tf.train.get_global_step () optimizer = Tf.train.GradientDescentOptimizer (0.01) train = t F.group (optimizer.minimize (loss), Tf.assign_add (Global_step, 1)) return Tf.contrib.learn.ModelFnOps (Mode=mode, pred Ictions=y, Loss=loss, train_op=train) estimator = tf.contrib.learn.Estimator (MODEL_FN = model) #define Our data set X = N P.array ([1., 2., 3., 4.]) y = Np.array ([0., -1., -2., -3.]) #change numpy format to INPUT_FN format INPUT_FN = Tf.contrib.
LEARN.IO.NUMPY_INPUT_FN ({"X": x}, Y, 4, num_epochs=1000) #train estimator.fit (INPUT_FN=INPUT_FN, steps=1000) #evaluate Print Estimator.evaluate (inPUT_FN=INPUT_FN, steps=10)
class Tf.contrib.learn.Estimator
Estimator (Model_fn=none, Model_dir=none, Config=none, Params=none, Feature_engineering_fn=none)
1. MODEL_FN : Model function
Parameters :
features: A dictionary of tensor or tensors, based on data passed to fit
labels: A dictionary consisting of a tensor or tensors. If the model is Modekeys.infer, the Labels=none is passed. If Model_fn's signature does not accept mode, then MODEL_FN can still use Labels=none
mode: Options available. Specifies whether the model is training,evaluation or prediction
params: Optional. Receive the params parameter passed to estimator, which can be used to adjust the parameters
config: options available. Receive the Config parameter passed to estimator, or the default Config. Can be used to change some configurations, such as Num_ps_replices
model_dir: Optional, specify the location of the model parameter, diagram, or other storage. Receives the Model_dir parameter passed to estimator, or the default model_dir.
return value
Back to Modelfnops
2. Model_dir:
Specify the location of the model parameter, diagram, or other storage
3. config:
Configuration
4. Params:
Some hyper-argument dictionaries passed to MODEL_FN, Keys is the name of the parameter, and the value is the basic Python type
5. Feature_engineering_fn
INPUT_FN the output of features and labels, and returns the features and labels passed to MODEL_FN.
Fit (*args, **kwargs)
Evaluate (*args, **kwargs)
Predict (*args, **kwargs)
Note: tf.get_variable () with TF. Variable () can define variables
Import TensorFlow as TF
TF. Variable (Initial_value=none, Trainable=true, Collections=none, Validate_shape=true, Caching_device=none, Name=None, Variable_def=none, Dtype=none, Expected_shape=none, Import_scope=none)
tf.get_variable (name, Shape=None, dtype= None, Initializer=none, Regularizer=none, Trainable=true, Collections=none, Caching_device=none, Partitioner=None, Validate_shape=true, Custom_getter=none)
The difference between the
is the use of TF. Variable, if a naming conflict is detected, the system processes itself.
Therefore, when we need to share a variable, we need to use tf.get_variable ()
In addition, variable () must specify the value of the variable when used ()