tensorflow 一些方法的总结

目录

tf.nn.l2_normalize的使用

按例计算

按行计算

tf.nn.embedding_lookup:

tf.contrib.layers.xavier_initializer

tf.reduce_mean()

tf.reduce_sum

tf.transpose()  转置

tf.reshape()


tf.nn.l2_normalize的使用

tf.nn.l2_normalize(x, dim, epsilon=1e-12, name=None) 
上式: 
x为输入的向量; 
dim为l2范化的维数,dim取值为0或0或1; 
epsilon的范化的最小值边界;

只能理解为:dim=0时,按列归一,dim=1时按行归一

代码:

结果:

 

tf.nn.embedding_lookup:

https://blog.csdn.net/laolu1573/article/details/77170407

tf.nn.embedding_lookup()就是根据input_ids中的id,寻找embeddings中的第id行。比如input_ids=[1,3,5],则找出embeddings中第1,3,5行,组成一个tensor返回。

embedding_lookup不是简单的查表,id对应的向量是可以训练的,训练参数个数应该是 category num*embedding size,也就是说lookup是一种全连接层。

看一段代码:

#!/usr/bin/env/python
# coding=utf-8
import tensorflow as tf
import numpy as np

# 定义一个未知变量input_ids用于存储索引
input_ids = tf.placeholder(dtype=tf.int32, shape=[None])

# 定义一个已知变量embedding,是一个5*5的对角矩阵
# embedding = tf.Variable(np.identity(5, dtype=np.int32))

# 或者随机一个矩阵
embedding = a = np.asarray([[0.1, 0.2, 0.3], [1.1, 1.2, 1.3], [2.1, 2.2, 2.3], [3.1, 3.2, 3.3], [4.1, 4.2, 4.3]])

# 根据input_ids中的id,查找embedding中对应的元素
input_embedding = tf.nn.embedding_lookup(embedding, input_ids)

sess = tf.InteractiveSession()
sess.run(tf.global_variables_initializer())
# print(embedding.eval())
print(sess.run(input_embedding, feed_dict={input_ids: [1, 2, 3, 0, 3, 2, 1]}))

tf.contrib.layers.xavier_initializer

https://blog.csdn.net/qq_30868235/article/details/80939462

xavier_initializer(
    uniform=True,
    seed=None,
    dtype=tf.float32
)

该函数返回一个用于初始化权重的初始化程序 “Xavier” 。

这个初始化器是用来保持每一层的梯度大小都差不多相同。

参数:

uniform: 使用uniform或者normal分布来随机初始化。 
seed: 可以认为是用来生成随机数的seed 
dtype: 只支持浮点数。

返回值:

初始化权重矩阵

tf.reduce_mean()

1.1 函数声明

tf.reduce_mean(input_tensor, reduction_indices=None, keep_dims=False, name=None)
  • 1

1.2 函数说明

Computes the mean of elements across dimensions of a tensor.

Reduces input_tensor along the dimensions given in reduction_indices. Unless keep_dims is true, the rank of the tensor is reduced by 1 for each entry in reduction_indices. If keep_dims is true, the reduced dimensions are retained with length 1.

If reduction_indices has no entries, all dimensions are reduced, and a tensor with a single element is returned.

1.3 参数说明

Args:

input_tensor: The tensor to reduce. Should have numeric type.
reduction_indices: The dimensions to reduce. If None (the default), reduces all dimensions.
keep_dims: If true, retains reduced dimensions with length 1.
name: A name for the operation (optional).

1.4 返回值

Returns:

The reduced tensor.

1.5 举例

For example:

 

# 'x' is [[1., 1.]
#         [2., 2.]]
tf.reduce_mean(x) ==> 1.5
tf.reduce_mean(x, 0) ==> [1.5, 1.5]
tf.reduce_mean(x, 1) ==> [1.,  2.]

 

tf.reduce_sum

  1. reduce_sum(

  2. input_tensor,

  3. axis=None,

  4. keep_dims=False,

  5. name=None,

  6. reduction_indices=None

  7. )

