InvalidType:执行了无效的操作

时间:2019-05-07 05:04:08

标签: neural-network autoencoder chainer

我正在尝试编写堆叠式自动编码器。由于这是一个堆叠式自动编码器,我们需要训练第一个自动编码器并将权重传递给第二个自动编码器。因此,在培训期间,我们需要定义train_data_for_next_layer。在这里我得到了错误:

InvalidType: 
Invalid operation is performed in: LinearFunction (Forward)

Expect: x.shape[1] == W.shape[1]
Actual: 784 != 250 

我对最后一行有疑问。是由于模型层不正确导致此问题,我想知道这里的问题是什么。我之前曾多次遇到此问题,欢迎提供详细说明。代码如下:

class AutoEncoder(chainer.Chain):
    def __init__(self, n_in, n_out, activation='relu', tied=True):
        if tied:
            super(AutoEncoder, self).__init__(
                l1 = L.Linear(n_in, n_out)
            )
            self.add_param('decoder_bias', n_in)
            self.decoder_bias.data[...] = 0
        else:
            super(AutoEncoder, self).__init__(
                l1 = L.Linear(n_in, n_out),
                l2 = L.Linear(n_out, n_in)
            )
        self.tied = tied
        self.n_in = n_in
        self.n_out = n_out
        self.activation = {'relu': F.relu, 'sigmoid': F.sigmoid, 
'identity': F.identity}[activation]



    def __call__(self, x, train=True):
        h1 = F.dropout(self.activation(self.l1(x)), train=train)
        if self.tied:
            return self.activation(F.linear(h1, F.transpose(self.l1.W), 
self.decoder_bias))
        else:
            return self.activation(self.l2(h1))

    def encode(self, x, train=True):
        return F.dropout(self.activation(self.l1(x)), train=train)

    def decode(self, x, train=True):
        if self.tied:
            return self.activation(F.linear(x, F.transpose(self.l1.W), 
self.decoder_bias))
        else:
            return self.activation(self.l2(x))

class StackedAutoEncoder(chainer.ChainList):
    def __init__(self, autoencoders):
        super(StackedAutoEncoder, self).__init__()
        for ae in autoencoders:
            self.add_link(ae)

    def __call__(self, x, train=True, depth=0):
        if depth == 0: depth = len(self)
        h = x
        for i in range(depth):
            h = self[i].encode(h, train=train)
        for i in range(depth):
            if i == depth-1: # do not use dropout in the output layer
                train = False
            h = self[depth-1-i].decode(h, train=train)
        return h

    def encode(self, x, train=True, depth=0):
        if depth == 0: depth = len(self)
        h = x
        for i in range(depth):
            h = self[i].encode(h, train=train)
        return h

    def decode(self, x, train=True, depth=0):
        if depth == 0: depth = len(self)
        h = x
        for i in range(depth):
            if i == depth-1: # do not use dropout in the output layer
                train = False
            h = self[depth-1-i].decode(h, train=train)
        return h

class Regression(chainer.Chain):
    def __init__(self, predictor):
        super(Regression, self).__init__(predictor=predictor)

    def __call__(self, x, t):
        y = self.predictor(x, True)
        self.loss = F.mean_squared_error(y, t)
        return self.loss

    def dump(self, x):
        return self.predictor(x, False)

initmodel = ''resume = ''
gpu = -1
epoch_pre = 20
epoch_fine = 20
batchsize = 100
noise = 0
optimizer = 'adam'
learningrate = 0.01
alpha = 0.001
unit = '1000, 500, 250, 2'
activation = 'sigmoid'
untied = False

batchsize = batchsize
n_epoch = epoch_pre
n_epoch_fine = epoch_fine

n_units = list(map(int, unit.split(',')))
activation = activation

mnist = fetch_mldata('MNIST original', data_home='.')
perm = np.random.permutation(len(mnist.data))
mnist.data = mnist.data.astype(np.float32) / 255
train_data = mnist.data[perm][:60000]
test_data = mnist.data[perm][60000:]

# prepare layers
aes = []
for idx in range(len(n_units)):
    n_in = n_units[idx-1] if idx > 0 else 28*28
    n_out = n_units[idx]
    ae = AutoEncoder(n_in, n_out, activation, tied = True)
    aes.append(ae)

# prepare train data for next layer
x = chainer.Variable(np.array(train_data))
train_data_for_next_layer = cuda.to_cpu(ae.encode(x, train=False))

1 个答案:

答案 0 :(得分:0)

InvalidType错误表明赋予F.linear的数组的输入形状错误。

Expect: x.shape[1] == W.shape[1]
Actual: 784 != 250 

在这种情况下,对于给定的输入xWF.linear期望  x.shape[1]W.shape[1]相同,但不相同。

有关错误消息的详细说明,请参见https://docs.chainer.org/en/stable/tips.html#how-do-i-fix-invalidtype-error,以了解如何解释该错误消息。