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

本文详细解释了Python中**操作符在列表和字典中的作用。列表前的*用于解压列表元素作为单独参数传入函数,而字典前的**则将字典转换为关键字参数。通过示例展示了如何在函数调用中使用这些操作符,以及它们在处理多维列表和字典时的行为。理解这一操作对于编写高效的Python代码至关重要。
摘要由CSDN通过智能技术生成
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]]}

 

def network_model(inputs,num_pitch,weights_file=None):#输入,音符的数量,训练后的参数文件 #测试时要指定weights_file #建立模子 model=tf.keras.Sequential() #第一层 model.add(tf.keras.layers.LSTM( 512,#LSTM层神经元的数目是512,也是LSTM层输出的维度 input_shape=(inputs.shape[1],inputs.shape[2]),#输入的形状,对于第一个LSTM必须设置 return_sequences=True#返回控制类型,此时是返回所有的输出序列 #True表示返回所有的输出序列 #False表示返回输出序列的最后一个输出 #在堆叠的LSTM层时必须设置,最后一层LSTM不用设置,默认值为False )) #第二层和第三层 model.add(tf.keras.layers.Dropout(0.75))#丢弃30%神经元,防止过拟合 model.add(tf.keras.layers.LSTM(512,return_sequences=True)) model.add(tf.keras.layers.Dropout(0.75))#丢弃30%神经元,防止过拟合 model.add(tf.keras.layers.LSTM(512))#千万不要丢括号!!!! #全连接层 model.add(tf.keras.layers.Dense(256))#256个神经元的全连接层 model.add(tf.keras.layers.Dropout(0.75)) model.add(tf.keras.layers.Dense(num_pitch))#输出的数目等于所有不重复的音调数 #激活层 model.add(tf.keras.layers.Activation('softmax'))#Softmax激活函数求概率 #配置神经网络模型 model.compile(loss='categorical_crossentropy',optimizer=tf.keras.optimizers.RMSprop(learning_rate=0.0004)) #选择的损失函数是交叉熵,用来计算误差。使用对于RNN来说比较优秀的优化器-RMSProp #优化器如果使用字符串的话会用默认参数导致效果不好 return model
06-10
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值