mxxhcm's blog

  • 首页

  • 标签

  • 分类

  • 归档

tensorflow gather

发表于 2019-05-11 | 更新于 2019-05-12 | 分类于 tensorflow

tf.gather_nd

一句话介绍

按照索引将输入tensor的某些维度拼凑成一个新的tenosr

API

1
2
3
4
5
tf.gather_nd(
params, # 输入参数
indices, # 索引
name=None #
)

indices是一个K维的整形tensor。
indices的最后一维至多和params的rank一样大,如果indices.shape==params.rank,那么对应的是elements,如果indices.shape $\lt$ params.rank,那么对应的是slices。输出的tensor shape是:
indices.shape[:-1] + params.shape[indices.shape[-1]:]
原文如下:

The last dimension of indices corresponds to elements (if indices.shape[-1] == params.rank) or slices (if indices.shape[-1] < params.rank) along dimension indices.shape[-1] of params. The output tensor has shape
indices.shape[:-1] + params.shape[indices.shape[-1]:]

如果indices是两维的,那么就相当于用第二维的indices去访问params,然后indices的第一维度相当于把第二维的tensor放入一个列表。
indices是高维(大于两维)的话,反正就是找最后一维的维度,然后到params中找对应的数。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
    indices = [[[1]], [[0]]]
params = [[['a0', 'b0'], ['c0', 'd0']],
[['a1', 'b1'], ['c1', 'd1']]]
output = [[[['a1', 'b1'], ['c1', 'd1']]],
[[['a0', 'b0'], ['c0', 'd0']]]]
# 直接看indices的最后一维,然后到params中找,比如[1],找params[1]=[['a1', 'b1'], ['c1', 'd1']]],params[0]=[['a0', 'b0'], ['c0', 'd0']]。然后在组成output,shape怎么确定?我的理解是,直接用params[1]的结果去替换indices中的[1],也就是[[params[1]]]

indices = [[[0, 1], [1, 0]], [[0, 0], [1, 1]]]
params = [[['a0', 'b0'], ['c0', 'd0']],
[['a1', 'b1'], ['c1', 'd1']]]
output = [[['c0', 'd0'], ['a1', 'b1']],
[['a0', 'b0'], ['c1', 'd1']]]


indices = [[[0, 0, 1], [1, 0, 1]], [[0, 1, 1], [1, 1, 0]]]
params = [[['a0', 'b0'], ['c0', 'd0']],
[['a1', 'b1'], ['c1', 'd1']]]
output = [['b0', 'b1'], ['d0', 'c1']]

代码示例1

代码地址

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import tensorflow as tf
import numpy as np

sess = tf.Session()

data = np.array([[0, 1, 2, 3, 4, 5],
[6, 7, 8, 9, 10, 11],
[12, 13, 14, 15, 16, 17],
[18, 19, 20, 21, 22, 23],
[24, 25, 26, 27, 28, 29]])
data = np.reshape(np.arange(30), [5, 6])
x = tf.constant(data)
print(sess.run(x))
# [[ 0 1 2 3 4 5]
# [ 6 7 8 9 10 11]
# [12 13 14 15 16 17]
# [18 19 20 21 22 23]
# [24 25 26 27 28 29]]

# Collecting elements from a tensor of rank 2
result = tf.gather_nd(x, [1, 2])
print(sess.run(result))
# indices.shape=(2,), indices.shape[:-1]=(), indices.shape[-1]=2, params.shape=(5,6), params.shape[indices.shape[-1]:]=(), outputs.shape=()+() = ()
# 8
result = tf.gather_nd(x, [[1, 2], [2,3]])
print(sess.run(result))
# indices.shape=(2,2), indices.shape[:-1]=(2,), indices.shape[-1]=2, params.shape=(5,6), params.shape[indices.shape[-1]:]=(), outputs.shape=(2,)+() = (2,)
# [8, 15]
# Collecting rows from a tensor of rank 2
result = tf.gather_nd(x, [[1],[2]])
print(sess.run(result))
# indices.shape=(2, 1), indices.shape[:-1]=(2,), indices.shape[-1]=1, params.shape=(5,6), params.shape[indices.shape[-1]:]=(6,), outputs.shape=(2,)+(6,) = (2,6,)
# [[ 6 7 8 9 10 11]
# [12 13 14 15 16 17]]

