import random
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import mean_squared_error
from keras.models import Sequential
from keras.layers import Dense
from numpy import asarray
from matplotlib import pyplot
import math
import numpy
define the dataset
x =numpy.arange(-3.14,3.14, 0.005)
y = asarray([math.sin(i)+ random.uniform(-0.3, 0.3) for i in x])
print(x.min(), x.max(), y.min(), y.max())
reshape arrays into into rows and cols
x = x.reshape((len(x), 1))
y = y.reshape((len(y), 1))
separately scale the input and output variables
scale_x = MinMaxScaler()
x = scale_x.fit_transform(x)
scale_y = MinMaxScaler()
y = scale_y.fit_transform(y)
print(x.min(), x.max(), y.min(), y.max())
design the neural network model
model = Sequential()
model.add(Dense(10, input_dim=1, activation='relu', kernel_initializer='he_uniform'))
model.add(Dense(10, activation='sigmoid', kernel_initializer='he_uniform'))
model.add(Dense(1))
define the loss function and optimization algorithm
model.compile(loss='mse', optimizer='adam')
ft the model on the training dataset
model.fit(x, y, epochs=500, batch_size=10, verbose=0)
make predictions for the input data
yhat = model.predict(x)
inverse transforms
x_plot = scale_x.inverse_transform(x)
y_plot = scale_y.inverse_transform(y)
yhat_plot = scale_y.inverse_transform(yhat)
report model error
print('MSE: %.3f' % mean_squared_error(y_plot, yhat_plot))
plot x vs y
pyplot.scatter(x_plot,y_plot, label='Actual')
plot x vs yhat
pyplot.scatter(x_plot,yhat_plot, label='Predicted')
pyplot.title('Input (x) versus Output (y) Sin Function')
pyplot.xlabel('Input Variable (x)')
pyplot.ylabel('Output Variable (y)')
pyplot.legend()
pyplot.show()
En esta página del sitio puede ver el video en línea Robust Sine Function Fitting de Duración hora minuto segunda en buena calidad , que subió el usuario Mutluhan özkan 28 febrero 2022, comparta el enlace con amigos y conocidos, en youtube este video ya ha sido visto 44 veces y le gustó 0 a los espectadores. Disfruta viendo!