错误:检查模型输入时出错:预期densed_input_6具有形状(无,784),但数组的形状为(784L,1L) [英] Error: Error when checking model input: expected dense_input_6 to have shape (None, 784) but got array with shape (784L, 1L)

查看:87
本文介绍了错误:检查模型输入时出错:预期densed_input_6具有形状(无,784),但数组的形状为(784L,1L)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当尝试将以下代码应用于MNIST样本数据集进行训练和测试时,出现错误.请帮忙

I get an error when trying to apply the below code onto the MNIST sample dataset for both training and testing. Please helpe

以下是我的代码:

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)

我收到以下错误:

异常:检查模型输入时出错:预期density_input_6具有形状(None,784)但具有形状为(784L,1L)的数组

Exception: Error when checking model input: expected dense_input_6 to have shape (None, 784) but got array with shape (784L, 1L)

推荐答案

我假设您正在使用我建议您使用熊猫来阅读您的格式:

I would recommend using pandas to read your format:

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  

这篇关于错误:检查模型输入时出错:预期densed_input_6具有形状(无,784),但数组的形状为(784L,1L)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