Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/295.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 加权损失函数:ValueError:维度1的切片索引1超出范围_Python_Tensorflow_Keras_Deep Learning_Loss Function - Fatal编程技术网

Python 加权损失函数:ValueError:维度1的切片索引1超出范围

Python 加权损失函数:ValueError:维度1的切片索引1超出范围,python,tensorflow,keras,deep-learning,loss-function,Python,Tensorflow,Keras,Deep Learning,Loss Function,我试图使用这个损失函数来训练模型,但得到了ValueError:维度1的切片索引1超出了范围。请帮忙 import tensorflow as tf import numpy as np import pandas as pd import seaborn as sns import matplotlib.pyplot as plt import PIL from PIL import Image from keras.preprocessing.image import ImageDataGe

我试图使用这个损失函数来训练模型,但得到了ValueError:维度1的切片索引1超出了范围。请帮忙

import tensorflow as tf
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import PIL
from PIL import Image
from keras.preprocessing.image import ImageDataGenerator
from keras.applications.densenet import DenseNet121
from keras.optimizers import Adam
from keras.layers import Dense, GlobalAveragePooling2D
from keras.models import Model
from tensorflow.keras.utils import to_categorical
from keras import backend as K
import seaborn  
from sklearn.metrics import (
    classification_report,
    confusion_matrix,
    average_precision_score,
    precision_recall_curve,
    roc_auc_score,
    roc_curve,
    f1_score,
)
from IPython.display import display
from keras.models import load_model


"""
print(trainX.shape)
print(trainY.shape)
(8076, 320, 320, 3)
(8076,)
print(validX.shape)
print(validY.shape)
(180, 320, 320, 3)
(180,)
"""
trainY = to_categorical(trainY)
validY = to_categorical(validY)

"""
trainY
array([[1., 0., 0.],
       [1., 0., 0.],
       [1., 0., 0.],
       ...,
       [0., 1., 0.],
       [0., 1., 0.],
       [0., 1., 0.]], dtype=float32)
"""

def compute_class_freqs(labels):
    """
    Compute positive and negative frequences for each class.

    Args:
        labels (np.array): matrix of labels, size (num_examples, num_classes)
    Returns:
        positive_frequencies (np.array): array of positive frequences for each
                                         class, size (num_classes)
        negative_frequencies (np.array): array of negative frequences for each
                                         class, size (num_classes)
    """    
    # total number of patients (rows)
    N = labels.shape[0]
    positive_frequencies = np.sum(labels == 0,axis=0) / N
    negative_frequencies = np.sum(labels == 1,axis=0) / N


    return positive_frequencies, negative_frequencies

freq_pos, freq_neg = compute_class_freqs(trainY)


# freq_pos : array([0.94502229, 0.5282318 , 0.52674591])

pos_weights = freq_neg.astype(np.float32)
neg_weights = freq_pos.astype(np.float32)
“”“ 下面的
weighted_loss
函数返回一个损失函数,用于计算每种损失的加权损失 一批。 回想一下,对于多类别损失,我们将每个类别的平均损失相加。 请注意,我们还希望在计算预测值之前,向预测值添加一个小值$\epsilon$ 日志。 这只是为了避免出现预测值时可能出现的数值错误 是零。 “”“

我在找到第一个纪元之前遇到的错误:

Epoch 1/50
323/324 [============================>.] - ETA: 0s - loss: 0.0101 - precision: 0.9988 - recall: 0.9937 - accuracy: 0.9975
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-26-d49655cfe3a9> in <module>()
      4 print(device_lib.list_local_devices())
      5 
----> 6 hist= model.fit(trainX, trainY, epochs=50, batch_size=25, validation_data= (validX, validY), verbose=1)
      7 hist_df = pd.DataFrame(hist.history)
      8 # hist_csv_file = '/content/drive/MyDrive/Covid-19/Data Augmentation/historyDenseNet121.csv'

12 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/func_graph.py in wrapper(*args, **kwargs)
    971           except Exception as e:  # pylint:disable=broad-except
    972             if hasattr(e, "ag_error_metadata"):
--> 973               raise e.ag_error_metadata.to_exception(e)
    974             else:
    975               raise

ValueError: in user code:

    /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:1224 test_function  *
        return step_function(self, iterator)
    <ipython-input-18-3cb53ecbce51>:30 weighted_loss  *
        loss +=K.mean(-(pos_weights[i] *y_true[:,i] * K.log(y_pred[:,i] + epsilon)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/util/dispatch.py:201 wrapper
        return target(*args, **kwargs)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/array_ops.py:1024 _slice_helper
        name=name)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/util/dispatch.py:201 wrapper
        return target(*args, **kwargs)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/array_ops.py:1196 strided_slice
        shrink_axis_mask=shrink_axis_mask)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/gen_array_ops.py:10352 strided_slice
        shrink_axis_mask=shrink_axis_mask, name=name)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/op_def_library.py:744 _apply_op_helper
        attrs=attr_protos, op_def=op_def)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/func_graph.py:593 _create_op_internal
        compute_device)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py:3485 _create_op_internal
        op_def=op_def)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py:1975 __init__
        control_input_ops, op_def)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py:1815 _create_c_op
        raise ValueError(str(e))

    ValueError: slice index 1 of dimension 1 out of bounds. for '{{node weighted_loss/strided_slice_4}} = StridedSlice[Index=DT_INT32, T=DT_FLOAT, begin_mask=1, ellipsis_mask=0, end_mask=1, new_axis_mask=0, shrink_axis_mask=2](ExpandDims, weighted_loss/strided_slice_4/stack, weighted_loss/strided_slice_4/stack_1, weighted_loss/strided_slice_4/stack_2)' with input shapes: [?,1], [2], [2], [2] and with computed input tensors: input[1] = <0 1>, input[2] = <0 2>, input[3] = <1 1>.
