Tensorflow之常用方法总结(1)

本文将对Tensorflow中的常用方法进行总结。

TensorFlow 将图形定义转换成分布式执行的操作, 以充分利用可用的计算资源(如 CPU 或 GPU。一般你不需要显式指定使用 CPU 还是 GPU, TensorFlow 能自动检测。如果检测到 GPU, TensorFlow 会尽可能地利用找到的第一个 GPU 来执行操作.并行计算能让代价大的算法计算加速执行,TensorFlow也在实现上对复杂操作进行了有效的改进。

Tensorflow中,主要有以下几种数据类型。

tf.int8:8位整数。
tf.int16:16位整数。
tf.int32:32位整数。
tf.int64:64位整数。
tf.uint8:8位无符号整数。
tf.uint16:16位无符号整数。
tf.float16:16位浮点数。
tf.float32:32位浮点数。
tf.float64:64位浮点数。
tf.double:等同于tf.float64。
tf.string:字符串。
tf.bool:布尔型。
tf.complex64:64位复数。
tf.complex128:128位复数。

下面介绍的方法如下

操作组操作
MathsAdd, Sub, Mul, Div, Exp, Log, Greater, Less, Equal
ArrayConcat, Slice, Split, Constant, Rank, Shape, Shuffle
MatrixMatMul, MatrixInverse, MatrixDeterminant
Neuronal NetworkSoftMax, Sigmoid, ReLU, Convolution2D, MaxPool
CheckpointingSave, Restore
Queues and syncronizationsEnqueue, Dequeue, MutexAcquire, MutexRelease
Flow controlMerge, Switch, Enter, Leave, NextIteration

TensorFlow的算术操作如下:

import tensorflow as tf

#定义‘符号’变量,也称为占位符
x = tf.placeholder("float")
y = tf.placeholder("float")
z = tf.placeholder("float")
#构造一个op节点
add    = tf.add(x, y)    #求和
sub    = tf.sub(x, y)    #减法
mul    = tf.mul(x, y)    #乘法
div    = tf.div(x, y)    #除法
mod    = tf.mod(x,y)     #取模
ads    = tf.abs(z)       #求绝对值
neg    = tf.neg(z)       #取负
sign   = tf.sign(z)      #返回符号
square = tf.square(x)    #计算平方
round  = tf.round(x)     #舍入最接近的整数
sqrt   = tf.sqrt(x)      #开根号
pow    = tf.pow(x, y)    #幂次方
exp    = tf.exp(x)       #计算e的次方
log    = tf.log(x)       #计算log,一个输入计算e的ln,两输入以第二输入为底
max    = tf.maximum(x,y) #最大值
min    = tf.maximum(x,y) #最小值
cos    = tf.cos(x)       #三角函数cosine
sin    = tf.sin(x)       #三角函数sin
tan    = tf.tan(x)       #三角函数tan
atan   = tf.atan(x)      #三角函数ctan

sess = tf.Session() #建立会话
#运行会话,输入数据,并计算节点
print(sess.run(add, feed_dict={x: 3, y: 3}))
print(sess.run(sub, feed_dict={x: 3, y: 3}))
print(sess.run(mul, feed_dict={x: 3, y: 3}))
print(sess.run(div, feed_dict={x: 3, y: 3}))
print(sess.run(mod, feed_dict={x: 3, y: 3}))
print(sess.run(ads, feed_dict={z: -3}))
print(sess.run(neg, feed_dict={z: -3}))
print(sess.run(sign, feed_dict={z: -3}))
print(sess.run(square, feed_dict={x: 3, y: 3}))
print(sess.run(round, feed_dict={x: 3.2}))
print(sess.run(sqrt, feed_dict={x: 9}))
print(sess.run(pow, feed_dict={x: 3, y: 3}))
print(sess.run(exp, feed_dict={x: 3}))
print(sess.run(log, feed_dict={x: 3}))
print(sess.run(max, feed_dict={x: 3, y: 5}))
print(sess.run(min, feed_dict={x: 3, y: 1}))
print(sess.run(cos, feed_dict={x: 3}))
print(sess.run(sin, feed_dict={x: 3}))
print(sess.run(tan, feed_dict={x: 3}))
print(sess.run(atan, feed_dict={x: 3}))
# 任务完成, 关闭会话.
sess.close()

结果如下

6.0
0.0
9.0
1.0
0.0
3.0
3.0
-1.0
9.0
3.0
3.0
27.0
20.0855
1.09861
5.0
3.0
-0.989992
0.14112
-0.142547
1.24905
Process finished with exit code 0

张量操作Tensor Transformations

  • 数据类型转换
import tensorflow as tf

#定义‘符号’变量,也称为占位符
x = tf.placeholder("float")
y = tf.placeholder("float")
z = tf.placeholder("string")
#构造一个op节点

result1 = tf.string_to_number(z) #字符串转为数字
result2 = tf.to_double(x)        #转为64位浮点类型–float64
result3 = tf.to_float(x)         #转为32位浮点类型–float32
result4 = tf.to_int32(x)         #转为32位整型–int32
result5 = tf.to_int64(x)         #转为64位整型–int64
result6 = tf.cast(x, tf.int32)    #将x或者x.values转换为dtype

sess = tf.Session()
print(sess.run(result1, feed_dict={z: "-3"}))
print(sess.run(result2, feed_dict={x: 3}))
print(sess.run(result3, feed_dict={x: 3}))
print(sess.run(result4, feed_dict={x: 3}))
print(sess.run(result5, feed_dict={x: 3}))
print(sess.run(result6, feed_dict={x: 3}))
# 任务完成, 关闭会话.
sess.close()

结果如下

-3.0
3.0
3.0
3
3
3

Process finished with exit code 0
  • 形状操作Shapes and Shaping
import tensorflow as tf

#定义‘符号’变量,也称为占位符
t = [[[1, 1, 1], [2, 2, 2]], [[3, 3, 3], [4, 4, 4]]]

print(tf.shape(t))  #返回数据的shape
print(tf.size(t))   #返回数据的元素数量
print(tf.rank(t))   #返回tensor的rank,注意:此rank不同于矩阵的rank,tensor的rank表示一个tensor需要的索引数目来唯一表示任何一个元素,也就是通常所说的 “order”, “degree”或”ndims”

ret = tf.reshape(t, [3,4]) #改变tensor的形状
print(tf.shape(ret))

at = tf.expand_dims(t, 1) #插入维度1进入一个tensor中
print(tf.shape(at))

结果如下

Connected to pydev debugger (build 171.4249.47)
Tensor("Shape:0", shape=(3,), dtype=int32)
Tensor("Size:0", shape=(), dtype=int32)
Tensor("Rank:0", shape=(), dtype=int32)
Tensor("Shape_1:0", shape=(2,), dtype=int32)
Tensor("Shape_2:0", shape=(4,), dtype=int32)

Process finished with exit code 0
  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值