Tensor变量创建-初始化-访问

一、Tensor 创建

1、常量Tensor的创建

1.1 Constant Value Tensor

import tensorflow as tf

sess = tf.Session()

产生全0的张量

  • tf.zeros(shape, dtype=tf.float32, name=None)
    tf.zeros_like(tensor, dtype=None, name=None)
# 产生全0的张量
# tf.zeros(shape, dtype=tf.float32, name=None)
a = tf.zeros([2,3], dtype=tf.float32, name="a")
print(a)
print(sess.run(a))
# tf.zeros_like(tensor, dtype=None, name=None)
b = tf.zeros_like(a, dtype=None, name="b")
print(b)
print(sess.run(b))

运行结果如下:
在这里插入图片描述
产生全1的张量

  • tf.ones(shape, dtype=tf.float32, name=None)
    tf.ones_like(tensor, dtype=None, name=None)
#产生全1的张量
#tf.ones(shape, dtype=tf.float32, name=None)
#tf.ones_like(tensor, dtype=None, name=None)
c = tf.ones([2,3], dtype=tf.float32,name="c")
print(c)
print(sess.run(c))

运行结果如下
在这里插入图片描述
Creates a tensor of shape and fills it with value

  • tf.fill(dims, value, name=None)
# Creates a tensor of shape  and fills it with value
#tf.fill(dims, value, name=None)
d = tf.fill([2,3], 9)
print(d)
print(sess.run(d))

运行结果如下:
在这里插入图片描述
产生常量 Tensor, value 值可为 python 标准数据类型、Numpy 等

  • tf.constant(value, dtype=None, shape=None, name=‘Const’)

e1 = tf.constant(-1.0, shape=[2,3])   # Note: 注意 shape 的用法(广播机制) 
e2 = tf.constant([1,2,3,4,5,6], shape=[2,3])
print(e1)
print(sess.run(e1))
print(e2)
print(sess.run(e2))

运行结果如下:
在这里插入图片描述

1.2 Sequences

产生 num 个等距分布在 [start, stop] 间元素组成的数组,包括 start & stop (需为 float 类型) increase by (stop - start) / (num - 1)

  • tf.linspace(start, stop, num, name=None)

f = tf.linspace(1.,10., 5, name="f1")
print(f)
print(sess.run(f))

运行结果如下:
在这里插入图片描述

  • tf.range([start], limit, delta=1, dtype=None, name=‘range’)

[]为可选参数,步长 delta 默认为 1,start 默认为 0, limit 的值取不到,它产生一个数字序列


f2 = tf.range(start=3, limit=18, delta=3, dtype=tf.int32, name="f2")
print(f2)
print(sess.run(f2))

运行结果如下:
在这里插入图片描述

1.3 Random Tensors

正态分布,默认均值为0,标准差为1.0,数据类型为float32

  • tf.random_normal(shape, mean=0.0, stddev=1.0, dtype=tf.float32, seed=None, name=None)
g1 = tf.random_normal([3,4],name="g1")
print(g1)
print(sess.run(g1))

运行结果如下:
在这里插入图片描述
正态分布,但那些到均值的距离超过2倍标准差的随机数将被丢弃,然后重新抽取,直到取得足够数量的随机数为止, 随机数 x
的取值范围是 [ m e a n − 2 ∗ s t d d e v , m e a n + 2 ∗ s t d d e v ] [mean - 2*stddev, mean + 2*stddev] [mean2stddev,mean+2stddev], 从而可以防止有元素与该张量中的其他元素显著不同的情况出现

  • tf.truncated_normal(shape, mean=0.0, stddev=1.0, dtype=tf.float32, seed=None, name=None)
g2 = tf.truncated_normal([3,4],name="g2")
print(g2)
print(sess.run(g2))

运行结果如下:
在这里插入图片描述
产生在[minval, maxval)之间形状为 shape 的均匀分布, 默认是[0, 1)之间形状为 shape 的均匀分布

  • tf.random_uniform(shape, minval=0.0, maxval=1, dtype=tf.float32, seed=None, name=None)
