strojové učení / машинне навчання (Python)
strojové učení / машинне навчання (Python)
import numpy as np
from keras.models import Sequential
from keras.layers import Dense
import matplotlib.pyplot as plt
x = np.linspace(-5, 5, 1000)
y = x**2
model = Sequential()
model.add(Dense(10, input_dim = 1, activation= "relu"))
model.add(Dense(1))
model.compile(loss = "mse" , optimizer="adam")
model.fit(x, y, epochs = 10000, verbose =0)
<keras.callbacks.History at 0x7f077d1028f0>
plt.plot(x, y, label = "ground truth")
plt.plot(x, model.predict(x), label = "predicted")
32/32 [==============================] - 0s 1ms/step
[<matplotlib.lines.Line2D at 0x7f077d886320>]
x_new = np.array([-2, -1, 0, 1, 2 ])
y_real1 = x_new ** 2
x_new = x_new.reshape( -1, 1)
y_pred = model.predict(x_new)
for i in range(x_new)):
print(f"{x_new[i][0]:.2f} ")