网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
| tf.DType.maxtf.DType.min | 返回这种数据类型能表示的最大值及其最小值 |
| tf.as_dtype(type_value) | 返回由type_value转变得的相应tf数据类型 |
* 通用函数(Utility functions)
操作 | 描述 |
---|---|
tf.device(device_name_or_function) | 基于默认的图,其功能便为Graph.device() |
tf.container(container_name) | 基于默认的图,其功能便为Graph.container() |
tf.name_scope(name) | 基于默认的图,其功能便为 Graph.name_scope() |
tf.control_dependencies(control_inputs) | 基于默认的图,其功能便为Graph.control_dependencies() |
tf.convert_to_tensor(value, dtype=None, name=None, as_ref=False) | 将value转变为tensor数据类型 |
tf.get_default_graph() | 返回返回当前线程的默认图 |
tf.reset_default_graph() | 清除默认图的堆栈,并设置全局图为默认图 |
tf.import_graph_def(graph_def, input_map=None,return_elements=None, name=None, op_dict=None,producer_op_list=None) | 将graph_def的图导入到python中 |
* 图收集(Graph collections)
操作 | 描述 |
---|---|
tf.add_to_collection(name, value) | 基于默认的图,其功能便为Graph.add_to_collection() |
tf.get_collection(key, scope=None) | 基于默认的图,其功能便为Graph.get_collection() |
* 定义新操作节点(Defining new operations)
tf.RegisterGradient
操作 | 描述 |
---|---|
class tf.RegisterGradient | 返回一个用于寄存op类型的梯度函数的装饰器 |
tf.NoGradient(op_type) | 设置操作节点类型op_type的节点没有指定的梯度 |
class tf.RegisterShape | 返回一个用于寄存op类型的shape函数的装饰器 |
class tf.TensorShape | 表示tensor的shape |
tf.TensorShape.merge_with(other) | 与other合并shape信息,返回一个TensorShape类 |
tf.TensorShape.concatenate(other) | 与other的维度相连结 |
tf.TensorShape.ndims | 返回tensor的rank |
tf.TensorShape.dims | 返回tensor的维度 |
tf.TensorShape.as_list() | 以list的形式返回tensor的shape |
tf.TensorShape.is_compatible_with(other) | 判断shape是否为兼容TensorShape(None)与其他任何shape值兼容 |
class tf.Dimension | |
tf.Dimension.is_compatible_with(other) | 判断dims是否为兼容 |
tf.Dimension.merge_with(other) | 与other合并dims信息 |
tf.op_scope(values, name, default_name=None) | 在python定义op时,返回一个上下文管理器 |
#tf.RegisterGradient
#该装饰器只使用于定义一个新的op类型时候,如果一个op有m个输入,n个输出。那么该梯度函数应该设置原始的
#操作类型,以及n个Tensor对象(表示每一个op输出的梯度),以及m个对象(表示每一个op输入的偏梯度)
#以操作节点类型为'Sub'为例,两输入为x,y。为一个输出x-y
@tf.RegisterGradient("Sub")
def \_sub\_grad(unused\_op, grad):
return grad, tf.neg(grad)
#tf.op\_scope
#定义一个名称为my\_op的python操作节点op
def my\_op(a, b, c, name=None):
with tf.op_scope([a, b, c], name, "MyOp") as scope:
a = tf.convert_to_tensor(a, name="a")
b = tf.convert_to_tensor(b, name="b")
c = tf.convert_to_tensor(c, name="c")
# Define some computation that uses `a`, `b`, and `c`.
return foo_op(..., name=scope)
2.2 输入和读取器(Inputs and Readers)
本节主要介绍tensorflow中数据的读入相关类或函数
* 占位符(Placeholders)
tf提供一种占位符操作,在执行时需要为其提供数据data。
操作 | 描述 |
---|---|
tf.placeholder(dtype, shape=None, name=None) | 为一个tensor插入一个占位符eg:x = tf.placeholder(tf.float32, shape=(1024, 1024)) |
tf.placeholder_with_default(input, shape, name=None) | 当输出没有fed时,input通过一个占位符op |
tf.sparse_placeholder(dtype, shape=None, name=None) | 为一个稀疏tensor插入一个占位符 |
* 读取器(Readers)
tf提供一系列读取各种数据格式的类。对于多文件输入,可以使用函数tf.train.string_input_producer,该函数将创建一个保持文件的FIFO队列,以供reader使用。或者如果输入的这些文件名有相雷同的字符串,也可以使用函数tf.train.match_filenames_once。
操作 | 描述 |
---|---|
class tf.ReaderBase | 不同的读取器类型的基本类 |
tf.ReaderBase.read(queue, name=None) | 返回下一个记录对(key, value),queue为tf文件队列FIFOQueue |
tf.ReaderBase.read_up_to(queue, num_records, name=None) | 返回reader产生的num_records对(key, value) |
tf.ReaderBase.reader_ref | 返回应用在该reader上的Op |
tf.ReaderBase.reset(name=None) | 恢复reader为初始状态 |
tf.ReaderBase.restore_state(state, name=None) | 恢复reader为之前的保存状态state |
tf.ReaderBase.serialize_state(name=None) | 返回一个reader解码后产生的字符串tansor |
class tf.TextLineReader | |
tf.TextLineReader.num_records_produced(name=None) | 返回reader已经产生的记录(records )数目 |
tf.TextLineReader.num_work_units_completed(name=None) | 返回该reader已经完成的处理的work数目 |
tf.TextLineReader.read(queue, name=None) | 返回reader所产生的下一个记录对 (key, value),该reader可以限定新产生输出的行数 |
tf.TextLineReader.reader_ref | 返回应用在该reader上的Op |
tf.TextLineReader.reset(name=None) | 恢复reader为初始状态 |
tf.TextLineReader.restore_state(state, name=None) | 恢复reader为之前的保存状态state |
tf.TextLineReader.serialize_state(name=None) | 返回一个reader解码后产生的字符串tansor |
class tf.WholeFileReader | 一个阅读器,读取整个文件,返回文件名称key,以及文件中所有的内容value,该类的方法同上,不赘述 |
class tf.IdentityReader | 一个reader,以key和value的形式,输出一个work队列。该类其他方法基本同上 |
class tf.TFRecordReader | 读取TFRecord格式文件的reader。该类其他方法基本同上 |
class tf.FixedLengthRecordReader | 输出 |
* 数据转换(Converting)
tf提供一系列方法将各种格式数据转换为tensor表示。
操作 | 描述 |
---|---|
tf.decode_csv(records, record_defaults, field_delim=None, name=None) | 将csv转换为tensor,与tf.TextLineReader搭配使用 |
tf.decode_raw(bytes, out_type, little_endian=None, name=None) | 将bytes转换为一个数字向量表示,bytes为一个字符串类型的tensor与函数 tf.FixedLengthRecordReader搭配使用,详见tf的CIFAR-10例子 |
选取与要输入的文件格式相匹配的reader,并将文件队列提供给reader的读方法( read method)。读方法将返回文件唯一标识的key,以及一个记录(record)(有助于对出现一些另类的records时debug),以及一个标量的字符串值。再使用一个(或多个)解码器(decoder) 或转换操作(conversion ops)将字符串转换为tensor类型。
#读取文件队列,使用reader中read的方法,返回key与value
filename_queue = tf.train.string_input_producer(["file0.csv", "file1.csv"])
reader = tf.TextLineReader()
key, value = reader.read(filename_queue)
record_defaults = [[1], [1], [1], [1], [1]]
col1, col2, col3, col4, col5 = tf.decode_csv(
value, record_defaults=record_defaults)
features = tf.pack([col1, col2, col3, col4])
with tf.Session() as sess:
# Start populating the filename queue.
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(coord=coord)
for i in range(1200):
# Retrieve a single instance:
example, label = sess.run([features, col5])
coord.request_stop()
coord.join(threads)
* Example protocol buffer
提供了一些Example protocol buffers,tf所推荐的用于训练样本的数据格式,它们包含特征信息,详情可见。
这是一种与前述将手上现有的各种数据类型转换为支持的格式的方法,这种方法更容易将网络结构与数据集融合或匹配。这种tensorflow所推荐的数据格式是一个包含tf.train.Example protocol buffers (包含特征Features域)的TFRecords文件。
1、获取这种格式的文件方式为,首先将一般的数据格式填入Example protocol buffer中,再将 protocol buffer序列化为一个字符串,然后使用tf.python_io.TFRecordWriter类的相关方法将字符串写入一个TFRecords文件中,参见MNIST例子,将MNIST 数据转换为该类型数据。
2、读取TFRecords格式文件的方法为,使用tf.TFRecordReader读取器和tf.parse_single_example解码器。parse_single_example操作将 example protocol buffers解码为tensor形式。参见MNIST例子
操作 | 描述 |
---|---|
class tf.VarLenFeature | 解析变长的输入特征feature相关配置 |
class tf.FixedLenFeature | 解析定长的输入特征feature相关配置 |
class tf.FixedLenSequenceFeature | 序列项目中的稠密(dense )输入特征的相关配置 |
tf.parse_example(serialized, features, name=None, example_names=None) | 将一组Example protos解析为tensor的字典形式解析serialized所给予的序列化的一些Example protos返回一个由特征keys映射Tensor和SparseTensor值的字典 |
tf.parse_single_example(serialized, features, name=None, example_names=None) | 解析一个单独的Example proto,与tf.parse_example方法雷同 |
tf.decode_json_example(json_examples, name=None) | 将JSON编码的样本记录转换为二进制protocol buffer字符串 |
#tf.parse\_example的使用举例
#输入序列化数据如下:
serialized = [
features
{ feature { key: "ft" value { float_list { value: [1.0, 2.0] } } } },
features
{ feature []},
features
{ feature { key: "ft" value { float_list { value: [3.0] } } }
]
#那么输出为一个字典(dict),如下:
{"ft": SparseTensor(indices=[[0, 0], [0, 1], [2, 0]],
values=[1.0, 2.0, 3.0],
shape=(3, 2)) }
#########
#再来看一个例子,给定两个序列化的原始输入样本:
[
features {
feature { key: "kw" value { bytes_list { value: [ "knit", "big" ] } } }
feature { key: "gps" value { float_list { value: [] } } }
},
features {
feature { key: "kw" value { bytes_list { value: [ "emmy" ] } } }
feature { key: "dank" value { int64_list { value: [ 42 ] } } }
feature { key: "gps" value { } }
}
]
#相关参数如下:
example_names: ["input0", "input1"],
features: {
"kw": VarLenFeature(tf.string),
"dank": VarLenFeature(tf.int64),
"gps": VarLenFeature(tf.float32),
}
#那么有如下输出:
{
"kw": SparseTensor(
indices=[[0, 0], [0, 1], [1, 0]],
values=["knit", "big", "emmy"]
shape=[2, 2]),
"dank": Spa