g3 = tf.random_uniform(shape=(3,4), minval=-1, maxval=1, name="g3")
print(g3)
print(sess.run(g3))

运行结果如下:
在这里插入图片描述
Randomly crops a tensor to a given size 随机裁剪函数

  • tf.random_crop(value, size, seed=None, name=None)

Note:If a dimension should not be cropped, pass the full size of that dimension.
For example, RGB images can be cropped with size = [crop_height, crop_width, 3]

c = tf.constant([[1,2],[3,4],[5,6]])
g4 = tf.random_crop(value=c, size=[2,2], name="g4")
print(g4)
print(sess.run(g4))

运行结果如下:
在这里插入图片描述

Randomly shuffles a tensor along its first dimension 随机洗牌函数
若要几个 op 以同样的顺序 shuffle,设置相同的 seed 即可

  • tf.random_shuffle(value, seed=None, name=None)
c = tf.constant([[1,2],[3,4],[5,6]])
g5 = tf.random_shuffle(c,seed=1,name="g5")
print(g5)
print(sess.run(g5))

运行结果如下:
在这里插入图片描述

Sets the graph-level random seed 设置随机种子

  • tf.set_random_seed(seed)
  1. To generate the same repeatable sequence for an op across sessions
    set the seed for the op, a = tf.random_uniform([1], seed=1)
  2. To make the random sequences generated by all ops be repeatable across sessions
    set a graph-level seed, tf.set_random_seed(1234)
tf.set_random_seed(123)
g6 = tf.random_uniform([1])
g7 = tf.random_normal([1])
with tf.Session() as sess1:
    print(sess1.run(g6))
    print(sess.run(g7))

with tf.Session() as sess2:
    print(sess2.run(g6))
    print(sess2.run(g7))

运行结果如下:
在这里插入图片描述
其它

  • tf.random_gamma(shape,alpha,beta=None,dtype=tf.float32,seed=None,name=None)
g8 = tf.random_gamma([10],[0.5,1.5])
print(sess.run(g8))

运行结果如下:
在这里插入图片描述

sess.close()

2、变量Tensor的创建

2.1 Class tf.Variable()

  • tf.Variable()类

tf.constant 是 op,而 tf.Variable() 是一个类,初始化的对象有多个op

var_obj = tf.Variable(
initial_value,
dtype=None,
name=None,
trainable=True,
collections=None,
validate_shape=True)

初始化参数

  • initial_value:可由 Python 内置数据类型提供,也可由常量 Tensor 的内置 op 来快速构建,但所有这些 op 都需要提供 shape

  • trainable:指明了该变量是否可训练, 会加入 GraphKeys.TRAINABLE_VARIABLES collection 中去。

  • collections: List of graph collections keys. The new variable is added to these collections. Defaults to [GraphKeys.GLOBAL_VARIABLES].

  • validate_shape: If False, allows the variable to be initialized with a value of unknown shape. If True, the default, the shape of initial_value must be known.

返回值

  • 变量实例对象(Tensor-like)
# tf.Variable()函数
import tensorflow as tf

sess = tf.Session()
with tf.variable_scope("foo"):
    v = tf.Variable(tf.zeros([10]))
    sess.run(tf.global_variables_initializer()) #初始化所有变量
    print(v)
    print(sess.run(v))

运行结果如下:
在这里插入图片描述

2.2 tf.get_variable()

Gets an existing variable with these parameters or create a new one

tf.get_variable(
name,
shape=None,
dtype=None,
initializer=None,
trainable=True,
regularizer=None,
collections=None,
caching_device=None,
partitioner=None,
validate_shape=True,
use_resource=None,
custom_getter=None
)

初始化参数

  • name: The name of the new or existing variable.

  • shape: Shape of the new or existing variable.

  • dtype: Type of the new or existing variable (defaults to DT_FLOAT).

  • initializer: Initializer for the variable if one is created.

  • trainable: If True also add the variable to the graph collection tf.GraphKeys.TRAINABLE_VARIABLES.

  • regularizer: A (Tensor -> Tensor or None) function; the result of applying it on a newly created variable will be added to the collection tf.GraphKeys.REGULARIZATION_LOSSES and can be used for regularization.

  • collections: List of graph collections keys to add the Variable to. Defaults to [GraphKeys.GLOBAL_VARIABLES] (see tf.Variable).

