栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

2022.5.20深度学习技术学习(BETA版)TensorFlow框架中的张量与变量(后期会不断完善)

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

2022.5.20深度学习技术学习(BETA版)TensorFlow框架中的张量与变量(后期会不断完善)

固定值张量

进入需要写一个

3行4列矩阵

指定类型的话
看值的话

学过numpy
用numpy创建数组

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


创建一个3行4列全0数组

随机值张量

Tensorflow实习正态分布

两种方法

确定一个绝对值在一个区间(一般用truncated比较多一些)

例如(stdv为标准差)

看值

偏离正中央,我们就多弄几个

这个就是截断

其他创建张量

一般来说做类型修改

(浮点型变整型)

形状变换(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()

解释如下

转换之前的张量,改变他的类型,返回值为t_cast,

打印出来

说明t_cast不会改变之前张量的值,只会返回一个不同数据类型的值

形状的改变

(动态形状,静态形状)

定义三个张量

先看看

这些都是静态形状(原始元素都是固定的)

Tensor1

Tensor2

Linear_squares_before

动态形状

无论在什么情况下,只要你想改变就能改变

什么情况下可以改变形状呢?

之前形状没有确定下来的时候

shape如果写上这个,不固定行和列(Demo243)

还有一种写法不固定行和列

行和列都确定下来

分别打印出来

看看效果

代码如下

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

【静态形状只能跟新,没有固定下来的部分】

静态形状在更新的时候,只能更新没有完全固定价格下来的部分

代码如下

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()

静态形状不能跨阶段

如果非要改的话

就利用动态形状改变

用reshape方式改

或者改c_p[本来3行2列的]

改为2行3列

形状可以变(3,2)----(3,2,1)

行数列数总数不能变

不能改成例如(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)

变量OP

在tensorflow里面

是在里面专门的OP组件

可以把程序里面的数据存储保存下来

深度学习的目的,为了解决一个模型,那么如何存储

权重为模型参数,希望模型参数能不断迭代更新,为了让模型参数保存下来

具体代码


这个比较简单

c是tensor,b,a为variable

开启会话

用sess.Run,参数

用
接受

结果只有警告

不担心(表示还没初始化值)

需要的操作

放在这里

还要记得运行初始化(demo251c)

代码如下

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()

用这个方法

Demo252a

运行之后

希望a,b在同一个命名空间,c在不同命名空间

就是再写一个

代码如下

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()

*命名空间的目的,为了让代码模块化*

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/917817.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号