我正在尝试研究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:Anaconda2libsite-packageskerasmodels.py", line 507, in fit
shuffle=shuffle, metrics=metrics)
File "C:Anaconda2libsite-packageskerasmodels.py", line 226, in _fit
outs = f(ins_batch)
File "C:Anaconda2libsite-packageskerasbackendtheano_backend.py", line 357, in __call__
return self.function(*inputs)
File "C:Anaconda2libsite-packagestheanocompilefunction_module.py", line 606, in __call__
storage_map=self.fn.storage_map)
File "C:Anaconda2libsite-packagestheanocompilefunction_module.py", line 595, in __call__
outputs = self.fn()
File "C:Anaconda2libsite-packagestheanogofop.py", line 768, in rval
r = p(n, [x[0] for x in i], o)
File "C:Anaconda2libsite-packagestheanotensorblas.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
通过设置Theano标志"优化器=fast_compile"。如果没有工作,可以使用"优化器=无"禁用Theano优化。提示:使用Theano标志"exception_verbosity=high"用于此应用节点的调试打印和存储映射封装。
PS。形状数据:
- 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'))