Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 具有多个输入以进行验证的自定义损失函数_Python_Tensorflow_Machine Learning_Keras_Functional Api - Fatal编程技术网

Python 具有多个输入以进行验证的自定义损失函数

Python 具有多个输入以进行验证的自定义损失函数,python,tensorflow,machine-learning,keras,functional-api,Python,Tensorflow,Machine Learning,Keras,Functional Api,我正在按照找到的指令创建自定义损失函数。当我添加validation_数据时,我会收到一条关于ValueError的错误消息。当我设置validation_data=None时,此错误消失。我在Stackoverflow上找到了,但我认为我的问题不同,因为我试图使用一个自定义的丢失函数 这是我的密码: from tensorflow.keras.layers import * from tensorflow.keras.models import Model import numpy as np

我正在按照找到的指令创建自定义损失函数。当我添加validation_数据时,我会收到一条关于ValueError的错误消息。当我设置validation_data=None时,此错误消失。我在Stackoverflow上找到了,但我认为我的问题不同,因为我试图使用一个自定义的丢失函数

这是我的密码:

from tensorflow.keras.layers import *
from tensorflow.keras.models import Model
import numpy as np
import tensorflow.keras.backend as K
from tensorflow.keras import regularizers

def loss_fcn(y_true, y_pred, w):
    loss = K.mean(K.square((y_true-y_pred)*w))
    return loss

# since tensor flow sets the batch_size default to 32.  The number of samples have to be a multiple of 32 when it is great than 32.
data_x = np.random.rand(32, 51)
data_w = np.random.rand(32, 5)
data_y = np.random.rand(32, 5)

val_x = np.random.rand(4, 51)
val_w = np.random.rand(4, 5)
val_y = np.random.rand(4, 5)

input_x = Input(shape=(51,), name="input")
y_true = Input(shape=(5,), name="true_y")
w = Input(shape=(5,), name="weights")

out = Dense(128, kernel_regularizer=regularizers.l2(0.001), name="HL1")(input_x)
y = Dense(5, name="HL2", activation="tanh")(out)

model = Model(inputs=[input_x, y_true, w], outputs=y)
model.add_loss(loss_fcn(y_true, y, w))

model.compile()
model.fit((data_x, data_y, data_w), validation_data=(val_x, val_y, val_w))
错误消息:

ValueError: Error when checking model input: the list of Numpy arrays that you are passing to your model is not the size the model expected. Expected to see 3 array(s), but instead got the following list of 1 arrays: [array([[0.74785946, 0.63599707, 0.45929641, 0.98855504, 0.84815295,
        0.28217452, 0.93502174, 0.23942027, 0.11885888, 0.32092279,
        0.47407394, 0.19737623, 0.85962504, 0.35906666, 0.22262...


您的模型有3个输入和一个输出

模型拟合的参数应为:

  • x=3个张量/数组的列表(或元组)
  • y=输出值
  • 验证数据=元组(3个输入、输出值的列表)

将培训和验证数据列为列表,而不是元组:

model.fit([data\u x,data\u y,data\u w],validation\u data=[val\u x,val\u y,val\u w])

该模型不需要输出,因为我将其作为输入传递,以便在自定义损耗函数中使用。例如,如果我设置:model.fit(x=(data_x,data_y,data_w),则模型工作正常。当我传递一个验证参数时,出现了一个问题。经过再三考虑,您的注释非常有用。我复制并粘贴了您的代码。不幸的是,它不能按列表工作。@Andrew我已经检查了colab中的代码及其工作技巧我正在使用我大学的超级计算tf.2.0-gpu。你使用的是谷歌Colab吗?是免费的吗?无论如何,今天下午我会试试可乐。看起来很有趣谷歌Colab工作!非常感谢。