用TensorFlow作好一个机器学习项目,须要具有多种代码能力:python
工程开发能力:怎么读取数据、怎么设计与运行Computation Graph、怎么保存与恢复变量、怎么保存统计结果、怎么共享变量、怎么分布式部署git
数据操做能力:怎么将原始数据一步步转化为模型须要的数据,中间可能涉及到Tensor转换、字符串处理、JSON处理等json
模型理论知识:线性回归,逻辑回归,softmax regression,支持向量机,决策树,随机森林,GBDT,CNN,RNNsegmentfault
数值计算理论知识:交叉熵数值计算的潜在问题(为何要用tf.nn.softmax_cross_entropy_with_logits
),梯度降低法,海森矩阵与特征向量,牛顿法,Adam梯度法。数组
本系列文章已对TensorFlow的工程开发和与模型理论知识的结合作了较多的总结。本文的目的是聚焦于数据操做能力,讲述TensorFlow中比较重要的一些API,帮助你们实现各自的业务逻辑。app
TensorFlow提供两种类型的拼接:机器学习
tf.concat(values, axis, name='concat')
:按照指定的已经存在的轴进行拼接分布式
tf.stack(values, axis=0, name='stack')
:按照指定的新建的轴进行拼接函数
t1 = [[1, 2, 3], [4, 5, 6]] t2 = [[7, 8, 9], [10, 11, 12]] tf.concat([t1, t2], 0) ==> [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]] tf.concat([t1, t2], 1) ==> [[1, 2, 3, 7, 8, 9], [4, 5, 6, 10, 11, 12]] tf.stack([t1, t2], 0) ==> [[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]] tf.stack([t1, t2], 1) ==> [[[1, 2, 3], [7, 8, 9]], [[4, 5, 6], [10, 11, 12]]] tf.stack([t1, t2], 2) ==> [[[1, 7], [2, 8], [3, 9]], [[4, 10], [5, 11], [6, 12]]]
上面的结果读起来不太直观,咱们从shape角度看一下就很容易明白了:学习
t1 = [[1, 2, 3], [4, 5, 6]] t2 = [[7, 8, 9], [10, 11, 12]] tf.concat([t1, t2], 0) # [2,3] + [2,3] ==> [4, 3] tf.concat([t1, t2], 1) # [2,3] + [2,3] ==> [2, 6] tf.stack([t1, t2], 0) # [2,3] + [2,3] ==> [2*,2,3] tf.stack([t1, t2], 1) # [2,3] + [2,3] ==> [2,2*,3] tf.stack([t1, t2], 2) # [2,3] + [2,3] ==> [2,3,2*]
tf.slice(input_, begin, size, name=None)
:按照指定的下标范围抽取连续区域的子集
tf.gather(params, indices, validate_indices=None, name=None)
:按照指定的下标集合从axis=0
中抽取子集,适合抽取不连续区域的子集
input = [[[1, 1, 1], [2, 2, 2]], [[3, 3, 3], [4, 4, 4]], [[5, 5, 5], [6, 6, 6]]] tf.slice(input, [1, 0, 0], [1, 1, 3]) ==> [[[3, 3, 3]]] tf.slice(input, [1, 0, 0], [1, 2, 3]) ==> [[[3, 3, 3], [4, 4, 4]]] tf.slice(input, [1, 0, 0], [2, 1, 3]) ==> [[[3, 3, 3]], [[5, 5, 5]]] tf.gather(input, [0, 2]) ==> [[[1, 1, 1], [2, 2, 2]], [[5, 5, 5], [6, 6, 6]]]
假设咱们要从input中抽取[[[3, 3, 3]]]
,这个输出在inputaxis=0
的下标是1,axis=1
的下标是0,axis=2
的下标是0-2,因此begin=[1,0,0]
,size=[1,1,3]
。
假设咱们要从input中抽取[[[3, 3, 3], [4, 4, 4]]]
,这个输出在inputaxis=0
的下标是1,axis=1
的下标是0-1,axis=2
的下标是0-2,因此begin=[1,0,0]
,size=[1,2,3]
。
假设咱们要从input中抽取[[[3, 3, 3], [5, 5, 5]]]
,这个输出在inputaxis=0
的下标是1-2,axis=1
的下标是0,axis=2
的下标是0-2,因此begin=[1,0,0]
,size=[2,1,3]
。
假设咱们要从input中抽取[[[1, 1, 1], [2, 2, 2]],[[5, 5, 5], [6, 6, 6]]]
,这个输出在input的axis=0
的下标是[0, 2]
,不连续,能够用tf.gather
抽取。
tf.string_to_number(string_tensor, out_type=None, name=None)
: 将字符串转化为tf.float32
(默认)和tf.int32
tf.to_double(x, name='ToDouble')
:转化为tf.float64
tf.to_float(x, name='ToFloat')
:转化为tf.float32
tf.to_int32(x, name='ToInt32')
:转化为tf.int32
tf.to_int64(x, name='ToInt64')
:转化为tf.int64
tf.cast(x, dtype, name=None)
:转化为dtype
指定的类型
tf.reshape(tensor, shape, name=None)
:转化为新shape
,如有一个维度设置为-1,会自动推导
TensorFlow使用三个dense tensor来表达一个sparse tensor:indices
、values
、dense_shape
。
假如咱们有一个dense tensor:
[[1, 0, 0, 0] [0, 0, 2, 0] [0, 0, 0, 0]]
那么用SparseTensor表达这个数据对应的三个dense tensor以下:
indices
:[[0, 0], [1, 2]]
values
:[1, 2]
dense_shape
:[3, 4]
能够经过如下两种方法,将sparse tensor转化为dense tensor:
tf.sparse_to_dense(sparse_indices, output_shape, sparse_values, default_value=0, validate_indices=True, name=None)
tf.sparse_tensor_to_dense(sp_input, default_value=0, validate_indices=True, name=None)
tf.string_split(source, delimiter=' ')
source
是一维数组,用于将一组字符串按照delimiter
拆分为多个元素,返回值为一个SparseTensor
。
假若有两个字符串,source[0]
是“hello world”,source[1]
是“a b c”,那么输出结果以下:
st.indices
: [0, 0; 0, 1; 1, 0; 1, 1; 1, 2]
st.values
: ['hello', 'world', 'a', 'b', 'c']
st.dense_shape
:[2, 3]
tf.string_join(inputs, separator=None, name=None)
,用起来比较简单:
tf.string_join(["hello", "world"], separator=" ") ==> "hello world"
经过tf.py_func(func, inp, Tout, stateful=True, name=None)
能够将任意的python函数func
转变为TensorFlow op。
func
接收的输入必须是numpy array,能够接受多个输入参数;输出也是numpy array,也能够有多个输出。inp传入输入值,Tout指定输出的基本数据类型。
先看一个解析json的例子,输入是一个json array,输出是一个特征矩阵。
import tensorflow as tf import numpy as np import json json_str_1 = ''' {"name": "shuiping.chen", "score": 95, "department": "industrial engineering", "rank": 2 } ''' json_str_2 = ''' {"name": "zhuibing.dan", "score": 87, "department": "production engineering", "rank": 4 } ''' input_array = np.array([json_str_1, json_str_2]) def parse_json(json_str_array): fea_dict_array = [ json.loads(item) for item in json_str_array ] ret_feature = [] for fea_dict in fea_dict_array: feature = [fea_dict["score"], fea_dict["rank"]] ret_feature.append(feature) return np.array(ret_feature, dtype=np.float32) parse_json_op = tf.py_func(parse_json, [input_array], tf.float32) sess = tf.Session() print sess.run(parse_json_op)
再看一个多输入多输出的例子,输入两个numpy array,输出三个array,分别是和、差、乘积。
array1 = np.array([[1, 2], [3, 4]], dtype=np.float32) array2 = np.array([[5, 6], [7, 8]], dtype=np.float32) def add_minus_dot(array1, array2): return array1 + array2, array1 - array2, np.dot(array1, array2) add_minus_dot_op = tf.py_func(add_minus_dot, [array1, array2], [tf.float32, tf.float32, tf.float32]) print sess.run(add_minus_dot_op)