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

标签 python pandas theano deep-learning 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)

最佳答案

检查代码中的这一行

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

关于python - 不适用于 Keras 框架的示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34228138/

相关文章:

python - 无法修复 StaleElementReferenceException @my send_keys

Python Pandas 计算多列的值并根据结果生成图表

python - 如何获得生命变化的数量(pygame)?

python - 如何检查列中的所有值是否满足 Data Frame 中的条件?

function - theano 函数的更新列表的更新顺序

python - theano 中用索引矩阵索引张量?

python - 在 angularjs Controller 中将 pandas json 转换为 highstock 数据格式

python - Pandas 宽格式转换为长格式

python - 选择按值加权的行索引

python - Conda安装的包,Python找不到