Readme updated sklearn

main
Gerardo Marx 5 days ago
parent d148e18301
commit 479d457486

@ -13,7 +13,7 @@ celsius = np.array([-40, -10, 0, 8, 15, 22, 38], dtype=float)
fahrenheit = np.array([-40, 14, 32, 46, 59, 72, 100], dtype=float)
# option 2: (X°C x 9/5) + 32 = 41 °F
points = 100
points = 1000
np.random.seed(99)
dataIn = np.linspace (-40,60, points)
target = dataIn*9/5 + 32 +4*np.random.randn(points)
@ -94,8 +94,7 @@ from tensorflow.keras.optimizers import Adam
#hyper parameters
epoch = 500
lr = 0.01
hn = 2 # hidden nodes
tf.random.set_seed(42) # For TensorFlow
tf.random.set_seed(99) # For TensorFlow
model.compile(optimizer=Adam(lr), loss='mean_squared_error')
@ -109,25 +108,50 @@ print("Model trainned!")
```python
plt.plot(historial.epoch, historial.history['loss'], '.k' )
plt.show()
```
![png](Readme_files/Readme_5_0.png)
```python
predict = model.predict(dataIn)
plt.plot(dataIn, predict, ':r', label='estimated')
plt.plot(dataIn,target, '.b', label='real', alpha=0.4)
plt.plot(dataIn, predict, '-r', label='estimated')
plt.plot(dataIn,target, '.b', label='real', alpha=0.1)
#plt.xlim([0, 20])
#plt.ylim([32, 39])
plt.legend()
plt.grid()
plt.show()
```
4/4 ━━━━━━━━━━━━━━━━━━━━ 0s 4ms/step
32/32 ━━━━━━━━━━━━━━━━━━━━ 0s 741us/step
![png](Readme_files/Readme_5_1.png)
![png](Readme_files/Readme_6_1.png)
```python
for layer in model.layers:
print(layer.get_weights())
```
[array([[ 0.7760521 , -0.18955402]], dtype=float32), array([8.428659, 8.034532], dtype=float32)]
[array([[2.4613197 ],
[0.63733613]], dtype=float32), array([6.2560296], dtype=float32)]
```python
# Get weights
for layer in model.layers:
@ -139,15 +163,49 @@ for layer in model.layers:
Layer: hidden
Weights (Kernel): (1, 2)
[[-0.27738443 0.7908125 ]]
[[ 0.7760521 -0.18955402]]
Biases: (2,)
[-8.219968 6.714554]
[8.428659 8.034532]
Layer: output
Weights (Kernel): (2, 1)
[[-1.9934888]
[ 1.5958738]]
[[2.4613197 ]
[0.63733613]]
Biases: (1,)
[5.1361823]
[6.2560296]
```python
inData = np.array([100])
model.predict(inData)
```
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 25ms/step
array([[211.05261]], dtype=float32)
```python
wih = np.array([[ 0.7760521, -0.18955402]])
bh = np.array([8.428659, 8.034532])
Xh = np.dot(inData, wih) + bh
who = np.array([[2.4613197 ],[0.63733613]])
bo = np.array([6.2560296])
O = np.dot(Xh,who) + bo
O
```
array([211.05262121])
# Testing the model
@ -158,13 +216,13 @@ inTest = np.array([100])
model.predict(inTest)
```
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 87ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 22ms/step
array([[213.73816]], dtype=float32)
array([[115.929985]], dtype=float32)
@ -188,6 +246,91 @@ Oo
# sklearn
```python
from sklearn.neural_network import MLPRegressor
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
import numpy as np
# Datos de ejemplo
# Escalado de los datos
scaler_X = StandardScaler()
scaler_y = StandardScaler()
X_scaled = scaler_X.fit_transform(dataIn.reshape(-1,1))
y_scaled = scaler_y.fit_transform(target.reshape(-1, 1)).ravel()
# Modelo equivalente al de Keras
mlp = MLPRegressor(
hidden_layer_sizes=(2,), # 1 capa oculta con 2 neuronas
activation='identity', # activación lineal
learning_rate_init=0.001, # 👈 Learning rate
solver='adam',
max_iter=1000,
tol=1e-6,
random_state=4
)
# Entrenar modelo
mlp.fit(X_scaled, y_scaled)
# Predicción
y_pred_scaled = mlp.predict(X_scaled)
y_pred = scaler_y.inverse_transform(y_pred_scaled.reshape(-1, 1))
# Visualizar resultados (opcional)
import matplotlib.pyplot as plt
plt.scatter(dataIn, target, label="Original data")
plt.plot(dataIn, y_pred, color='red', label="MLPRegressor output")
plt.legend()
plt.show()
```
![png](Readme_files/Readme_15_0.png)
```python
plt.plot(mlp.loss_curve_,'.k')
plt.xlabel("Épocas")
plt.ylabel("Error (loss)")
plt.title("Evolución del error en entrenamiento")
plt.grid(True)
plt.show()
```
![png](Readme_files/Readme_16_0.png)
```python
print("Pesos entre capa de entrada y oculta:", mlp.coefs_[0])
print("Pesos entre capa oculta y salida:", mlp.coefs_[1])
print("Bias de capa oculta:", mlp.intercepts_[0])
print("Bias de salida:", mlp.intercepts_[1])
```
Pesos entre capa de entrada y oculta: [[ 1.70549238 -0.37235861]]
Pesos entre capa oculta y salida: [[ 0.30934654]
[-1.25842791]]
Bias de capa oculta: [1.02819949 1.02732046]
Bias de salida: [0.97683886]
# Model scheme
```python
def generate_ascii_ann(model):

Binary file not shown.

After

Width:  |  Height:  |  Size: 27 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 18 KiB

After

Width:  |  Height:  |  Size: 39 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 39 KiB

Loading…
Cancel
Save