TensorFlow学习日记32

1.RMSprop
解析:keras.optimizers.RMSprop(lr=0.001, rho=0.9, epsilon=1e-06)。除学习率可调整外,建议保持优化器的其他默认参数不变,该优化器通常是面对递归神经网络时的一个良好选择。如下所示:
(1)lr:大或等于0的浮点数,学习率。
(2)rho:大或等于0的浮点数。
(2)epsilon:大或等于0的小浮点数,防止除0错误。

2.Adagrad
解析:keras.optimizers.Adagrad(lr=0.01, epsilon=1e-06),建议保持优化器的默认参数不变。如下所示:
(1)lr:大或等于0的浮点数,学习率.
(2)epsilon:大或等于0的小浮点数,防止除0错误。

3.SGD
解析:keras.optimizers.SGD(lr=0.01, momentum=0.0, decay=0.0, nesterov=False),随机梯度下降法,支持动量参数,支持学习衰减率,支持Nesterov动量。如下所示:
(1)lr:大或等于0的浮点数,学习率。
(2)momentum:大或等于0的浮点数,动量参数。
(3)decay:大或等于0的浮点数,每次更新后的学习率衰减值。
(4)nesterov:布尔值,确定是否使用Nesterov动量。

4.ZeroPadding3D层
解析:keras.layers.convolutional.ZeroPadding3D(padding=(1, 1, 1), data_format=None),将数据的三个维度上填充0。

5.keras模型可视化
解析:keras.utils.vis_utils模块提供了画出Keras模型的函数(利用graphviz),该函数将画出模型结构图,并保存成图片。如下所示:

from keras.utils import plot_model
plot_model(model, to_file='model.png')

plot_model接收两个可选参数,如下所示:
(1)show_shapes:指定是否显示输出数据的形状,默认为False。
(2)show_layer_names:指定是否显示层名称,默认为True。
说明:pip install pydot-ng;sudo yum install graphviz。


6.InceptionV3模型
解析:keras.applications.inception_v3.InceptionV3(include_top=True,weights=‘imagenet’,input_tensor=None,input_shape=None,pooling=None,classes=1000)。InceptionV3网络权重训练自ImageNet。如下所示:
(1)include_top:是否保留顶层的全连接网络。
(2)weights:None代表随机初始化,即不加载预训练权重。'imagenet’代表加载预训练权重。
(3)input_tensor:可填入Keras tensor作为模型的图像输出tensor。
(4)input_shape:可选,仅当include_top=False有效,应为长为3的tuple,指明输入图片的shape,图片的宽高必须大于197,如(200,200,3)。
(5)pooling:当include_top=False时,该参数指定了池化方式。None代表不池化,最后一个卷积层的输出为4D张量。'avg’代表全局平均池化,'max’代表全局最大值池化。
(6)classes:可选,图片分类的类别数,仅当include_top=True并且不加载预训练权重时可用。

7.tf.reset_default_graph
解析:clears the default graph stack and resets the global default graph.

8.tf.Session
解析:__init__(target=’’,graph=None,config=None),creates a new TensorFlow session。如下所示:
(1)target:(Optional) the execution engine to connect to. Defaults to using an in-process engine.
(2)graph:(Optional) the Graph to be launched.
(3)config:(Optional) a ConfigProto protocol buffer with configuration options for the session.

9.tf.Session().run()与Tensor.eval()
解析:假设x为tf下的一个Tensor对象,t.eval()执行的动作就是tf.Session().run(t)。

10.tf.train.import_meta_graph
解析:import_meta_graph(meta_graph_or_file,clear_devices=False,import_scope=None,**kwargs),recreates a Graph saved in a MetaGraphDef proto。如下所示:
(1)meta_graph_or_file:MetaGraphDef protocol buffer or filename (including the path) containing a MetaGraphDef.
(2)clear_devices:Whether or not to clear the device field for an Operation or Tensor during import.
(3)import_scope:Optional string. Name scope to add. Only used when initializing from protocol buffer.
(4)**kwargs:Optional keyed arguments.

11.TensorFlow保存文件.meta,.index和.data
解析:
(1)meta file保存graph结构,包括GraphDef,SaverDef等。当存在meta file,不在文件中定义模型也可以运行,而如果没有meta file,需要定义好模型,再加载data file以得到变量值。
(2)index file为一个string-string table,table的key值为tensor名,value为BundleEntryProto。
(3)data file保存了模型所有变量的值。

12.tf.get_default_graph().as_graph_def()
解析:as_graph_def(from_version=None,add_shapes=False),returns a serialized GraphDef representation of this graph。

13.tf.train.global_step
解析:global_step(sess,global_step_tensor),small helper to get the global step,如下所示:

# Creates a variable to hold the global_step.
global_step_tensor = tf.Variable(10, trainable=False, name='global_step')
# Creates a session.
sess = tf.Session()
# Initializes the variable.
print('global_step: %s' % tf.train.global_step(sess, global_step_tensor))

(1)sess:a TensorFlow Session object.
(2)global_step_tensor:Tensor or the name of the operation that contains the global step.

14.tf.train.write_graph
解析:write_graph(graph_or_graph_def,logdir,name,as_text=True),writes a graph proto to a file。如下所示:

v = tf.Variable(0, name='my_variable')
sess = tf.Session()
tf.train.write_graph(sess.graph_def, '/tmp/my-model', 'train.pbtxt')

或者

