python - 参数的多个值
问题描述
我正在尝试将此代码转换为使用 pysyft 引用传递它
像这样 :
class SyNet(sy.Module):
def __init__(self,embedding_size, num_numerical_cols, output_size, layers, p ,torch_ref):
super(SyNet, self ).__init__( embedding_size, num_numerical_cols , output_size , layers , p=0.4 ,torch_ref=torch_ref )
self.all_embeddings=self.torch_ref.nn.ModuleList([nn.Embedding(ni, nf) for ni, nf in embedding_size])
self.embedding_dropout=self.torch_ref.nn.Dropout(p)
self.batch_norm_num=self.torch_ref.nn.BatchNorm1d(num_numerical_cols)
all_layers= []
num_categorical_cols = sum((nf for ni, nf in embedding_size))
input_size = num_categorical_cols + num_numerical_cols
for i in layers:
all_layers.append(self.torch_ref.nn.Linear(input_size,i))
all_layers.append(self.torch_ref.nn.ReLU(inplace=True))
all_layers.append(self.torch_ref.nn.BatchNorm1d(i))
all_layers.append(self.torch_ref.nn.Dropout(p))
input_size = i
all_layers.append(self.torch_ref.nn.Linear(layers[-1], output_size))
self.layers = self.torch_ref.nn.Sequential(*all_layers)
def forward(self, x_categorical, x_numerical):
embeddings= []
for i,e in enumerate(self.all_embeddings):
embeddings.append(e(x_categorical[:,i]))
x_numerical = self.batch_norm_num(x_numerical)
x = self.torch_ref.cat([x, x_numerical], 1)
x = self.layers(x)
return x
但是当我尝试创建模型的实例时
model = SyNet( categorical_embedding_sizes, numerical_data.shape[1], 2, [200,100,50], p=0.4 ,torch_ref= th)
我有一个类型错误
TypeError:参数“torch_ref”的多个值
我试图更改参数的顺序,但我收到关于位置参数的错误。你能帮我吗,我在类和函数方面不是很有经验(oop)
先感谢您 !
解决方案
查看PySyft的源代码。Module
您的父类的构造函数只接受一个参数:torch_ref
.
因此,您应该使用以下命令调用超级构造函数:
super(SyNet, self).__init__(torch_ref=torch_ref) # line 3
删除所有参数,但从torch_ref
调用中删除。
推荐阅读
- ios - 自定义事件未登录 Firebase Analytics 事件部分
- c# - 为什么我在使用 IronPython 导入库时收到 Microsoft.Scripting.SyntaxErrorException: unexpected token ','
- python - python pandas根据一个条件更改数据框中的几列
- visual-c++ - opencv trackbar not showing
- spring - Retrieve healthcheck info if Spring couldn't load context
- javascript - [变量] .AddEventListener 在 Javascript 中?
- android - 当 Button 具有可绘制对象时,Android 应用程序停止
- node.js - uploading images in mlab
- python - MongoDB Atlas 连接错误 - UnicodeError: label too long, UnicodeError: encoding with 'idna' codec failed (UnicodeError: label too long)
- java - 将某些数据发送到某些字段改造时需要授权 401