疑问点3

tf.placeholder函数说明

函数形式:

tf.placeholder(
    dtype,
    shape=None,
    name=None
)

参数:
dtype:数据类型。常用的是tf.float32,tf.float64等数值类型
shape:数据形状。默认是None,就是一维值,也可以是多维(比如[2,3], [None, 3]表示列是3,行不定)
name:名称
为什么要用placeholder?
Tensorflow的设计理念称之为计算流图,在编写程序时,首先构筑整个系统的graph,代码并不会直接生效,这一点和python的其他数值计算库(如Numpy等)不同,graph为静态的,类似于docker中的镜像。然后,在实际的运行时,启动一个session,程序才会真正的运行。这样做的好处就是:避免反复地切换底层程序实际运行的上下文,tensorflow帮你优化整个系统的代码。我们知道,很多python程序的底层为C语言或者其他语言,执行一行脚本,就要切换一次,是有成本的,tensorflow通过计算流图的方式,帮你优化整个session需要执行的代码,还是很有优势的。

   所以placeholder()函数是在神经网络构建graph的时候在模型中的占位,此时并没有把要输入的数据传入模型,它只会分配必要的内存。等建立session,在会话中,运行模型的时候通过feed_dict()函数向占位符喂入数据。

python shutil.copy()用法

shutil.copyfile(src, dst):复制文件内容(不包含元数据)从src到dst。

DST必须是完整的目标文件名;
如果src和dst是同一文件,就会引发错误shutil.Error。
dst必须是可写的,否则将引发异常IOError。如果dst已经存在,它会被替换。
特殊文件,例如字符或块设备和管道不能使用此功能,因为copyfile会打开并阅读文件。
src和dst的是字符串形式的路径名。

np.argwhere()的用法

np.argwhere( a )
Find the indices of array elements that are non-zero, grouped by element.
返回非0的数组元组的索引,其中a是要索引数组的条件
在这里插入图片描述
返回数组中所有大于1的数字的索引值。

tf.nn.in_top_k的用法

解释:这个函数的作用是返回 input 中每行最大的 k 个数,并且返回它们所在位置的索引。

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import tensorflow as tf
import numpy as np
 
input = tf.constant(np.random.rand(3,4))
k = 2
output = tf.nn.top_k(input, k)
with tf.Session() as sess:
    print(sess.run(input))
    print(sess.run(output))

[[ 0.98925872 0.15743092 0.76471106 0.5949957 ]
[ 0.95766488 0.67846336 0.21058844 0.2644312 ]
[ 0.65531991 0.61445187 0.65372938 0.88111084]]
TopKV2(values=array([[ 0.98925872, 0.76471106],
[ 0.95766488, 0.67846336],
[ 0.88111084, 0.65531991]]), indices=array([[0, 2],
[0, 1],
[3, 0]]))
输入参数:

input: 一个张量,数据类型必须是以下之一:float32、float64、int32、int64、uint8、int16、int8。数据维度是 batch_size 乘上 x 个类别。
k: 一个整型,必须 >= 1。在每行中,查找最大的 k 个值。
name: 为这个操作取个名字。
输出参数:

一个元组 Tensor ,数据元素是 (values, indices),具体如下:

values: 一个张量,数据类型和 input 相同。数据维度是 batch_size 乘上 k 个最大值。

indices: 一个张量,数据类型是 int32 。每个最大值在 input 中的索引位置。

tf.nn.in_top_k(predictions, targets, k, name=None)

解释:这个函数的作用是返回一个布尔向量,说明目标值是否存在于预测值之中。

输出数据是一个 targets 长度的布尔向量,如果目标值存在于预测值之中,那么 out[i] = true。

注意:targets 是predictions中的索引位,并不是 predictions 中具体的值。

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import tensorflow as tf
import numpy as np
 
input = tf.constant(np.random.rand(3,4), tf.float32)
k = 2   #targets对应的索引是否在最大的前k(2)个数据中
output = tf.nn.in_top_k(input, [3,3,3], k)
with tf.Session() as sess:
    print(sess.run(input))
    print(sess.run(output))
[[ 0.43401602  0.29302254  0.40603295  0.21894781]
 [ 0.77089119  0.95353228  0.04788217  0.37489092]
 [ 0.83710146  0.2505011   0.28791779  0.97788286]]

tf.add_n函数的用法

tf.add_n([p1, p2, p3…])函数是实现一个列表的元素的相加。就是输入的对象是一个列表,列表里的元素可以是向量,矩阵,等

import tensorflow as tf;  
import numpy as np;  
  
input1 = tf.constant([1.0, 2.0, 3.0])  
input2 = tf.Variable(tf.random_uniform([3]))  
output = tf.add_n([input1, input2])  
  
with tf.Session() as sess:  
    sess.run(tf.initialize_all_variables())  
    print sess.run(input1 + input2)  
    print sess.run(output)  

输出:[ 1.68921876 2.73008633 3.04061747]
[ 1.68921876 2.73008633 3.04061747]

tf.losses.get_regularization_loss

tf.losses.get_regularization_loss(
    scope=None,
    name='total_regularization_loss'
)

定义在:tensorflow/python/ops/losses/util.py.

获取总正则化loss.
参数:
scope:用于过滤要返回的loss的可选范围名称.
name:返回张量的名称.
返回:
标量正则化loss.

tf.square()

tf.math.square(
    x,
    name=None
)

功能:计算元素x的平方
Args:
x: A Tensor or SparseTensor. Must be one of the following types: half, float32, float64, int32, int64, complex64, complex128.
name: A name for the operation (optional).

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

YEGE学AI算法

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

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

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

打赏作者

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

抵扣说明:

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

余额充值