LoginSignup
2
1

More than 5 years have passed since last update.

chainerで2つの違うモーダルを共通空間に射影するときのモデルコード

Last updated at Posted at 2017-03-01
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させればよい.

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1