tensorflow常用函数整理(不定期更新)

1、变量常量声明函数

tf.constant(x) #声明常量

tf.Variable(x) #声明变量

#Creates a constant tensor.
tf.constant(value, dtype=None, shape=None, name='Const', verify_shape=False)

#Creates a variable tensor
tf.Variable(initial_value=None, trainable=True, collections=None, validate_shape=True, caching_device=None, name=None, variable_def=None, dtype=None, expected_shape=None, import_scope=None, constraint=None)

2、算术变量声明函数 

tf.add   tf.subtract   tf.multiply  tf.div

#Returns x + y element-wise.
tf.add(x, y, name=None)

#Returns x - y element-wise.
tf.subtract(x, y, name=None)

#Returns x * y element-wise.
tf.multiply(x, y, name=None)

#Divides x / y elementwise (using Python 2 division operator semantics).
tf.div(x, y, name=None)

import tensorflow as tf

with tf.Session() as sess:
    print('Tensorflow求平方',tf.square(2).eval())
    print("Tensorflow求平方根",tf.sqrt(4.).eval())
    print("Tensorflow加法 2+3=",tf.add(tf.constant(2), tf.constant(3)).eval())
    print("Tensorflow减法 5-2=",tf.subtract(tf.constant(5), tf.constant(2)).eval())
    print("Tensorflow乘法 2*3=",tf.multiply(tf.constant(2), tf.constant(3)).eval())
    print("Tensorflow除法 9/3=",tf.div(tf.constant(9), tf.constant(3)).eval())

 3、赋值函数

tf.assign()

#Update 'ref' by assigning 'value' to it.
tf.assign(ref, value, validate_shape=None, use_locking=None, name=None)
import tensorflow as tf

#tf.assign(state, new_value)需要注意被赋值不能是tf.constant()函数申请的常量,必须是变量才能赋
#值。
zero_tsr = tf.Variable([0,0])

#变量初始化
init = tf.global_variables_initializer()

#启动会话
with tf.Session() as sess:
    sess.run(init)
#    print(sess.run(zero_tsr))
    print(zero_tsr.eval())
    sess.run(tf.assign(zero_tsr,[3,5]))
    print(zero_tsr.eval())

4、类型转换函数

tf.cast

#Casts a tensor to a new type.
tf.cast(x, dtype, name=None)
import tensorflow as tf

x = tf.Variable([[1,2], [3,4]])
y = tf.cast(x, dtype=bool)
z = tf.cast(x, dtype=tf.float16)
init = tf.global_variables_initializer()

with tf.Session() as sess:
    sess.run(init)
    print(x.eval())
    print(y.eval())
    print(z.eval())

 5、标准正态分布函数

1、tf.truncated_normal(shape, mean, stddev)  其中shape表示生成张量的维度,mean表示均值,stddev表示标准差

2、tf.random_normal(shape, mean, stddev) 参数和tf.truncated_normal()相同

#Outputs random values from a truncated normal distribution.
tf.truncated_normal(shape, mean=0.0, stddev=1.0, dtype=tf.float32, seed=None, name=None)

#Outputs random values from a normal distribution.
tf.random_normal(shape, mean=0.0, stddev=1.0, dtype=tf.float32, seed=None, name=None)
import tensorflow as tf

norm1 = tf.random_normal((2,3), mean=1, stddev=2)
norm2 = tf.truncated_normal((2,3), mean=-1, stddev=3)

with tf.Session() as sess:
    print(sess.run(norm1))
    print("\n")
    print(sess.run(norm2))

6、特殊功能函数

1)  tf.random_uniform((m, n), minval=low, maxval=high, dtype=tf.float32))) 

    返回m*n的矩阵,值在low和high之间

2)   tf.random_shuffle(a)  打乱序列顺序

3)tf.reduce_sum(input_tensor, axis=None,keep_dims=False, name=None)  

input: 输入的向量

axis: 在那个轴上进行统计    

keep_dims=False: 是否保持维度  

name=None: 名称      

tf.reduce_sum()函数主要是可以按照各种维度进行求和,同时可以计算两个概率之间的交叉熵

4)tf.expend_dims(input, dim,  #增加维度位置name=None)

将input维度增加一维, dim就是自己可以选择增加维度的位置,dim=-1就表示从最后开始增加,可通过下面示例理解此函数含义

#Outputs random values from a uniform distribution.
tf.random_uniform(shape, minval=0, maxval=None, dtype=tf.float32, seed=None, name=None)

#Randomly shuffles a tensor along its first dimension.
tf.random_shuffle(value, seed=None, name=None)

#Computes the sum of elements across dimensions of a tensor. (deprecated arguments)
tf.reduce_sum(input_tensor, axis=None, keepdims=None, name=None, reduction_indices=None, keep_dims=None)

#Inserts a dimension of 1 into a tensor's shape.
tf.expend_dims
import tensorflow as tf
import numpy as np

a1 = tf.random_uniform((2,2), 0, 1)
b1 = tf.Variable([[1,2,3], [4,5,6]])
b2 = tf.random_shuffle(b1)
c = np.array([[1,2,3], [4,5,6]])


init = tf.global_variables_initializer()

with tf.Session() as sess:
    sess.run(init)
    print(sess.run(a1))
    print("\n")
    print(sess.run(b1))
    print(sess.run(b2))
    print("\n")
    print(sess.run(tf.reduce_sum(c,axis=0)))
    print(sess.run(tf.reduce_sum(c,axis=1)))

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值