返回值
The created or existing Variable, 拥有变量类的所有属性和方法。

Note:

  • name 参数必须要指定,如果仅给出 shape 参数而未指定 initializer,那么它的值将由 tf.glorot_uniform_initializer 随机产生,数据类型为tf.float32;
  • 另外,initializer 可以为一个张量,这种情况下,变量的值和形状即为此张量的值和形状(就不必指定shape 了)。
  • 此函数经常和 tf.variable_scope() 一起使用,产生共享变量
value = [0,1,2,3,4,5,6,7]
init = tf.constant_initializer(value)
with tf.variable_scope("foo"):
    f = tf.get_variable("f", shape=[2,4], initializer=init)
    sess.run(tf.global_variables_initializer())
    print(f.name)

运行结果如下:
在这里插入图片描述

2.3 initializer 参数的初始化

  • tf.constant_initializer()、tf.zeros_initializer()、tf.ones_initializer()

一般要在 tf.get_variable() 函数中指定 shape,因为initializer要用到。

tf.constant_initializer(
value=0,
dtype=dtypes.float32,
verify_shape=False
)

通常偏置项就是用它初始化的。由它衍生出的两个初始化方法:
I、tf.zeros_initializer()
II、tf.ones_initializer()

init = tf.constant_initializer(value=1.,dtype=tf.float32)
x = tf.get_variable(name='v_x', shape=[2,3], initializer=init) #必须指定shape
sess.run(x.initializer)
print(sess.run(x))

运行结果如下:
在这里插入图片描述

  • tf.truncated_normal_initializer()、tf.random_normal_initializer()

生成截断正态分布的随机数,方差一般选0.01等比较小的数

tf.truncated_normal_initializer(
mean=0.0,
stddev=1.0,
seed=None,
dtype=tf.float32
)

生成标准正态分布的随机数,方差一般选0.01等比较小的数

tf.random_normal_initializer(
mean=0.0,
stddev=1.0,
seed=None,
dtype=tf.float32
)

init = tf.truncated_normal_initializer()
x1 = tf.get_variable(name='v_x1', shape=[2,3], initializer=init)
sess.run(x1.initializer)
print(sess.run(x1))

运行结果如下:
在这里插入图片描述

  • tf.random_uniform_initializer()、tf.uniform_unit_scaling_initializer()

生成均匀分布的随机数

tf.random_uniform_initializer(
minval=0,
maxval=None,
seed=None,
dtype=tf.float32
)

init = tf.random_uniform_initializer(minval=2,maxval=5)
x2 = tf.get_variable(name='v_x22', shape=[2,3], initializer=init)
sess.run(x2.initializer)
print(sess.run(x2))

运行结果如下:
在这里插入图片描述

  • tf.variance_scaling_initializer()

tf.variance_scaling_initializer(
scale=1.0,
mode=‘fan_in’,
distribution=‘normal’,
seed=None,
dtype=tf.float32
)

初始化参数

  • scale: Scaling factor (positive float).
  • mode: One of “fan_in”, “fan_out”, “fan_avg”.
  • distribution: Random distribution to use. One of “normal”, “uniform”.

1、当 distribution=“normal” 的时候:
生成 truncated normal distribution(截断正态分布)的随机数,其中mean = 0, stddev = sqrt(scale / n),
n 的计算与 mode 参数有关:

  • 如果mode = “fan_in”, n 为输入单元的结点数
  • 如果mode = “fan_out”,n 为输出单元的结点数
  • 如果mode = “fan_avg”,n 为输入和输出单元结点数的平均值

2、当distribution="uniform”的时候:
生成均匀分布的随机数,假设分布区间为[-limit, limit],则 limit = sqrt(3 * scale / n)

init = tf.variance_scaling_initializer()
x3 = tf.get_variable(name='v_x3', shape=[2,3], initializer=init)
sess.run(x3.initializer)
print(sess.run(x3))

