keras简单实现神经网络

import keras
import numpy as np
import matplotlib.pyplot as plt
from keras.models import Sequential
from keras.layers import Dense

# from keras.optimizers import adam

x_data=np.linspace(-0.5,0.5,200)
noise=np.random.normal(0,0.02,x_data.shape)
y_data=np.square(x_data)+noise

# plt.scatter(x_data,y_data)
# plt.show()

model=Sequential()
model.add(Dense(units=10,input_dim=1,activation='relu'))#units为输出,input输入
model.add(Dense(units=1))
model.compile(optimizer='adam',loss='mse')


for step in range(501):
    cost=model.train_on_batch(x_data,y_data)
    if step%100==0:
        print(cost)

y_pred=model.predict(x_data)
plt.scatter(x_data,y_data)
plt.plot(x_data,y_pred)
plt.show()

keras简单实现神经网络_第1张图片

你可能感兴趣的:(机器学习)