Tensorflow基础

1.tf.nn、tf.layers、tf.contrib

(1)tf.nn:提供神经网络相关操作的支持。

(2)tf.layers:主要提供高层的神经网络。

(3)tf.contrib:提供够将计算图中的  网络层、正则化、摘要操作,是构建计算图的高级操作,但是tf.contrib包含不稳定和实验代码,有可能以后API会改变。

以上三个模块的封装程度是逐个递进的。

  • contrib中的slim模块
def convolution(inputs,num_outputs,
                kernel_size,
                stride=1,
                padding='SAME',
                data_format=None,
                rate=1,
                activation_fn=nn.relu,
                normalizer_fn=None,
                normalizer_params=None,
                weights_initializer=initializers.xavier_initializer(),
                weights_regularizer=None,
                biases_initializer=init_ops.zeros_initializer(),
                biases_regularizer=None,
                reuse=None,
                variables_collections=None,
                outputs_collections=None,
                trainable=True,
                scope=None)

conv = slim.conv2d(inputs, 256, [3, 3], stride=1, scope='conv1_1')
pool = slim.max_pool2d(conv, [2, 2], scope='pool1')
@slim.add_arg_scope
def function():
    ...

with slim.arg_scope([function], parameters):

详见https://blog.csdn.net/lcczzu/article/details/84865544

2.tf.Variable、tf.get_variable和tf.name_scope、tf.variable_scope

  • name是变量的唯一标识
  • tf.Variable会自动检测命名冲突并处理(即每次都生成新的变量),tf.get_variable遇到重名的变量且没有设置reuse=True时会报错。tf.Variable可以不写name,tf.get_variable必须写。
  • tf.name_scope对tf.get_variable没有作用
  • tf.name_scope主要结合tf.Variable来使用,方便参数命名管理。在不同命名空间name相同的变量不会冲突。tf.variable_scope主要结合tf.get_variable来使用,实现变量共享。
import tensorflow as tf
with tf.name_scope('scope1'):
    a = tf.Variable([1, 1], name='v')
    b = tf.Variable([1, 1], name='v')
with tf.Session() as sess:
    print(a.name)
    print(b.name)

运行结果:
scope1/v:0
scope1/v_1:0
import tensorflow as tf
with tf.variable_scope('scope1'):
    a = tf.get_variable('v',[1, 1])
with tf.variable_scope('scope1'):
    b = tf.get_variable('v',[1, 1])
with tf.Session() as sess:
    print(a.name)
    print(b.name)

运行结果:
Error
import tensorflow as tf
with tf.variable_scope('scope1'):
    a = tf.get_variable('v',[1, 1])
with tf.variable_scope('scope1', reuse=True):
    b = tf.get_variable('v',[1, 1])
with tf.Session() as sess:
    print(a.name)
    print(b.name)

运行结果:
scope1/v:0
scope1/v:0

详见 https://www.cnblogs.com/adong7639/p/8136273.html

3.tf.add_to_collections、tf.get_collection、tf.add_n

(1)tf.add_to_collection(‘list_name’, element):将元素element添加到列表list_name中
(2)tf.get_collection(‘list_name’):返回名称为list_name的列表
(3)tf.add_n(list):将列表元素相加并返回

tf.add_to_collection('losses', regularizer(fc1_weights))  

上述代码将正则项添加到了losses这个列表中。

4.tf.argmax(input,axis)

axis=0时比较每一列的元素,将每一列最大元素的索引记录下来,最后输出每一列最大元素的索引数组;axis=1的时候,将每一行最大元素的索引记录下来,最后返回每一行最大元素的索引数组。

4.tf.reduce_mean(input_tensor, axis, keepdims)

  • keep_dims:是否降维度。True,输出的结果保持输入tensor的形状;False,输出结果会降低维度
  • axis: 指定轴。如果不指定,则计算所有元素的均值

5.tf.nn.sparse_softmax_cross_entropy_with_logits(logits,labels)

这是一个用来计算交叉熵的函数。当分类问题只有一个正确答案时,用这个函数来加速。如果labels已经是one-hot格式,则可以使用tf.nn.softmax_cross_entropy_with_logits()来进行计算。

  • logits为神经网络输出层的输出,shape=[batch_size, num_classes]
  • labels是一个一维向量,shape=[batch_size, 1],每一个值的取值区间必须是[0,num_classes-1],代表了batch中每个样本对应的类别
  • 输出cross_entropy,shape=[batch, 1]
cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=a, labels=tf.argmax(y, 1))
losses = tf.reduce_mean(cross_entropy)

6.tf.Print()

tf.Print(input, data, message=None, first_n=None, summarize=None, name=None)

最低要求两个输入,input和data,input是需要打印的变量的名字,data要求是一个list,里面包含要打印的内容;message是需要输出的错误信息;first_n指只记录前n次;summarize是对每个tensor只打印的条目数量,如果是None,对于每个输入tensor只打印3个元素;name是op的名字。
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值