使用 lasagne 和 theano 构建网络

buiding a network using lasagne and theano

我正在尝试建立网络,但出现以下错误

这是构建和训练代码

网络是由以下视频构建的code

我使用此存储库创建的数据githup repository

训练包含 24 class 个图像,测试包含 24 class 个标签

import os
import numpy as np
import lasagne
import theano
import theano.tensor as T


def load_mnist_images(filename):
    with gzip.open(filename,'rb') as f :
        data = np.frombuffer(f.read(),np.uint8,offset=16)
        data = data.reshape(-1,1,28,28)
        print(type(data))
        return data/np.float32(256)

def load_mnist_labels(filename):
    with gzip.open(filename,'rb')as f:
        data=np.frombuffer(f.read(),np.uint8,offset=8)
        return data

def load_dataset():
    x_train = load_mnist_images('train-images-idx3-ubyte.gz')
    y_train = load_mnist_labels('train-labels-idx1-ubyte.gz')
    x_test = load_mnist_images('test-images-idx3-ubyte.gz')
    y_test = load_mnist_labels('test-labels-idx1-ubyte.gz')

    return x_train,y_train,x_test,y_test

def build_nn(input_var=None):

    l_in = lasagne.layers.InputLayer(shape=(None,1,28,28),input_var=input_var)

    l_in_drop = lasagne.layers.DropoutLayer(l_in,p=0.2)

    l_hid1 = lasagne.layers.DenseLayer(l_in_drop,num_units=800,
                                       nonlinearity=lasagne.nonlinearities.rectify,
                                       W=lasagne.init.GlorotUniform())

    l_hid1_drop = lasagne.layers.DropoutLayer(l_hid1,p=0.5)

    l_hid2 = lasagne.layers.DenseLayer(l_hid1_drop, num_units=800,
                                       nonlinearity=lasagne.nonlinearities.rectify,
                                       W=lasagne.init.GlorotUniform())

    l_hid2_drop = lasagne.layers.DropoutLayer(l_hid2, p=0.5)

    l_out = lasagne.layers.DenseLayer(l_hid2_drop,num_units=24 ,
                                      nonlinearity= lasagne.nonlinearities.softmax)

    return l_out
if __name__ == "__main__":
    x_train,y_train,x_test,y_test = load_dataset()

    input_var = T.tensor4('inputs')
    target_var = T.tensor4('targets')
    print(target_var)
    network = build_nn(input_var)

    prediction = lasagne.layers.get_output(network)

    loss = lasagne.objectives.categorical_crossentropy(prediction,target_var)

    loss = loss.mean()

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

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

    train_fn = theano.function([input_var,target_var],loss , updates=updates)

    num_training_steps = 10

    for steps in range(num_training_steps):
        train_err = train_fn(x_train,y_train)
        print("current step is " + str(steps))

这是我得到的错误

Traceback (most recent call last):
  File "/home/hassan/JPG-PNG-to-MNIST-NN-Format/mnist_test.py", line 64, in <module>
    loss = lasagne.objectives.categorical_crossentropy(prediction,target_var)
  File "/home/hassan/anaconda3/envs/object-detection/lib/python3.7/site-packages/lasagne/objectives.py", line 181, in categorical_crossentropy
    return theano.tensor.nnet.categorical_crossentropy(predictions, targets)
  File "/home/hassan/anaconda3/envs/object-detection/lib/python3.7/site-packages/theano/tensor/nnet/nnet.py", line 2101, in categorical_crossentropy
    raise TypeError('rank mismatch between coding and true distributions')
TypeError: rank mismatch between coding and true distributions

我解决了,写代码有误

而不是写这个

target_var = T.ivector('targets')

这是我写的

target_var = T.tensor4('targets')