2. nonlinear regression

Import keras
 Import numpy AS NP
 Import matplotlib.pyplot AS PLT
 # model Sequential sequence composed of 
from keras.models Import Sequential
 # the Dense layer fully connected 
from keras.layers Import the Dense, Activation
 from keras.optimizers Import the SGD
# Use numpy generated 200 random points 
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 

# Display random point 
plt.scatter (x_data, y_data) 
plt.show ()

# Build a sequential model 
Model = the Sequential ()
 # add a layer fully connected model 
# 1-10-1 
model.add (the Dense (Units = 10, = input_dim. 1, Activation = ' RELU ' ))
 # model.add (Activation ( 'tanh')) 
model.add (the Dense (Units =. 1, Activation = ' RELU ' ))
 # model.add (Activation ( 'tanh')) 
# define optimization algorithm 
SGD the SGD = (LR = 0.3 )
 # sgd: stochastic gradient descent, stochastic gradient descent method 
# MSE: on Mean Squared error, mean square error 
model.compile (SGD = Optimizer, Loss = ' MSE ' ) 

# training 3001 batch
for STEP in Range (3001 ):
     # each training a batch 
    cost = model.train_on_batch (x_data, y_data)
     # per 500 prints a batch cost value 
    IF STEP 500% == 0:
         Print ( ' cost: ' , cost ) 

# x_data network input, a prediction value obtained y_pred 
y_pred = model.predict (x_data) 

# display random point 
plt.scatter (x_data, y_data)
 # displays the forecast 
plt.plot (x_data, y_pred, ' R- ' , LW = 3 ) 
plt.show ()

Guess you like

Origin www.cnblogs.com/liuwenhua/p/11566037.html