from keras.models import Sequential
from keras.layers import Dense
from keras.utils import np_utils
from sklearn.datasets import make_blobs
from sklearn.preprocessing import MinMaxScaler
# 生成数据集
X, y = make_blobs(n_samples=1000, centers=3, n_features=2, random_state=42)
# 对数据进行归一化
scaler = MinMaxScaler()
X = scaler.fit_transform(X)
# 将类别转换成独热编码
y = np_utils.to_categorical(y)
# 创建模型
model = Sequential()
model.add(Dense(units=10, input_dim=2, activation='relu'))
model.add(Dense(units=3, activation='softmax'))
# 编译模型
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
# 训练模型
model.fit(X, y, epochs=50, batch_size=10)
# 评估模型
scores = model.evaluate(X, y)
print('\n%s: %.2f%%' % (model.metrics_names[1], scores[1]*100))
这个例子使用`make_blobs`生成3个中心点的数据集,使用`MinMaxScaler`对数据进行归一化,使用`np_utils.to_categorical`将类别转换成独热编码。模型有一个具有10个节点的隐藏层和一个具有3个节点的softmax输出层。模型使用交叉熵损失和Adam优化器进行编译。最后,模型在训练集上训练50个epochs,并在整个数据集上进行评估。