代码示例2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import tensorflow as tf
import numpy as np

sess = tf.Session()

data = np.array([[[0, 1],
[2, 3],
[4, 5]],
[[6, 7],
[8, 9],
[10,11]]])
data = np.reshape(np.arange(12), [2, 3, 2])
x = tf.constant(data)
print(sess.run(x))
#[[[ 0 1]
# [ 2 3]
# [ 4 5]]
# [[ 6 7]
# [ 8 9]
# [10 11]]]

# Collecting elements from a tensor of rank 3
result = tf.gather_nd(x, [[0, 0, 0], [1, 2, 1]])
print(sess.run(result))
# indices.shape=(2, 3), indices.shape[:-1]=(2,), indices.shape[-1]=3, params.shape=(2, 3, 2), params.shape[indices.shape[-1]:]=(), outputs.shape=(2,)+() = (2,)
# [0 11]

# Collecting batched rows from a tensor of rank 3
result = tf.gather_nd(x, [[[0, 0], [0, 1]], [[1, 0], [1, 1]]])
print(sess.run(result))
# indices.shape=(2, 2, 2), indices.shape[:-1]=(2, 2, ), indices.shape[-1]=2, params.shape=(2, 3, 2), params.shape[indices.shape[-1]:]=(2,), outputs.shape=(2, 2)+(2, ) = (2, 2, 2)
# [[[0 1]
# [2 3]]
#
# [[6 7]
# [8 9]]]

result = tf.gather_nd(x, [[0, 0], [0, 1], [1, 0], [1, 1]])
print(sess.run(result))
# indices.shape=(4, 2), indices.shape[:-1]=(4,), indices.shape[-1]=2, params.shape=(2, 3, 2), params.shape[indices.shape[-1]:]=(2,), outputs.shape=(4,)+(2,) = (4, 2)
# [[0 1]
# [2 3]
# [6 7]
# [8 9]]

参考文献

1.https://www.tensorflow.org/api_docs/python/tf/gather_nd

tensorflow cond

发表于 2019-05-10 | 更新于 2019-05-12 | 分类于 tensorflow

tf.cond

一句话介绍

和if语句的功能和很像,如果条件为真,返回一个函数,如果条件为假,返回另一个函数。

API

1
2
3
4
5
6
7
8
9
tf.cond(
pred, # 条件
true_fn=None, # 如果条件为真,执行该函数
false_fn=None, # 如果条件为假,执行该函数
strict=False,
name=None,
fn1=None,
fn2=None
)

最后返回的是true_fn或者false_fn返回的还是tf.Tensor类型的变量。

代码示例1

代码地址

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import tensorflow as tf
import numpy as np


x = tf.placeholder(tf.int32, [10])
y = tf.constant([10, 3.2])

# for i in range(10):
# if tf.equal(x[i], 0):
# y = tf.add(y, 1)
# else:
# y = tf.add(y, 10)

# 上面的代码起到了和下面代码相同的作用,但是上面的代码在tensorflow中会报错,不能运行,因为x[i]==0返回的不是python的bool类型,而是bool类型的tf.Tensor。
# TypeError: Using a tf.Tensor as a Python bool is not allowed.

for i in range(10):
y = tf.cond(tf.equal(x[i], 0), lambda: tf.add(y, 1), lambda: tf.add(y, 10))

result = tf.log(y)

with tf.Session() as sess:
inputs = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
print(sess.run(result, feed_dict={x: inputs}))

代码示例2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
def myfunc(x):
if (x > 0):
return 1
return 0


