如何搭建一个神经网络
import numpy as np
import matplotlib.pyplot as plt
n = 5000
x = np.random.rand(n,1)-0.5
y = np.random.rand(n,1)-0.5
z = np.sqrt(x*x+y*y)
label = np.zeros((n,3))
label[np.where(z<0.2),0]=1
label[np.where(z>0.4),1]=1
label[np.where((z<=0.4)&(z>=0.2)),2]=1
plt.scatter(x,y,c=label)
X = np.hstack((x,y))
from keras.models import Sequential
from keras.layers import Dense
from keras.optimizers import SGD
model = Sequential()
model.add(Dense(10, activation='relu', input_shape=(2,)))
model.add(Dense(20, activation='relu'))
model.add(Dense(30, activation='relu'))
model.add(Dense(3, activation='softmax'))
model.compile(loss='categorical_crossentropy',optimizer='adam',metrics=['accuracy'])
model.fit(X,label,batch_size=200,epochs=100)
ypred = model.predict(X)
plt.scatter(x,y,c=ypred)