Python 如何格式化深层信念神经网络的训练/测试集

Python 如何格式化深层信念神经网络的训练/测试集,python,neural-network,deep-learning,Python,Neural Network,Deep Learning,我正在尝试使用实现来自的代码。但我无法正确设置数据(训练集/测试集)的格式。我的代码: numpy_rng = numpy.random.RandomState(123) dbn = DBN(numpy_rng=numpy_rng, n_ins=2,hidden_layers_sizes=[50, 50, 50],n_outs=1) train_set_x = [ ([1,2],[2,]), #first element in the tuple is

我正在尝试使用实现来自的代码。但我无法正确设置数据(训练集/测试集)的格式。我的代码:

    numpy_rng = numpy.random.RandomState(123)
    dbn = DBN(numpy_rng=numpy_rng, n_ins=2,hidden_layers_sizes=[50, 50, 50],n_outs=1)

    train_set_x = [
        ([1,2],[2,]), #first element in the tuple is the input, the second is the output
        ([4,5],[5,])
    ]

    testing_set_x = [
        ([6,1],[3,]), #same format as the training set
    ]

    #when I looked at the load_data function found elsewhere in the tutorial (I'll show the code they used at the bottom for ease) I found it rather confusing, but this was my first attempt at recreating what they did
    train_set_xPrime = [theano.shared(numpy.asarray(train_set_x[0][0],dtype=theano.config.floatX),borrow=True),theano.shared(numpy.asarray(train_set_x[0][1],dtype=theano.config.floatX),borrow=True)]

    pretraining_fns = dbn.pretraining_functions(train_set_x=train_set_xPrime,batch_size=10,k=1)
产生此错误的原因:

    Traceback (most recent call last):
      File "/Users/spudzee1111/Desktop/Code/NNChatbot/DeepBeliefScratch.command", line 837, in <module>
        pretraining_fns = dbn.pretraining_functions(train_set_x=train_set_xPrime,batch_size=10,k=1)
      File "/Users/spudzee1111/Desktop/Code/NNChatbot/DeepBeliefScratch.command", line 532, in pretraining_functions
        n_batches = train_set_x.get_value(borrow=True).shape[0] / batch_size
    AttributeError: 'list' object has no attribute 'get_value'
但后来它说:

    Traceback (most recent call last):
      File "/Users/spudzee1111/Desktop/Code/NNChatbot/DeepBeliefScratch.command", line 834, in <module>
        train_set_xPrime = theano.shared([theano.shared(numpy.asarray(train_set_x[0][0],dtype=theano.config.floatX),borrow=True),theano.shared(numpy.asarray(train_set_x[0][1],dtype=theano.config.floatX),borrow=True)],borrow=True) #,borrow=True),numpy.asarray(train_set_x[0][1],dtype=theano.config.floatX),borrow=True))
      File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/theano/compile/sharedvalue.py", line 228, in shared
        (value, kwargs))
    TypeError: No suitable SharedVariable constructor could be found. Are you sure all kwargs are supported? We do not support the parameter dtype or type. value="[<TensorType(float64, vector)>, <TensorType(float64, vector)>]". parameters="{'borrow': True}"
回溯(最近一次呼叫最后一次):
文件“/Users/spudzee1111/Desktop/Code/NNChatbot/DeepBeliefScratch.command”,第834行,在
train\u set\u xPrime=theano.shared([theano.shared(numpy.asarray(train\u set\u x[0][0],dtype=theano.config.floatX),borrow=True),theano.shared(numpy.asarray(train\u set\u x[0][1],dtype=theano.config.floatX),borrow=True)],borrow=True),numpy.asarray(train\u set\u\u x[0][1],dtype=theano.config.floatX,borroad=True))
文件“/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site packages/theano/compile/sharedvalue.py”,第228行,在共享文件中
(价值,克瓦格)
TypeError:找不到合适的SharedVariable构造函数。您确定支持所有KWARG吗?我们不支持参数dtype或type。value=“[,]”。parameters=“{'brook':True}”
我尝试了其他组合,但没有一个有效。

这应该有效

numpy_rng = numpy.random.RandomState(123)
dbn = DBN(numpy_rng=numpy_rng, n_ins=2, hidden_layers_sizes=[50, 50, 50], n_outs=1)

train_set = [
    ([1,2],[2,]),
    ([4,5],[5,])
]

train_set_x = [train_set[i][0] for i in range(len(train_set))]
nparray = numpy.asarray(train_set_x, dtype=theano.config.floatX)
train_set_x = theano.shared(nparray, borrow=True)

pretraining_fns = dbn.pretraining_functions(train_set_x=train_set_x, batch_size=10, k=1)
方法
pretraining\u fns
期望输入一个大小的共享变量(样本数、输入维度)。您可以通过查看MNIST数据集的形状来检查这一点,MNIST数据集是本例的标准输入

它不接受列表作为输入,因为此方法仅适用于预训练函数。DBN使用无监督学习算法进行预训练,因此使用标签是没有意义的

此外,用于创建numpy数组的输入列表没有意义
train\u set\u x[0][0]
只生成第一个训练示例。您希望
train\u set\u xPrime
包含所有培训示例。即使你做了
train\u set\u x[0]
你也会得到第一个训练示例,但带有标签

numpy_rng = numpy.random.RandomState(123)
dbn = DBN(numpy_rng=numpy_rng, n_ins=2, hidden_layers_sizes=[50, 50, 50], n_outs=1)

train_set = [
    ([1,2],[2,]),
    ([4,5],[5,])
]

train_set_x = [train_set[i][0] for i in range(len(train_set))]
nparray = numpy.asarray(train_set_x, dtype=theano.config.floatX)
train_set_x = theano.shared(nparray, borrow=True)

pretraining_fns = dbn.pretraining_functions(train_set_x=train_set_x, batch_size=10, k=1)