运行结果如下:
在这里插入图片描述

  • tf.glorot_uniform_initializer()、tf.glorot_normal_initializer()

为了使得在经过多层网络后,信号不被过分放大或过分减弱,我们尽可能保持每个神经元的输入和输出的方差一致! 从数学角度来讲,就是让权重满足均值为 0,方差为 2 f a n i n + f a n o u t \frac{2}{fan_{in}+ fan_{out}} fanin+fanout2,随机分布的形式可以为均匀分布或者高斯分布。

又称 Xavier uniform initializer

tf.glorot_uniform_initializer(
seed=None,
dtype=tf.float32
)

It draws samples from a uniform distribution within [a=-limit, b=limit]

  • limit: sqrt(6 / (fan_in + fan_out))
  • fan_in:the number of input units in the weight tensor
  • fan_out:the number of output units in the weight tensor
  • mean = (b + a) / 2
  • stddev = (b - a)**2 /12

又称 Xavier normal initializer

tf.glorot_normal_initializer(
seed=None,
dtype=tf.float32
)

It draws samples from a truncated normal distribution centered on 0 with stddev = sqrt(2 / (fan_in + fan_out))

  • fan_in:the number of input units in the weight tensor
  • fan_out:the number of output units in the weight tensor
init = tf.glorot_uniform_initializer()
x4 = tf.get_variable(name='v_x4', shape=[2,3], initializer=init)
sess.run(x4.initializer)
print(sess.run(x4))

运行结果如下:
在这里插入图片描述

二、Tensor 的初始化及访问

1 初始化

  • Constants 初始化

Constants are initialized when you call tf.constant, and their value can never change.

  • Variables 初始化

Variables are not initialized when you call tf.Variable. To initialize all the variables in a TensorFlow program, you must explicitly call a special operation as follows:

变量使用前一定要初始化
init = tf.global_variables_initializer() # 初始化全部变量
sess.run(init)

使用变量的 initializer 属性初始化
sess.run(v.initializer)

由于tf.global_variables_initializer()是并行地初始化所有变量,所以直接使用另一个>- 在这种情况下需要使用另一个变量的initialized_value()方法。你可以直接把已初始化的值作为新变量的初始值,或者把它当做tensor计算得到一个值赋予新变量。

# Create a variable with a random value.
weights = tf.Variable(tf.random_normal([7, 2], stddev=0.35), name="weights")

# Create another variable with the same value as 'weights'.
w2 = tf.Variable(weights.initialized_value(), name="w2")

# Create another variable with twice the value of 'weights'
w_twice = tf.Variable(weights.initialized_value() * 0.2, name="w_twice")

sess.run(tf.global_variables_initializer())
print(sess.run(weights))

运行结果如下:
在这里插入图片描述

print(sess.run(w_twice))

运行结果如下:
在这里插入图片描述

print(sess.run(w2))

运行结果如下:
在这里插入图片描述

  • 改变变量的值:通过 TF 中的赋值操作,update = tf.assign(old_variable, new_value) or v.assign(new_value)
w3 = tf.Variable(weights.initialized_value()*0.5, name="w3")
update = tf.assign(weights, w3)
sess.run(tf.global_variables_initializer())
print(sess.run(w3))

运行结果如下:
在这里插入图片描述

2 Tensor访问

  • 索引
  • 一维 Tensor 的索引和 Python 列表类似(可以逆序索引(arr[ : : -1])和负索引arr[-3])
  • 二维 Tensor 的索引: arr[i, j] == arr[i][j]
  • 在多维 Tensor 中,如果省略了后面的索引,则返回的对象会是一个维度低一点的ndarray(但它含有高一级维度上的某条轴上的所有数据)
    条件索引:arr[conditon] # conditon 可以使用 & | 进行多条件组合
  • 切片
  • 一维 Tensor 的切片和 Python 列表类似
  • 二维 Tensor 的索引:arr[r1:r2, c1:c2:step] # 也可指定 step 进行切片
  • RGB 与 BGR 相互转换
  • img_bgr = tf.reverse(img_rgb, axis=[-1])
  • img_rgb = tf.reverse(img_bgr, axis=[-1])

