TensorFlow基本概念

tensorflow 提供两个层次的API,底层的TensorFlow Core提供完整的控制,适用于研究者。高层的使用起来则更简单,如tf.contrib.learn,但是contrib仍在更新中


TensorFlow Core层的操作

TensorFlow中的数据单元都是用tensor表示,tensor的rank代表它的维度

  1. 使用时首先需要import
    import tensorflow as tf
  2. Computational Graph
    computational graph 是将一系列TensorFlow操作放入一张图中
    TensorFlow的程序可以看做创建 computational graph和运行 computational graph两部分
    图中的节点可以输入0个或者多个tensor,输出1个tensor。
    节点可以是操作,也可以是tensor,常量型节点没有输入,输出它所存储的值
node1 = tf.constant(3.0, tf.float32)
node2 = tf.constant(4.0)

运行computational graph时需要创建一个session

sess = tf.Session()
a, b = sess.run([node1, node2])

之后可以创建一个操作型的节点

node3 = tf.add(node1, node2)
sess.run(node3)

computational graph也可以接收外部的输入
定义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})

graph中利用Variables来调节参数

W = tf.Variable([.3], tf.float32)
b = tf.Varialbe([-.3], tf.float32)
x = tf.placeholder(tf.float32)
linear_model = W * x + b

在使用时首先要对变量初始化

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)

更改变量的值,需要用tf.assign

fixW = tf.assign(W, [-1])
fixb = tf.assign(b, [1])
sess.run([fixW, fixb])

训练与优化 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]})

tf.contrib.learn的操作
tf.contrib.learn主要可以在以下四个方面简化机器学习的过程:

  • running training loops.

  • running evaluation loops

  • managing data sets

  • managing feeding

首先定义features

features = [tf.contrib.layers.real_valued_column("x", dimension = 1)]

dimension指特征的维度
定义estimator
TF中有许多定义好的模型,如linear regression,logistic regression, linear classification, logistic classification, neural network 等

estimator = tf.contrib.learn.LinearRegressor(feature_columns=features)
estimator = tf.contrib.learn.DNNClassifier(feature_columns=feature,
                       hidden_units=[10, 20, 10],
                       n_classes=3,
                       model_dir="./temp"

注意,模型的训练结果会被保存下来,下次运行程序时自动从上次的结果处开始
定义训练数据

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)

训练与评估

estimator.fit(input_fn = input_fn, steps = 1000)
estimator.evaluate(input_fn = input_fn)

自定义模型
之前所使用的tf.contrib.learn.LinearRegressor实际上是tf.contrib.learn.Estimator的一个子类,Estimatior中也有一个函数model_fn来告诉tf.contrib.learn如何评估预测,训练步长,代价。

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 = tf.group(optimizer.minimize(loss), tf.assign_add(global_step, 1))
  return tf.contrib.learn.ModelFnOps(
    mode=mode, predictions=y, loss=loss, train_op=train)

estimator = tf.contrib.learn.Estimator(model_fn = model)
#define our data set
x = np.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 :模型函数
参数
features: 一个Tensor或者Tensors构成的字典,根据传递给fit的数据
labels: 一个Tensor或者Tensors构成的字典。如果模型是ModeKeys.INFER,则传递labels=None。如果model_fn的签名不接受mode,则model_fn仍可以用labels=None
mode:可选项。指定模型是training,evaluation还是prediction
params: 可选项。接收传递给Estimator的params参数,可以用来调整超参数
config: 可选项。接收传递给Estimator的config参数,或者是默认的config。可以用来更改一些配置,如num_ps_replices
model_dir: 可选项,指定模型参数,图或者其他存储的位置。接收传递给Estimator的model_dir参数,或者是默认的model_dir。
返回值
返回ModelFnOps

2. model_dir:
指定模型参数,图或者其他存储的位置
3. config:
配置
4. params:
传递给model_fn的一些超参数字典,keys是参数的名字,值则是基本的python类型
5. feature_engineering_fn
input_fn的输出的features和labels,返回传递给model_fn的features和labels。

fit(*args, **kwargs)
evaluate(*args, **kwargs)
predict(*args, **kwargs)


注意:tf.get_variable()与tf.Variable()都可以定义变量

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)

两者的区别在于使用tf.Variable时,如果检测到命名冲突,系统会自己处理。
因此,当我们需要共享变量时,需要使用tf.get_variable()
另外,variable()使用时必须要指定变量的值

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值