tensorflow入门到精通——基本知识入门
2024-11-06 07:14:42 阅读次数:23
变量,数据流
tensorflow1.x 特点
- tensflow1版本是依据计算图来构建,属于符号型编程。流程为:
- 定义变量
- 建立数据流图
- 规定计算各个变量之间的关系
- 数据流图进编译
# -*- coding: utf-8 -*-
"""
@Time : 2021/8/12 上午2:06
@Auth : 陈伟峰
@File :test02.py
@phone: 15882085601
@IDE :PyCharm
@Motto:ABC(Always Be Coding)
"""
import tensorflow as tf
# 定义数据
x = tf.constant([1.0,2.0])
y = tf.constant([3.0,4.0])
# 定义数据计算关系
c = x*y
print(x,y,c)
sess = tf.Session()
# 会话编译
with tf.Session():
# 计算值
pre = sess.run(c)
print(pre)
# 关闭会话
sess.close()
计算图的构建
# -*- coding: utf-8 -*-
"""
@Time : 2021/8/12 上午2:29
@Auth : 陈伟峰
@File :test03.py
@phone: 15882085601
@IDE :PyCharm
@Motto:ABC(Always Be Coding)
"""
import tensorflow as tf
# 创建一个常量运算矩阵
matrix1 = tf.constant([[3,3]],dtype=tf.float32)
matrix2 = tf.constant([[2,3]],dtype=tf.float32)
predict = tf.matmul(matrix1,matrix2)
建立会话
# -*- coding: utf-8 -*-
"""
@Time : 2021/8/12 上午2:29
@Auth : 陈伟峰
@File :test03.py
@phone: 15882085601
@IDE :PyCharm
@Motto:ABC(Always Be Coding)
"""
import tensorflow as tf
# 创建一个常量运算矩阵
matrix1 = tf.constant([[3,3]],dtype=tf.float32)
matrix2 = tf.constant([[2],[3]],dtype=tf.float32)
predict = tf.matmul(matrix1,matrix2)
#执行会话
with tf.Session() as sess:
result = sess.run([predict])
print(result)
变量
# -*- coding: utf-8 -*-
"""
@Time : 2021/8/12 上午2:47
@Auth : 陈伟峰
@File :test04.py
@phone: 15882085601
@IDE :PyCharm
@Motto:ABC(Always Be Coding)
"""
import tensorflow as tf
state = tf.Variable(0,name="counter",dtype=64)
print(state)
input1 = tf.constant(3,name="tensor")
print(input1)
版权声明:本文内容来自第三方投稿或授权转载,原文地址:https://blog.51cto.com/u_13859040/5814600,作者:qq5b42bed9cc7e9,版权归原作者所有。本网站转在其作品的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如因作品内容、版权等问题需要同本网站联系,请发邮件至ctyunbbs@chinatelecom.cn沟通。
上一篇:深度学习从入门到精通——pandas的基本使用
下一篇:Opencv实战之前景背景分割无参数概率密度估计KNN算法