2015-12-11 55 views
2

我努力学习Keras库,并建立跟踪脚本例子:不能与Keras框架合作例子

from keras.models import Sequential 
from keras.layers.core import Dense, Dropout, Activation 
from keras.optimizers import SGD 
from keras.utils import np_utils 

import pandas as pd 
import numpy as np 
import time 
import memory_profiler as mprof 


def write_preds(preds, fname): 
    pd.DataFrame({"ImageId": list(range(1,len(preds)+1)), "Label": preds}).to_csv(fname, index=False, header=True) 


start = time.time() 
# read data 
train = pd.read_csv("..\\data\\train_small.csv") 
labels = train.ix[:,0].values.astype('int32') 
X_train = (train.ix[:,1:].values).astype('float32') 
print 'Loaded train', time.time() - start, mprof.memory_usage() 

test = pd.read_csv("..\\data\\test_small.csv") 
X_test = (test.values).astype('float32') 
# convert list of labels to binary class matrix 
y_train = np_utils.to_categorical(labels) 
print 'Loaded test', time.time() - start, mprof.memory_usage() 

# pre-processing: divide by max and substract mean 
scale = np.max(X_train) 
X_train /= scale 
X_test /= scale 

mean = np.std(X_train) 
X_train -= mean 
X_test -= mean 

input_dim = X_train.shape[1] 
nb_classes = y_train.shape[1] 
print 'Prepare data', time.time() - start, mprof.memory_usage() 
# Here's a Deep Dumb MLP (DDMLP) 
model = Sequential() 
model.add(Dense(64, input_dim=20, init='uniform')) 
model.add(Activation('tanh')) 
model.add(Dropout(0.5)) 
model.add(Dense(64, init='uniform')) 
model.add(Activation('tanh')) 
model.add(Dropout(0.5)) 
model.add(Dense(2, init='uniform')) 
model.add(Activation('softmax')) 
print 'Created model', time.time() - start, mprof.memory_usage() 
# we'll use MSE (mean squared error) for the loss, and RMSprop as the optimizer 
model.compile(loss='mse', optimizer='rmsprop') 
print 'Training ...', time.time() - start, mprof.memory_usage() 
model.fit(X_train, y_train, nb_epoch=10, batch_size=16, show_accuracy=True, verbose=1) 
print 'Generating ...', time.time() - start, mprof.memory_usage() 
preds = model.predict_classes(X_test, verbose=0) 
print 'Predicted', time.time() - start, mprof.memory_usage() 

write_preds(preds, "..\\data\\keras-mlp.csv") 
print 'Finished experiment', time.time() - start, mprof.memory_usage() 

在我看来,这个脚本应该工作:),但我得到了一个错误:

Traceback (most recent call last): 
    File "X:/new_test.py", line 58, in <module> 
    model.fit(X_train, y_train, nb_epoch=10, batch_size=16, show_accuracy=True, verbose=1) 
    File "C:\Anaconda2\lib\site-packages\keras\models.py", line 507, in fit 
    shuffle=shuffle, metrics=metrics) 
    File "C:\Anaconda2\lib\site-packages\keras\models.py", line 226, in _fit 
    outs = f(ins_batch) 
    File "C:\Anaconda2\lib\site-packages\keras\backend\theano_backend.py", line 357, in __call__ 
    return self.function(*inputs) 
    File "C:\Anaconda2\lib\site-packages\theano\compile\function_module.py", line 606, in __call__ 
    storage_map=self.fn.storage_map) 
    File "C:\Anaconda2\lib\site-packages\theano\compile\function_module.py", line 595, in __call__ 
    outputs = self.fn() 
    File "C:\Anaconda2\lib\site-packages\theano\gof\op.py", line 768, in rval 
    r = p(n, [x[0] for x in i], o) 
    File "C:\Anaconda2\lib\site-packages\theano\tensor\blas.py", line 1612, in perform 
    z[0] = numpy.asarray(numpy.dot(x, y)) 
ValueError: ('shapes (9,784) and (20,64) not aligned: 784 (dim 1) != 20 (dim 0)', (9L, 784L), (20L, 64L)) 
Apply node that caused the error: Dot22(<TensorType(float32, matrix)>, <TensorType(float32, matrix)>) 
Inputs types: [TensorType(float32, matrix), TensorType(float32, matrix)] 
Inputs shapes: [(9L, 784L), (20L, 64L)] 
Inputs strides: [(3136L, 4L), (256L, 4L)] 
Inputs values: ['not shown', 'not shown'] 

HINT: Re-running with most Theano optimization disabled could give you a back-trace of when this node was created. This can be done with 

by setting the Theano flag 'optimizer=fast_compile'. If that does not work, Theano optimizations can be disabled with 'optimizer=None'. HINT: Use the Theano flag 'exception_verbosity=high' for a debugprint and storage map footprint of this apply node.

PS。形状数据:

  • X_train(9L,784L)的尺寸
  • X_test的尺寸(9L,784L)

回答

2

入住这行代码中的

model.add(Dense(64, input_dim=20, init='uniform')) 

为什么20个输入尺寸是多少? MNIST具有28X28的图像,即输入尺寸为784。该错误消息确认,以及:

ValueError: ('shapes (9,784) and (20,64) not aligned: 784 (dim 1) != 20 (dim 0)', (9L, 784L), (20L, 64L)) 

您可以进一步验证输入

print "Size of X_train", x_train.shape 
print "Size of X_test", x_test.shape 

的大小并相应地改变上面的一行:

model.add(Dense(64, input_dim=784, init='uniform')) 
+0

是的,它帮助。现在我得到了另一个'error' - > * ValueError:输入维度不匹配。 (输入[0] .shape [1] = 2,输入[1] .shape [1] = 8)* – SpanishBoy