yarn run dev
函数使用落入同心圆的数据集生成二进制分类问题。
make_circles()
如何实现单层神经网络在python中对这些数据进行分类?
答案 0 :(得分:2)
用Keras
制成的最简单的神经网络如下所示:
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation
from keras.optimizers import SGD
import pandas as pd
import numpy as np
model = Sequential()
model.add(Dense(128, input_dim=2, init='uniform'))
model.add(Activation('relu'))
model.add(Dense(128, init='uniform'))
model.add(Activation('relu'))
model.add(Dense(2, init='uniform'))
model.add(Activation('sigmoid'))
sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss='categorical_crossentropy',
optimizer=sgd,metrics=['accuracy'])
model.summary()
model.fit(data, pd.get_dummies(label),nb_epoch=500,batch_size=data.shape[0])
model.evaluate(data, pd.get_dummies(label))
predictions=np.argmax(model.predict(data),axis=1) #OR
predictions=model.predict_classes(data)
# Epoch 500/500
#1000/1000 [==============================] - 0s 5us/step - loss: 0.6897 - acc: 0.9990
神经网络分类输出:
请记住,您将调整神经网络架构和超参数: