示例#1
0
    def __init__(self, encode_set=False):
        super().__init__()

        # per element encoder is a conv neural network
        cfe = get_MNIST_extractor()

        self.cfe = ContextFreeEncoder(cfe, '2d')
        self.flatten = FlattenElements()
        # if encode_set:
        #     self.cbl1 = ContextBasedLinear(nonlinearity=nn.ReLU)
        #     self.cbl2 = ContextBasedLinear(nonlinearity=nn.ReLU)
        #     self.encode_set = True
        # else:
        self.encode_set = False

        self.lss = LinearSumSet()
        # self.lineartoh1 = nn.Linear(128, 128)
        # self.relu = nn.ReLU()
        # self.lineartoh2 = nn.Linear(128, 32)
        # self.relu = nn.ReLU()
        # self.linearout = nn.Linear(32, 1)

        self.l1 = nn.Linear(128, 128)
        self.l2 = nn.Linear(128, 100)
        self.l3 = nn.Linear(100, 1)
        self.relu = nn.ReLU()
示例#2
0
 def __init__(self, logprobs=True):
     super().__init__()
     mnist_extractor = get_MNIST_extractor()
     classifier = nn.Sequential(mnist_extractor, nn.Linear(128, 128),
                                nn.ReLU(), nn.Linear(128, 64), nn.ReLU(),
                                nn.Linear(64, 1))
     self.cfe = ContextFreeEncoder(classifier, '2d')
     self.logprobs = logprobs
def test_2d_inputs():
    # 2 sets
    # 3 images per set
    # 3 channels per image
    # 32x32 is image size
    pix = torch.ones(2, 3, 3, 32, 32)
    pix[0][0].add_(1)
    pix[0][2].add_(1)
    pix[1][2].add_(1)

    cfe = ContextFreeEncoder(Simple2DConvNet(), '2d')
    cfe.train(False)  # stop stochastic dropouts
    y = cfe(Variable(pix))
    # since this is a flattened extractor we should get:
    assert tuple(y.size()) == (2, 3, 10)
    y = y.data.numpy()
    assert np.allclose(y[0][0], y[0][2])
    assert np.allclose(y[0][1], y[1][1])
    assert np.allclose(y[0][0], y[1][2])
def test_1d_inputs():
    # 2 sets
    # 3 elements per set
    # (1, 10) features per element per set
    x = torch.ones(2, 3, 1, 10)
    x[:, 1] = x[:, 1] + 1
    x[1, 2] = x[1, 2] + 1

    cfe = ContextFreeEncoder(nn.Conv1d(1, 2, 2), '1d')
    y = cfe(Variable(x))
    assert tuple(y.size()) == (2, 3, 2, 9)
    y = y.view(2, 3, 2 * 9).data.numpy()
    assert np.allclose(y[0, 1], y[1, 1])
    assert np.allclose(y[0, 0], y[0, 2])
    assert np.allclose(y[0, 1], y[1, 2])
示例#5
0
    def __init__(self, logprobs=True):
        super().__init__()

        # per element encoder is a conv neural network
        cfe = get_MNIST_extractor()

        self.cfe = ContextFreeEncoder(cfe, '2d')
        self.cbe = ContextBasedMultiChannelLinear(128,
                                                  128,
                                                  nonlinearity=nn.ReLU)
        self.cbe2 = ContextBasedMultiChannelLinear(128,
                                                   64,
                                                   nonlinearity=nn.ReLU)
        self.cbe3 = ContextBasedMultiChannelLinear(64, 1, nonlinearity=None)
        self.logprobs = logprobs
示例#6
0
    def __init__(self, logprobs=True, null_model=False):
        super().__init__()

        self.null_model = null_model

        if self.null_model:
            # so many layers just to have the same number of parameters!
            cfe = nn.Sequential(nn.Linear(8, 32), nn.ReLU(), nn.Linear(32, 64),
                                nn.ReLU(), nn.Linear(64, 64), nn.ReLU(),
                                nn.Linear(64,
                                          32), nn.ReLU(), nn.Linear(32, 64),
                                nn.ReLU(), nn.Linear(64, 64), nn.ReLU(),
                                nn.Linear(64,
                                          64), nn.ReLU(), nn.Linear(64, 64),
                                nn.ReLU(), nn.Linear(64, 64), nn.ReLU(),
                                nn.Linear(64, 32), nn.ReLU(),
                                nn.Linear(32, 16), nn.ReLU(), nn.Linear(16, 1))
        else:
            cfe = nn.Sequential(nn.Linear(8, 32), nn.ReLU(), nn.Linear(32, 64),
                                nn.ReLU(), nn.Linear(64, 64), nn.ReLU(),
                                nn.Linear(64, 32), nn.ReLU())

        self.cfe = ContextFreeEncoder(cfe, '1d')
        if not self.null_model:
            self.cbe = ContextBasedMultiChannelLinear(32,
                                                      64,
                                                      nonlinearity=nn.ReLU)
            self.cbe2 = ContextBasedMultiChannelLinear(64,
                                                       64,
                                                       nonlinearity=nn.ReLU)
            self.cbe3 = ContextBasedMultiChannelLinear(64,
                                                       64,
                                                       nonlinearity=nn.ReLU)
            self.cbe4 = ContextBasedMultiChannelLinear(64,
                                                       64,
                                                       nonlinearity=nn.ReLU)
            self.cbe5 = ContextBasedMultiChannelLinear(64,
                                                       64,
                                                       nonlinearity=nn.ReLU)
            self.cbe6 = ContextBasedMultiChannelLinear(64,
                                                       32,
                                                       nonlinearity=nn.ReLU)
            self.cbe7 = ContextBasedMultiChannelLinear(32,
                                                       16,
                                                       nonlinearity=nn.ReLU)
            self.cbe8 = ContextBasedMultiChannelLinear(16, 1)

        self.logprobs = logprobs
示例#7
0
    def __init__(self):
        super().__init__()

        # per element encoder is a conv neural network
        cfe = get_MNIST_extractor()

        self.cfe = ContextFreeEncoder(cfe, '2d')
        self.flatten = FlattenElements()

        # self.lstm = nn.LSTM(128, 64, 1)
        # self.relu = nn.ReLU()
        # self.linearout = nn.Linear(64, 1)

        # attempt to make it more equal to the set based method
        self.lstm = nn.LSTM(128, 32, 2)
        self.relu = nn.ReLU()
        self.linearout = nn.Linear(32, 1)
示例#8
0
    def __init__(self,
                 input_dim,
                 hidden_dim=10,
                 prob_net_layers=1,
                 logprobs=False):
        super().__init__()
        assert prob_net_layers >= 1, 'Must have at least one linear layer'

        if prob_net_layers == 1:
            hidden_dim = 1

        seq = nn.Sequential(nn.Linear(input_dim, hidden_dim))

        for l in range(prob_net_layers - 1):
            seq.add_module(str(l) + 'relu', nn.ReLU())
            seq.add_module(
                str(l) + 'linear', nn.Linear(hidden_dim, hidden_dim))

        if not logprobs:
            seq.add_module('sigmoid', nn.Sigmoid())

        self.prob = ContextFreeEncoder(seq, '1d')