内容简介:最近事有点杂,并且还很多这次为数模 不用matlab 这次决定使用TensorFlow 额 用点新鲜东东 matlab 太老了 仅仅用作数模 ....数模需要使用 两个 一个是基础 一个是波士顿房价的预测 对于TensorFlow就学习这两个
前言
最近事有点杂,并且还很多
这次为数模 不用matlab 这次决定使用TensorFlow 额 用点新鲜东东 matlab 太老了 仅仅用作数模 ....
数模需要使用 两个 一个是基础 一个是波士顿房价的预测 对于TensorFlow就学习这两个
课程 https://mooc.study.163.com/course/2001396000#/info
配置Pycharm
过程过
(venv) ➜ untitled2
Hello world
import tensorflow as tf # 创建一个常运算 作为节点加入默认计算图中 hello = tf.constant("hello world") # 创建绘画 sess = tf.Session() # 获取解雇 print(sess.run(hello))
一些概念
Tensor 张量 对应数据结构 多维数组
Flow 流 计算模型 多维数组 通过计算转化的模型 称为流
总结 tensorflow 通过计算图表达计算的编程系统 计算 计算图上的节点 节点之间描述了两个张量之间的关系
数据流图
计算图为有向图 由以下内容构成
一组节点 节点代表操作 为一种运算
一组有向边 边代表节点之间的关系 数据传递和控制依赖
有两种边
常规边 值传递
特殊边 节点控制
图
import tensorflow as tf # 计算图 node1 = tf.constant(3.0, tf.float32, name="node1") node2 = tf.constant(4.0, tf.float32, name="node2") node3 = tf.add(node1, node2) print(node3)
Tensor("Add:0", shape=(), dtype=float32)
输出张量结构
会话
import tensorflow as tf # 计算图 node1 = tf.constant(3.0, tf.float32, name="node1") node2 = tf.constant(4.0, tf.float32, name="node2") node3 = tf.add(node1, node2) print(node3) # 建立对话 sess = tf.Session() # 运行结果 print(sess.run(node1)) # 关闭session sess.close()
/home/ming/PycharmProjects/untitled2/venv/bin/python /home/ming/PycharmProjects/untitled2/index2.py 2019-05-13 20:33:49.636022: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA Tensor("Add:0", shape=(), dtype=float32) 2019-05-13 20:33:49.640973: I tensorflow/core/platform/profile_utils/cpu_utils.cc:94] CPU Frequency: 3493455000 Hz 2019-05-13 20:33:49.641242: I tensorflow/compiler/xla/service/service.cc:150] XLA service 0x1b09c80 executing computations on platform Host. Devices: 2019-05-13 20:33:49.641274: I tensorflow/compiler/xla/service/service.cc:158] StreamExecutor device (0): <undefined>, <undefined> 3.0
/home/ming/PycharmProjects/untitled2/venv/bin/python /home/ming/PycharmProjects/untitled2/index2.py Tensor("Add:0", shape=(), dtype=float32) 2019-05-13 20:34:29.977653: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA 2019-05-13 20:34:29.983183: I tensorflow/core/platform/profile_utils/cpu_utils.cc:94] CPU Frequency: 3493455000 Hz 2019-05-13 20:34:29.983552: I tensorflow/compiler/xla/service/service.cc:150] XLA service 0x1d8cc00 executing computations on platform Host. Devices: 2019-05-13 20:34:29.983602: I tensorflow/compiler/xla/service/service.cc:158] StreamExecutor device (0): <undefined>, <undefined> 7.0 Process finished with exit code 0
张量
张量 多维数组
零阶张量 标量
一阶 向量 一维数组
n阶 n维数组
张量保存的计算过程
张量属性
Tensor("Add:0", shape=(), dtype=float32)
Add 节点名称 第几个节点输出
shape 形状 shape=() 表示标量
dtype 类型
张量形状
import tensorflow as tf # 计算图 test1 = tf.constant([[[1,2,3], [2,3,4]], [[23,34,5], [23,44,5]]] ,name="test1") print(test1)
Tensor("test1:0", shape=(2, 2, 3), dtype=int32)
外层两个 次之 2个 最里面3个
阶
维度
获取张量的元素
类比数组
import tensorflow as tf # 计算图 test1 = tf.constant([[[1,2,3], [2,3,4]], [[23,34,5], [23,44,5]]] ,name="test1") print(test1) sess = tf.Session() print(sess.run(test1)[1,1,0])
/home/ming/PycharmProjects/untitled2/venv/bin/python /home/ming/PycharmProjects/untitled2/index2.py 2019-05-13 20:47:22.263391: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA Tensor("test1:0", shape=(2, 2, 3), dtype=int32) 2019-05-13 20:47:22.268317: I tensorflow/core/platform/profile_utils/cpu_utils.cc:94] CPU Frequency: 3493455000 Hz 2019-05-13 20:47:22.268869: I tensorflow/compiler/xla/service/service.cc:150] XLA service 0x14fdb30 executing computations on platform Host. Devices: 2019-05-13 20:47:22.268921: I tensorflow/compiler/xla/service/service.cc:158] StreamExecutor device (0): <undefined>, <undefined> 23 Process finished with exit code 0
类型
自动判断
张量运算
矩阵运算
import tensorflow as tf # 计算图 test1 = tf.constant([[[1,2,3], [2,3,4]], [[23,34,5], [23,44,5]]] ,name="test1") test2 = tf.constant([[[1,2,3], [2,3,4]], [[23,34,5], [23,44,5]]] ,name="test2") print(test1 + test2) sess = tf.Session() print(sess.run(test1 + test2)[1,1,0])
操作
计算图的节点为操作
加法操作
乘法操作
构造变量初始值为一个操作
运算操作有属性,构建图会确定下来
操作和运算设备做绑定
操作存在顺序 依赖边
计算图操作
import tensorflow as tf # 清空图 tf.reset_default_graph() # 定义变量 a a = tf.Variable(1, name="a") # 定义操作b = a + 1 b = tf.add(a, 1, name="b") # 定义操作c 为 b * 4 c = tf.multiply(b, 4, name="c") # 定义d = c - b d = tf.subtract(c, b, name="d")
生成图操作
import tensorflow as tf # 清空图 tf.reset_default_graph() # 定义变量 a a = tf.Variable(1, name="a") # 定义操作b = a + 1 b = tf.add(a, 1, name="b") # 定义操作c 为 b * 4 c = tf.multiply(b, 4, name="c") # 定义d = c - b d = tf.subtract(c, b, name="d") logdir = "log1" # 写入日子 writer = tf.summary.FileWriter(logdir, tf.get_default_graph()) writer.close();
写入当前空间下的log1文件
启动
(venv) ➜ untitled2 tensorboard --logdir ./log1/ TensorBoard 1.13.1 at http://ming-pc:6006 (Press CTRL+C to quit)
这样就可以看到生成图
基本运算
会话
session拥有所要管理的资源
import tensorflow as tf # 定义图 test1 = tf.constant([1 , 3,3]) # 创建会话 sess = tf.Session() # 进行运算 print(sess.run(test1)) sess.close()
import tensorflow as tf # 定义图 test1 = tf.constant([1 , 3,3]) # 创建会话 sess = tf.Session() # 进行运算 try: print(sess.run(test1)) except: print("Exception") finally: sess.close()
上下文管理会话
import tensorflow as tf # 定义图 node1 = tf.constant(3.0, tf.float32, name="node1") node2 = tf.constant(4.0, tf.float32, name="node2") result = tf.add(node1, node2) # 创建会话 上下文管理 with tf.Session() as sess: print(sess.run(result))
指定默认会话
import tensorflow as tf # 定义图 node1 = tf.constant(3.0, tf.float32, name="node1") node2 = tf.constant(4.0, tf.float32, name="node2") result = tf.add(node1, node2) sess = tf.Session() with sess.as_default(): print(result.eval())
常量 变量
创建常量
node2 = tf.constant(4.0, tf.float32, name="node2")
变量
import tensorflow as tf node1 = tf.Variable(3.0, tf.float32, name="node1") node2 = tf.Variable(4.0, tf.float32, name="node2") result = tf.add(node1, node2, name="add") sess = tf.Session() # 变量初始化 init = tf.global_variables_initializer() sess.run(init) print(sess.run(result))
由于是静态图 所以需要对图进行赋初值
变量赋值
变量定义后无需要人工赋值,训练会自动调整变量对应的数值
import tensorflow as tf value = tf.Variable(0, name="value") one = tf.constant(1) new_value = tf.add(value, one) # new_value 赋值给value update_value = tf.assign(value, new_value) init = tf.global_variables_initializer() with tf.Session() as sess: sess.run(init) for _ in range(10): sess.run(update_value) print(sess.run(value))
/home/ming/PycharmProjects/untitled2/venv/bin/python /home/ming/PycharmProjects/untitled2/index2.py WARNING:tensorflow:From /home/ming/PycharmProjects/untitled2/venv/lib/python3.7/site-packages/tensorflow/python/framework/op_def_library.py:263: colocate_with (from tensorflow.python.framework.ops) is deprecated and will be removed in a future version. Instructions for updating: Colocations handled automatically by placer. 2019-05-13 21:54:38.645047: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA 2019-05-13 21:54:38.652219: I tensorflow/core/platform/profile_utils/cpu_utils.cc:94] CPU Frequency: 3493455000 Hz 2019-05-13 21:54:38.652580: I tensorflow/compiler/xla/service/service.cc:150] XLA service 0x1f3ac80 executing computations on platform Host. Devices: 2019-05-13 21:54:38.652617: I tensorflow/compiler/xla/service/service.cc:158] StreamExecutor device (0): <undefined>, <undefined> 1 2 3 4 5 6 7 8 9 10 Process finished with exit code 0
运行流程
import tensorflow as tf # 清除计算图 tf.reset_default_graph() value = tf.Variable(0, name="value") one = tf.constant(1) new_value = tf.add(value, one) # new_value 赋值给value update_value = tf.assign(value, new_value) init = tf.global_variables_initializer() with tf.Session() as sess: sess.run(init) for _ in range(10): sess.run(update_value) print(sess.run(value)) logdir = "log" # 写日志 writer = tf.summary.FileWriter(logdir, tf.get_default_graph()) writer.close()
变量依赖于控制init
占位符
运行的时候需要输入 %
x = tf.placeholder(tf.fload32, [2, 3], name="tx")
提交数据
import tensorflow as tf a = tf.placeholder(tf.float32, name="a") b = tf.placeholder(tf.float32, name="b") c = tf.multiply(a, b, name="c") init = tf.global_variables_initializer() with tf.Session() as sess: sess.run(init) result = sess.run(c, feed_dict={a:10.0, b:2}) print(result)
/home/ming/PycharmProjects/untitled2/venv/bin/python /home/ming/PycharmProjects/untitled2/index2.py 2019-05-13 22:16:29.402834: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA 2019-05-13 22:16:29.407957: I tensorflow/core/platform/profile_utils/cpu_utils.cc:94] CPU Frequency: 3493455000 Hz 2019-05-13 22:16:29.408692: I tensorflow/compiler/xla/service/service.cc:150] XLA service 0x26e9c80 executing computations on platform Host. Devices: 2019-05-13 22:16:29.408751: I tensorflow/compiler/xla/service/service.cc:158] StreamExecutor device (0): <undefined>, <undefined> 20.0 Process finished with exit code 0
使用字典提交数据
import tensorflow as tf a = tf.placeholder(tf.float32, name="a") b = tf.placeholder(tf.float32, name="b") c = tf.multiply(a, b, name="c") d = tf.multiply(a, b, name="d") init = tf.global_variables_initializer() with tf.Session() as sess: sess.run(init) result = sess.run([c,d], feed_dict={a:[23.34,53.23], b:[23.45,243.5]}) print(result) print(result[0])
/home/ming/PycharmProjects/untitled2/venv/bin/python /home/ming/PycharmProjects/untitled2/index2.py 2019-05-13 22:18:28.478916: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA 2019-05-13 22:18:28.483828: I tensorflow/core/platform/profile_utils/cpu_utils.cc:94] CPU Frequency: 3493455000 Hz 2019-05-13 22:18:28.484084: I tensorflow/compiler/xla/service/service.cc:150] XLA service 0x1359c80 executing computations on platform Host. Devices: 2019-05-13 22:18:28.484115: I tensorflow/compiler/xla/service/service.cc:158] StreamExecutor device (0): <undefined>, <undefined> [array([ 547.323, 12961.505], dtype=float32), array([ 547.323, 12961.505], dtype=float32)] [ 547.323 12961.505] Process finished with exit code 0
可视化应用
使用的是TensorBoard
从日志中读取数据
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:- TensorFlow 一
- GAN入门实践(一)--Tensorflow实现
- 使用 Tensorflow 实现口算检查器(一):模型选择
- PyTorch一周年战绩总结:是否比TensorFlow来势凶猛?
- 在TensorFlow+Keras环境下使用RoI池化一步步实现注意力机制
- DC/OS深度学习教程(一):TensorFlow、Nvidia与Apache Mesos(DC/OS)
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。