import numpy as np
np.random.seed(1337)
from keras.models import Sequential
from keras.layers import Dense
import matplotlib.pyplot as plt
X=np.linspace(-1,1,200)
np.random.shuffle(X)
Y=0.5 * X +2 +np.random.normal(0,0.05,(200,))
plt.scatter(X,Y)
plt.show()
X_train,Y_train = X[:160],Y[:160]
X_test, Y_test = X[160:],Y[160:]
#构建神经网络模型
model = Sequential()
model.add(Dense(input_dim=1,units=1))
#选定loss和优化器
model.compile(loss='mse', optimizer='sgd')
print('Training ------------')
for step in range(501):
cost = model.train_on_batch(X_train, Y_train)
if step % 50 ==0:
print("After %d trainings, the cost: %f" % (step,cost))
print('\nTesting ----------')
cost = model.evaluate(X_test, Y_test, batch_size=40)
print('test cost:', cost)