model.py
# Network definition
class MLP(chainer.Chain):
def __init__(self, n_units, n_out):
super(MLP, self).__init__(
# the size of the inputs to each layer will be inferred
l1=L.Linear(None, n_units), # n_in -> n_units
l2=L.Linear(None, n_units), # n_units -> n_units
l3=L.Linear(None, n_out), # n_units -> n_out
)
def __call__(self, x):
h1 = F.relu(self.l1(x))
h2 = F.relu(self.l2(h1))
return self.l3(h2)
class CommonNet(chainer.Chain):
def __init__(self, _model1, _model2):
super(CommonNet, self).__init__(
model1 = _model1,
model2 = _model2,
)
def __call__(self, x, y):
h1 = self.model1(x)
h2 = self.model2(y)
self.loss = F.mean_squared_error(h1, h2)
reporter.report({'loss': self.loss}, self)
return self.loss
enc1 = MLP(10,10)
enc2 = MLP(10,10)
model = CommonNet(enc1, enc2)
上記のようなモデルを組み,あとは,tupled_datasetで2つのデータを用意して,trainerでtrainさせればよい.