参考资料

【1】https://tensorflow.google.cn/guide/tensors
【2】https://tensorflow.google.cn/api_docs/python/tf/dtypes/DType
【3】TensorFlow 的数据模型-----张量(Tensor): https://blog.csdn.net/mzpmzk/article/details/78636137

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在PyTorch中,可以使用以下方法来释放存储的Tensor变量: 1. 使用del关键字:使用`del`关键字可以直接删除Tensor对象并释放其存储空间。例如:`del tensor_variable` 2. 使用torch.Tensor变量的`detach()`方法:`detach()`方法返回一个与原始Tensor不再共享存储空间的新Tensor,并释放原始Tensor的存储空间。例如:`detached_tensor = tensor_variable.detach()` 3. 使用torch.Tensor变量的`zero_()`方法:`zero_()`方法会将Tensor变量中的元素全部置为0,释放存储空间并重置Tensor的形状和数据类型。例如:`tensor_variable.zero_()` 值得注意的是,通过上述方法释放存储的Tensor变量,仅会释放该Tensor对象的存储空间,而不会修改其他引用该存储的Tensor对象。如果该存储仍被其他Tensor对象引用,则仍然不能从内存中完全释放。 此外,Python的垃圾回收机制会自动处理不再被引用的Tensor对象的释放,即当所有对Tensor对象的引用都消失时,垃圾回收会自动释放该Tensor对象的内存。因此,在不再需要使用Tensor对象时,及时清除对其的引用即可释放内存。 ### 回答2: 使用PyTorch释放存储的Tensor变量非常简单。PyTorch会自动处理内存管理,但我们可以使用torch.Tensor.detach()或torch.Tensor.data属性来释放Tensor变量。 方法一:使用detach()方法 detach()方法会返回一个新的Tensor对象,但该对象不再与底层的计算图相关联。这样可以避免梯度传播到该Tensor,有效地释放存储。以下是示例代码: ``` import torch x = torch.tensor([1, 2, 3]) y = x.detach() del x # 删除变量x ``` 在上面的例子中,我们使用detach()方法将x的值赋给y,然后删除变量x。这样PyTorch会立即释放变量x的内存。 方法二:使用.data属性 .data属性提供了Tensor的底层数据,你可以将其赋值给其他变量并删除原始变量。以下是示例代码: ``` import torch x = torch.tensor([1, 2, 3]) y = x.data del x # 删除变量x ``` 在上面的例子中,我们将x的数据赋给y,然后删除变量x。这样,存储在Tensor中的数据将被释放。 无论采用哪种方法,PyTorch会在不需要时自动释放Tensor的内存。但如果你想显式地释放Tensor变量,可以使用上述方法。 ### 回答3: 在PyTorch中,释放存储的tensor变量可以通过调用`del`关键字或使用`tensor_variable.data`属性实现。 方法一:使用`del`关键字 可以使用`del`关键字来删除存储的tensor变量,并释放它所占用的内存。例如: ``` import torch # 创建一个tensor变量 x = torch.tensor([1, 2, 3]) # 使用del关键字释放存储的tensor变量 del x ``` 在这个例子中,`del x`将删除变量`x`,并释放它所占用的内存。 方法二:使用`.data`属性 PyTorch的tensor变量有一个`.data`属性,它可以返回一个共享存储的tensor副本。通过对这个副本进行操作,可以释放原始tensor变量所占用的内存。例如: ``` import torch # 创建一个存储的tensor变量 x = torch.tensor([1, 2, 3], requires_grad=True) # 使用.data属性释放存储的tensor变量 x.data = torch.tensor([0, 0, 0]) # 使用新的tensor值继续操作 y = x + 1 ``` 在上述例子中,将新的tensor值`[0, 0, 0]`赋值给`x.data`,这将释放原始tensor变量`x`所占用的内存。然后,可以通过对新的`x`进行操作,比如加1,得到结果`y`。 建议在使用完tensor变量后及时释放它们,以防止不必要的内存占用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值