Код: Выделить всё
results = scipy.optimize.minimize(
fun=get_loss_and_grads,
x0=params,
jac=True,
method='L-BFGS-B',
options={'maxiter': 20001, 'disp': True, 'ftol': 0.0, 'gtol': 0.0, 'maxcor': 50},
callback=PrintLossCallback(print_freq=100)
)
Код: Выделить всё
def get_loss_and_grads(params):
shapes = [w.shape for w in model.get_weights()]
split_params = np.split(params, np.cumsum([np.prod(shape) for shape in shapes[:-1]]))
weights = [tf.Variable(param.reshape(shape)) for param, shape in zip(split_params, shapes)]
model.set_weights(weights)
with tf.GradientTape() as tape:
loss = combined_loss(X_train_tf[:, 0:1], X_train_tf[:, 1:2], x_init_tf, t_init_tf, u_initial_tf, x_bc_tf, t_bc_tf)
gradients = tape.gradient(loss, model.trainable_variables)
flattened_gradients = np.concatenate([grad.numpy().flatten() for grad in gradients])
return loss.numpy().astype(np.float64), flattened_gradients
X_train_tf = tf.convert_to_tensor(X_train.astype(np.float32))
x_init_tf = tf.convert_to_tensor(x_init.astype(np.float32))
t_init_tf = tf.convert_to_tensor(t_init.astype(np.float32))
u_initial_tf = tf.convert_to_tensor(u_initial.astype(np.float32))
x_bc_tf = tf.convert_to_tensor(x_bc.astype(np.float32))
t_bc_tf = tf.convert_to_tensor(t_bc.astype(np.float32))
shapes = [w.shape for w in model.get_weights()]
params = np.concatenate([w.flatten() for w in model.get_weights()])
class PrintLossCallback:
def __init__(self, print_freq=100):
self.print_freq = print_freq
self.iteration = 0
def __call__(self, xk):
self.iteration += 1
if self.iteration % self.print_freq == 0:
loss, _ = get_loss_and_grads(xk)
print(f"Iteration {self.iteration}, Loss: {loss}")
start_time = time.time()
results = scipy.optimize.minimize(
fun=get_loss_and_grads,
x0=params,
jac=True,
method='L-BFGS-B',
options={'maxiter': 20001, 'disp': True, 'ftol': 0.0, 'gtol': 0.0, 'maxcor': 50},
callback=PrintLossCallback(print_freq=100)
)
optimized_params = results.x
split_params = np.split(optimized_params, np.cumsum([np.prod(shape) for shape in shapes[:-1]]))
model.set_weights([param.reshape(shape) for param, shape in zip(split_params, shapes)])
elapsed_time = time.time() - start_time
elapsed_minutes = elapsed_time / 60.0 # Convert seconds to minutes
print(f"Execution time: {elapsed_minutes:.2f} minutes")
Самый лучший,
Саиф Ур Рехман.
Я старался установить для 'ftol': 0.0, 'gtol': 0.0 и maxiter большое число, например 200001,, но все напрасно.
Подробнее здесь: https://stackoverflow.com/questions/786 ... completely