deeponet
deeponet copied to clipboard
Follow up question for #32
Hi,
As suggested in the reply for #32, I modified the function safe_test
y_pred.append(model.predict(dde.nn.tensorflow_compat_v1.nn.NN.apply_feature_transform(self,X_add)))
but it gives an error saying
TypeError: apply_feature_transform() missing 1 required positional argument: 'transform'
First of all, I am not sure if this is how I am supposed to change the code based on the answer for #32.
If I put apply_feature_transform(self,X_add))
,
and it gives
NameError: name 'self' is not defined
I see in nn.py, self is defined. What should I do in this case?
Please refer the full code below. Thanks.
def safe_test(model, data, X_test, y_test, fname=None):
def is_nonempty(X):
return len(X[0]) > 0 if isinstance(X, (list, tuple)) else len(X) > 0
y_pred = []
X = X_test
while is_nonempty(X):
X_add, X = trim_to_65535(X)
#Original Code
#y_pred.append(model.predict(data.transform_inputs(X_add)))
#Modified Code
y_pred.append(model.predict(dde.nn.tensorflow_compat_v1.nn.NN.apply_feature_transform(self,X_add)))
y_pred = np.vstack(y_pred)
error = np.mean((y_test - y_pred) ** 2)
print("Test MSE: {}".format(error))
error = mean_squared_error_outlier(y_test, y_pred)
print("Test MSE w/o outliers: {}\n".format(error))
if fname is not None:
np.savetxt(fname, np.hstack((X_test[1], y_test, y_pred)))
# build a net then train it.
def periodic(inputs): # just a function
inputs *= 2 * np.pi
out = tf.concat([tf.cos(inputs), tf.sin(inputs), tf.cos(2 * inputs), tf.sin(2 * inputs)], 1)
return out
def main(_,_):
net = MIONetIn4CartesianProd(
[m, w*2, w*2, m*2], [m, w*2, w*2, m*2],
[m*_r, w*2, w*2, m*2], [m*_r, w*2, w*2, m*2],
[2, w, w, m],
activation,
"Glorot normal")
net.apply_feature_transform(periodic) # without self
if model trained, just predict with model.preict(input_data),
because input feature layer is in the graph of the net.
# so, code may be like this:
y_pred = model.predict(X_add)