input, noise = {ndarry: }
input.size = {tuple: }
希望预测出的非线性函数:x^2
generate 200 training examples:(x,x^2+noise), x between -0.5 and 0.5
import numpy as np
import keras
import matplotlib.pyplot as plt
from keras.models import Sequential
from keras.layers import Dense
from keras.optimizers import SGD
# generate training dataset
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
# change learning rate of the optimizer: sgd
sgd = SGD(lr=0.3)
# build a model which is a DNN. We need a hidden layer to create a more complex function set.
# Also, we need activation function to introduce non-linearity.
# the dim change is 1-10-1
# use_bias = True, which means the total number of parameters (w, b) in this DNN is:
# w: 1x10 + 10x1, b = 10 + 1 so in total = 31
# relu被用在regression任务时,预测出的拟合直线可能不平滑,比较刚硬!
model = Sequential()
model.add(Dense(units=10, input_dim=1, activation='tanh'))
model.add(Dense(units=1, activation='tanh'))
model.compile(optimizer=sgd, loss='mse')
# train the model
for step in range(3001):
# batch = 100, each epoch a batch
cost = model.train_on_batch(x_data, y_data)
if step % 500 == 0:
print('cost:', cost)
# inference
y_pred = model.predict(x_data)
# plot
plt.scatter(x_data, y_data)
plt.plot(x_data, y_pred, 'r_', lw=3)
plt.show()