每次一篇10分钟,小步快跑攻陷Tensorflow(常量)
2019-11-18 本文已影响0人
一块自由的砖
tensorflow中的张量分类
在tensorflow中的张量可以通俗的理解为n维的矩阵
实际使用中基本上也就分为这3种:
0-d 零维 tensor :标量
1-d 一维 tensor :向量
n-d n维 tensor :矩阵
常量类型
数据类型 | tensorflow类型 |
---|---|
float | tf.float32 |
double | tf.float64 |
int8 | tf.int8 |
int16 | tf.int16 |
int32 | tf.int32 |
int64 | tf.int64 |
uint8 | tf.uint8 |
uint16 | tf.uint16 |
string | tf.string |
bool | tf.bool |
complex64 | tf.complex64 |
complex128 | tf.complex128 |
qint8 | tf.qint8 |
qint32 | tf.int32 |
quint8 | tf.quint8 |
常量命名
tf.constant(
value,
dtype=None,
shape=None,
name='Const',
verify_shape=False
)
使用name来命名常量,比如a=1
a = tf.constant(1, name='a')
常量基本四则运算
操作 | 说明 |
---|---|
add | 加法 |
sub | 减法 |
mul | 乘法 |
div | 除法 |
mod | 求余 |
更多的方法参看地址:tensorflow中文社区数学库操作
实践
# -*- coding: utf-8 -*-
"""
Spyder Editor
This is a demo script file.
"""
#引入tensorflow
import tensorflow as tf
#tensor
a = tf.constant(2, name='a')
b = tf.constant(3, name='b')
#定义加操作
opAdd = tf.add(a,b)
#定义减操作
opSub = tf.subtract(a,b)
#定义乘操作
opMul = tf.multiply(a,b)
#定义除操作
opDiv = tf.truediv(a,b)
#session
with tf.Session() as sess:
#写到图日志文件里,方便看board
writer = tf.summary.FileWriter('./graphs', sess.graph)
#运行并输出加操作运行结果
print(sess.run(opAdd))
#运行并输出减操作运行结果
print(sess.run(opSub))
#运行并输出乘操作运行结果
print(sess.run(opMul))
#运行并输出除操作运行结果
print(sess.run(opDiv))
#关闭writer
writer.close()