- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
努力使子类损失函数在 Tensorflow (2.2.0) 中工作。
最初尝试了此代码(我知道它对其他人有用 - 请参阅 https://github.com/keras-team/keras/issues/2115#issuecomment-530762739 ):
import tensorflow.keras.backend as K
from tensorflow.keras.losses import CategoricalCrossentropy
class WeightedCategoricalCrossentropy(CategoricalCrossentropy):
def __init__(self, cost_mat, name='weighted_categorical_crossentropy', **kwargs):
assert(cost_mat.ndim == 2)
assert(cost_mat.shape[0] == cost_mat.shape[1])
super().__init__(name=name, **kwargs)
self.cost_mat = K.cast_to_floatx(cost_mat)
def __call__(self, y_true, y_pred):
return super().__call__(
y_true=y_true,
y_pred=y_pred,
sample_weight=get_sample_weights(y_true, y_pred, self.cost_mat),
)
def get_sample_weights(y_true, y_pred, cost_m):
num_classes = len(cost_m)
y_pred.shape.assert_has_rank(2)
y_pred.shape[1].assert_is_compatible_with(num_classes)
y_pred.shape.assert_is_compatible_with(y_true.shape)
y_pred = K.one_hot(K.argmax(y_pred), num_classes)
y_true_nk1 = K.expand_dims(y_true, 2)
y_pred_n1k = K.expand_dims(y_pred, 1)
cost_m_1kk = K.expand_dims(cost_m, 0)
sample_weights_nkk = cost_m_1kk * y_true_nk1 * y_pred_n1k
sample_weights_n = K.sum(sample_weights_nkk, axis=[1, 2])
return sample_weights_n
model.compile(optimizer='adam',
loss={'simple_Class': 'categorical_crossentropy',
'soundClass': 'binary_crossentropy',
'auxiliary_soundClass':'binary_crossentropy',
'auxiliary_class_training': WeightedCategoricalCrossentropy(cost_matrix),
'class_training':WeightedCategoricalCrossentropy(cost_matrix)
},
loss_weights={'simple_Class': 1.0,
'soundClass': 1.0,
'auxiliary_soundClass':0.7,
'auxiliary_class_training': 0.7,
'class_training':0.4})
cost_matrix
是一个二维 numpy 数组)。训练槽
model.fit()
与
batch_size=512
.
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-21-3428d6d8967a> in <module>()
82 'class_training': class_lables_test}),
83
---> 84 epochs=nb_epoch, batch_size=batch_size, initial_epoch=initial_epoch, verbose=0, shuffle=True, callbacks=[se, tb, cm, mc, es, rs])
85
86 #model.save(save_version_dir,save_format='tf')
10 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py in _method_wrapper(self, *args, **kwargs)
64 def _method_wrapper(self, *args, **kwargs):
65 if not self._in_multi_worker_mode(): # pylint: disable=protected-access
---> 66 return method(self, *args, **kwargs)
67
68 # Running inside `run_distribute_coordinator` already.
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_batch_size, validation_freq, max_queue_size, workers, use_multiprocessing)
846 batch_size=batch_size):
847 callbacks.on_train_batch_begin(step)
--> 848 tmp_logs = train_function(iterator)
849 # Catch OutOfRangeError for Datasets of unknown size.
850 # This blocks until the batch has finished executing.
/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/def_function.py in __call__(self, *args, **kwds)
578 xla_context.Exit()
579 else:
--> 580 result = self._call(*args, **kwds)
581
582 if tracing_count == self._get_tracing_count():
/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/def_function.py in _call(self, *args, **kwds)
625 # This is the first call of __call__, so we have to initialize.
626 initializers = []
--> 627 self._initialize(args, kwds, add_initializers_to=initializers)
628 finally:
629 # At this point we know that the initialization is complete (or less
/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/def_function.py in _initialize(self, args, kwds, add_initializers_to)
504 self._concrete_stateful_fn = (
505 self._stateful_fn._get_concrete_function_internal_garbage_collected( # pylint: disable=protected-access
--> 506 *args, **kwds))
507
508 def invalid_creator_scope(*unused_args, **unused_kwds):
/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/function.py in _get_concrete_function_internal_garbage_collected(self, *args, **kwargs)
2444 args, kwargs = None, None
2445 with self._lock:
-> 2446 graph_function, _, _ = self._maybe_define_function(args, kwargs)
2447 return graph_function
2448
/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/function.py in _maybe_define_function(self, args, kwargs)
2775
2776 self._function_cache.missed.add(call_context_key)
-> 2777 graph_function = self._create_graph_function(args, kwargs)
2778 self._function_cache.primary[cache_key] = graph_function
2779 return graph_function, args, kwargs
/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/function.py in _create_graph_function(self, args, kwargs, override_flat_arg_shapes)
2665 arg_names=arg_names,
2666 override_flat_arg_shapes=override_flat_arg_shapes,
-> 2667 capture_by_value=self._capture_by_value),
2668 self._function_attributes,
2669 # Tell the ConcreteFunction to clean up its graph once it goes out of
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/func_graph.py in func_graph_from_py_func(name, python_func, args, kwargs, signature, func_graph, autograph, autograph_options, add_control_dependencies, arg_names, op_return_value, collections, capture_by_value, override_flat_arg_shapes)
979 _, original_func = tf_decorator.unwrap(python_func)
980
--> 981 func_outputs = python_func(*func_args, **func_kwargs)
982
983 # invariant: `func_outputs` contains only Tensors, CompositeTensors,
/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/def_function.py in wrapped_fn(*args, **kwds)
439 # __wrapped__ allows AutoGraph to swap in a converted function. We give
440 # the function a weak reference to itself to avoid a reference cycle.
--> 441 return weak_wrapped_fn().__wrapped__(*args, **kwds)
442 weak_wrapped_fn = weakref.ref(wrapped_fn)
443
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/func_graph.py in wrapper(*args, **kwargs)
966 except Exception as e: # pylint:disable=broad-except
967 if hasattr(e, "ag_error_metadata"):
--> 968 raise e.ag_error_metadata.to_exception(e)
969 else:
970 raise
TypeError: in user code:
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:571 train_function *
outputs = self.distribute_strategy.run(
/usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:951 run **
return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:2290 call_for_each_replica
return self._call_for_each_replica(fn, args, kwargs)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:2649 _call_for_each_replica
return fn(*args, **kwargs)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:533 train_step **
y, y_pred, sample_weight, regularization_losses=self.losses)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/compile_utils.py:205 __call__
loss_value = loss_obj(y_t, y_p, sample_weight=sw)
TypeError: __call__() got an unexpected keyword argument 'sample_weight'
__call__()
时,此问题已解决。魔术方法与
call()
并手动实现一些底层逻辑。这有效,用法相同。
__call__
方法改为:
def call(self, y_true, y_pred):
return super().call(y_true, y_pred) * get_sample_weights(y_true, y_pred, self.cost_mat)
y_true
上计算分类交叉熵损失和
y_pred
然后直接乘以我们的权重矩阵,而不是传递
y_true
,
y_pred
和
self-cost_mat
到分类交叉熵
call
方法并使用继承方法自己的逻辑将损失乘以权重。这不是一个大问题,因为代码确实有效 - 但我不明白为什么我无法使用继承类自己的
__call__
正确实现(根据原始代码)。
y_pred.shape[1].assert_is_compatible_with(num_classes)
至
assert(y_pred.shape[1] == num_classes)
- 这是因为
y_pred.shape[1]
正在返回
int
.我不知道为什么,因为,检查
y_pred
,当然是
tf.Tensor
,等等,
.shape[1]
应该返回
tf.TesnorShape
对象,在其上
.assert_is_compatible_with()
可以调用。
model.save(save_format='tf')
启用整个模型 + 优化器状态保存.其中一些功能很难实现:我必须实现对 NumPy 数组的显式转换(参见
__init__
方法的第一行)。
class WeightedCategoricalCrossentropy(tensorflow.keras.losses.CategoricalCrossentropy):
def __init__(self, cost_mat, name='weighted_categorical_crossentropy', **kwargs):
cost_mat = np.array(cost_mat)
## when loading from config, self.cost_mat returns as a list, rather than an numpy array.
## Adding the above line fixes this issue, enabling .ndim to call sucessfully.
## However, this is probably not the best implementation
assert(cost_mat.ndim == 2)
assert(cost_mat.shape[0] == cost_mat.shape[1])
super().__init__(name=name, **kwargs)
self.cost_mat = K.cast_to_floatx(cost_mat)
def call(self, y_true, y_pred):
return super().call(y_true, y_pred) * get_sample_weights(y_true, y_pred, self.cost_mat)
def get_config(self):
config = super().get_config().copy()
# Calling .update on the line above, during assignment, causes an error with config becoming None-type.
config.update({'cost_mat': (self.cost_mat)})
return config
@classmethod
def from_config(cls, config):
# something goes wrong here and changes self.cost_mat to a list variable.
# See above for temporary fix
return cls(**config)
def get_sample_weights(y_true, y_pred, cost_m):
num_classes = len(cost_m)
y_pred.shape.assert_has_rank(2)
assert(y_pred.shape[1] == num_classes)
y_pred.shape.assert_is_compatible_with(y_true.shape)
y_pred = K.one_hot(K.argmax(y_pred), num_classes)
y_true_nk1 = K.expand_dims(y_true, 2)
y_pred_n1k = K.expand_dims(y_pred, 1)
cost_m_1kk = K.expand_dims(cost_m, 0)
sample_weights_nkk = cost_m_1kk * y_true_nk1 * y_pred_n1k
sample_weights_n = K.sum(sample_weights_nkk, axis=[1, 2])
return sample_weights_n
tf.keras.losses.WeightedCategoricalCrossentropy = WeightedCategoricalCrossentropy
model.save(save_version_dir,save_format='tf')
model = tf.keras.models.load_model(
save_version_dir,
compile=True,
custom_objects={
'WeightedCategoricalCrossentropy': WeightedCategoricalCrossentropy(cost_matrix)
}
)
最佳答案
根据评论;这里的问题是 TensorFlow 现在强制继承原始方法签名。
以下已在玩具问题上进行了测试(通过比较 cost_matrix 中的相等权重与将除单个类别之外的所有类别加权为无)并且有效:
class WeightedCategoricalCrossentropy(tf.keras.losses.CategoricalCrossentropy):
def __init__(self, cost_mat, name='weighted_categorical_crossentropy', **kwargs):
cost_mat = np.array(cost_mat)
## when loading from config, self.cost_mat returns as a list, rather than an numpy array.
## Adding the above line fixes this issue, enabling .ndim to call sucessfully.
## However, this is probably not the best implementation
assert(cost_mat.ndim == 2)
assert(cost_mat.shape[0] == cost_mat.shape[1])
super().__init__(name=name, **kwargs)
self.cost_mat = K.cast_to_floatx(cost_mat)
def __call__(self, y_true, y_pred, sample_weight=None):
assert sample_weight is None, "should only be derived from the cost matrix"
return super().__call__(
y_true=y_true,
y_pred=y_pred,
sample_weight=get_sample_weights(y_true, y_pred, self.cost_mat),
)
def get_config(self):
config = super().get_config().copy()
# Calling .update on the line above, during assignment, causes an error with config becoming None-type.
config.update({'cost_mat': (self.cost_mat)})
return config
@classmethod
def from_config(cls, config):
# something goes wrong here and changes self.cost_mat to a list variable.
# See above for temporary fix
return cls(**config)
def get_sample_weights(y_true, y_pred, cost_m):
num_classes = len(cost_m)
y_pred.shape.assert_has_rank(2)
assert(y_pred.shape[1] == num_classes)
y_pred.shape.assert_is_compatible_with(y_true.shape)
y_pred = K.one_hot(K.argmax(y_pred), num_classes)
y_true_nk1 = K.expand_dims(y_true, 2)
y_pred_n1k = K.expand_dims(y_pred, 1)
cost_m_1kk = K.expand_dims(cost_m, 0)
sample_weights_nkk = cost_m_1kk * y_true_nk1 * y_pred_n1k
sample_weights_n = K.sum(sample_weights_nkk, axis=[1, 2])
return sample_weights_n
# Register the loss in the Keras namespace to enable loading of the custom object.
tf.keras.losses.WeightedCategoricalCrossentropy = WeightedCategoricalCrossentropy
cost_matrix
是一个二维 NumPy 数组,例如:
[
[ Weight Category 1 predicted as Category 1,
Weight Category 1 predicted as Category 2,
Weight Category 1 predicted as Category 3 ]
[ Weight Category 2 predicted as Category 1,
...,
... ]
[ ...,
...,
Weight Category 3 predicted as Category 3 ]
]
model.compile(
optimizer='adam',
loss=WeightedCategoricalCrossentropy(cost_matrix)
)
model.save(save_version_dir,save_format='tf')
model = tf.keras.models.load_model(
save_version_dir,
compile=True,
custom_objects={
'WeightedCategoricalCrossentropy': WeightedCategoricalCrossentropy(cost_matrix)
}
)
关于tensorflow2.0 - 对 tensorflow 损失类 (categorical_crossentropy) 进行子分类以创建加权损失函数时出现意外的关键字参数 'sample_weight',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61919774/
我是pytorch的新手。请问添加'loss.item()'有什么区别?以下2部分代码: for epoch in range(epochs): trainingloss =0 for
我有一个包含 4 列的 MySQL 表,如下所示。 TransactionID | Item | Amount | Date ------------------------------------
我目前正在使用 cocos2d、Box2D 和 Objective-C 为 iPad 和 iPhone 制作游戏。 每次更新都会发生很多事情,很多事情必须解决。 我最近将我的很多代码重构为几个小方法,
我一直在关注 Mixed Precision Guide .因此,我正在设置: keras.mixed_precision.set_global_policy(mixed_precision) 像这样
double lnumber = Math.pow(2, 1000); 打印 1.0715086071862673E301 我尝试过的事情 我尝试使用 BigDecimal 类来扩展这个数字: St
我正在尝试创建一个神经网络来近似函数(正弦、余弦、自定义...),但我在格式上遇到困难,我不想使用输入标签,而是使用输入输出。我该如何更改它? 我正在关注this tutorial import te
我有一个具有 260,000 行和 35 列的“单热编码”(全一和零)数据矩阵。我正在使用 Keras 训练一个简单的神经网络来预测一个连续变量。制作网络的代码如下: model = Sequenti
什么是像素级 softmax 损失?在我的理解中,这只是一个交叉熵损失,但我没有找到公式。有人能帮我吗?最好有pytorch代码。 最佳答案 您可以阅读 here所有相关内容(那里还有一个指向源代码的
我正在训练一个 CNN 架构来使用 PyTorch 解决回归问题,其中我的输出是一个 20 个值的张量。我计划使用 RMSE 作为模型的损失函数,并尝试使用 PyTorch 的 nn.MSELoss(
在每个时代结束时,我得到例如以下输出: Epoch 1/25 2018-08-06 14:54:12.555511: 2/2 [==============================] - 86
我正在使用 Keras 2.0.2 功能 API (Tensorflow 1.0.1) 来实现一个网络,该网络接受多个输入并产生两个输出 a 和 b。我需要使用 cosine_proximity 损失
我正在尝试设置很少层的神经网络,这将解决简单的回归问题,这应该是f(x) = 0,1x 或 f(x) = 10x 所有代码如下所示(数据生成和神经网络) 4 个带有 ReLu 的全连接层 损失函数 R
我正在研究在 PyTorch 中使用带有梯度惩罚的 Wasserstein GAN,但始终得到大的、正的生成器损失,并且随着时间的推移而增加。 我从 Caogang's implementation
我正在尝试在 TensorFlow 中实现最大利润损失。这个想法是我有一些积极的例子,我对一些消极的例子进行了采样,并想计算类似的东西 其中 B 是我的批处理大小,N 是我要使用的负样本数。 我是 t
我正在尝试预测一个连续值(第一次使用神经网络)。我已经标准化了输入数据。我不明白为什么我会收到 loss: nan从第一个纪元开始的输出。 我阅读并尝试了以前对同一问题的回答中的许多建议,但没有一个对
我目前正在学习神经网络,并尝试训练 MLP 以使用 Python 中的反向传播来学习 XOR。该网络有两个隐藏层(使用 Sigmoid 激活)和一个输出层(也是 Sigmoid)。 网络(大约 20,
尝试在 keras 中自定义损失函数(平滑 L1 损失),如下所示 ValueError: Shape must be rank 0 but is rank 5 for 'cond/Switch' (
我试图在 tensorflow 中为门牌号图像创建一个卷积神经网络 http://ufldl.stanford.edu/housenumbers/ 当我运行我的代码时,我在第一步中得到了 nan 的成
我正在尝试使用我在 Keras 示例( https://github.com/keras-team/keras/blob/master/examples/variational_autoencoder
我试图了解 CTC 损失如何用于语音识别以及如何在 Keras 中实现它。 我认为我理解的内容(如果我错了,请纠正我!)总体而言,CTC 损失被添加到经典网络之上,以便逐个元素(对于文本或语音而言逐个
我是一名优秀的程序员,十分优秀!