Keras-regression

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()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值