我正在嘗試在我的Keras順序模型 (TensorFlow 2.6.0) 中使用自定義損失函式。這種自定義損失(理想情況下)將計算資料損失加上物理方程的殘差(例如,擴散方程、Navier Stokes 等)。這個殘差基于模型輸出導數及其輸入,我想使用GradientTape.
在這個 MWE 中,我洗掉了資料丟失項和其他方程丟失,只使用輸出與其輸入的導數。資料集可以在這里找到。
from numpy import loadtxt
from keras.models import Sequential
from keras.layers import Dense
import tensorflow as tf #tf.__version__ = '2.6.0'
# load the dataset
dataset = loadtxt('pima-indians-diabetes.csv', delimiter=',')
# split into input (X) and output (y) variables
X = dataset[:,0:8] #X.shape = (768, 8)
y = dataset[:,8]
X = tf.convert_to_tensor(X, dtype=tf.float32)
y = tf.convert_to_tensor(y, dtype=tf.float32)
def customLoss(y_true,y_pred):
x_tensor = tf.convert_to_tensor(model.input, dtype=tf.float32)
# x_tensor = tf.cast(x_tensor, tf.float32)
with tf.GradientTape() as t:
t.watch(x_tensor)
output = model(x_tensor)
DyDX = t.gradient(output, x_tensor)
dy_t = DyDX[:, 5:6]
R_pred=dy_t
# loss_data = tf.reduce_mean(tf.square(yTrue - yPred), axis=-1)
loss_PDE = tf.reduce_mean(tf.square(R_pred))
return loss_PDE
model = Sequential()
model.add(Dense(12, input_dim=8, activation='relu'))
model.add(Dense(12, activation='relu'))
model.add(Dense(12, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss=customLoss, optimizer='adam', metrics=['accuracy'])
model.fit(X, y, epochs=15)
執行后,我得到這個ValueError:
ValueError: Passed in object of type <class 'keras.engine.keras_tensor.KerasTensor'>, not tf.Tensor
當我更改loss=customLoss為 時loss='mse',模型開始訓練,但使用它customLoss才是重點。有任何想法嗎?
uj5u.com熱心網友回復:
問題似乎來自損失函式中的model.input,如果我正確理解您的代碼,您可以使用 loss :
def custom_loss_pass(model, x_tensor):
def custom_loss(y_true,y_pred):
with tf.GradientTape() as t:
t.watch(x_tensor)
output = model(x_tensor)
DyDX = t.gradient(output, x_tensor)
dy_t = DyDX[:, 5:6]
R_pred=dy_t
# loss_data = tf.reduce_mean(tf.square(yTrue - yPred), axis=-1)
loss_PDE = tf.reduce_mean(tf.square(R_pred))
return loss_PDE
return custom_loss
進而:
model.compile(loss=custom_loss_pass(model, X), optimizer='adam', metrics=['accuracy'])
我不確定它是否能滿足您的要求,但至少它有效!
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/323912.html
