tensorflow的基本知识

一、tensorflow的基本类型

  • tf.bool
      特别注意: tf.bool的基本操作和python中的bool是不相同的, 首先:tf.bool是不支持缩写的if tf.bool:if not tf.bool:这种tensorflow是无法识别的。需要改写为if tf.bool == Ture:if tf.bool == False这种形式。
      其次, tf.bool是不支持取反操作的。

二、tensorflow的基本操作

  • tf.one_hot(indices=sparse_labels, depth=3)
      这里的depth是类别数.

  • tf.math
      该库下有各种数学函数, 比如tf.math.log等.

  • tf.random_normal(shape=(4, 3, 2))
      tf.random_也可以生成多种随机函数, 这个dtype一定不能是tf.int32, 会报错的。

  • tf.reduce_mean()
      如果不指定axis,则会将所有的元素都相加, 然后得到一个值.

  • tf.reduce_max()
      如果指定维度, 会将该维度下的值取max.
      举例如下:

eg:
input = tf.Variable([[1, 2, 3, 4], [1, 3, 4, 2], [1, 1, 1, 1]])
output = tf.reduce_max(input, axis=1)

with tf.Session() as sess:
	sess.run(tf.global_variables_initializer())
	print(output.eval())

# output:
[4, 4, 1]          在[1, 2, 3, 4] 中选择max, 因为这几个数包含在axis=1维度上
eg:
input = tf.Variable([[[0, 1, 0, 0, 0], [0, 0, 1, 0, 0]], 
                     [[0, 2, 0, 0, 0], [2, 0, 0, 0, 0]], 
                     [[3, 3, 1, 0, 0], [1, 4, 3, 2, 2]]])
output = tf.reduce_max(input, axis=1)

with tf.Session() as sess:
	sess.run(tf.global_variables_initializer())
	print(output.eval())

# output:
[[0, 1, 1, 0, 0], 
 [2, 2, 0, 0, 0], 
 [3, 4, 3, 2, 2]]
  • tf.split()
responses_keywords_target = tf.random_normal(shape=[5, 3])
rest_target = tf.split(responses_keywords_target, [2, 1], 1)

  在axis=1维度下, 将3分为了2和1, 结果如下:

array([[ 0.6911732 , -0.42219454],
       [ 2.4536731 ,  0.9960419 ],
       [ 2.1226938 , -0.94682676],
       [ 0.12294053,  0.00386119],
       [ 0.44646996,  0.6912047 ]], dtype=float32)>, <tf.Tensor: id=10, shape=(5, 1), dtype=float32, numpy=
array([[-0.08385469],
       [ 1.6161975 ],
       [-1.6264105 ],
       [ 0.18627058],
       [ 0.29483977]], dtype=float32)>]
  • tf.cumsum()

说明:

tf.cumsum([a, b, c], reverse=True)

结果是: [a+b+c, b+c, c]
tf.cumsum(tf.one_hot(self.responses_length - 1, decoder_len), 
									reverse=True, axis=1)

上述代码实现的是:对真实文本长度的mask.

eg: 
one_hot的结果如下所示: 
[[0, 0, 0, 0, 1, 0, 0, 0],
 [0, 0, 1, 0, 0, 0, 0, 0],
 [0, 0, 0, 0, 0, 1, 0, 0]]

经过cumsum()后的结果为: 
[[1, 1, 1, 1, 1, 0, 0, 0],
 [1, 1, 1, 0, 0, 0, 0, 0],
 [1, 1, 1, 1, 1, 1, 0, 0]]    
# 其中1表示有效文本
  • tf.cast()数据类型转换
import tensorflow as tf
 
t1 = tf.Variable([1,2,3,4,5])
t2 = tf.cast(t1,dtype=tf.float32)
 
print 't1: {}'.format(t1)
print 't2: {}'.format(t2)
 
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    sess.run(t2)
    print t2.eval()
  • tf.clip_by_value()
    值裁剪.
value = tf.Variable([[1, 4, 6], [2, 2, 1]])
output = tf.clip_by_value(value, 2, 5)

with tf.Session() as sess:
	sess.run(tf.global_variables_initializer())
	print(output.eval())

# 
[[2, 4, 5], [2, 2, 2]]
  • tf.shape()[0]
max_candidate_num =  tf.shape(self._fact_candidate_embedding)[1]
  • tf.tile()
input = tf.Variable([[[1, 1, 1]], 
                     [[2, 2, 2]], 
                     [[1, 1, 1]])       
# tf.shape = (3, 1, 3)

output = tf.tile(input, [1, 3, 1])
with tf.Session() as sess:
	sess.run(tf.global_variables_initializer())
	print(output.eval())

# 
[[[1, 1, 1], [1, 1, 1]], 
 [[2, 2, 2], [2, 2, 2]], 
 [[1, 1, 1], [1, 1, 1]]   这样在dim = 1上复制了2份. 
  • tf.expand_dims()
    在指定维度,扩展.
value = tf.Variable([[1, 4, 6], [2, 2, 1]])
output = tf.expand_dims(value, -1)

with tf.Session() as sess:
	sess.run(tf.global_variables_initializer())
	print(output.eval())

# [[[1], [4], [6]], 
   [[2], [2], [1]]]       在最后一维添加. 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值