神经网络模型

时间:2019-01-18 05:41:10

标签: python machine-learning keras neural-network deep-learning

我有6列100行的样本数据(所有值都是整数)。输入数据分为20类。这是我尝试构建的模型:

model = Sequential()
model.add(Dense(50,input_shape=X.shape[1:],activation='relu'))

model.add(Dense(20,activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='rmsprop', 
              metrics=['accuracy'])
model.summary()
model.fit(X, Y, epochs=1000, verbose=0)
predictions=model.predict(test_data)

但是,我得到一个错误:

Error when checking target: expected dense_2 to have shape (20,) but got array with shape (1,)

我有两个问题:

  1. 我在做什么错了?
  2. 您能为此给我合适的体系结构吗?

1 个答案:

答案 0 :(得分:2)

您需要使用Ydocs)将to_categorical转换为二进制类矩阵。

import sklearn.datasets
X,Y = sklearn.datasets.make_classification(n_samples=100, n_features=6, n_redundant=0,n_informative=6, n_classes=20)

import numpy as np
from keras import Sequential
from keras.layers import Dense
from keras.utils import to_categorical
from keras import backend as K
K.clear_session()

model = Sequential()
model.add(Dense(50,input_dim=X.shape[1],activation='softmax'))
model.add(Dense(20,activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='rmsprop', 
              metrics=['accuracy'])
model.summary()
model.fit(X, to_categorical(Y), epochs=1000, verbose=1) # <---

您也可以使用sklearn