with tf.Session() as sess:
x = tf.constant(4)
# print(myfunc(x))
# raise TypeError("Using a `tf.Tensor` as a Python `bool` is not allowed. "
# TypeError: Using a `tf.Tensor` as a Python `bool` is not allowed. Use `if t is not None:` instead of `if t:` to test if a tensor is defined, and use TensorFlow ops such as tf.cond to execute subgraphs conditioned on the value of a tensor.
result = tf.cond(tf.greater(x, 0), lambda: 1, lambda: 0)
print(type(result))
print(result.eval())

上述代码中定义了一个函数,实现判断某个值是否大于0。但是这个函数是错误的,因为$x\gt 0$返回一个bool类型的tf.Tensor不能用作if的判断条件,所以需要使用tf.cond语句。

代码示例3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# Example 3
x = tf.constant(4)
y = tf.constant(4)

with tf.Session() as sess:
print(x)
print(y)
if x == y:
print(True)
else:
print(False)
result = tf.equal(x, y)
print(result.eval())
def f1():
print("f1 declare")
return [1, 1]
def f2():
print("f2 declare")
return [0, 0]
res = tf.cond(tf.equal(x, y), f1, f2)
print(res)

参考文献

1.https://www.tensorflow.org/api_docs/python/tf/cond
2.https://stackoverflow.com/questions/48571521/tensorflow-error-using-a-tf-tensor-as-a-python-bool-is-not-allowed
3.https://blog.csdn.net/Cerisier/article/details/79819248

python cv2.imresize图像缩放

发表于 2019-05-09 | 更新于 2019-05-10 | 分类于 python

cv2.resize

cv2是python的opencv包,实现的功能是对一个图片进行缩放。
python3下安装命令:
~$:pip install opencv-python

示例代码

1
2
3
4
5
6
import cv2
import numpy as np
img = np.random.rand(210, 160 ,3)
print(img.shape)
img_scale = cv2.resize(img, (84, 84))
print(img_scale.shape)

参考文献

tensorflow model save load

发表于 2019-05-09 | 更新于 2019-12-17 | 分类于 tensorflow

tf.train.Saver保存和恢复模型

1
2
saver = tf.train.Saver()
saver.save()

调用上述代码之后会存存储以下几个文件:

1
2
3
4
checkpoint
model.ckpt.data-00000-of-00001
model.ckpt.index
model.ckpt.meta

其中checkpoint文件存储的是最近保存的文件的名字,meta文件存放的是计算图的定义,index和data文件存放的是权重文件。

下面介绍一下上述代码中出现的两个API,tf.train.Saver()和tf.train.Saver().save()。

tf.train.Saver()

Saver是类,不是函数。可以用来保存,恢复variable和model,Saver对象提供save()和restore()等函数,save()保存模型,restore()加载模型。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
__init__(
var_list=None, # 指定要保存的variablelist
reshape=False,
sharded=False,
max_to_keep=5, # 最多保留最近的几个checkpoints
keep_checkpoint_every_n_hours=10000.0,
name=None,
restore_sequentially=False,
saver_def=None,
builder=None,
defer_build=False,
allow_empty=False,
write_version=tf.train.SaverDef.V2,
pad_step_number=False,
save_relative_paths=False,
filename=None
)

tf.train.Saver.save()

1
2
3
4
5
6
7
8
9
10
11
save(
sess, \\传入当前要保存的session
save_path, \\指定checkpoint的路径
global_step=None, \\当前存的model的step
latest_filename=None,
meta_graph_suffix='meta',
write_meta_graph=True, \\指定是否要保存计算图
write_state=True,
strip_default_attrs=False,
save_debug_info=False
)

这里说一下save_path,如果不指定的话,文件名默认是空的,在linux下是以.开头的(即当前目录),所以会显示成隐藏文件。通常情况下我们指定checkpoint要保存的路径,以及名字,比如叫model.ckpt,在load的时候还使用这个名字就行。指定了global_step之后,tf会自动在路径后面加上step进行区分。

读取graph

读取图的定义

