Keras/TensorFlow中的自定义损失功能
我正在尝试创建一个无监督的神经网络,该网络可以建模此功能:F(x1,x2)= x1+x2^2。为此,我需要一个自定义损失功能。这是我所拥有的:
import keras
import numpy as np
import pandas as pd
import tensorflow as tf
def custom_loss(X1,X2):
def loss(y_true,y_pred):
# I don't have any values for y_true
z = tf.math.add(X1,tf.math.multiply(X2,X2))
return tf.math.square(tf.math.subtract(z,y_pred))
# The "loss" should be [(X1+X2^2)-y_pred]^2
return loss
df = pd.read_csv('inputs.csv')
INPUT = df.drop(columns=['OUTPUT']).astype(np.float32)
# "INPUT" contains the values for X1 and X2
X1 = df['X1'].astype(np.float32)
# "X1" contains the values for X1 only
X2 = df['X2'].astype(np.float32)
# "X2" contains the values for X2 only
y_true = df['OUTPUT'].astype(np.float32)
# I don't have a y_true. So I made an extra column full of zeros.
model = keras.models.Sequential()
model.add(keras.layers.Dense(20, activation='tanh', input_shape=(2,)))
model.add(keras.layers.Dense(20, activation='tanh'))
model.add(keras.layers.Dense(1, activation='tanh'))
model.compile(optimizer='adam', loss=custom_loss(X1, X2))
model.fit(INPUT, y_true, epochs=400)
test_data = np.array([0.5, 0.6])
print(model.predict(test_data.reshape(1, 2), batch_size=1))
如果代码正确,测试输出应为0.5+(0.6)^2 = 0.86,但完全关闭。我为我的编程差而道歉。我仍然没有经验。非常感谢您的任何帮助。
I am trying to create an unsupervised neural network that can model this function: f(x1,x2) = x1+x2^2. To do this, I need a custom loss function. Here is what I have:
import keras
import numpy as np
import pandas as pd
import tensorflow as tf
def custom_loss(X1,X2):
def loss(y_true,y_pred):
# I don't have any values for y_true
z = tf.math.add(X1,tf.math.multiply(X2,X2))
return tf.math.square(tf.math.subtract(z,y_pred))
# The "loss" should be [(X1+X2^2)-y_pred]^2
return loss
df = pd.read_csv('inputs.csv')
INPUT = df.drop(columns=['OUTPUT']).astype(np.float32)
# "INPUT" contains the values for X1 and X2
X1 = df['X1'].astype(np.float32)
# "X1" contains the values for X1 only
X2 = df['X2'].astype(np.float32)
# "X2" contains the values for X2 only
y_true = df['OUTPUT'].astype(np.float32)
# I don't have a y_true. So I made an extra column full of zeros.
model = keras.models.Sequential()
model.add(keras.layers.Dense(20, activation='tanh', input_shape=(2,)))
model.add(keras.layers.Dense(20, activation='tanh'))
model.add(keras.layers.Dense(1, activation='tanh'))
model.compile(optimizer='adam', loss=custom_loss(X1, X2))
model.fit(INPUT, y_true, epochs=400)
test_data = np.array([0.5, 0.6])
print(model.predict(test_data.reshape(1, 2), batch_size=1))
If the code was correct, the test output should be 0.5+(0.6)^2 = 0.86, but it is totally off. I apologize for my poor programming. I am still very inexperienced. Thank you so much for any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论