[英]Neural Network model
我有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,activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='rmsprop',
metrics=['accuracy'])
model.summary()
model.fit(X, to_categorical(Y), epochs=1000, verbose=1) # <---
你也可以使用sklearn 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.