meta文件中存放了计算图的定义,可以直接使用API tf.train.import_meta_graph()函数调用:

1
2
3
import tensorflow as tf
with tf.Session() as sess:
saver = tf.train.import_meta_graph("model.ckpt.meta")

这时计算图就已经定义在当前sess中了。上述代码会保留原始的device信息,如果迁移到其他设备时,可能由于没有指定设备出错,这个问题可以通过指定一个特殊的参数clear_devices解决:

1
2
3
import tensorflow as tf
with tf.Session() as sess:
saver = tf.train.import_meta_graph("model.ckpt.meta", clear_devices=True)

这样子就和device无关了。

访问graph中的参数

通过collection访问计算图中collection的键

这里的键指的是graph中都有哪些collections。

  • 1
    print(sess.graph.get_all_collection_keys())
  • 1
    print(sess.graph.collections)
  • 1
    tf.get_default_graph().get_all_collection_keys()

访问collection

  • 1
    sess.graph.get_collection("summaries")
  • 1
    tf.get_collection("")

示例

1
2
3
4
5
6
7
8
import tensorflow as tf

#saver = tf.train.Saver()
with tf.Session() as sess:
new_saver = tf.train.import_meta_graph("saver1.ckpt.meta")
print(sess.graph)
for var in tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES):
print(var)

通过operation访问

  • 1
    sess.graph.get_opeartions()
  • 1
    2
    for op in sess.graph.get_opeartions():
    print(op.name, op.values())
  • 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    sess.graph.get_operation_by_name("op_name").node_def
    ```

    ## 保存和恢复variables
    ### 保存和恢复全部variables
    - 恢复variable时,无需初始化。
    - 恢复variable时,使用的是variable的name,不是op的name。只要知道variable的name即可。save和restore的op name不需要相同,只要variable name相同即可。
    - 对于使用tf.Variable()创建的variable,如果没有指定variable名字的话,系统会为其生成默认名字,在恢复的时候,需要使用tf.get_variable()恢复variable,同时传variable name和shape。

    #### 保存全部variables
    ``` python
    saver = tf.train.Saver()
    saver.save(sess, save_path) # 需要指定的是checkpoint的名字而不是目录

恢复全部variables

1
2
saver = tf.train.Saver()
saver.restore(sess, save_path)

保存和恢复部分variables

保存全部variable

1
2
saver = tf.train.Saver({"variable_name1": op_name1,..., "variable_namen": op_namen})
saver.save(sess, save_path) # 需要指定的是checkpoint的名字而不是目录

恢复全部variable

1
2
saver = tf.train.Saver({"variable_name1": op_name1,..., "variable_namen": op_namen})
saver.restore(sess, save_path)

保存和恢复模型

其实和保存恢复变量没有什么区别。只是把整个模型的variables都save和restore了。

代码示例

代码地址

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import tensorflow as tf

graph = tf.Graph()
with graph.as_default():
W = tf.Variable([0.3], dtype=tf.float32)
b = tf.Variable([-0.3], dtype=tf.float32)

# input and output
x = tf.placeholder(tf.float32)
y = tf.placeholder(tf.float32)
predicted_y = W*x+b

# MSE loss
loss = tf.reduce_mean(tf.square(y - predicted_y))
# optimizer
optimizer = tf.train.GradientDescentOptimizer(0.01)
train_op = optimizer.minimize(loss)

inputs = [1, 2, 3, 4]
outputs = [2, 3, 4, 5]

with tf.Session(graph=graph) as sess:
saver = tf.train.Saver()
init_op = tf.global_variables_initializer()
sess.run(init_op)
for i in range(5000):
sess.run(train_op, feed_dict={x: inputs, y: outputs})
l_, W_, b_ = sess.run([loss, W, b], feed_dict={x: inputs, y: outputs})
print("loss: ", l_, "w: ", W_, "b:", b_)
checkpoint = "./checkpoint/saver1.ckpt"
save_path = saver.save(sess, checkpoint)
print("Model has been saved in %s." % save_path)

