2016-06-18 42 views
3

嘗試將以下代碼應用於MNIST示例數據集和測試。請helpe錯誤:檢查模型輸入時出錯:期望的dense_input_6有形狀(無,784)但有形狀的陣列(784L,1L)

以下是我的代碼:

import pandas 
import numpy 
import numpy 
from keras.datasets import mnist 
from keras.models import Sequential 
from keras.layers import Dense 
from keras.layers import Dropout 
from keras.utils import np_utils 
# fix random seed for reproducibility 
seed = 7 
numpy.random.seed(seed) 
# Read in the TRAINING dataset 
f = open("C:/Users/USER/Desktop/mnist/mnist_train_100.csv", 'r') 
a = f.readlines() # place everythig in a lsit called 'a' 
#print(a) 
f.close() 
# go through the list a and split by comma 
output_nodes = 10 
for record in a: #go through the big list "a" 
    all_values = record.split(',') 
    X_train = (numpy.asfarray(all_values[1:])/255.0 * 0.99) + 0.01 
    y_train = numpy.zeros(output_nodes) + 0.01 
    y_train[int(all_values[0])] = 0.99 
# Read in the TEST data set and then split 
f = open("C:/Users/USER/Desktop/mnist/mnist_test_10.csv", 'r') 
a = f.readlines() # place everythig in a lsit called 'a' 
#print(a) 
f.close() 
# go through the list a and split by comma 
for record in a: #go through the big list "a" 
    all_values = record.split(',') 
    X_test = (numpy.asfarray(all_values[1:])/255.0 * 0.99) + 0.01  
    y_test = numpy.zeros(output_nodes) + 0.01 
    y_test[int(all_values[0])] = 0.99 

num_pixels = len(X_train) 
# define baseline model 
def baseline_model(): 
    # create model 
    model = Sequential() 
    model.add(Dense(num_pixels, input_dim=num_pixels, init='normal', activation='relu')) 
    model.add(Dense(output_nodes, init='normal', activation='softmax')) 
    # Compile model 
    model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) 
    return model 
## build the model 
#model = baseline_model() 
## Fit the model 
#model.fit(X_train, y_train, validation_data=(X_test, y_test), nb_epoch=10, batch_size=200,verbose=2) 

我收到以下錯誤:

異常:錯誤檢查時模型的輸入:預計dense_input_6有形狀(無,784),但有數組與形狀(784L,1L)

+0

爲什麼你想存儲在MNIST一個CSV文件? – nemo

+0

我認爲他使用[本教程](http://makeyourownneuralnetwork.blogspot.fr/2015/03/the-mnist-dataset-of-handwitten-digits.html),它將MNIST作爲一個CSV文件。 –

+0

http://stackoverflow.com/a/40489174/776515? – Luis

回答

0

我假設你正在與this tutorial一起工作。

我會建議使用熊貓閱讀您的格式:

import pandas as pd 
import numpy as np 

data = pd.read_csv('mnist_train_100.csv', header=None) 

# numpy array of shape (100, 784), type float32 
X_train = data.ix[:, 1:].values.astype(np.float32) 

# numpy array of shape (100,), type int64 
y_train = data.ix[:, 0].values 
+0

這是一個不錯的選擇,但沒有回答OP的問題。 –

相關問題