Python,Theano - ValueError:输入维度不匹配

时间:2016-08-18 15:58:42

标签: python machine-learning neural-network theano lasagne

我根据Lasagne中的mnist.py示例在Theano中构建了一个DNN。 我试图首先训练由单个隐藏层构成的神经网络,定义为

def build_first_auto(input_var=None):

    l_input=lasagne.layers.InputLayer(shape=(None, 1, 48, 1), input_var=input_var)
    l_hidden1=lasagne.layers.DenseLayer(l_input,num_units=256,nonlinearity=lasagne.nonlinearities.sigmoid,W=lasagne.init.GlorotUniform())

    return l_hidden1

这在

中使用
from load_dataset import load_dataset
from build_DNNs import build_first_auto

import sys
import os
import time

import numpy as np
from numpy import linalg as LA
import theano
import theano.tensor as T

import lasagne
import scipy.io as sio

def iterate_minibatches(inputs, targets, batchsize, shuffle=False):

    assert len(inputs) == len(targets)
    if shuffle:
        indices = np.arange(len(inputs))
        np.random.shuffle(indices)
    for start_idx in range(0, len(inputs) - batchsize + 1, batchsize):
        if shuffle:
            excerpt = indices[start_idx:start_idx + batchsize]
        else:
                excerpt = slice(start_idx, start_idx + batchsize)
        yield inputs[excerpt], targets[excerpt]


def train_autoencoder(num_epochs):

    Xtrain, ytrain = load_dataset()

# Prepare Theano variables for inputs and targets
    input_var = T.tensor4('inputs')
    target_var = T.matrix('targets')
# Create neural network model
    network = build_first_auto(input_var)

    prediction = lasagne.layers.get_output(network)
    params = lasagne.layers.get_all_params(network, trainable=True)

    loss = lasagne.objectives.binary_crossentropy(prediction, target_var)
    loss = loss.mean()

    updates = lasagne.updates.nesterov_momentum(loss, params, learning_rate=0.01, momentum=0.9)
    np.save('params', params)

#Monitoring the training
    test_prediction = lasagne.layers.get_output(network, deterministic=True)
    test_loss = lasagne.objectives.categorical_crossentropy(test_prediction,target_var)
    test_loss = test_loss.mean()

    test_acc = T.mean(T.eq(T.argmax(test_prediction,axis=1),target_var),dtype=theano.config.floatX)


#Compile    
    train_fn = theano.function([input_var, target_var], loss, updates=updates, on_unused_input='ignore' ) #on_unused_input='ignore'

# Compile a second function computing the validation loss and accuracy:
    val_fn = theano.function([input_var, target_var], [test_loss, test_acc])


#Training
    print("Starting training...")
    for epoch in range(num_epochs):
    # In each epoch, we do a full pass over the training data:
        train_err = 0
        train_batches = 0
        start_time = time.time()
        for batch in iterate_minibatches(Xtrain, ytrain, 30821, shuffle=True):
            inputs, targets = batch

            train_err += train_fn(inputs, targets)
            train_batches += 1


    # And a full pass over the validation data:
        val_err = 0
        val_acc = 0
        val_batches = 0
        for batch in iterate_minibatches(Xtrain, ytrain, 30821, shuffle=False):
            inputs, targets = batch
            err, acc = val_fn(inputs, targets)
            val_err += err
            val_acc += acc
            val_batches += 1


    # Then we print the results for this epoch:
        print("Epoch {} of {} took {:.3f}s".format(
        epoch + 1, num_epochs, time.time() - start_time))
        print("  training loss:\t\t{:.6f}".format(train_err / train_batches))
        print("  validation loss:\t\t{:.6f}".format(val_err / val_batches))
        print("  validation accuracy:\t\t{:.2f} %".format(
        val_acc / val_batches * 100))

损失函数是二元交叉熵。问题是我收到了与数组维度相关的错误:

  

ValueError:输入维度不匹配。 (输入[1] .shape [1] = 1,输入[3] .shape [1] = 256)

     

应用导致错误的节点:Elemwise {Composite {(((i0 * i1 *(i2-scalar_sigmoid(i3)))/ i4) - ((i0 * i5 * scalar_sigmoid(i3))/ i4))} }(TensorConstant {(1,1)-1.0},目标,TensorConstant {(1,1)1.0},Elemwise {Add} [(0,0)]。0,Elemwise {mul,no_inplace} .0, Elemwise {子,no_inplace} 0.0)

     

Toposort指数:17

     

输入类型:[TensorType(float64,(True,True)),TensorType(float64,矩阵),TensorType(float64,(True,True)),TensorType(float64,矩阵),TensorType(float64,(True, True)),TensorType(float64,matrix)]

     

输入形状:[(1,1),(30821,1),(1,1),(30821,256),(1,1),(30821,1)]

     

输入步幅:[(8,8),(8,8),(8,8),(2048,8),(8,8),(8,8)]

     

输入值:[array([[ - 1。]]),'未显示',array([[1.]]),'未显示',array( [[30821.]]),'未显示']

     

输出客户:[[Dot22Scalar(InplaceDimShuffle {1,0} .0,Elemwise {Composite {(((i0 * i1 *(i2-scalar_sigmoid(i3)))/ i4) - ((i0 * i5 * scalar_sigmoid (i3))/ i4))}}。0,TensorConstant {0.01}),Sum {axis = [0],acc_dtype = float64}(Elemwise {Composite {(((i0 * i1 *(i2-scalar_sigmoid(i3) ))/ i4) - ((i0 * i5 * scalar_sigmoid(i3))/ i4))}}。0)]]

作为提示我可以说输入的维度是(30821,1,48,1)和目标(30821,1)。 我已经阅读了几个关于如何使用重塑修复此错误的页面,但它对我的案例不起作用。 同样定义target_var = T.matrix()而不是T.ivector()也没有帮助。 为隐藏层设置适当的维度是有效的,但是这个神经网络的功能应该由这个数字独立。谢谢你的帮助。

1 个答案:

答案 0 :(得分:0)

对于您的网络,输出为256-dim。 由于您使用的是二进制交叉熵损失函数,我想您希望将样本分为两类。您需要一个num_units = 2和softmax

的输出层
def build_first_auto(input_var=None):
    l_input=lasagne.layers.InputLayer(shape=(None, 1, 48, 1),input_var=input_var)
    l_hidden1=lasagne.layers.DenseLayer(l_input,num_units=256,nonlinearity=lasagne.nonlinearities.sigmoid,W=lasagne.init.GlorotUniform())
    l_output=lasagne.layers.DenseLayer(l_hidden1,num_units=2,nonlinearity=lasagne.nonlinearities.softmax,W=lasagne.init.GlorotUniform())

return l_output

这应该有效。如果有任何问题,请告诉我。