内容简介:TensorFlow入门
在MAC上用的是 Python 3,直接用pip3安装:
pip3 install tensorflow
测试Hello World:
import tensorflow as tf
hello = tf.constant('Hello, TensorFlow!')
sess = tf.Session()
print(sess.run(hello))
运行OK,但是会有 警告 信息(一些电脑上有的指令没有被编译):
2017-06-01 07:36:31.843345: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.1 instructions, but these are available on your machine and could speed up CPU computations. 2017-06-01 07:36:31.843359: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.2 instructions, but these are available on your machine and could speed up CPU computations. 2017-06-01 07:36:31.843363: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations. 2017-06-01 07:36:31.843367: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX2 instructions, but these are available on your machine and could speed up CPU computations. 2017-06-01 07:36:31.843371: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use FMA instructions, but these are available on your machine and could speed up CPU computations.
解决办法可以参考 这里 (未亲测)。
基本概念
可以用一个DAG图来描述数据处理的逻辑,在TensorFlow中对各个角色进行了封装。
Graph
使用DAG来描述计算任务:
-
Operation:代表一个计算逻辑,可以看做是图中的节点; -
Tensor:代表数据流,可以看做是节点之间的边;
在不显示创建Graph时,会将Operation添加到默认的图(tf.get_default_graph)中,当然显示的创建也是可以的,比如:
g=tf.Graph()
with g.as_default():
d=tf.constant(value=2)
print(d.graph is g)
根据逻辑的需要可以向图中增加节点:
node1 = tf.constant(3.0)
node2 = tf.constant(4.0)
node3 = tf.add(node1, node2)
print(node3) # Tensor("Add:0", shape=(), dtype=float32)
with tf.Session() as sess:
print(sess.run(node3)) # 7.0
得到的图看起来是这个样子:
同时可以使用placeholder在构建是代替,而在真正执行时才赋值(有点像模板):
node1 = tf.placeholder(tf.float32)
node2 = tf.placeholder(tf.float32)
node3 = tf.add(node1, node2)
print(node3) # Tensor("Add:0", dtype=float32)
with tf.Session() as sess:
print(sess.run(node3, {node1 : 3, node2 : 4 })) # 7.0
print(sess.run(node3, {node1 : [1, 2], node2 : [3, 4]})) # [ 4. 6.]
构建好Graph之后需要迭代优化, 那么哪些值是在迭代过程中可以改变的(需要优化的)? 用Variable来表示:
import tensorflow as tf
# 变量在创建时并不会赋值
W = tf.Variable([.3], tf.float32)
b = tf.Variable([-.3], tf.float32)
x = tf.placeholder(tf.float32)
y = tf.placeholder(tf.float32)
# 定义线性模型
linear_model = W * x + b
# 定义误差,也就是方差
loss = tf.reduce_sum(tf.square(linear_model - y))
# 定义优化方式,通过梯度下降来减小误差
optimizer = tf.train.GradientDescentOptimizer(0.01)
train = optimizer.minimize(loss)
with tf.Session() as sess:
# 为变量赋值(直到这里变量才能用。。。)
sess.run(tf.global_variables_initializer())
# 迭代计算
for i in range(1000):
sess.run(train, {x: [1, 2, 3, 4], y: [0, -1, -2, -3]})
# 打印参数
print(sess.run([W, b])) # [array([-0.9999969], dtype=float32), array([ 0.99999082], dtype=float32)]
Session
用来包装一次运行需要的资源、逻辑等(连接计算逻辑和计算资源),用法为:
sess = tf.Session()
# RUN
sess.close()
# 可以使用with,省略close
with tf.Session() as sess :
# RUN
学习框架
如果每个模型都需要我们明确地知道公式,那门槛、成本都非常高,在TensorFlow中封装了很多算法(更高层次、更简单的玩法),比如线性回归:
- 构建模型
- 准备数据
- 迭代优化
代码如下(不在需要关心loss啥的):
import tensorflow as tf
import numpy as np
# 构造一个维度的线性回归模型
features = [tf.contrib.layers.real_valued_column("x", dimension=1)]
estimator = tf.contrib.learn.LinearRegressor(feature_columns=features)
# 训练数据(每次4个,给1000次)
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)
# 优化1000次
estimator.fit(input_fn=input_fn, steps=1000)
# 计算在数据上的误差
print(estimator.evaluate(input_fn=input_fn)) # {'loss': 5.1855992e-08, 'global_step': 1000}
如果已有的模型不能满足需求,我们也可以利用这套框架来训练自定义的模型(仅仅是需要自己定义一个模型,其他的都差不多):
import numpy as np
import tensorflow as tf
# 自定义模型
def model(features, labels, mode):
# 根据特征构建模型
W = tf.get_variable("W", [1], dtype=tf.float64)
b = tf.get_variable("b", [1], dtype=tf.float64)
y = W * features['x'] + b
# 损失函数
loss = tf.reduce_sum(tf.square(y - labels))
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)
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, 4, num_epochs=1000)
estimator.fit(input_fn=input_fn, steps=1000)
print(estimator.evaluate(input_fn=input_fn, steps=10)) # {'loss': 5.3041564e-12, 'global_step': 1000}
识别手写
每张图片有28 * 28 = 784个像素,并且会标注对应的数字(0到9),相应的模型为:
$$
y = \mathrm{SoftMax} (W · x + b)
$$
而训练的目的是 交叉熵 最小化:
其中:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:- TiDB入门(四):从入门到“跑路”
- MyBatis从入门到精通(一):MyBatis入门
- MyBatis从入门到精通(一):MyBatis入门
- Docker入门(一)用hello world入门docker
- 赵童鞋带你入门PHP(六) ThinkPHP框架入门
- 初学者入门 Golang 的学习型项目,go入门项目
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
正则表达式在线测试
正则表达式在线测试
RGB HSV 转换
RGB HSV 互转工具