🤾
MLStudy
  • README
  • Linux
    • command
    • Basic_commands
    • Advanced_commands
    • Linuxvirtual_machine_operate
    • virtual machine
  • Python
    • Deploy_Issue
    • Model_Analysis
    • Model_concept
    • Model_Grammar
    • Python_print_format
    • Model_ComFuc
    • Deep_learning_know
  • Theoretical knowledge
    • MLE_MAP_Bayesian
    • TV_denoise
  • Research
    • Writing
    • How to read
  • To_be_classified
    • tqdm
    • fast_visit_github
    • windows_issue
    • Zotero
    • data_struct
    • dataset
      • chapter_1
      • chapter_2
      • chapter_3
      • chapter_4
      • chapter_5
      • chapter_6
      • chapter_7
Powered by GitBook
On this page
  • python List拷贝复制
  • 直接赋值:
  • 浅拷贝:
  • 深拷贝:
  • reshape中的-1
  1. Python

Model_Grammar

python List拷贝复制

直接赋值:

如果用 = 直接赋值,是非拷贝方法。

这两个列表是等价的,修改其中任何一个列表都会影响到另一个列表。

old = [1,[1,2,3],3]
new = []
for i in range(len(old)):
    new.append(old[i])

new[0] = 3
new[1][0] = 3

'''
-----------------------
Before:
[1, [1, 2, 3], 3]
[1, [1, 2, 3], 3]
After:
[3, [3, 2, 3], 3]
[3, [3, 2, 3], 3]
-----------------------
'''

浅拷贝:

1.copy()方法

对于List来说,其第一层,是实现了深拷贝,但对于其内嵌套的List,仍然是浅拷贝。

因为嵌套的List保存的是地址,复制过去的时候是把地址复制过去了,嵌套的List在内存中指向的还是同一个。

old = [1,[1,2,3],3]
new = old.copy()

new[0] = 3
new[1][0] =3

'''
---------------------
Before:
[1, [1, 2, 3], 3]
[1, [1, 2, 3], 3]
After:
[1, [3, 2, 3], 3]
[3, [3, 2, 3], 3]
---------------------
'''

2.使用列表生成式

使用列表生成式产生新列表也是一个浅拷贝方法,只对第一层实现深拷贝。

old = [1,[1,2,3],3]
new = [i for i in old]

new[0] = 3
new[1][0] = 3

'''
----------------------
Before
[1, [1, 2, 3], 3]
[1, [1, 2, 3], 3]
After
[1, [3, 2, 3], 3]
[3, [3, 2, 3], 3]
----------------------
'''

3.for循环遍历

通过for循环遍历,将元素一个个添加到新列表中。这也是一个浅拷贝方法,只对第一层实现深拷贝。

old = [1,[1,2,3],3]
new = []
for i in range(len(old)):
    new.append(old[i])

new[0] = 3
new[1][0] = 3

'''
-----------------------
Before:
[1, [1, 2, 3], 3]
[1, [1, 2, 3], 3]
After:
[1, [3, 2, 3], 3]
[3, [3, 2, 3], 3]
-----------------------
'''

4.使用切片

通过使用 [ : ] 切片,可以浅拷贝整个列表,同样的,只对第一层实现深拷贝。

old = [1,[1,2,3],3]
new = old[:]

new[0] = 3
new[1][0] = 3

'''
------------------
Before:
[1, [1, 2, 3], 3]
[1, [1, 2, 3], 3]
After:
[1, [3, 2, 3], 3]
[3, [3, 2, 3], 3]
------------------
'''

深拷贝:

如果用deepcopy()方法,则无论多少层,无论怎样的形式,得到的新列表都是和原来无关的,这是最安全最清爽最有效的方法。

需要import copy

import copy
old = [1,[1,2,3],3]
new = copy.deepcopy(old)

new[0] = 3
new[1][0] = 3

'''
-----------------------
Before:
[1, [1, 2, 3], 3]
[1, [1, 2, 3], 3]
After:
[1, [1, 2, 3], 3]
[3, [3, 2, 3], 3]
-----------------------
'''

reshape中的-1

在深度学习对模型代码的脉络整理中,输入输出的维度变化是一个最重要的线索,其中tensorflow中的api——reshape扮演了重要的角色。reshape中有个参数-1比较难以理解,这篇文章主要讲解其用法。

官方文档中这样解释它:

If one component ofshapeis the special value -1, the size of that dimension is computed so that the total size remains constant. In particular, ashapeof[-1]flattens into 1-D. At most one component ofshapecan be -1

中文释意为:如果shape参数中包含特殊值-1,维度的尺寸在数组总大小保持不变的基础上进行计算。特殊的,如果shape参数为【-1】,即将该数组打平。

打平很好理解,即无论该数组形状是什么样的,统一变为一维。打平顺序默认为先行后列。

如果shape参数中包含特殊值-1,维度的尺寸在数组总大小保持不变的基础上进行计算

这句如何理解呢?举例来看吧。

import tensorflow as tf
#(3,2,3)
t1 = [[[1, 1, 1],
       [2, 2, 2]],
      [[3, 3, 3],
       [4, 4, 4]],
      [[5, 5, 5],
       [6, 6, 6]]]

t1 = tf.constant(t1)
sess = tf.Session()
result = sess.run(tf.reshape(t1, [2,-1]))
print(result)

最后输出结果为:

[[1 1 1 2 2 2 3 3 3]
 [4 4 4 5 5 5 6 6 6]]

即,从维度(3,2,3)变为(2,9)。

这个变化按照什么规则呢?

首先3*2*3=18,数组总大小为18。shape参数【2,-1】,这个参数意义为reshape后的数组为二维数组,其中第一维度值为2。为了保持总大小不变,18除以2为9。所以变化后的数组形状为(2,9)

看了很多博客,没有领会意思。后来发现官方文档写的很清楚。一定要养成不会先看官方文档,再结合各种博客理解的方法论。

PreviousModel_conceptNextPython_print_format

Last updated 2 years ago

本文转自 ,如有侵权,请联系删除。

本文转自 ,如有侵权,请联系删除。

https://blog.csdn.net/qq_24502469/article/details/104185122
https://zhuanlan.zhihu.com/p/343541487