加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python-神经网络模型

发布时间:2020-12-17 17:35:55 所属栏目:Python 来源:网络整理
导读:我有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_cro

我有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,)

我有两个问题:

>我做错了什么?
>您能为此给我合适的架构吗?

最佳答案
您需要使用to_categorical(docs)将Y转换为二进制类矩阵.

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,to_categorical(Y),verbose=1) # <---

您也可以使用sklearn.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读