阅读此文章需要结合我上传的word文档阅读。
此篇仅为个人学习笔记,特此说明
固定值张量
进入
需要写一个

3行4列矩阵

指定类型的话
看值的话

学过numpy
用numpy创建数组

跟上面类似,只不过上面是用tensorflow创建数组

创建一个3行4列全0数组
随机值张量
Tensorflow实习正态分布
两种方法

确定一个绝对值在一个区间(一般用truncated比较多一些)
例如(stdv为标准差)

看值
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-eb7jQ2H9-1653011362374)(file:///C:\Users\ADMINI~1\AppData\Local\Temp\ksohtml17484\wps11.jpg)]](https://i-blog.csdnimg.cn/blog_migrate/836670e4a624b46a242d1a49aef56c8c.png)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-9Sp2qo1D-1653011362375)(file:///C:\Users\ADMINI~1\AppData\Local\Temp\ksohtml17484\wps12.jpg)]](https://i-blog.csdnimg.cn/blog_migrate/b70dfab0e20c2a235728d881f1fdf6a9.png)
偏离正中央,我们就多弄几个
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ffxZa9y2-1653011362375)(file:///C:\Users\ADMINI~1\AppData\Local\Temp\ksohtml17484\wps13.jpg)]](https://i-blog.csdnimg.cn/blog_migrate/f7925ff8ee9888b43eb48a9baed7d0c4.png)
这个就是截断
其他创建张量
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-jx7P8eVh-1653011362376)(file:///C:\Users\ADMINI~1\AppData\Local\Temp\ksohtml17484\wps14.jpg)]](https://i-blog.csdnimg.cn/blog_migrate/c781aa770f6f503586073fcac8437119.png)
一般来说做类型修改
(浮点型变整型)
形状变换(2,3)-----(1.6)

主要点(重要)只用这个
Demo243.py
代码如下:
import tensorflow as tf
def tensor_demo():
‘’’
张量的演示
‘’’
tensor1 = tf.constant(4.0)
tensor2 = tf.constant([1, 2, 3, 4])
linear_squares = tf.constant([[4], [9], [16], [25]], dtype=tf.int32)
print(‘tensor1:\n’, tensor1)
print(‘tensor2:\n’, tensor2)
print(‘linear_squares_before:\n’, linear_squares)
# 张量类型的修改
t_cast = tf.cast(linear_squares, dtype=tf.float32)
print(‘linear_squares_after:\n’, linear_squares)
print(‘t_cast:\n’, t_cast)
if name == ‘main’:
tensor_demo()
解释如下
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ni64eOrR-1653011362378)(file:///C:\Users\ADMINI~1\AppData\Local\Temp\ksohtml17484\wps18.jpg)]](https://i-blog.csdnimg.cn/blog_migrate/08dd6181c59fec1c1c26b12d4b97077b.png)
转换之前的张量,改变他的类型,返回值为t_cast,
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-SmssOv0V-1653011362378)(file:///C:\Users\ADMINI~1\AppData\Local\Temp\ksohtml17484\wps19.jpg)]](https://i-blog.csdnimg.cn/blog_migrate/9ea869baab37c1e7a80559b4e6205124.png)
打印出来
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-brleIRJS-1653011362379)(file:///C:\Users\ADMINI~1\AppData\Local\Temp\ksohtml17484\wps20.jpg)]](https://i-blog.csdnimg.cn/blog_migrate/3aa5010873570db1df48f47a82983cef.png)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-YMeaQuxc-1653011362379)(file:///C:\Users\ADMINI~1\AppData\Local\Temp\ksohtml17484\wps21.jpg)]](https://i-blog.csdnimg.cn/blog_migrate/b5a01c0fb725fee05b1d2babdfd8b829.png)
说明t_cast不会改变之前张量的值,只会返回一个不同数据类型的值
形状的改变
(动态形状,静态形状)
定义三个张量
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-3RoHAkEb-1653011362380)(file:///C:\Users\ADMINI~1\AppData\Local\Temp\ksohtml17484\wps22.jpg)]](https://i-blog.csdnimg.cn/blog_migrate/ef20189e97a0199ff4e382b0e1231a63.png)
先看看
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-3uqwM4t0-1653011362382)(file:///C:\Users\ADMINI~1\AppData\Local\Temp\ksohtml17484\wps23.jpg)]](https://i-blog.csdnimg.cn/blog_migrate/06ee30cd8a8bcb0f9097ff41fd61cb45.png)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-j5ilj9zm-1653011362383)(file:///C:\Users\ADMINI~1\AppData\Local\Temp\ksohtml17484\wps24.jpg)]](https://i-blog.csdnimg.cn/blog_migrate/ecb0349f4310a7381e2652b476951f6c.png)
这些都是静态形状(原始元素都是固定的)
Tensor1
Tensor2
Linear_squares_before
动态形状
无论在什么情况下,只要你想改变就能改变
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-qHfvpTna-1653011362383)(file:///C:\Users\ADMINI~1\AppData\Local\Temp\ksohtml17484\wps25.jpg)]](https://i-blog.csdnimg.cn/blog_migrate/3352190765ed574a5bee8e6af493e53a.png)
什么情况下可以改变形状呢?
之前形状没有确定下来的时候
shape如果写上这个,不固定行和列(Demo243)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-S1wqRHUN-1653011362384)(file:///C:\Users\ADMINI~1\AppData\Local\Temp\ksohtml17484\wps26.jpg)]](https://i-blog.csdnimg.cn/blog_migrate/0944018a958ce6ac2f13ea83d4068557.png)
还有一种写法不固定行和列
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-kovd1ZhG-1653011362384)(file:///C:\Users\ADMINI~1\AppData\Local\Temp\ksohtml17484\wps27.jpg)]](https://i-blog.csdnimg.cn/blog_migrate/f9c26c16967f5c6262fd7198fdf24026.png)
行和列都确定下来
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-4Nl29NDJ-1653011362385)(file:///C:\Users\ADMINI~1\AppData\Local\Temp\ksohtml17484\wps28.jpg)]](https://i-blog.csdnimg.cn/blog_migrate/2e4ef6b88552342d47d475489e2baffb.png)
分别打印出来
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Nt6H7mcc-1653011362385)(file:///C:\Users\ADMINI~1\AppData\Local\Temp\ksohtml17484\wps29.jpg)]](https://i-blog.csdnimg.cn/blog_migrate/a3222f0fd6a4f65643bc5f2714790964.png)
看看效果
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-UEvjmJ40-1653011362386)(file:///C:\Users\ADMINI~1\AppData\Local\Temp\ksohtml17484\wps30.jpg)]](https://i-blog.csdnimg.cn/blog_migrate/c6a40b4c64fc7d6a29e50d0d52ee4e0d.png)
代码如下
import tensorflow as tf
def tensor_demo():
‘’’
张量的演示
‘’’
tensor1 = tf.constant(4.0)
tensor2 = tf.constant([1, 2, 3, 4])
linear_squares = tf.constant([[4], [9], [16], [25]], dtype=tf.int32)
print(‘tensor1:\n’, tensor1)
print(‘tensor2:\n’, tensor2)
print(‘linear_squares_before:\n’, linear_squares)
# 张量类型的修改
t_cast = tf.cast(linear_squares, dtype=tf.float32)
print(‘linear_squares_after:\n’, linear_squares)
print(‘t_cast:\n’, t_cast)
# 更新/改变静态形状
# 定义占位符
# 没有完全固定下来的静态形状
a_p = tf.placeholder(dtype=tf.float32, shape=[None,None])
b_p = tf.placeholder(dtype=tf.float32, shape=[None,10])
c_p = tf.placeholder(dtype=tf.float32, shape=[3,2])
print(‘a_p:\n’, a_p)
print(‘b_p:\n’, b_p)
print(‘c_p:\n’, c_p)
if name == ‘main’:
tensor_demo()
问号代表没有固定下来的静态形状
不是随便****改变形状****的
传入参数是有要求的
维度只能在那个维度(不能够改变维度的)【维数不能改】
还有一个就是第二个情况,后面那个为10
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-hBuGCCVA-1653011362386)(file:///C:\Users\ADMINI~1\AppData\Local\Temp\ksohtml17484\wps31.jpg)]](https://i-blog.csdnimg.cn/blog_migrate/c1ceb075ea14b6c9fbab11f47f13a6c6.png)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-LPO3DyXJ-1653011362387)(file:///C:\Users\ADMINI~1\AppData\Local\Temp\ksohtml17484\wps32.jpg)]](https://i-blog.csdnimg.cn/blog_migrate/f0e56504a819da0582d4125c6ede0df6.png)
【静态形状只能跟新,没有固定下来的部分】
静态形状在更新的时候,只能更新没有完全固定价格下来的部分
代码如下
import tensorflow as tf
def tensor_demo():
‘’’
张量的演示
‘’’
tensor1 = tf.constant(4.0)
tensor2 = tf.constant([1, 2, 3, 4])
linear_squares = tf.constant([[4], [9], [16], [25]], dtype=tf.int32)
print(‘tensor1:\n’, tensor1)
print(‘tensor2:\n’, tensor2)
print(‘linear_squares_before:\n’, linear_squares)
# 张量类型的修改
t_cast = tf.cast(linear_squares, dtype=tf.float32)
print(‘linear_squares_after:\n’, linear_squares)
print(‘t_cast:\n’, t_cast)
# 更新/改变静态形状
# 定义占位符
# 没有完全固定下来的静态形状
a_p = tf.placeholder(dtype=tf.float32, shape=[None,None])
b_p = tf.placeholder(dtype=tf.float32, shape=[None,10])
c_p = tf.placeholder(dtype=tf.float32, shape=[3,2])
print(‘a_p:\n’, a_p)
print(‘b_p:\n’, b_p)
print(‘c_p:\n’, c_p)
# 更新形状未确定的部分
a_p.set_shape([2, 3])
b_p.set_shape([2, 10])
print(‘a_p:\n’, a_p)
print(‘b_p:\n’, b_p)
if name == ‘main’:
tensor_demo()
静态形状不能跨阶段
如果非要改的话
就利用动态形状改变
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-bUNu2zkk-1653011362387)(file:///C:\Users\ADMINI~1\AppData\Local\Temp\ksohtml17484\wps33.jpg)]](https://i-blog.csdnimg.cn/blog_migrate/0aada979ccc6e9ad74e5d1e724dc5b48.png)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ORPsg9ZC-1653011362388)(file:///C:\Users\ADMINI~1\AppData\Local\Temp\ksohtml17484\wps34.jpg)]](https://i-blog.csdnimg.cn/blog_migrate/5443f3f4de4d8e2da30929df3874cf2e.png)
用reshape方式改
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-vHvVagwV-1653011362388)(file:///C:\Users\ADMINI~1\AppData\Local\Temp\ksohtml17484\wps35.jpg)]](https://i-blog.csdnimg.cn/blog_migrate/1339ec5b1734a45c3c2c34cd84cbe658.png)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-8K2BeV8I-1653011362389)(file:///C:\Users\ADMINI~1\AppData\Local\Temp\ksohtml17484\wps36.jpg)]](https://i-blog.csdnimg.cn/blog_migrate/544d9cab16bfae2dab4767d772c3f3c5.png)
或者改c_p[本来3行2列的]
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-hD3cbfmr-1653011362389)(file:///C:\Users\ADMINI~1\AppData\Local\Temp\ksohtml17484\wps37.jpg)]](https://i-blog.csdnimg.cn/blog_migrate/3b5d72a8426964ec61ac96ee96a5e876.png)
改为2行3列
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-zFAFMu7P-1653011362390)(file:///C:\Users\ADMINI~1\AppData\Local\Temp\ksohtml17484\wps38.jpg)]](https://i-blog.csdnimg.cn/blog_migrate/d6fb4738e0e5f916a46122a468b68293.png)
形状可以变(3,2)----(3,2,1)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Vk06A784-1653011362390)(file:///C:\Users\ADMINI~1\AppData\Local\Temp\ksohtml17484\wps39.jpg)]](https://i-blog.csdnimg.cn/blog_migrate/54ff809142af89c22f68790b2b42939f.png)
行数列数总数不能变
不能改成例如(3,2)---->(3,2,2)
这样改同样会报错,概括
tf.reshape 改变动态形状
tf.set_shape 改变静态形状
import tensorflow as tf
def tensor_demo():
‘’’
张量的演示
‘’’
tensor1 = tf.constant(4.0)
tensor2 = tf.constant([1, 2, 3, 4])
linear_squares = tf.constant([[4], [9], [16], [25]], dtype=tf.int32)
print(‘tensor1:\n’, tensor1)
print(‘tensor2:\n’, tensor2)
print(‘linear_squares_before:\n’, linear_squares)
# 张量类型的修改
t_cast = tf.cast(linear_squares, dtype=tf.float32)
print(‘linear_squares_after:\n’, linear_squares)
print(‘t_cast:\n’, t_cast)
# 更新/改变静态形状
# 定义占位符
# 没有完全固定下来的静态形状
a_p = tf.placeholder(dtype=tf.float32, shape=[None,None])
b_p = tf.placeholder(dtype=tf.float32, shape=[None,10])
c_p = tf.placeholder(dtype=tf.float32, shape=[3,2])
print(‘a_p:\n’, a_p)
print(‘b_p:\n’, b_p)
print(‘c_p:\n’, c_p)
# 更新形状未确定的部分
#a_p.set_shape([2, 3]) # 尝试 a_p.set_shape([2, 3, 1])
#b_p.set_shape([2, 10]) # 尝试 b_p.set_shape([2, 5])
# 动态形状的改变
a_p_reshape = tf.reshape(a_p, shape=[2,3,1])
print(‘a_p:\n’, a_p)
print(‘a_p_reshape:\n’, a_p_reshape)
c_p_reshape = tf.reshape(c_p, shape=[2,3])
print(‘c_p:\n’, c_p)
print(‘c_p_reshape:\n’, c_p_reshape)
if name == ‘main’:
tensor_demo()
张量的数学运算(有API)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-BGQhGTzj-1653011362391)(file:///C:\Users\ADMINI~1\AppData\Local\Temp\ksohtml17484\wps40.jpg)]](https://i-blog.csdnimg.cn/blog_migrate/2bdd74e397ef636b4f33414f9872f3a0.png)
变量OP
在tensorflow里面
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-kQLlIDyF-1653011362391)(file:///C:\Users\ADMINI~1\AppData\Local\Temp\ksohtml17484\wps41.jpg)]](https://i-blog.csdnimg.cn/blog_migrate/b5d98bdd5f8c09b75e2770fbab152995.png)
是在里面专门的OP组件
可以把程序里面的数据存储保存下来
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-nCronybN-1653011362392)(file:///C:\Users\ADMINI~1\AppData\Local\Temp\ksohtml17484\wps42.jpg)]](https://i-blog.csdnimg.cn/blog_migrate/d133dde16adaa2ef3e410539ccc72eac.png)
深度学习的目的,为了解决一个模型,那么如何存储
权重为模型参数,希望模型参数能不断迭代更新,为了让模型参数保存下来
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-IIMEb156-1653011362393)(file:///C:\Users\ADMINI~1\AppData\Local\Temp\ksohtml17484\wps43.jpg)]](https://i-blog.csdnimg.cn/blog_migrate/c3f867c95d527005431a7e9632c0b9e6.png)
具体代码
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-zNdSLXK3-1653011362393)(file:///C:\Users\ADMINI~1\AppData\Local\Temp\ksohtml17484\wps44.jpg)]](https://i-blog.csdnimg.cn/blog_migrate/843eb813250608890a03f86f025f23ed.png)
这个比较简单
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-OEszx8G4-1653011362394)(file:///C:\Users\ADMINI~1\AppData\Local\Temp\ksohtml17484\wps45.jpg)]](https://i-blog.csdnimg.cn/blog_migrate/76b6e46951f95d6ae0cb10c95344bb38.png)
c是tensor,b,a为variable
开启会话
用sess.Run,参数
用![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-pIDaM2PJ-1653011362394)(file:///C:\Users\ADMINI~1\AppData\Local\Temp\ksohtml17484\wps46.jpg)]](https://i-blog.csdnimg.cn/blog_migrate/9d30736aaa8ae6e398a2614b3e0b5e96.png)
接受
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-PshGnQUY-1653011362395)(file:///C:\Users\ADMINI~1\AppData\Local\Temp\ksohtml17484\wps47.jpg)]](https://i-blog.csdnimg.cn/blog_migrate/46d70803c2cce4cd9ac6baa3c26f111f.png)
结果只有警告
不担心(表示还没初始化值)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-UbqUpGKN-1653011362395)(file:///C:\Users\ADMINI~1\AppData\Local\Temp\ksohtml17484\wps48.jpg)]](https://i-blog.csdnimg.cn/blog_migrate/0fd3a05e5ede4278c1ef82da89b439af.png)
需要的操作
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-o2CtR95B-1653011362396)(file:///C:\Users\ADMINI~1\AppData\Local\Temp\ksohtml17484\wps49.jpg)]](https://i-blog.csdnimg.cn/blog_migrate/ed0482c43e995d5d74a2f348e02985ce.png)
放在这里
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-2sGMyMEw-1653011362396)(file:///C:\Users\ADMINI~1\AppData\Local\Temp\ksohtml17484\wps50.jpg)]](https://i-blog.csdnimg.cn/blog_migrate/17f1a2911c2c62173f38c56aedc406f0.png)
还要记得运行初始化(demo251c)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-KI7lwH3C-1653011362397)(file:///C:\Users\ADMINI~1\AppData\Local\Temp\ksohtml17484\wps51.jpg)]](https://i-blog.csdnimg.cn/blog_migrate/31eda4e431e4cb2765c39eafa2f69c04.png)
代码如下
import tensorflow as tf
def variable_demo():
‘’’
变量的演示
‘’’
a = tf.Variable(initial_value=50)
b = tf.Variable(initial_value=40)
c = tf.add(a, b)
print(‘a:\n’, a)
print(‘b:\n’, b)
print(‘c:\n’, c)
# 初始化变量
init = tf.global_variables_initializer()
# 开启会话
with tf.Session() as sess:
#运行初始化
sess.run(init)
a_value, b_value, c_value = sess.run([a,b,c])
print(‘a_value:\n’, a_value)
print(‘b_value:\n’, b_value)
print(‘c_value:\n’, c_value)
if name == ‘main’:
variable_demo()

用这个方法
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-hYtPdc3V-1653011362399)(file:///C:\Users\ADMINI~1\AppData\Local\Temp\ksohtml17484\wps53.jpg)]](https://i-blog.csdnimg.cn/blog_migrate/662b6f8eb5c003bd1fc48943f1b56dd5.png)
Demo252a
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-OlCFMlY3-1653011362399)(file:///C:\Users\ADMINI~1\AppData\Local\Temp\ksohtml17484\wps54.jpg)]](https://i-blog.csdnimg.cn/blog_migrate/ffda9898e109ddc7e74d88960bd561e5.png)
![原来[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-0jt9XW3X-1653011362400)(file:///C:\Users\ADMINI~1\AppData\Local\Temp\ksohtml17484\wps55.jpg)]](https://i-blog.csdnimg.cn/blog_migrate/c13dfe184bf6d27612f3e8a7f95b5ca3.png)
运行之后
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-1PNRrrMK-1653011362400)(file:///C:\Users\ADMINI~1\AppData\Local\Temp\ksohtml17484\wps56.jpg)]](https://i-blog.csdnimg.cn/blog_migrate/696bc3fa73c6cc87f8b7957f04cd8d46.png)
希望a,b在同一个命名空间,c在不同命名空间
就是再写一个
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-NrHCMIui-1653011362401)(file:///C:\Users\ADMINI~1\AppData\Local\Temp\ksohtml17484\wps57.jpg)]](https://i-blog.csdnimg.cn/blog_migrate/d0ea0d611d577fa3d2fab5eba03f5b7a.png)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-d5CKA9EN-1653011362401)(file:///C:\Users\ADMINI~1\AppData\Local\Temp\ksohtml17484\wps58.jpg)]](https://i-blog.csdnimg.cn/blog_migrate/3a29f292f75c404d36efcc7587591f04.png)
代码如下
import tensorflow as tf
def variable_demo():
‘’’
变量的演示
‘’’
with tf.variable_scope(‘my_score’):
a = tf.Variable(initial_value=50)
b = tf.Variable(initial_value=40)
with tf.variable_scope(‘your_score’):
c = tf.add(a, b)
print(‘a:\n’, a)
print(‘b:\n’, b)
print(‘c:\n’, c)
# 初始化变量
init = tf.global_variables_initializer()
# 开启会话
with tf.Session() as sess:
#运行初始化
sess.run(init)
a_value, b_value, c_value = sess.run([a,b,c])
print(‘a_value:\n’, a_value)
print(‘b_value:\n’, b_value)
print(‘c_value:\n’, c_value)
if name == ‘main’:
variable_demo()
*命名空间的目的,为了让代码模块化*
variable_demo():
‘’’
变量的演示
‘’’
with tf.variable_scope(‘my_score’):
a = tf.Variable(initial_value=50)
b = tf.Variable(initial_value=40)
with tf.variable_scope(‘your_score’):
c = tf.add(a, b)
print(‘a:\n’, a)
print(‘b:\n’, b)
print(‘c:\n’, c)
# 初始化变量
init = tf.global_variables_initializer()
# 开启会话
with tf.Session() as sess:
#运行初始化
sess.run(init)
a_value, b_value, c_value = sess.run([a,b,c])
print(‘a_value:\n’, a_value)
print(‘b_value:\n’, b_value)
print(‘c_value:\n’, c_value)
if name == ‘main’:
variable_demo()
*命名空间的目的,为了让代码模块化*
本文介绍了在TensorFlow中如何创建和操作张量,包括固定值张量、随机值张量的创建,以及张量的类型修改和形状变换。通过示例代码展示了张量的静态和动态形状的改变,并讨论了变量的概念,如何初始化和使用变量进行运算,以及变量的作用域和命名空间管理。
2867

被折叠的 条评论
为什么被折叠?



