用序贯模型实现
'''
用keras实现非线性回归
用Sequential
'''
import keras
import numpy as np
from keras.models import Sequential
from keras.layers import Dense
from keras.optimizers import SGD
import matplotlib.pyplot as plt
x = np.linspace(-0.5, 0.5, 200)
noise = np.random.normal(loc=0, scale=0.01, size=x.shape)
y = np.square(x) + noise
model = Sequential()
model.add(Dense(units=10, input_dim=1, activation='tanh'))
model.add(Dense(units=1, activation='tanh'))
sgd = SGD(lr=0.3) # 如果没有这一句,下面compile中的optimizer='sgd',注意要加引号
model.compile(
loss='mse',
optimizer=sgd
)
model.summary()
for step in range(3000):
cost = model.train_on_batch(x,y)
if step%500==0:
print("cost:",cost)
y_pred = model.predict(x)
plt.scatter(x,y)
plt.plot(x, y_pred, 'r-')
plt.show()
用函数式模型实现
'''
用keras实现非线性回归
'''
import keras
import numpy as np
from keras.layers import Dense, Input
from keras.models import Model
import matplotlib.pyplot as plt
from keras.optimizers import SGD
inputs = Input(shape=(1,), dtype='float32', name="inputLayer")
dense = Dense(units=10, input_dim=1, activation='tanh')(inputs)
dense = Dense(units=1, activation='tanh')(dense)
model = Model(inputs=inputs, outputs=dense)
sgd = SGD(lr=0.3)
model.compile(
loss='mse',
optimizer=sgd,
metrics=['accuracy']
)
model.summary()
# 生成输入数据
x = np.linspace(-0.5, 0.5, 200) # 在-0.5到0.5区间取值范围内生成200个数
noise = np.random.normal(loc=0, scale=0.01, size=x.shape)
y = np.square(x) + noise
model.fit(x, y, epochs=3000, verbose=0)
# 下面这种在Sequential()才有用
# for step in range(3001):
# cost = model.train_on_batch(x, y)
# if step%500==0:
# print("cost:",cost)
w, b = model.layers[1].get_weights()
print("weights_1 = ",w, "bias_1 = ",b)
w, b = model.layers[2].get_weights()
print("weights_2 = ",w, "bias_2 = ",b)
y_pred = model.predict(x)
plt.scatter(x, y)
plt.plot(x, y_pred, 'r-', lw=3)
plt.show()
效果如下图