input_tensor:表示输入 
axis:表示在那个维度进行sum操作。 
keep_dims:表示是否保留原始数据的维度,False相当于执行完后原始数据就会少一个维度。 
reduction_indices:为了跟旧版本的兼容,现在已经不使用了。 
官方的例子:

  1. # 'x' is [[1, 1, 1]

  2. # [1, 1, 1]]

  3. tf.reduce_sum(x) ==> 6

  4. tf.reduce_sum(x, 0) ==> [2, 2, 2]

  5. tf.reduce_sum(x, 1) ==> [3, 3]

  6. tf.reduce_sum(x, 1, keep_dims=True) ==> [[3], [3]]

  7. tf.reduce_sum(x, [0, 1]) ==> 6

tf.transpose()  转置

这里的Perm是指定的转置维度,不指定,默认二维的行列交换

transpose(
    a,
    perm=None,
    name='transpose'
)
  •  

For example:默认情况

x = tf.constant([[1, 2, 3], [4, 5, 6]])
tf.transpose(x)  # [[1, 4]
                 #  [2, 5]
                 #  [3, 6]]
  • 这里,指定1维与0维交换,也就是与默认情况相同

tf.transpose(x, perm=[1, 0])  # [[1, 4]
                              #  [2, 5]
                              #  [3, 6]]
  • perm在多维情况下更有用,如下
# 'perm' is more useful for n-dimensional tensors, for n > 2
x = tf.constant([[[ 1,  2,  3],
                  [ 4,  5,  6]],
                 [[ 7,  8,  9],
                  [10, 11, 12]]])

# Take the transpose of the matrices in dimension-0
tf.transpose(x, perm=[0, 2, 1])  # [[[1,  4],
                                 #   [2,  5],
                                 #   [3,  6]],
                                 #  [[7, 10],
                                 #   [8, 11],
                                 #   [9, 12]]]
  • 本来矩阵是[2,2,3]的,但perm指定第二维和第三维交换,转后变为了[2,3,2]
  • a的转置是根据 perm 的设定值来进行的。 

返回数组的 dimension(尺寸、维度) i与输入的 perm[i]的维度相一致。如果未给定perm,默认设置为 (n-1…0),这里的 n 值是输入变量的 rank 。

 

tf.reshape()

reshape(tensor, shape, name=None)

主要通过改变张量形状,可以从高维变低维,也可以从低维变高维;

a = tf.Variable(initial_value=[[1,2,3],[4,5,6]]) ==> shape:[2,3]
b = tf.Variable(initial_value=[[[1,2,3],[4,5,6]],[[7,8,9],[1,0,2]]]) ==> shape:[2,2,3]

a_1 = tf.reshape(a,[2,1,1,3]) ==> [[[[1,2,3]]],[[[4,5,6]]]]
a_2 = tf.reshape(a,[2,1,3]) ==> [[[1,2,3]],[[4,5,6]]]
b_1 = tf.reshape(b,[2,2,1,3]) ==> [[[[1,2,3]],[[4,5,6]]],[[[7,8,9]],[[1,0,2]]]]

new_1 = tf.concat([b_1,a_1],1)
new_2 = tf.reshape(tf.concat([b,a_2],1),[2,3,1,3])
"""
new_1:
[[[[1 2 3]]

  [[4 5 6]]

  [[1 2 3]]]


 [[[7 8 9]]

  [[1 0 2]]

  [[4 5 6]]]]
new_2;
[[[[1 2 3]]

  [[4 5 6]]

  [[1 2 3]]]


 [[[7 8 9]]

  [[1 0 2]]

  [[4 5 6]]]]

 tf.train.exponential_decay

用一个例子说明,这个是用来使学习率自动下降的东西

from ctypes import *
import tensorflow as tf
import matplotlib.pyplot as plt
alpha = 0.1
decay_rate = 0.96
decay_step = 1000
global_ = tf.Variable(tf.constant(0))
global_step = 10000

learn_rate = tf.train.exponential_decay(alpha,global_,decay_step,decay_rate,staircase=True)

c = []
with tf.Session() as sess:
    for i in range(global_step):
        tc = sess.run(learn_rate,feed_dict={global_:i})
        c.append(tc)

plt.figure(1)
plt.plot(range(global_step),c,'r-')

plt.show()
  •  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值