Skip to main content
 首页 » 编程设计

python之不适用于 Keras 框架的示例

2024年10月24日10artech

我正在尝试研究 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.

附言。形状数据:

  • X_train 的尺寸(9L、784L)
  • X_test 的大小(9L、784L)

请您参考如下方法:

检查代码中的这一行

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'))