TensorFlow 从入门到精通(0)—— tensorflow编程基础
2023-09-14 09:14:57 时间
import tensorflow as tf
一、初体验
node1 = tf.constant([[1,2,3],[4,5,6]])
node2 = tf.constant([[1,2,3],[4,5,6]])
node3 = tf.add(node1,node2)
node3
<tf.Tensor: shape=(2, 3), dtype=int32, numpy=
array([[ 2, 4, 6],
[ 8, 10, 12]], dtype=int32)>
# 通过.numpy()获取值
node3.numpy()
array([[ 2, 4, 6],
[ 8, 10, 12]], dtype=int32)
node3.dtype
tf.int32
node3.shape
TensorShape([2, 3])
node3.get_shape()
TensorShape([2, 3])
二、常量
a = tf.constant(1)
a
<tf.Tensor: shape=(), dtype=int32, numpy=1>
b = tf.constant(2.0)
b
<tf.Tensor: shape=(), dtype=float32, numpy=2.0>
# 强制类型转换
a = tf.cast(a,tf.float32)
tf.add(a,b).numpy()
3.0
三、变量
v1 = tf.Variable([1,2])
v1
<tf.Variable 'Variable:0' shape=(2,) dtype=int32, numpy=array([1, 2], dtype=int32)>
v2 = tf.Variable([3,4],tf.float32)
v2
<tf.Variable 'Variable:0' shape=(2,) dtype=int32, numpy=array([3, 4], dtype=int32)>
tf.Variable(a)
<tf.Variable 'Variable:0' shape=() dtype=float32, numpy=1.0>
v2
<tf.Variable 'Variable:0' shape=(2,) dtype=int32, numpy=array([3, 4], dtype=int32)>
# 更改变量的值
v2.assign(v2+1)
<tf.Variable 'UnreadVariable' shape=(2,) dtype=int32, numpy=array([4, 5], dtype=int32)>
v2.assign_add([2,1])
<tf.Variable 'UnreadVariable' shape=(2,) dtype=int32, numpy=array([6, 6], dtype=int32)>
v2.assign_sub([1,2])
<tf.Variable 'UnreadVariable' shape=(2,) dtype=int32, numpy=array([5, 4], dtype=int32)>
四、tf2执行tf1代码
import tensorflow.compat.v1 as tf
tf.disable_eager_execution()
node1 = tf.constant(3)
node2 = tf.constant(4)
node3 = node1 + node2
node3
<tf.Tensor 'add:0' shape=() dtype=int32>
sess = tf.Session()
sess.run(node3)
7
sess.close()
五、会话模式
5.1三种对话模式
# 1.会话模式一
tens1 = tf.constant([1])
sess = tf.Session()
print(sess.run(tens1))
sess.close()
[1]
# 2.会话模式二
tens1 = tf.constant(1)
sess = tf.Session()
try:
print(sess.run(tens1))
except:
...
finally:
sess.close()
1
# 3.会话模式三
tens1 = tf.constant(1)
with tf.Session() as sess:
print(sess.run(tens1))
1
5.2指定默认的对话
- 指定了默认会话可以通过tf.Tensor.eval函数来计算一个张量的取值
tens1 = tf.constant(1)
sess = tf.Session()
with sess.as_default():
print(tens1.eval())
1
# 下面代码和上面同理
tens1 = tf.constant(1)
sess = tf.Session()
print(sess.run(tens1))
print(tens1.eval(session=sess))
1
1
# tf.InteractiveSession() 这个函数会自动将生成的会话注册为默认会话
tens1 = tf.constant(1)
sess = tf.InteractiveSession()
print(tens1.eval())
1
六、变量的初始化
node1 = tf.Variable(3.0)
node2 = tf.Variable(4.0)
result = node1+node2 # tf.add(node1,node2)
node1_init = node1.initializer
node2_init = node2.initializer
sess = tf.Session()
sess.run(node1_init)
sess.run(node2_init)
print(sess.run([result,node1,node2]))
[7.0, 3.0, 4.0]
# 可以对变量进行批量初始化
node1 = tf.Variable(3.0)
node2 = tf.Variable(4.0)
result = node1+node2
init = tf.global_variables_initializer()
sess.run(init)
print(sess.run(result))
7.0
七、占位符 placeholder
- 类似于python中的%s
x = tf.placeholder(tf.float32)
x_value = float(input())
123
with tf.Session() as sess:
result = sess.run(x,feed_dict={x:x_value})
print(result)
123.0
相关文章
- Python基础14-内置模块
- Python基础21-网络编程
- MySQL基础之流程控制结构
- java的基础代码_java编程入门基础教程
- 1.PS编程入门基础语法
- 学习基础Proxy
- Shell编程基础01
- Java基础编程练习题
- opencv3编程入门_java基础与入门教程
- Python编程基础:f-字符串格式
- shell基础编程(一)
- 常见消息队列基础原理和选型ppt
- 英语基础-定语从句
- Socket编程基础-Socket编程基础-示例
- python-数据库编程-SQL的基础语法和命令
- Linux基础:如何配置本地yum源,国内yum源,配置epel源等步骤
- MongoDB 数据库基础 之 用户管理
- 学Python,不需要有编程基础!
- 学习Linux编程:熟悉基础工具(linux的编程工具)
- 掌握Grub参数 基础上学习Linux系统(grublinux参数)
- Linux基础编程:快速入门与实践(linux基础编程)
- Linux C编程入门:新手快速掌握语言基础(linuxcnew)
- Linux网络编程:从基础到深入PDF指南(linux网络编程pdf)
- Linux编程基础:下载指南(linux编程基础下载)
- 深入了解Linux分支图,为你的编程生涯打下扎实基础(linux分支图)
- 「Linux 网络编程基础」让您快速入门!(linux网络编程基础)
- 深入学习Linux SPI编程:从基础入门到高级应用(linuxspi编程)
- 学习Oracle P6从基础到精通(oracle p6学习)
- 《JavaScriptDOM编程艺术》读书笔记之DOM基础