我试图创建一个基于theano / lasagne的神经网络,它将(基本上)尝试进行多变量回归。
代码的内容是:
train_value = train_df.values[:, 0]
train_data = train_df.values[:, 1:]
#print "train:", train_data.shape, train_label.shape
#test_data = test_df.values
#print "test:", test_data.shape
train_data = train_data.astype(np.float)
train_value = train_value.astype(np.int32)
fc_1hidden = NeuralNet(
layers = [ # three layers: one hidden layer
('input', layers.InputLayer),
('hidden', layers.DenseLayer),
('dropout', layers.DropoutLayer),
('output', layers.DenseLayer),
],
# layer parameters:
input_shape = (None, 36), # 36 rows of data
hidden_num_units = 100, # number of units in hidden layer
dropout_p = 0.25, # dropout probability
output_nonlinearity = softmax, # output layer uses softmax function
output_num_units = 10, # 10 labels
# optimization method:
#update = nesterov_momentum,
update = sgd,
update_learning_rate = 0.001,
#update_momentum = 0.9,
eval_size = 0.1,
# batch_iterator_train = BatchIterator(batch_size = 20),
# batch_iterator_test = BatchIterator(batch_size = 20),
max_epochs = 100, # we want to train this many epochs
verbose = 1,
)
fc_1hidden.fit(train_data, train_value)
plot_loss(fc_1hidden)
这里,train_value只是我希望训练我的NN预测的(数字)数据的一列,以下57列(train_data)是所有应该适当加权以预测的参数/值(所有数字)第一列中的值。
但是,当我运行此脚本时,我收到以下错误:
Epoch | Train loss | Valid loss | Train / Val | Valid acc | Dur
--------|--------------|--------------|---------------|-------------|-------
Traceback (most recent call last):
File "neuralnetwork.py", line 77, in <module>
fc_1hidden.fit(train_data, train_value)
File "/Users/spadavec/anaconda/lib/python2.7/site-packages/nolearn/lasagne.py", line 150, in fit
self.train_loop(X, y)
File "/Users/spadavec/anaconda/lib/python2.7/site-packages/nolearn/lasagne.py", line 188, in train_loop
batch_train_loss = self.train_iter_(Xb, yb)
File "/Users/spadavec/anaconda/lib/python2.7/site-packages/theano/compile/function_module.py", line 606, in __call__
storage_map=self.fn.storage_map)
File "/Users/spadavec/anaconda/lib/python2.7/site-packages/theano/compile/function_module.py", line 595, in __call__
outputs = self.fn()
ValueError: Shape mismatch: x has 83 cols (and 29 rows) but y has 36 rows (and 100 cols)
Apply node that caused the error: Dot22(x_batch, W)
Inputs types: [TensorType(float64, matrix), TensorType(float64, matrix)]
Inputs shapes: [(29, 83), (36, 100)]
Inputs strides: [(664, 8), (800, 8)]
Inputs values: ['not shown', 'not shown']
我不确定这个形状的位置 - 我的数据都没有83列或行。 (注意:我已经尝试改编这个剧本,这个剧本最初用于查看面部图片并猜测不同部位(眼睛,鼻子,嘴巴等))。
我在pybrain中编写了一个更简单的版本(sans-dropout方法),但我正在尝试迁移到sklearn / lasagne / theano,因为它打开了更多的门。
答案 0 :(得分:0)
由于您要进行回归,请确保正确设置输出类型:
output_nonlinearity = linear
您确定还有10个输出单位吗?我在烤宽面条中经历了一些奇怪的行为。我认为API随着时间的推移而发生了变化,并且包含一些错误。我成功地使用了最新的API demo并根据我的需要进行调整。