在这段代码之后,我在categoricalfocalloss中得到错误,我没有得到wherint64错误来了
def categorical_focal_loss(gamma=2., alpha=.25):
def categorical_focal_loss_fixed(y_true, y_pred):
y_pred /= K.sum(y_pred, axis=-1, keepdims=True)
epsilon = K.epsilon()
y_pred = K.clip(y_pred, epsilon, 1. - epsilon)
y_pred = tf.cast(y_pred, dtype= tf.float32)
cross_entropy = -y_true * K.log(y_pred)
loss = alpha * K.pow(1 - y_pred, gamma) * cross_entropy
return K.sum(loss, axis=1)
return categorical_focal_loss_fixed
模型描述在此代码中,在loss分类中使用了focal loss
with strategy.scope():
ef7 =tf.keras.Sequential()
ef7.add(enet)
ef7.add(tf.keras.layers.MaxPooling2D())
ef7.add(tf.keras.layers.Conv2D(4096,3,padding='same'))
ef7.add(tf.keras.layers.BatchNormalization())
ef7.add(tf.keras.layers.ReLU())
ef7.add(tf.keras.layers.GlobalAveragePooling2D())
ef7.add(tf.keras.layers.Dropout(0.35))
ef7.add(tf.keras.layers.Flatten())
ef7.add(tf.keras.layers.Dense(2048,activation='relu'))
ef7.add(tf.keras.layers.BatchNormalization())
ef7.add(tf.keras.layers.LeakyReLU())
ef7.add(tf.keras.layers.Dropout(0.35))
ef7.add(tf.keras.layers.Dense(1024,activation='relu'))
ef7.add(tf.keras.layers.BatchNormalization())
ef7.add(tf.keras.layers.LeakyReLU())
ef7.add(tf.keras.layers.Dropout(0.25))
ef7.add(tf.keras.layers.Dense(3,activation='softmax'))
ef7.compile(
optimizer=tf.optimizers.Adam(lr=0.0001),
loss=categorical_focal_loss(gamma=2., alpha=.25),
metrics=['categorical_accuracy',
tf.keras.metrics.Recall(),
tf.keras.metrics.Precision(),
tf.keras.metrics.AUC(),
tfa.metrics.F1Score(num_classes=3, average="macro")
])
在模型中,我使用了分类焦点损失当我在训练数据集中运行这个时,我不知道如何将其转换为intoint64
h7=ef7.fit(
train_dataset,
steps_per_epoch=train_labels.shape[0] // BATCH_SIZE,
callbacks=[lr_callback],
epochs=EPOCHS)
得到的错误如下所示
Epoch 1/20
```Epoch 00001: LearningRateScheduler reducing learning rate to 1e-05.```
---------------------------------------------------------------------------
>TypeError Traceback (most recent call last)
<ipython-input-133-d27eee469b2b> in <module>()
3 steps_per_epoch=train_labels.shape[0] // BATCH_SIZE,
4 callbacks=[lr_callback],
----> 5 epochs=EPOCHS)
9 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/func_graph.py in wrapper(*args, **kwargs)
975 except Exception as e: # pylint:disable=broad-except
976 if hasattr(e, "ag_error_metadata"):
--> 977 raise e.ag_error_metadata.to_exception(e)
978 else:
979 raise
TypeError: in user code:
>/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:805 train_function *
>return step_function(self, iterator)
><ipython-input-68-de42355e464e>:7 categorical_focal_loss_fixed *
cross_entropy = -y_true * K.log(y_pred)
>/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/math_ops.py:1180 binary_op_wrapper
>raise e
>/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/math_ops.py:1164 binary_op_wrapper
>return func(x, y, name=name)
>/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/math_ops.py:1496 _mul_dispatch
>return multiply(x, y, 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/math_ops.py:518 multiply
>return gen_math_ops.mul(x, y, name)
>/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/gen_math_ops.py:6078 mul
> "Mul", x=x, y=y, name=name)
>/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/op_def_library.py:558 _apply_op_helper
> inferred_from[input_arg.type_attr]))
>TypeError: Input 'y' of 'Mul' Op has type float32 that does not match type int64 of argument 'x'.```
错误指向这行代码:
cross_entropy = -y_true * K.log(y_pred)
是从tensorflow包中的math_ops.py
中的乘法函数抛出的。深入研究该文件,我找到了参数需求的摘要。
Args:
x: A Tensor. Must be one of the following types: `bfloat16`,
`half`, `float32`, `float64`, `uint8`, `int8`, `uint16`,
`int16`, `int32`, `int64`, `complex64`, `complex128`.
y: A `Tensor`. Must have the same type as `x`.
name: A name for the operation (optional).
Returns:
A `Tensor`. Has the same type as `x`.
Raises:
* InvalidArgumentError: When `x` and `y` have incompatible shapes or types
回头看错误
TypeError: Input 'y' of 'Mul' Op has type float32 that does not match type int64 of argument 'x'.```
表示-y_true
为'x'
,K.log(y_pred)
为'y'
。要执行此操作,您必须将-y_true
强制转换为float32或强制转换K.log(y_pred)
转换为int64类型,或者只要它们匹配,就将它们都转换为任何其他类型。.