在使用Keras的卷积网模型中找不到精确度过低的原因

时间:2018-09-21 17:31:38

标签: python keras conv-neural-network

我已经建立并训练了卷积神经网络模型,并使用Handwritten_Dataset对其进行了训练,并使用epochs = 2并分批发送了128个训练数据,但找不到其准确性很低的原因。

代码是:

Awake()

有人可以指出如此低的准确性背后的原因吗? 我是否正确划分了数据集?

输出精度为:

import pandas as pd
import numpy as np

import matplotlib.pyplot as plt
import keras

from sklearn.model_selection import train_test_split

import warnings
warnings.filterwarnings('ignore')

import tables

from keras.models import Sequential
from keras.utils import np_utils 

from keras.layers import Conv2D, MaxPooling2D

from keras.layers import Activation, Flatten, Dropout, Dense


from keras.utils import to_categorical

#hd=pd.read_hdf('data.h5')
hd=pd.read_csv('../input/handwritten_data_785.csv')
hd.head()

Y=hd.iloc[:,0]
X=hd.iloc[:,1:]

Y=to_categorical(Y)

X_train,X_test,Y_train,Y_test=train_test_split(X,Y,stratify=Y,random_state=34,test_size=0.25)
X_train=X_train.values.reshape(X_train.shape[0],28,28,1)
X_test=X_test.values.reshape(X_test.shape[0],28,28,1)


X_train = X_train.astype('float32')
X_test = X_test.astype('float32')

print("X.shape ",X.shape)
print("Y.shape ",Y.shape)
type(Y)

input_shape=(28,28,1)
n_classes=Y_train.shape[1]
batch_size=128
epochs=2
model=Sequential()
model.add(Conv2D(filters=32,kernel_size=(4,4),strides=(1,1),padding='same',activation='relu',input_shape=input_shape))

model.add(MaxPooling2D(pool_size=(2,2)))#,strides=(1,1)))

model.add(Conv2D(filters=64,kernel_size=(4,4),strides=(1,1),padding='same',activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2),strides=(1,1)))

model.add(Flatten())

model.add(Dense(1000,activation='relu'))
model.add(Dense(n_classes,activation='softmax'))

model.compile(loss=keras.losses.categorical_crossentropy,optimizer=keras.optimizers.SGD(lr=0.05),metrics=["accuracy"])
model.fit(X_train,Y_train,batch_size=batch_size,epochs=epochs,verbose=1,validation_data=(X_test,Y_test))

model.evaluate(X_test,Y_test,verbose=0)

1 个答案:

答案 0 :(得分:1)

以下内容可帮助我获得良好的准确性:

1)归一化数据会大规模影响卷积神经网络。

标准化数据:X /=255

在不更改历元的情况下,准确性似乎可以达到90%。

2)增加历元也将提高模型准确性。