v = tf.Variable(0, name='my_variable')
sess = tf.Session()
tf.train.write_graph(sess.graph, '/tmp/my-model', 'train.pbtxt')

(1)graph_or_graph_def:A Graph or a GraphDef protocol buffer.
(2)logdir:Directory where to write the graph. This can refer to remote filesystems, such as Google Cloud Storage (GCS).
(3)name:Filename for the graph.
(4)as_text:If True, writes the graph as an ASCII proto.


15.tf.train.Saver和max_to_keep
解析:表示保存的最大checkpoint文件数。当一个新文件创建的时候,旧文件就会被删掉。如果值为None或0,表示保存所有的checkpoint文件。max_to_keep的默认值为5。


16.tf.get_default_graph()
解析:返回当前线程的默认图。

17.tf.Graph.as_graph_def()
解析:返回一个图的序列化的GraphDef表示,序列化的GraphDef可以使用tf.import_graph_def()导入至另一个图中。

18.TensorFlow中Graph的操作
解析:
(1)graph的保存

# -*- coding: utf-8 -*-
import tensorflow as tf
g1 = tf.Graph()
with g1.as_default():
    c1 = tf.constant(4.0, name='c1')
g2 = tf.Graph()
with g2.as_default():
    c2 = tf.constant(20.0)
with tf.Session(graph=g1) as sess1:
    print(sess1.run(c1))
with tf.Session(graph=g2) as sess2:
    print(sess2.run(c2))
tf.train.write_graph(g1.as_graph_def(), '.', 'graph.pb', False)

(2)graph的调用

# -*- coding: utf-8 -*-
import tensorflow as tf
from tensorflow.python.platform import gfile
with gfile.FastGFile("./graph.pb", 'rb') as f:
    graph_def = tf.GraphDef()
    graph_def.ParseFromString(f.read())
    tf.import_graph_def(graph_def, name='')
sess = tf.Session()
c1_tensor = sess.graph.get_tensor_by_name("c1:0")
c1 = sess.run(c1_tensor)
print(c1)
sess.close()

(3)混合调用

# -*- coding: utf-8 -*-
import tensorflow as tf
g1 = tf.Graph()
with g1.as_default():
    c1 = tf.constant(4.0, name="c1")
g2 = tf.Graph()
with g2.as_default():
    c2 = tf.constant(20.0, name="c2")
with tf.Session(graph=g2) as sess1:
    c1_list = tf.import_graph_def(g1.as_graph_def(), return_elements=["c1:0"], name='')
    print(sess1.run(c1_list[0] + c2))

19.binary_crossentropy和categorical_crossentropy区别
解析:
(1)binary_crossentropy:使用激活函数为sigmoid,只能用于2分类输出。class不需要one_hot编码。[但当class使用one_hot编码时并没有报错,具体原因未知]
(2)categorical_crossentropy:使用激活函数为softmax,只能用于多分类输出,但可把2分类作为多分类。class需要one_hot编码。

20.could not create cudnn handle: CUDNN_STATUS_INTERNAL_ERROR
解析:sudo rm -rf ~/.nv/。

21.tf.get_variable(name, shape, initializer)
解析:name表示变量的名称,shape表示变量的维度,initializer表示变量初始化的方式。

22.get_variable()与Variable的本质区别
解析:

import tensorflow as tf
with tf.variable_scope("scope1"):
    w1 = tf.get_variable("w1", shape=[])
    w2 = tf.Variable(0.0, name="w2")
with tf.variable_scope("scope1", reuse=True):
    w1_p = tf.get_variable("w1", shape=[])
    w2_p = tf.Variable(1.0, name="w2")
print(w1 is w1_p, w2 is w2_p)
#输出
#True  False

由于tf.Variable()每次都在创建新对象,所有reuse=True和它并没有什么关系。对于get_variable()来说,如果已经创建的变量对象,就把那个对象返回,否则就创建一个新的。

23.Graph类
解析:一个Graph实例就是一个图,由一组Operation对象和Tensor对象构成:每个Operation对象(简记为op)表示最小的计算单元,每个Tensor对象表示在operations间传递的基本数据单元。

24.with tf.Session().as_default()
解析:创建一个默认会话。

25.Data Augmentation[数据增强][1]
解析:Data Augmentation[数据增强]指的是在使用以下或者其它方法增加数据输入量,这里特指图像数据。
[1]旋转|反射变换[rotation/reflection]:随机旋转图像一定角度,改变图像内容的朝向
[2]翻转变换[flip]:沿着水平或者垂直方向翻转图像
[3]缩放变换[zoom]:按照一定的比例放大或者缩小图像
[4]平移变换[shift]:在图像平面上对图像以一定方式进行平移
[5]尺度变换[scale]:对图像按照指定的尺度因子,进行放大或缩小;或者参照SIFT特征提取思想,利用指定的尺度因子对图像滤波构造尺度空间,改变图像内容的大小或模糊程度
[6]对比度变换[contrast]:在图像的HSV颜色空间,改变饱和度S和V亮度分量,保持色调H不变,对每个像素的S和V分量进行指数运算[指数因子在0.25到4之间],增加光照变化
[7]噪声扰动[noise]:对图像的每个像素RGB进行随机扰动,常用的噪声模式是椒盐噪声和高斯噪声

参考文献:
[1]Keras ImageDataGenerator参数:https://blog.csdn.net/jacke121/article/details/79245732

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

NLP工程化

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值