You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I want to know why the code not to do center the dataset before POD, and also not to do normalize or feature scaling befoere to fit using ANN or GPR?
it try to add to this functionality into to the rom class as following code:
from sklearn.preprocessing import StandardScaler
class ReducedOrderModelNormCenter():
"""
Reduced Order Model class.
This class performs the actual reduced order model using the selected
methods for approximation and reduction.
:param ezyrb.Database database: the database to use for training the
reduced order model.
:param ezyrb.Reduction reduction: the reduction method to use in reduced
order model.
:param ezyrb.Approximation approximation: the approximation method to use
in reduced order model.
:param object scaler_red: the scaler for the reduced variables (eg. modal
coefficients). Default is None.
:cvar ezyrb.Database database: the database used for training the reduced
order model.
:cvar ezyrb.Reduction reduction: the reduction method used in reduced order
model.
:cvar ezyrb.Approximation approximation: the approximation method used in
reduced order model.
:cvar object scaler_red: the scaler for the reduced variables (eg. modal
coefficients).
:Example:
>>> from ezyrb import ReducedOrderModel as ROM
>>> from ezyrb import POD, RBF, Database
>>> pod = POD()
>>> rbf = RBF()
>>> # param, snapshots and new_param are assumed to be declared
>>> db = Database(param, snapshots)
>>> rom = ROM(db, pod, rbf).fit()
>>> rom.predict(new_param)
"""
def __init__(self, database, reduction, approximation,
plugins=None):
self.database = database
self.reduction = reduction
self.approximation = approximation
self.X_mapping = None # For input X
self.Y_mapping = None # For POD coeffs
self.mean_snapshots_matrix = None
if plugins is None:
plugins = []
self.plugins = plugins
def fit(self):
r"""
Calculate reduced space
"""
print("GO into fit training step---------------------------------------:")
import copy
self._full_database = copy.deepcopy(self.database)
# FULL-ORDER PREPROCESSING here
for plugin in self.plugins:
plugin.fom_preprocessing(self)
self.shifted_database = self._full_database
# Compute the database
print("input design parameter mu X configs shape:", self._full_database.parameters_matrix.shape)
print("input snapshots Y shape:", self._full_database.snapshots_matrix.shape)
# =======================
# Step 1: Center snapshots
# =======================
print("DO Center snapshots ---------------------------------------------:")
Y_snapshots = self._full_database.snapshots_matrix
# self.mean_snapshots_matrix = np.mean(self._full_database.snapshots_matrix, axis=0) # shape (201,)
# Y_snapshots_centered = self._full_database.snapshots_matrix - self.mean_snapshots_matrix # subtract mean row-wise
# print(" mean_snapshots_matrix shape:", self.mean_snapshots_matrix.shape)
# print("Y_snapshots_centered shape:", Y_snapshots_centered.shape)
print("Finished Center snapshots ---------------------------------------:")
# Y_snapshots = self._full_database.snapshots_matrix
# =======================
# Step 2: Perform POD via SVD
# =======================
self.reduction.fit(Y_snapshots.T) # POD or AE
# self.reduction.fit(Y_snapshots_centered.T) # POD or AE
# Compute the reduced space
# bugs
# reduced_snapshots = self.reduction.transform(
# Y_snapshots_centered.T).T
reduced_snapshots = self.reduction.transform(Y_snapshots.T).T
self._reduced_database = Database(
self._full_database.parameters_matrix, reduced_snapshots)
# REDUCED-ORDER PREPROCESSING here
for plugin in self.plugins:
plugin.rom_preprocessing(self)
# REDUCED-ORDER POSTPROCESSING here
print("input design parameter mu X configs reduced shape:", self._reduced_database.parameters_matrix.shape)
print("input snapshots Y redued shape:", self._reduced_database.snapshots_matrix.shape)
X_parameters = self._reduced_database.parameters_matrix #
Y_snapshots = self._reduced_database.snapshots_matrix # centered
# =======================
# Step 3: Normalize X and POD coefficients
# =======================
self.X_mapping = StandardScaler()
X_para_configs_scaled = self.X_mapping.fit_transform(X_parameters)
self.Y_mapping = StandardScaler()
Y_coeffs_scaled = self.Y_mapping.fit_transform(Y_snapshots)
print("input design parameter X scaled configs shape:", X_para_configs_scaled.shape)
print("input snapshots Y scaled reduced shape:", Y_coeffs_scaled.shape)
# =======================
# Step 4: Train GPR or ANN model
# =======================
self.approximation.fit(
X_para_configs_scaled,
Y_coeffs_scaled)
# self.approximation.fit(
# X_parameters,
# Y_snapshots)
return self
def predict(self, mu):
"""
Calculate predicted solution for given mu
:return: the database containing all the predicted solution (with
corresponding parameters).
:rtype: Database
"""
print("GO into predictiion step---------------------------------------:")
mu = np.atleast_2d(mu)
# Y_snapshots = self.approximation.predict(mu)
# =======================
# scaled
# =======================
print("input design parameter mu configs shape:", mu.shape)
mu_scaled = self.X_mapping.transform(mu)
print("input design parameter mu after normilization configs shape:", mu_scaled.shape)
# =======================
# Step 5: Predict POD coefficients for new input
# =======================
# Y_coeffs_pred = self.approximation.predict(mu)
# print("output coeff snapshots Y after prediction shape:", Y_coeffs_pred.shape)
Y_coeffs_scaled_pred = self.approximation.predict(mu_scaled)
print("output coeff snapshots Y after prediction shape:", Y_coeffs_scaled_pred.shape)
# =======================
# Step 6: Reconstruct snapshots
# =======================
# 1. Denormalize back to original scale
Y_coeff_snapshots = self.Y_mapping.inverse_transform(Y_coeffs_scaled_pred)
print(" output coeff snapshots Y after denormalization shape:", Y_coeff_snapshots.shape)
# 2. add the data into database object
# self._reduced_database = Database(
# mu, np.atleast_2d(Y_coeffs_pred))
self._reduced_database = Database(mu, np.atleast_2d(Y_coeff_snapshots))
for plugin in self.plugins:
plugin.rom_postprocessing(self)
# 2. Multiply by POD basis, inverse_transform : self.modes.dot(X) back to full dimension
Y_snapshots = self.reduction.inverse_transform(self._reduced_database.snapshots_matrix.T).T
print("output coeff snapshots Y after inverse transform shape:", Y_snapshots.shape)
# Y_snapshots_centered = self.reduction.inverse_transform(self._reduced_database.snapshots_matrix.T).T
# print("output coeff snapshots Y after inverse transform shape:", Y_snapshots_centered.shape)
# 3. Add the mean back to the snapshots
# print("DO Anti Center snapshots ---------------------------------------:")
# print("Y_snapshots_centered shape:", Y_snapshots_centered.shape)
# print("mean_snapshots_matrix shape:", self.mean_snapshots_matrix.shape)
# Ensure the mean is reshaped as row vector
# mean_row = self.mean_snapshots_matrix.reshape(1, -1)
# print("reshaped mean shape:", mean_row.shape)
# Y_snapshots = Y_snapshots_centered + mean_row
# print("Original snapshot sample[0]:", self._full_database.snapshots_matrix[0])
# print("Centered snapshot sample[0]:", Y_snapshots_centered[0])
# print("Recovered snapshot sample[0]:", Y_snapshots[0])
# print("Finishing Anti Center snapshots ---------------------------------------:")
# 4. add the data into database object
self._full_database = Database(
np.atleast_2d(mu),Y_snapshots
)
# FULL-ORDER POSTPROCESSING here
for plugin in self.plugins:
plugin.fom_postprocessing(self)
return self._full_database
Now, the problem is that, if i try to add the centered step, then the predicted result error is verry big. I test the Normalize step is right by comparing the result with the origin code. I really want to know why i do the wrong step about center and anti- center.
as i new into machine learning, i add the # Y_snapshots = Y_snapshots_centered + mean_row, here, mean_row is the mean vector of training set snapshots's mean( nameling, this line self.mean_snapshots_matrix = np.mean(self._full_database.snapshots_matrix, axis=0) ). why this simple two step, causing my code get wrong results, big error.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
I want to know why the code not to do center the dataset before POD, and also not to do normalize or feature scaling befoere to fit using ANN or GPR?
it try to add to this functionality into to the rom class as following code:
from sklearn.preprocessing import StandardScaler
class ReducedOrderModelNormCenter():
"""
Reduced Order Model class.
Beta Was this translation helpful? Give feedback.
All reactions