with tf.Session(graph=graph) as sess:
saver = tf.train.Saver()
saver.restore(sess, checkpoint)
l_, W_, b_ = sess.run([loss, W, b], feed_dict={x: inputs, y: outputs})
print("loss: ", l_, "w: ", W_, "b:", b_)
print("Model has been restored.")

获取最新的checkpoint文件

tf.train.get_checkpoint_state()

给出checkpoint文件所在目录,可以使用get_checkpoint_state()获得最新的checkpoint文件:

1
2
3
ckpt = tf.train.get_checkpoint_state(checkpoint_dir)
if ckpt and ckpt.model_checkpoint_path:
save.restore(sess, ckpt.model_checkpoint_path)

使用inspect_checkpoint库

1
2
3
4
5
6
7
8
9
10
# import the inspect_checkpoint library
from tensorflow.python.tools import inspect_checkpoint as chkp

# 打印checkpoint文件中所有variable
chkp.print_tensors_in_checkpoint_file("saver/variables/all_variables.ckpt", tensor_name='', all_tensors=True)

# 打印变量"v1"
chkp.print_tensors_in_checkpoint_file("saver/variables/all_variables.ckpt", tensor_name='v1', all_tensors=False)

chkp.print_tensors_in_checkpoint_file("saver/variables/all_variables.ckpt", tensor_name='v2', all_tensors=False)

模型的冻结

模型的冻结是不在训练模型,只用于正向推导,所以把变量转换成常量后,和计算图一起保存在协议缓冲区文件(.pb)文件中,因此需要在计算图中预先定义输出节点的名称,示例如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import tensorflow as tf

output_nodes = ["Accuracy/prediction", "Metric/Dice"]

# 加载计算图
saver = tf.train.import_meta_graph("model.ckpt.meta", clear_devices=True)

with tf.Session() as sess:
input_graph_def = sess.graph.as_graph_def()
# load model
saver.restore(sess, "model.ckpt")
# 将变量转换为常量
output_graph_def = tf.graph_util.convert_variables_to_constants(sess, input_graph_def, output_nodes)
# 写入pb文件
with open("frozen_model.pb", "wb") as f:
f.write(output_graph_def.SerializeToString())

模型的执行

从协议缓冲区文件(.pb)文件中读取模型,导入计算图

1
2
3
4
5
6
7
8
# 读取模型并保存到序列化模型对象中
with open(frozen_graph_path, "rb") as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
# 导入计算图
graph = tf.Graph()
with graph.as_default():
tf.import_graph_def(graph_def, name="Test")

获取输入和输出的张量,然后将测试数据feed给输入张量,得到结果。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
x_tensor = graph.get_tensor_by_name("Test/input/image-input:0")
y_tensor = graph.get_tensor_by_name("Test/input/label-input:0")
keep_prob = graph.get_tensor_by_name("Test/dropout/Placeholder:0")
acc_op = graph.get_tensor_by_name("Test/accuracy/prediction:0")

from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("mnist_data", one_hot=True)
x_values, y_values = mnist.test.next_batch(10000)

with tf.Session(graph=graph) as sess:
accuracy = sess.run(acc_op, feed_dict={x_tensor: x_values,
y_tensor: y_values,
keep_prob: 1.0})
print(accuracy)

参考文献

1.https://www.jarvis73.cn/2018/04/25/Tensorflow-Model-Save-Read/
2.https://www.tensorflow.org/guide/saved_model
3.https://www.tensorflow.org/api_docs/python/tf/train/Saver
4.https://www.bilibili.com/read/cv681031/
5.https://cv-tricks.com/tensorflow-tutorial/save-restore-tensorflow-models-quick-complete-tutorial/

tensorflow reduction

发表于 2019-05-09 | 更新于 2019-05-12 | 分类于 tensorflow

