Não funciona exemplo, com quadro Keras

votos
2

Eu estou tentando estudar Kerasbiblioteca e roteiro follow criado como exemplo:

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

Na minha opinião, este script deve funcionar :), no entanto eu tenho seguinte erro:

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

definindo o sinalizador Theano 'otimizador = fast_compile'. Se isso não funcionar, otimizações THEANO pode ser desativado com 'otimizador = None'. DICA: Use o sinalizador Theano 'exception_verbosity = Alto' para uma debugprint e armazenamento mapa pegada deste nó aplicar.

PS. Dados formas:

  • Tamanho de X_train (9L, 784L)
  • Tamanho de X_test (9L, 784L)
Publicado 11/12/2015 em 14:26
fonte usuário
Em outras línguas...                            


1 respostas

votos
2

Verifique esta linha em seu código

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

Por 20 dimensões de entrada? MNIST tem imagens 28x28, ou seja, uma dimensão de entrada 784. A mensagem de erro confirma que tão bem:

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

Você pode verificar ainda mais o tamanho da sua entrada

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

E, consequentemente, alterar a linha acima para:

model.add(Dense(64, input_dim=784, init='uniform'))
Respondeu 11/12/2015 em 16:26
fonte usuário

Cookies help us deliver our services. By using our services, you agree to our use of cookies. Learn more