initial commit

This commit is contained in:
ІО-23 Шмуляр Олег 2025-10-04 21:03:58 +03:00
commit 99b144c3c7
11 changed files with 239 additions and 0 deletions

51
generic.py Normal file
View File

@ -0,0 +1,51 @@
import tensorflow as tf
import numpy as np
from matplotlib import pyplot as plt
import itertools as it
from sklearn.metrics import r2_score
from math import sin
def f(x, y): return (1 + sin(x**2 + 5*y)) / 2
def train_generic(model, marker):
model.compile(optimizer = "adam", loss = "mse")
X = np.linspace(0, 10, 300)
Y = np.linspace(0, 10, 300)
ins = np.array(list(it.product(X, Y)))
ous = np.array(list(f(*i) for i in ins))
result = model.fit(ins, ous, epochs = 200, batch_size = 2048)
model.save_weights(f"save-{marker}.weights.h5")
def verify_generic(model, marker):
model.compile(optimizer = "adam", loss = "mse")
model.load_weights(f"save-{marker}.weights.h5")
X = np.linspace(0, 10, 50)
Y = np.linspace(0, 10, 50)
ins = np.array(list(it.product(X, Y)))
ous = np.array(list(f(*i) for i in ins))
preds = model.predict(ins)
print(f"Model {marker} has {r2_score(ous, preds)} r2 score")
preds_flat = [i[0] for i in preds]
px = np.array([X] * len(X))
py = np.array([[x] * (len(X)) for x in X])
pz1 = np.array([ous[i*len(X):(i+1)*len(X)] for i, _ in enumerate(X)])
pz2 = np.array([preds_flat[i*len(X):(i+1)*len(X)] for i, _ in enumerate(X)])
#print([ous[i*len(X):(i+1)*len(X)] for i, _ in enumerate(X)])
p = plt.figure().add_subplot(projection='3d')
p.plot_surface(px, py, pz1, edgecolor = "lime", alpha = 0.1)
p.plot_surface(px, py, pz2, edgecolor = "blue", alpha = 0.3)
plt.show()

13
nn1-normal.py Normal file
View File

@ -0,0 +1,13 @@
import tensorflow as tf
import tensorflow.keras.layers as l
import generic as g
m = tf.keras.models.Sequential([
l.Input(shape = (2,)),
l.Dense(5, activation='relu'),
l.Dense(1)
])
#g.train_generic(m, "ff")
g.verify_generic(m, "ff")

23
nn10-elman.py Normal file
View File

@ -0,0 +1,23 @@
import tensorflow as tf
import tensorflow.keras.layers as l
import generic as g
m = tf.keras.models.Sequential([
l.Input(shape = (2, 1)),
l.SimpleRNN(100, activation = "relu", return_sequences = True),
l.SimpleRNN(100, activation = "relu", return_sequences = True),
l.SimpleRNN(100, activation = "relu"),
l.Dense(1)
])
#i = l.Input(shape = (2,))
#h1 = l.SimpleRNN(10, activation = "relu", input_shape = (2, 1))(i)
#o = l.Dense(1)(h1)
#m = tf.keras.models.Model(inputs = i, outputs = o)
m.summary()
#g.train_generic(m, "r3")
g.verify_generic(m, "r3")

13
nn2-normal.py Normal file
View File

@ -0,0 +1,13 @@
import tensorflow as tf
import tensorflow.keras.layers as l
import generic as g
m = tf.keras.models.Sequential([
l.Input(shape = (2,)),
l.Dense(1000, activation = "relu"),
l.Dense(1)
])
#g.train_generic(m, "ff2")
g.verify_generic(m, "ff2")

22
nn3-normal.py Normal file
View File

