您的位置:首页 > 其它

tensorflow中一些常见的函数及模块介绍

2018-01-15 18:37 441 查看
此处介绍的是一些tensorflow框架中的一些函数及模块
ps:如果有什么介绍不对的地方,请指出相互交流


__future__模块
Python提供了__future__模块,把下一个新版本的特性导入到当前版本
 
itertools模块:
一般来说,itertools模块包含创建有效迭代器的函数,可以用各种方式对数据进行循环操作,此模块中的所有函数返回的迭代器都可以与for循环语句以及其他包含迭代器(如生成器和生成器表达式)的函数联合使用。<链接中有itertools中的所有函数介绍>

 
来自 <http://www.jb51.net/article/55626.htm>

 

 
 
Inspect模块:
(1)对是否是模块,框架,函数等进行类型检查。

(2). 获取源码

(3).获取类或函数的参数的信息

(4). 解析堆栈
来自 <http://blog.csdn.net/weixin_35955795/article/details/53053762>

 
reader.ptb_producer(data, batch_size, num_steps, name=name):
 """  Iterate on the raw PTBdata.

  
  This chunks up raw_data into batches ofexamples and returns Tensors that

  are drawn from these batches.

  Args: (参数解释)
    raw_data: one of the raw data outputs fromptb_raw_data.

    batch_size: int, the batch size.

    num_steps: int, the number of unrolls.

    name: the name of this operation(optional).

  Returns:

    A pair of Tensors, each shaped [batch_size,num_steps]. The second element

    of the tuple is the same data time-shiftedto the right by one.

  Raises:

    tf.errors.InvalidArgumentError: ifbatch_size or num_steps are too high.

  """ 

来自<http://blog.csdn.net/liyuan123zhouhui/article/details/70169806>
 
tf.nn.embedding_lookup介绍
tf.nn.embedding_lookup(a,[0,3,1])->将a中的0,3,1行拿出

 a =np.array([[1,2,3],[4,5,6],[7,8,9],[10,11,12]])

b= tf.nn.embedding_lookup(a,[0,3,1])

sess= tf.Session()

print(sess.run(b))
输出:[[1  2 3]
      
                  [10 11 12]
    
                    [ 4 5  6]]
 
tf.nn.dropout介绍

tf.nn.dropout(x, keep_prob,noise_shape=None, seed=None, name=None)
根据给出的keep_prob参数,将输入tensor
x按比例输出(防止数据太多造成过拟合)。
x                :  输入tensor
keep_prob    : float类型,每个元素被保留下来的概率
noise_shape  :
一个1维的int32张量,代表了随机产生“保留/丢弃”标志的shape。
seed            :
整形变量,随机数种子。
name           :
名字,没啥用。 
 
来自 <http://www.cnblogs.com/qggg/p/6849881.html>

 
 
tf.get_variable_scope() :获取当前scope
tf.get_variable_scope().reuse_variables() 共享变量
 
来自 <http://www.2cto.com/kf/201611/562404.html>

 
 
tf.contrib.rnn.BasicLSTMCell(HIDDEN_SIZE)
 
 
tf.spli()函数介绍
tf.split(dimension,num_split, input):
dimension:的意思就是输入张量的哪一个维度,如果是0就表示对第0维度进行切割。
num_split 
:就是切割的数量,如果是2就表示输入张量被切成2份,每一份是一个列表。
Input            
:就是输入tensor
 
来自 <http://blog.csdn.net/uestc_c2_403/article/details/73350457>

 
 
将一个array形式的数据转换成tensor形式
feature_column_data = [1,
2.4,
0, 9.9,
3, 120]

feature_tensor =
tf.constant(feature_column_data)
 
来自 <http://blog.csdn.net/sinat_33761963/article/details/62433150>

 
 
tf.SparseTensor()函数
 

对于稀疏型数据,类别下数据,你可以使用tf.SparseTensor来创建tensor:
sparse_tensor = tf.SparseTensor(indices=[[0,1], [2,4]],

                                values=[6,
0.5],

                               dense_shape=[3,
5])

可见,tf.SparseTensor有3个参数,分别是: 

(1)dense_shape 

这是tensor的shape,比如dense_shape=[3,6],表示tensor有3*6共2个维度;dense_shape=[2,3,4]表示tensor有2*3*4共3个维度;dense_shape=[9]表示tensor有1个维度,这个维度里有9个元素。

(2)indices 

表示在这个tensor中indices索引所在的位置是非0值,其余都是0值。比如[0,0]表示在第1行第1列的值非0.
 ( 

)
values 

value是一个1维的tensor,其元素与indices中的索引一一对应,比如indices=[[1,3], [2,4]],values=[18,3.6],表示在行索引为1列索引为3的位置值为18,在行索引为2列索引为4的位置值为3.6

因此上面的代码意思一目了然了,创建一个稀疏tensor,大小是3*5,在行索引为0列索引为1的位置值为6,在行索引为2,列索引为4的位置值为0.5,其余位置值为0.

打印出来应是:
[[0,
6, 0,
0, 0]

 [0,
0, 0,
0, 0]

 [0,
0, 0,
0, 0.5]]
 
来自 <http://blog.csdn.net/sinat_33761963/article/details/62433150>

 
 
pandas.read_csv()函数
第一个参数是数据文件的路径,第二个参数是是否需要取出前后空值,第三个参数是去除的行数,第四个参数是列名
 
来自 <http://blog.csdn.net/sinat_33761963/article/details/62433150>

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: