tensorFlow基础操作及常用函数

  • 1. 安装Tensorflow
  • 2. TensorFlow基本操作
  • 3. TensorFlow常用函数
    • 3.1 常用矩阵创建方式
    • 3.2 高斯初始化及洗牌操作
    • 3.3 赋值操作
    • 3.4 Numpy和TensorFlow相互转换
    • 3.5 placeholder操作

1. 安装Tensorflow

使用pip工具安装TensorFlow

pip install tensorflow

引入TensorFlow

import tensorflow as tf

2. TensorFlow基本操作

# 创建变量
w = tf.Variable([[0.5, 1.0]])
x = tf.Variable([[2.0], [1.0]])y = tf.matmul(w, x)# session初始化
init_op = tf.global_variables_initializer()
with tf.Session() as sess:sess.run(init_op)print(y.eval())

3. TensorFlow常用函数

3.1 常用矩阵创建方式

# 创建全零矩阵
a = tf.zeros([3, 4], tf.int32)
# 创建同维度全零矩阵
b = tf.zeros_like([[1, 2, 3, 4], [5, 6, 7, 8]])
# 创建全一矩阵
c = tf.ones([2, 3], tf.int32)
# 创建同维度全零矩阵
d = tf.ones_like([[1, 2, 3], [4, 5, 6]])
# 根据指定的值和维度创建矩阵
e = tf.constant(-1.0, shape=[2, 3])
# 根据区间和步长创建矩阵
f = tf.linspace(10.0, 12.0, 3, name='linspace')
g = tf.range(3, 18, 3)# session初始化
init_op = tf.global_variables_initializer()
with tf.Session() as sess:sess.run(init_op)print(a.eval())print('-----------------------')print(b.eval())print('-----------------------')print(c.eval())print('-----------------------')print(d.eval())print('-----------------------')print(e.eval())print('-----------------------')print(f.eval())print('-----------------------')print(g.eval())

3.2 高斯初始化及洗牌操作

# 随机高斯初始化
# mean-->均值
# stddev-->标准差
norm = tf.random_normal([2, 3], mean=-1, stddev=4)
cs = tf.constant([[1, 2], [3, 4], [5, 6]])
# 洗牌操作
shuff = tf.random_shuffle(cs)
sess = tf.Session()
print(sess.run(norm))
print('-----------------------')
print(sess.run(shuff))

3.3 赋值操作

state = tf.Variable(0)
new_value = tf.add(state, tf.constant(1))
# 赋值操作
update = tf.assign(state, new_value)
with tf.Session() as sess:sess.run(tf.global_variables_initializer())print(sess.run(state))for _ in range(3):sess.run(update)print(sess.run(state))

3.4 Numpy和TensorFlow相互转换

# Numpy转TensorFlow
a = np.zeros((3, 3))
ta = tf.convert_to_tensor(a)
with tf.Session() as sess:print(sess.run(ta))

3.5 placeholder操作

# 使用placeholder定义变量的shape或者类型,但是不传具体的值
input1 = tf.placeholder(tf.float32)
input2 = tf.placeholder(tf.float32)
output = tf.multiply(input1, input2)
with tf.Session() as sess:print(sess.run([output], feed_dict={input1: [7.], input2: [2.]}))

tensorFlow基础操作及常用函数相关推荐

  1. 【Excel基础训练】函数入门:基础操作与常用函数

    函数基础操作: 1.公式-插入函数-搜索想要的函数 2.引用多个区域 3.相对引用,绝对引用 4.混合引用:固定列/固定行 常用运算函数: 1.排名:rank 2.极值:max和min 3.计数:co ...

  2. 深度学习(11)TensorFlow基础操作七: 向前传播(张量)实战

    深度学习(11)TensorFlow基础操作七: 向前传播(张量)实战 1. 导包 2. 加载数据集 3. 转换数据类型 4. 查看x.shape, y.shape, x.dtype, y.dtype ...

  3. 深度学习(6)TensorFlow基础操作二: 创建Tensor

    深度学习(6)TensorFlow基础操作二: 创建Tensor 一. 创建方式 1. From Numpy,List 2. zeros,ones (1) tf.zeros() (2) tf.zero ...

  4. 深度学习(9)TensorFlow基础操作五: Broadcasting

    深度学习(9)TensorFlow基础操作五: Broadcasting 1. 操作思想 2. 具体例子 3. 理解 (1) How to understand? (2) Why Broadcasti ...

  5. Linux 基础操作、常用shell命令、vi常用命令、man帮助手册

    Linux 基础操作.常用shell命令.vi常用命令.man帮助手册 一.简述 记录简单的Linux 基础操作.常用shell命令.vi编辑器常用命令.man帮助手册的使用. 二.Linux 基础操 ...

  6. 深度学习(10)TensorFlow基础操作六: 数学运算

    深度学习(10)TensorFlow基础操作六: 数学运算 1. Operation type 2. + - * / % // 3. tf.math.log & tf.exp 4. log2, ...

  7. 深度学习(8)TensorFlow基础操作四: 维度变换

    深度学习(8)TensorFlow基础操作四: 维度变换 1. View 2. 示例 3. Reshape操作可能会导致潜在的bug 4. tf.transpose 5. Squeeze VS Exp ...

  8. 深度学习(7)TensorFlow基础操作三: 索引与切片

    深度学习(7)TensorFlow基础操作三: 索引与切片 一. 基础索引 1. Basic indexing 2. Numpy-style indexing 3. start : end 4. 切片 ...

  9. 深度学习(5)TensorFlow基础操作一: TensorFlow数据类型

    深度学习(5)TensorFlow基础操作一: TensorFlow数据类型 Data Container(数据载体) What's Tensor TF is a computing lib(科学计算 ...

最新文章

  1. css之命名规范 BEM
  2. SQL SERVER镜像切换
  3. springboot的thymeleaf一个页面中引入其它页面
  4. 更快更强,来试试 Mybatis 的增强版——EasyMybatis
  5. win下 git gui 使用教程
  6. ef core code first from exist db
  7. 安装win7系统时出现蓝屏0000007e解决方案
  8. word文档通配符换行_Word指定位置批量插入表格,别人花一天搞不出来,我只用了10秒...
  9. webpack整合bable
  10. HP LaserJet Pro P1106网络打印机64位驱动安装
  11. android 虚拟按键遮挡布局,完美解决虚拟按键遮盖底部视图的问题
  12. 当前量子计算技术前沿是什么水平?
  13. 用Python画漂亮的专业插图 ?So easy!
  14. 超强大在线词频统计功能,从一篇文章到一个G的文本均可轻松统计
  15. Spring JPA 随手记
  16. (实测可用)STM32CubeMX教程-STM32L431RCT6开发板研究串口通信(RS485)
  17. SQL入门之第七讲——INSERT插入语句
  18. 水泥混凝土摊铺机对于建设中模具的调整和效率的均衡
  19. 1454E - Number of Simple Paths
  20. 英文文学研究助手(Python)

热门文章

  1. 横沥机械制造生产过程中,零件的加工是最基本的
  2. 英灵神殿好友服务器不显示,英灵神殿Valheim服务器常见问题解答
  3. 免App Store分发iOS应用
  4. ESP32设备驱动-RFID-RC522模块驱动
  5. 产品CEO?别傻了,你不是拿破仑
  6. 现在用微博的人多吗?
  7. 上门按摩预约小程序APP软件系统部署讲解
  8. 【低轨卫星定轨思路】将动力学与PPP结合
  9. 用于光学相干断层扫描技术的迈克尔逊干涉仪
  10. 学习python的第六天(元组、字典和集合操作)