首页 > 其他分享 >model(**inputs)括号里边是什么ne?

model(**inputs)括号里边是什么ne?

时间:2022-11-23 10:58:30浏览次数:40  
标签:inputs labels ne ids batch print model type

	inputs = {"input_ids": batch[0], "attention_mask": batch[1], "labels": batch[3]}
            if args.model_type != "distilbert":
                # XLM and RoBERTa don"t use segment_ids
                inputs["token_type_ids"] = (batch[2] if args.model_type in ["bert", "xlnet"] else None)
            outputs = model(**inputs)

outputs = model(inputs)为啥这样写,这是啥,是指针?
Python中的list(列表)和dict(字典)变量前面加星号*的作用
列表
作用1:将列表解开成几个独立的参数,传入函数。类似的运算符还有两个星号(
),是将字典解开成独立的元素作为形参。
我当时学过列表加*

作用:作用是:将列表解开成几个独立的参数,传入函数。

list变量前加一个星号*,目的是将该list变量拆解开多个独立的参数,传入函数中
举例:

list1 = [1, 2, 3] 
print(*list1)
输出结果:
1 2 3

输出结果为三个元素,可以作为参数传入某个函数中

作用2:*号也可以作用于二维的列表

作用于二维列表

array = [[1, 2, 3],
         [4, 5, 6]
         ]
print(array)
print(*array)
print(type(array))
print(type(*array))
输出:
[[1, 2, 3], [4, 5, 6]]
[1, 2, 3] [4, 5, 6]
<class 'list'>
print(type(*array))
TypeError: type() takes 1 or 3 arguments

字典
作用:字典前面加两个星号,Python中,*会把接收到的参数形成一个元组,**会把接收到的参数存入一个字典。

def print_1(input_ids, attention_mask, token_type_ids, intent_label_ids, slot_labels_ids):
    print("input_ids:",input_ids)
    print("attention_mask:", attention_mask)
    print("token_type_ids:", token_type_ids)
    print("intent_label_ids:", intent_label_ids)
    print("slot_labels_ids:", slot_labels_ids)
if __name__ == '__main__':
    batch = [ [ [11],
                [22]],
              [ [33],
                [44]],
              [ [55],
                [66]],
              [ [77],
                [88]],
              [ [99],
                [00]]       ]
    inputs = {'input_ids': batch[0],  # 第一行
              'attention_mask': batch[1],  # 第二行
              'token_type_ids': batch[2],
              'intent_label_ids': batch[3],
              'slot_labels_ids': batch[4]   }
    print_1(**inputs)
    print(inputs)

** 的作用是把字典 inputs 变成关键字参数传递
结果如下图所示。

input_ids: [[11], [22]]
attention_mask: [[33], [44]]
token_type_ids: [[55], [66]]
intent_label_ids: [[77], [88]]
slot_labels_ids: [[99], [0]]
{'input_ids': [[11], [22]], 
'attention_mask': [[33], [44]], 
'token_type_ids': [[55], [66]], 
'intent_label_ids': [[77], [88]], 
'slot_labels_ids': [[99], [0]]}

标签:inputs,labels,ne,ids,batch,print,model,type
From: https://www.cnblogs.com/chaofengya/p/16917582.html

相关文章