@ -0,0 +1,22 @@
import tensorflow as tf
import tensorflow.keras.layers as l
import generic as g
m = tf.keras.models.Sequential([
l.Input(shape = (2,)),
l.Dense(100, activation = "relu"),
l.Dense(100, activation = "relu"),
l.Dense(100, activation = "relu"),
l.Dense(100, activation = "relu"),
l.Dense(100, activation = "relu"),
l.Dense(100, activation = "relu"),
l.Dense(100, activation = "relu"),
l.Dense(100, activation = "relu"),
l.Dense(100, activation = "relu"),
l.Dense(100, activation = "relu"),
l.Dense(1)
])
#g.train_generic(m, "ff3")
g.verify_generic(m, "ff3")

17
nn4-normal.py Normal file
View File

@ -0,0 +1,17 @@
import tensorflow as tf
import tensorflow.keras.layers as l
import generic as g
m = tf.keras.models.Sequential([
l.Input(shape = (2,)),
l.Dense(500, activation = "relu"),
l.Dense(500, activation = "relu"),
l.Dense(500, activation = "relu"),
l.Dense(500, activation = "relu"),
l.Dense(500, activation = "relu"),
l.Dense(1)
])
#g.train_generic(m, "ff4")
g.verify_generic(m, "ff4")

19
nn5-cascade.py Normal file
View File

@ -0,0 +1,19 @@
import tensorflow as tf
import tensorflow.keras.layers as l
import generic as g
i = l.Input(shape = (2,))
h1 = l.Dense(20, activation='relu')(i)
y = l.Dense(1)(h1)
co = l.Dense(1)(i)
o = l.Add()([y, co])
m = tf.keras.models.Model(inputs = i, outputs = o)
#g.train_generic(m, "c1")
g.verify_generic(m, "c1")

19
nn6-cascade.py Normal file
View File

@ -0,0 +1,19 @@
import tensorflow as tf
import tensorflow.keras.layers as l
import generic as g
i = l.Input(shape = (2,))
h1 = l.Dense(1000, activation='relu')(i)
y = l.Dense(1)(h1)
co = l.Dense(1)(i)
o = l.Add()([y, co])
m = tf.keras.models.Model(inputs = i, outputs = o)
#g.train_generic(m, "c2")
g.verify_generic(m, "c2")

24
nn7-cascade.py Normal file
View File

@ -0,0 +1,24 @@
import tensorflow as tf
import tensorflow.keras.layers as l
import generic as g
i = l.Input(shape = (2,))
h1 = l.Dense(100, activation='tanh')(i)
h2 = l.Dense(100, activation='tanh')(h1)
h3 = l.Dense(100, activation='tanh')(h2)
h4 = l.Dense(100, activation='tanh')(h3)
h5 = l.Dense(100, activation='tanh')(h4)
h6 = l.Dense(100, activation='tanh')(h5)
y = l.Dense(1)(h6)
co = l.Dense(1)(i)
o = l.Add()([y, co])
m = tf.keras.models.Model(inputs = i, outputs = o)
#g.train_generic(m, "c3")
g.verify_generic(m, "c3")

19
nn8-elman.py Normal file
View File

@ -0,0 +1,19 @@
import tensorflow as tf
import tensorflow.keras.layers as l
import generic as g
m = tf.keras.models.Sequential([
l.Input(shape = (2, 1)),
l.SimpleRNN(10, activation = "relu"),
l.Dense(1)
])
#i = l.Input(shape = (2,))
#h1 = l.SimpleRNN(10, activation = "relu", input_shape = (2, 1))(i)
#o = l.Dense(1)(h1)
#m = tf.keras.models.Model(inputs = i, outputs = o)
#g.train_generic(m, "r1")
g.verify_generic(m, "r1")

19
nn9-elman.py Normal file
View File

@ -0,0 +1,19 @@
import tensorflow as tf
import tensorflow.keras.layers as l
import generic as g
m = tf.keras.models.Sequential([
l.Input(shape = (2, 1)),
l.SimpleRNN(1000, activation = "relu"),
l.Dense(1)
])
#i = l.Input(shape = (2,))
#h1 = l.SimpleRNN(10, activation = "relu", input_shape = (2, 1))(i)
#o = l.Dense(1)(h1)
#m = tf.keras.models.Model(inputs = i, outputs = o)
#g.train_generic(m, "r2")
g.verify_generic(m, "r2")