tf.Reduction

  • tf.reduce_sum(input_tensor, reduction_indices=None, keep_dims=False, name=None) # 计算input_tensor的和,可指定dim。
  • tf.reduce_mean(input_tensor, reduction_indices=None, keep_dims=False, name=None) # 计算input_tensor的均值,可指定dim。
  • tf.reduce_min(input_tensor, reduction_indices=None, keep_dims=False, name=None) # 计算input_tensor的最小值,可指定dim。
  • tf.reduce_max(input_tensor, reduction_indices=None, keep_dims=False, name=None) # 计算input_tensor的最大值,可指定dim。
  • tf.recude_proc(input_tensor, reduction_indices=None, keep_dims=False, name=None) # 计算input_tensor的乘积,可指定dim。
  • tf.reduce_all(input_tensor, reduction_indices=None, keep_dims=False, name=None) # 计算input_tensor中所有元素的逻辑与,可指定dim。
  • tf.reduce_any(input_tensor, reduction_indices=None, keep_dims=False, name=None) # 计算input_tensor的所有元素的逻辑或,可指定dim。
  • tf.accumulate_n(inputs, shape=None, tensor_dtype=None, name=None) # 计算inputs的和。
  • tf.cumsum(x, axis=0, exclusive=False, reverse=False, name=None) # 计算input_tensor的累积和。

代码示例

tf.reduce_sum()

代码地址

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import tensorflow as tf
import numpy as np


x = tf.placeholder(dtype=tf.float32, shape=[None, 2])
y = tf.log(x)
# 对所有y求和
loss = tf.reduce_sum(y)

with tf.Session() as sess :
# inputs = tf.constant([1.0, 2.0])
inputs = np.array([[1.0, 2.0], [3, 4]])
l = sess.run(loss, feed_dict={x: inputs})
print(l)

参考文献

pytorch

发表于 2019-05-08 | 更新于 2019-05-17 | 分类于 pytorch

torch

torch提供了很多基础操作,包括数学操作等等。

torch.cat

函数原型

将多个tensor在某一个维度上(默认是第0维)拼接到一起(除了拼接的维度上,其他维度的shape必须一定),最后返回一个tensor。
torch.cat(tensors, dim=0, out=None) → Tensor

Concatenates the given sequence of seq tensors in the given dimension. All tensors must either have the same shape (except in the concatenating dimension) or be empty.

参数

