if __name__=='__main__':, argparse.ArgumentParser(), set

1. collections.OrderedDict() in
python Dictionary objects in python can access data in the form of "key: value". OrderedDict is a subclass of it that implements sorting of elements in dictionary objects.
Note that the Keys of OrderedDict will be arranged in the order of insertion, not the Key itself:

import collections 
print 'Regular dictionary:'
d1={
    
    }
d1['a']='A'
d1['b']='B'
d1['c']='C'
 
d2={
    
    }
d2['c']='C'
d2['a']='A'
d2['b']='B'
 
print d1==d2  #Ture
print '\nOrderedDict:'
d1=collections.OrderedDict()
d1['a']='A'
d1['b']='B'
d1['c']='C'
 
d2=collections.OrderedDict()
d2['c']='C'
d2['a']='A'
d2['b']='B'
 
print  d1==d2 #False

2.torch.autograd.grad

torch.autograd.grad(
outputs,   # 计算图的数据结果张量--它就是需要进行求导的函数
inputs,   # 需要对计算图求导的张量--它是进行求导的变量
grad_outputs=None,   # 如果outputs不是标量,需要使用此参数
retain_graph=None,   # 保留计算图
create_graph=None,   # 创建计算图
)

3. Run the python program.
Run the source file in the command line tool or Terminal.
Enter the command line tool or Terminal, switch to the directory where demo.txt is located, and then enter the following command to run the source file:
python demo.py

Understanding and summary of "if name ==' main ':" in Python
and directly running python file
1: python is directly followed by the file name. At this time, the python program is run as a script. This is how it works whether you execute a command like ""python file.py"" in cmd or click run in the IDE. At this time, the built-in __name__ attribute is __main__.

2: Import with import. At this time, the python program is imported as a module.
4.argparse.ArgumentParser()

add_argument() method

ArgumentParser.add_argument(name or flags...[, action][, nargs][, const][, default][, type][, choices][, required][, help][, metavar][, dest])

name or flags - a name or a list of option strings, such as foo or -f, –foo .
action - the action base type used when the argument appears on the command line.
nargs - The number of command line arguments that should be consumed.
const - a constant required by some actions and nargs selections.
default - the value used when the parameter does not appear on the command line .
type - The type that the command line argument should be converted to .
choices - a container of available parameters.
required - whether this command line option can be omitted (only options are available).
help - a brief description of what this option does.
metavar - Example of parameter values ​​used in usage messages.
dest - the attribute name to be added to the object returned by parse_args().

example:

import argparse
parser = argparse.ArgumentParser()#创建解析器
#添加参数
parser.add_argument('--dataset_dir', default='/Datasets/miniImageNet--ravi', help='/miniImageNet')
parser.add_argument('--data_name', default='miniImageNet', help='miniImageNet|StanfordDog|StanfordCar|CubBird')
parser.add_argument('--mode', default='test', help='train|val|test')
parser.add_argument('--outf', default='./results/DN4')
parser.add_argument('--resume', default=model_trained, type=str, help='path to the lastest checkpoint (default: none)')
parser.add_argument('--basemodel', default='Conv64F', help='Conv64F|ResNet256F')
parser.add_argument('--workers', type=int, default=8)
parser.add_argument('--print_freq', '-p', default=100, type=int, metavar='N', help='print frequency (default: 100)')

Usage:
From the command line:

python DN4_Train_5way1shot.py --dataset_dir ./datasets/miniImageNet --data_name miniImageNet

5. Collection

>>> filelists = {
    
    'base':{
    
    },'val':{
    
    },'novel':{
    
    } }
>>> filelists
{
    
    'base': {
    
    }, 'val': {
    
    }, 'novel': {
    
    }}
>>> filelists[base]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'base' is not defined
>>> filelists['base']
{
    
    }
>>> filelists['base']='data'
>>> filelists['base']
'data'
>>> filelists['base']='data','aa'
>>> filelists['base']
('data', 'aa')
>>> filelists['base']={
    
    'data','aa'}
>>> filelists['base']
{
    
    'aa', 'data'}
>>> filelists['base']={
    
    'data':'data','aa':'aaa'}
>>> filelists['base']
{
    
    'data': 'data', 'aa': 'aaa'}
>>> filelists['base'][aa]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'aa' is not defined
>>> filelists['base']['aa']
'aaa'
>>>

Collections have no duplication, but
tuples can have duplication.

['a', 'b', 'c', 'd', 'e']
>>> t=('a','b','ab','a','ab')
>>> t
('a', 'b', 'ab', 'a', 'ab')
>>> s={
    
    'a','b','a','b'}
>>> s
{
    
    'a', 'b'}

Guess you like

Origin blog.csdn.net/weixin_44040169/article/details/128430216