1/50纪元
323/324[=========================>。]-预计到达时间:0s-损失:0.0101-准确度:0.9988-召回率:0.9937-准确度:0.9975
---------------------------------------------------------------------------
ValueError回溯(最近一次调用上次)
在()
4打印(设备库列表本地设备()
5.
---->6 hist=模型拟合(trainX,trainY,epochs=50,批次大小=25,验证数据=(validX,validY),详细度=1)
7历史数据帧(历史记录)
8#hist#u csv_file='/content/drive/MyDrive/Covid-19/Data Augmentation/historydenseneet121.csv'
12帧
/包装器中的usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/func_graph.py(*args,**kwargs)
971例外情况为e:#pylint:disable=broad except
972如果hasattr(e,“AGU错误元数据”):
-->973将e.ag\u错误\u元数据引发到\u异常(e)
974其他:
975提高
ValueError:在用户代码中:
/usr/local/lib/python3.6/dist包/tensorflow/python/keras/engine/training.py:1224测试函数*
返回步骤_函数(self、迭代器)
:30加权损失*
损失+=K.平均值(-(位置权重[i]*y_真[:,i]*K.对数(y_pred[:,i]+ε)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/util/dispatch.py:201包装器
返回目标(*args,**kwargs)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/array\u-ops.py:1024\u-slice\u-helper
名称=名称)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/util/dispatch.py:201包装器
返回目标(*args,**kwargs)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/array_-ops.py:1196跨步_-slice
收缩轴遮罩=收缩轴遮罩)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/gen_-array_-ops.py:10352跨步切片
收缩轴遮罩=收缩轴遮罩,名称=名称)
/usr/local/lib/python3.6/dist packages/tensorflow/python/framework/op_def_library.py:744_apply_op_helper
attrs=attr_protos,op_def=op_def)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/func\u-graph.py:593\u-create\u-op\u-internal
计算机(U设备)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py:3485\u create\u op\u internal
op_def=op_def)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py:1975__
控制(输入操作,操作定义)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py:1815\u-create\u-c\u-op
提升值错误(str(e))
ValueError:维度1的切片索引1超出范围。对于“{node weighted_loss/stripped_slice_4}}}=stripdslice[Index=DT_INT32,T=DT_FLOAT,begin_mask=1,ellipsis_mask=0,end_mask=1,new_axis_mask=0,short_axis_mask=2](ExpandDims,weighted_loss/stripped_slice_slice_4/stack,weighted_slice_4/stack,weighted_loss/stried_slice_4/stack_1,weighted_slice_4/stack_1,weighted_loss/stack_4/stack使用计算的输入张量:输入[1]=,输入[2]=,输入[3]=。
Epoch 1/50
323/324 [============================>.] - ETA: 0s - loss: 0.0101 - precision: 0.9988 - recall: 0.9937 - accuracy: 0.9975
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-26-d49655cfe3a9> in <module>()
      4 print(device_lib.list_local_devices())
      5 
----> 6 hist= model.fit(trainX, trainY, epochs=50, batch_size=25, validation_data= (validX, validY), verbose=1)
      7 hist_df = pd.DataFrame(hist.history)
      8 # hist_csv_file = '/content/drive/MyDrive/Covid-19/Data Augmentation/historyDenseNet121.csv'

12 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/func_graph.py in wrapper(*args, **kwargs)
    971           except Exception as e:  # pylint:disable=broad-except
    972             if hasattr(e, "ag_error_metadata"):
--> 973               raise e.ag_error_metadata.to_exception(e)
    974             else:
    975               raise

ValueError: in user code:

    /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:1224 test_function  *
        return step_function(self, iterator)
    <ipython-input-18-3cb53ecbce51>:30 weighted_loss  *
        loss +=K.mean(-(pos_weights[i] *y_true[:,i] * K.log(y_pred[:,i] + epsilon)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/util/dispatch.py:201 wrapper
        return target(*args, **kwargs)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/array_ops.py:1024 _slice_helper
        name=name)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/util/dispatch.py:201 wrapper
        return target(*args, **kwargs)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/array_ops.py:1196 strided_slice
        shrink_axis_mask=shrink_axis_mask)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/gen_array_ops.py:10352 strided_slice
        shrink_axis_mask=shrink_axis_mask, name=name)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/op_def_library.py:744 _apply_op_helper
        attrs=attr_protos, op_def=op_def)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/func_graph.py:593 _create_op_internal
        compute_device)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py:3485 _create_op_internal
        op_def=op_def)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py:1975 __init__
        control_input_ops, op_def)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py:1815 _create_c_op
        raise ValueError(str(e))

    ValueError: slice index 1 of dimension 1 out of bounds. for '{{node weighted_loss/strided_slice_4}} = StridedSlice[Index=DT_INT32, T=DT_FLOAT, begin_mask=1, ellipsis_mask=0, end_mask=1, new_axis_mask=0, shrink_axis_mask=2](ExpandDims, weighted_loss/strided_slice_4/stack, weighted_loss/strided_slice_4/stack_1, weighted_loss/strided_slice_4/stack_2)' with input shapes: [?,1], [2], [2], [2] and with computed input tensors: input[1] = <0 1>, input[2] = <0 2>, input[3] = <1 1>.