tensors (sequence of Tensors) – 任意类型相同python序列或者tensor
dim (int, optional) - 在第几个维度上进行拼接(只有在拼接的维度上可以不同,其余维度必须相同。
out (Tensor, optional) – 输出的tensor

例子

1
2
3
4
5
6
import torch

x1 = torch.randn(3, 4, 4)
x2 = torch.randn(3, 1, 4)
x = torch.cat([x1, x2], 1)
print(x.size())

输出如下:

torch.Size([3, 5, 4])

torch中图像(img)格式

torch中图像的shape是(‘RGB’,width, height),而numpy和matplotlib中都是(width, height, ‘RGB’)
matplotlib.pyplot.imshow()需要的参数是图像矩阵,如果矩阵中是整数,那么它的值需要在区间[0,255]之内,如果是浮点数,需要在[0,1]之间。

Clipping input data to the valid range for imshow with RGB data ([0…1] for floats or [0…255] for integers).

参考文献

1.https://pytorch.org/docs/stable/torch.html

pytorch initialize parameters

发表于 2019-05-08 | 更新于 2019-05-17 | 分类于 pytorch

神经网络参数初始化

方法$1$.Model.apply(fn)

示例如下

1
2
3
4
5
6
7
8
9
import torch.nn as nn
def init_weights(m):
print(m)
if type(m) == nn.Linear:
m.weight.data.fill_(1.0)
print(m.weight)

net = nn.Sequential(nn.Linear(2, 2), nn.Linear(2, 2))
net.apply(init_weights)

输出结果如下:

Linear(in_features=2, out_features=2, bias=True)
Parameter containing:
tensor([[1., 1.],
[1., 1.]], requires_grad=True)
Linear(in_features=2, out_features=2, bias=True)
Parameter containing:
tensor([[1., 1.],
[1., 1.]], requires_grad=True)
Sequential(
(0): Linear(in_features=2, out_features=2, bias=True)
(1): Linear(in_features=2, out_features=2, bias=True)
)
Linear(in_features=2, out_features=2, bias=True)
Linear(in_features=2, out_features=2, bias=True)

其中最后两行为net对象调用self.children()函数返回的模块,就是模型中所有网络的参数。事实上,调用net.apply(fn)函数,会对self.children()中的所有模块应用fn函数,

参考文献

pytorch optim

发表于 2019-05-08 | 更新于 2019-05-17 | 分类于 pytorch

torch.optim

基类class Optimizer(object)

Optimizer是所有optimizer的基类。
调用任何优化器都要先初始化Optimizer类,这里拿Adam优化器举例子。Adam optimizer的init函数如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class Adam(Optimizer):
"""
params (iterable): iterable of parameters to optimize or dicts defining
parameter groups
lr (float, optional): learning rate (default: 1e-3)
betas (Tuple[float, float], optional): coefficients used for computing
running averages of gradient and its square (default: (0.9, 0.999))
eps (float, optional): term added to the denominator to improve
numerical stability (default: 1e-8)
weight_decay (float, optional): weight decay (L2 penalty)
amsgrad (boolean, optional): whether to use the AMSGrad variant of this

"""

def __init__(self, params, lr=1e-3, betas=(0.9, 0.999), eps=1e-8,
weight_decay=0, amsgrad=False):
if not 0.0 <= lr:
raise ValueError("Invalid learning rate: {}".format(lr))
if not 0.0 <= eps:
raise ValueError("Invalid epsilon value: {}".format(eps))
if not 0.0 <= betas[0] < 1.0:
raise ValueError("Invalid beta parameter at index 0: {}".format(betas[0]))
if not 0.0 <= betas[1] < 1.0:
raise ValueError("Invalid beta parameter at index 1: {}".format(betas[1]))
defaults = dict(lr=lr, betas=betas, eps=eps,
weight_decay=weight_decay, amsgrad=amsgrad)
super(Adam, self).__init__(params, defaults)

上述代码将学习率lr,beta,epsilon,weight_decay,amsgrad等封装在一个dict中,然后将其传给Optimizer的init函数,其代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class Optimizer(object):
.. warning::
Parameters need to be specified as collections that have a deterministic
ordering that is consistent between runs. Examples of objects that don't
satisfy those properties are sets and iterators over values of dictionaries.

Arguments:
params (iterable): an iterable of :class:`torch.Tensor` s or
:class:`dict` s. Specifies what Tensors should be optimized.
defaults: (dict): a dict containing default values of optimization
options (used when a parameter group doesn't specify them).
"""

def __init__(self, params, defaults):
self.defaults = defaults

if isinstance(params, torch.Tensor):
raise TypeError("params argument given to the optimizer should be "
"an iterable of Tensors or dicts, but got " +
torch.typename(params))

self.state = defaultdict(dict)
self.param_groups = []

param_groups = list(params)
if len(param_groups) == 0:
raise ValueError("optimizer got an empty parameter list")
if not isinstance(param_groups[0], dict):
param_groups = [{'params': param_groups}]

for param_group in param_groups:
self.add_param_group(param_group)

从这里可以看出来,每个pytorch给出的optimizer至少有以下三个属性和四个函数:
属性:

  • self.defaults # 字典类型,主要包含学习率等值
  • self.state # defaultdict(<class ‘dict’>, {}) state存放的是
  • self.param_gropus # <class ‘list’>:[],prama_groups是一个字典类型的列表,用来存放parameters。

函数:

  • self.zero_grad() # 将optimizer中参数的梯度置零
  • self.step() # 将梯度应用在参数上
  • self.state_dict() # 返回optimizer的state,包括state和param_groups。
  • self.load_state_dict() # 加载optimizer的state。
  • self.add_param_group() # 将一个param group添加到param_groups。可以用在fine-tune上,只添加我们需要训练的层数,然后其他层不动。

如果param已经是一个字典列表的话,就无需操作,否则就需要把param转化成一个字典param_groups。然后对param_groups中的每一个param_group调用add_param_group(param_group)函数将param_group字典和defaults字典拼接成一个新的param_group字典添加到self.param_groups中。

参考文献

1.https://pytorch.org/docs/stable/optim.html

pytorch utils data

发表于 2019-05-08 | 更新于 2019-05-17 | 分类于 pytorch

torch.utils.data

Dataloader

原型

1
2
3
4
5
6
7
8
9
10
11
12
13
class torch.utils.data.DataLoader(
dataset, # 从哪加载数据
batch_size=1, # batch大小 (default: 1).
shuffle=False, # 每个epoch的数据是否打乱 (default: False).
sampler=None, # 定义采样策略。如果指定这个参数, shuffle必须是False.
batch_sampler=None,
num_workers=0, # 多少个子进程用来进行数据加载。0代表使用主进程加载数据 (default: 0)
collate_fn=<function default_collate>,
pin_memory=False,
drop_last=False,
timeout=0,
worker_init_fn=None
)

例子

1
2
3
4
import torch
import torchvision
dataset = torchvision.datasets.CIFAR100(root='./data', train=True, download=True, transform=None)
train_loader = torch.utils.data.DataLoader(dataset, bath_size=16, shuffle=False, num_worker=2)

如何访问DataLoader返回值

train_loader不是整数,所以不能用range,这里用enumerate(),i是

1
2
for i, data in enumerate(train_loader):
images, labels = data

pytorch torchvision

发表于 2019-05-08 | 更新于 2019-05-17 | 分类于 pytorch

torchvision

torchvision是pytorch提供的一些工具包,主要包含下列几个模块

  • torchvision.datasets
  • torchvision.utils
  • torchvision.transforms
  • torchvision.models

torchvision.datasets

torchvision提供了很多数据集

1
2
import torchvision
print(torchvision.datasets.__all__)

(‘LSUN’, ‘LSUNClass’, ‘ImageFolder’, ‘DatasetFolder’, ‘FakeData’, ‘CocoCaptions’, ‘CocoDetection’, ‘CIFAR10’, ‘CIFAR100’, ‘EMNIST’, ‘FashionMNIST’, ‘MNIST’, ‘STL10’, ‘SVHN’, ‘PhotoTour’, ‘SEMEION’, ‘Omniglot’)

CIFAR10

原型

1
calss torchvision.datasets.CIFAR10(root, train=True, transform=None, target_transform=None, download=False)

参数

root (string) – cifar-10-batches-py的存放目录或者download设置为True时将会存放的目录。
train (bool, optional) – 设置为True的时候, 从training set创建dataset, 否则从test set创建dataset.
transform (callable, optional) – 输入是一个 PIL image,返回一个transformed的版本。如,transforms.RandomCrop
target_transform (callable, optional) – A function/transform that takes in the target and transforms it.
download (bool, optional) – If true, downloads the dataset from the internet and puts it in root directory. If dataset is already downloaded, it is not downloaded again.

例子

1
2
3
import torchvision
trainset = torchvision.datasets.CIFAR100(root="./datasets", train=True, transform= None, download=True)
testset = torchvision.datasets.CIFAR100(root="./datasets", train=False, transform= None, download=True)

torchvision.models

模型

torchvision.transforms

transform

torchvision.utils

一些工具包

参考文献

1.https://pytorch.org/docs/stable/torchvision/index.html

1…222324…34
马晓鑫爱马荟荟

马晓鑫爱马荟荟

记录硕士三年自己的积累

337 日志
26 分类
77 标签
RSS
GitHub E-Mail
© 2022 马晓鑫爱马荟荟
由 Hexo 强力驱动 v3.8.0
|
主题 – NexT.Pisces v6.6.0