Я пишу настраиваемую модель с TF2
class NN(tf.keras.Model):
def __init__(self,
output_dim: int,
controller_dime:int=128,
interface_dim: int=35,
netsize: int=100,
degree: int=20,
k:float=2,
name:str='dnc_rn')->None:
Она полна случайных параметров, которые невозможно обучить! поэтому мне нужно полностью сохранить модель, и я не могу использовать save_weights, потому что поезд каждой модели зависит от ее собственных случайных параметров ...
файл тренера имеет вид:
import numpy as np
import tensorflow as tf
def trainer(model: tf.keras.Model,
loss_fn: tf.keras.losses,
X_train: np.ndarray,
y_train: np.ndarray = None,
optimizer: tf.keras.optimizers = tf.keras.optimizers.Adam(learning_rate=1e-3),
loss_fn_kwargs: dict = None,
epochs: int = 1000000,
batch_size: int = 1,
buffer_size: int = 2048,
shuffle: bool = False,
verbose: bool = True,
show_model_interface_vector: bool = False
) -> None:
"""
Train TensorFlow model.
Parameters
----------
model
Model to train.
loss_fn
Loss function used for training.
X_train
Training batch.
y_train
Training labels.
optimizer
Optimizer used for training.
loss_fn_kwargs
Kwargs for loss function.
epochs
Number of training epochs.
batch_size
Batch size used for training.
buffer_size
Maximum number of elements that will be buffered when prefetching.
shuffle
Whether to shuffle training data.
verbose
Whether to print training progress.
"""
model.show_interface_vector=show_model_interface_vector
# Create dataset
if y_train is None: # Unsupervised model
train_data = X_train
else:
train_data = (X_train, y_train)
train_data = tf.data.Dataset.from_tensor_slices(train_data)
if shuffle:
train_data = train_data.shuffle(buffer_size=buffer_size).batch(batch_size)
# Iterate over epochs
history=[]
for epoch in range(epochs):
if verbose:
pbar = tf.keras.utils.Progbar(target=epochs, width=40, verbose=1, interval=0.05)
# Iterate over the batches of the dataset
for step, train_batch in enumerate(train_data):
if y_train is None:
X_train_batch = train_batch
else:
X_train_batch, y_train_batch = train_batch
with tf.GradientTape() as tape:
preds = model(X_train_batch)
if y_train is None:
ground_truth = X_train_batch
else:
ground_truth = y_train_batch
# Compute loss
if tf.is_tensor(preds):
args = [ground_truth, preds]
else:
args = [ground_truth] + list(preds)
if loss_fn_kwargs:
loss = loss_fn(*args, **loss_fn_kwargs)
else:
loss = loss_fn(*args)
if model.losses: # Additional model losses
loss += sum(model.losses)
grads = tape.gradient(loss, model.trainable_weights)
optimizer.apply_gradients(zip(grads, model.trainable_weights))
if verbose:
loss_val = loss.numpy().mean()
pbar_values = [('loss', loss_val)]
pbar.update(epoch+1, values=pbar_values)
history.append(loss.numpy().mean())
model.show_interface_vector= not show_model_interface_vector
return history
после тренировки очень я пытался сохранить модель, но когда я звоню TF2 .save:
model.save('a.h5')
У меня ошибка:
NotImplementedError: Saving the model to HDF5 format requires the model to be a Functional model or a Sequential model. It does not work for subclassed models, because such models are defined via the body of a Python method, which isn't safely serializable. Consider saving to the Tensorflow SavedModel format (by setting save_format="tf") or using `save_weights`.
Я изменяю на .tf отформатируйте, но еще раз:
ValueError: Model <model2.NN object at 0x11448b390> cannot be saved because the input shapes have not been set. Usually, input shapes are automatically determined from calling .fit() or .predict(). To manually set the shapes, call model._set_inputs(inputs).
но он уже обучен, и если я _set_inputs
ValueError: Cannot infer num from shape (None, 12, 4)
Я не знаю, что мне делать? Я - любитель наук и любителей с TF2. Помогите мне, это важно для моего проекта ...