Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/314.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.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_Keras_Loss Function_Cross Entropy - Fatal编程技术网

Python 对一系列图像使用分类交叉熵

Python 对一系列图像使用分类交叉熵,python,tensorflow,keras,loss-function,cross-entropy,Python,Tensorflow,Keras,Loss Function,Cross Entropy,我有一个模型,它接受一系列图像作为输入(无,n步,128,128)(而不是单个图像),其中n步是一个固定数字10。我用分类交叉熵对四类问题进行分类。但我有一个错误,如下所示 ValueError: A target array with shape (1342, 10, 4) was passed for an output of shape (None, 1, 4) while using as loss `categorical_crossentropy`. This loss expect

我有一个模型,它接受一系列图像作为输入
(无,n步,128,128)
(而不是单个图像),其中
n步
是一个固定数字
10
。我用分类交叉熵对四类问题进行分类。但我有一个错误,如下所示

ValueError: A target array with shape (1342, 10, 4) was passed for an output of shape (None, 1, 4) while using as loss `categorical_crossentropy`. This loss expects targets to have the same shape as the output.
我从错误中了解到,它一次只能看到一张图像。有没有什么方法可以让我把这个损失用于一系列图像

模型的输出也将是一组
10
标签

编辑:


输入是用于回归任务的图像及其相关信息,输出是标签和下一个可预测值。

问题在于输入到分类任务的密集和重塑层。由于输入在
(10128128)
中,因此单位数应为
10*num\u class
。所以改变这个

x_reg = (Dense(8, activation='relu', kernel_regularizer=regularizers.l2(0.001)))(x)
x_class = (Dense(8, activation='relu', kernel_regularizer=regularizers.l2(0.001)))(x)

x_reg = Reshape((2, 4))(x_reg)

output_regression = (Dense(2, name='main_output'))(x_reg)
output_class = (Dense(4, name='classification_output', activation='softmax'))(x_class)
这就解决了问题

x_reg = (Dense(8, activation='relu', kernel_regularizer=regularizers.l2(0.001)))(x)
x_class = (Dense(40, activation='relu', kernel_regularizer=regularizers.l2(0.001)))(x)

x_reg = Reshape((2, 4))(x_reg)
x_class = Reshape((10,4))(x_class)

output_regression = (Dense(2, name='main_output'))(x_reg)
output_class = (Dense(4, name='classification_output', activation='softmax'))(x_class)

你是如何调用分类交叉熵的?我有两个输出(分类和回归),所以对于分类,我只是在编译中使用它,就像这个
模型一样。编译(optimizer=adam,loss={'main_output':'mse','classification_output':'categorical_crossentropy')
我仍然希望得到更多的信息,例如,关于模型的输入。这个qu在问题中有更多的信息,在回答中有线索,我能够解决这个问题。谢谢如果你的问题对将来的人有帮助,请随时回答
x_reg = (Dense(8, activation='relu', kernel_regularizer=regularizers.l2(0.001)))(x)
x_class = (Dense(40, activation='relu', kernel_regularizer=regularizers.l2(0.001)))(x)

x_reg = Reshape((2, 4))(x_reg)
x_class = Reshape((10,4))(x_class)

output_regression = (Dense(2, name='main_output'))(x_reg)
output_class = (Dense(4, name='classification_output', activation='softmax'))(x_class)