#tensorflow #keras #loss-function
Вопрос:
Я внедряю обычай loss
для своей модели keras:
def loss(self, y_true, y_pred):
self.y_true = tf.convert_to_tensor(y_true)
self.y_pred = tf.convert_to_tensor(y_pred)
tf.print("====================")
tf.print(y_pred)
tf.print(y_true)
tf.print("====================")
s = K.mean(((self.y_true - K.log(self.y_pred)) ((1-self.y_true) - K.log(1-self.y_pred))), axis = -1)
return s
но это приводит к nan
:
[[-nan]
[-nan]
[-nan]
...
[-nan]
[-nan]
[-nan]]
[[0.132541135]
[0.102164917]
[0.0687385723]
...
[0.0518041]
[0.127239496]
[0.0189646874]]
итак, я решил изменить an epsilon
на log
часть в функции потерь:
s = K.mean(((self.y_true - K.log(K.max(self.y_pred, [[0.00000001]]))) ((1-self.y_true) - K.log(K.max(1-self.y_pred, [[0.00000001]])))), axis = -1)
затем я столкнулся с этой ошибкой:
File "/home/m.shah/projects/projects/models/_regression.py", line 52, in train
result = self.model.fit(train_x,
File "/home/m.shah/.conda/envs/tensorflow/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py", line 1100, i
n fit
tmp_logs = self.train_function(iterator)
File "/home/m.shah/.conda/envs/tensorflow/lib/python3.8/site-packages/tensorflow/python/eager/def_function.py", line 828, in __
call__
result = self._call(*args, **kwds)
File "/home/m.shah/.conda/envs/tensorflow/lib/python3.8/site-packages/tensorflow/python/eager/def_function.py", line 871, in _c
all
self._initialize(args, kwds, add_initializers_to=initializers)
File "/home/m.shah/.conda/envs/tensorflow/lib/python3.8/site-packages/tensorflow/python/eager/def_function.py", line 725, in _i
nitialize
self._stateful_fn._get_concrete_function_internal_garbage_collected( # pylint: disable=protected-access
File "/home/m.shah/.conda/envs/tensorflow/lib/python3.8/site-packages/tensorflow/python/eager/function.py", line 2969, in _get_
concrete_function_internal_garbage_collected
graph_function, _ = self._maybe_define_function(args, kwargs)
File "/home/m.shah/.conda/envs/tensorflow/lib/python3.8/site-packages/tensorflow/python/eager/function.py", line 2969, in _get_
concrete_function_internal_garbage_collected
graph_function, _ = self._maybe_define_function(args, kwargs)
File "/home/m.shah/.conda/envs/tensorflow/lib/python3.8/site-packages/tensorflow/python/eager/function.py", line 3361, in _maybe_define_function
graph_function = self._create_graph_function(args, kwargs)
File "/home/m.shah/.conda/envs/tensorflow/lib/python3.8/site-packages/tensorflow/python/eager/function.py", line 3196, in _create_graph_function
func_graph_module.func_graph_from_py_func(
File "/home/m.shah/.conda/envs/tensorflow/lib/python3.8/site-packages/tensorflow/python/framework/func_graph.py", line 990, in func_graph_from_py_func
func_outputs = python_func(*func_args, **func_kwargs)
File "/home/m.shah/.conda/envs/tensorflow/lib/python3.8/site-packages/tensorflow/python/eager/def_function.py", line 634, in wrapped_fn
out = weak_wrapped_fn().__wrapped__(*args, **kwds)
File "/home/m.shah/.conda/envs/tensorflow/lib/python3.8/site-packages/tensorflow/python/framework/func_graph.py", line 977, in wrapper
raise e.ag_error_metadata.to_exception(e)
TypeError: in user code:
/home/m.shah/.conda/envs/tensorflow/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py:805 train_function *
return step_function(self, iterator)
/home/m.shah/projects/projects/modules/_loss.py:14 loss *
s = K.mean((self.y_true - K.log(K.max(self.y_pred, 0.0000001))) ((1-self.y_true) - K.log(K.max(1-self.y_pred, 0.0000001))), axis = -1)
/home/m.shah/.conda/envs/tensorflow/lib/python3.8/site-packages/tensorflow/python/util/dispatch.py:201 wrapper **
return target(*args, **kwargs)
/home/m.shah/.conda/envs/tensorflow/lib/python3.8/site-packages/tensorflow/python/keras/backend.py:2179 max
return math_ops.reduce_max(x, axis, keepdims)
/home/m.shah/.conda/envs/tensorflow/lib/python3.8/site-packages/tensorflow/python/util/dispatch.py:201 wrapper
return target(*args, **kwargs)
/home/m.shah/.conda/envs/tensorflow/lib/python3.8/site-packages/tensorflow/python/ops/math_ops.py:2745 reduce_max
return reduce_max_with_dims(input_tensor, axis, keepdims, name,
/home/m.shah/.conda/envs/tensorflow/lib/python3.8/site-packages/tensorflow/python/ops/math_ops.py:2757 reduce_max_with_dims
gen_math_ops._max(input_tensor, dims, keepdims, name=name))
/home/m.shah/.conda/envs/tensorflow/lib/python3.8/site-packages/tensorflow/python/ops/gen_math_ops.py:5626 _max
_, _, _op, _outputs = _op_def_library._apply_op_helper(
/home/m.shah/.conda/envs/tensorflow/lib/python3.8/site-packages/tensorflow/python/framework/op_def_library.py:628 _apply_op_helper
_SatisfiesTypeConstraint(base_type,
/home/m.shah/.conda/envs/tensorflow/lib/python3.8/site-packages/tensorflow/python/framework/op_def_library.py:59 _SatisfiesTypeConstraint
raise TypeError(
TypeError: Value passed to parameter 'reduction_indices' has DataType float32 not in list of allowed values: int32, int64
Затем я изменил код на:
s = K.mean(((self.y_true - K.log(K.max([self.y_pred, [[00000001]]]))) ((1-self.y_true) - K.log(K.max([1-self.y_pred, [[00000001]]])))), axis = -1)
and again faced this error:
Traceback (most recent call last):
File "experiment5.py", line 80, in <module>
history = model.train(X_train, y_train, epochs=100)
File "/home/m.shah/projects/projects/models/_regression.py", line 52, in train
result = self.model.fit(train_x,
File "/home/m.shah/.conda/envs/tensorflow/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py", line 1100, in fit
tmp_logs = self.train_function(iterator)
File "/home/m.shah/.conda/envs/tensorflow/lib/python3.8/site-packages/tensorflow/python/eager/def_function.py", line 828, in __call__
result = self._call(*args, **kwds)
File "/home/m.shah/.conda/envs/tensorflow/lib/python3.8/site-packages/tensorflow/python/eager/def_function.py", line 888, in _call
return self._stateless_fn(*args, **kwds)
File "/home/m.shah/.conda/envs/tensorflow/lib/python3.8/site-packages/tensorflow/python/eager/function.py", line 2942, in __call__
return graph_function._call_flat(
File "/home/m.shah/.conda/envs/tensorflow/lib/python3.8/site-packages/tensorflow/python/eager/function.py", line 1918, in _call_flat
return self._build_call_outputs(self._inference_function.call(
File "/home/m.shah/.conda/envs/tensorflow/lib/python3.8/site-packages/tensorflow/python/eager/function.py", line 555, in call
outputs = execute.execute(
File "/home/m.shah/.conda/envs/tensorflow/lib/python3.8/site-packages/tensorflow/python/eager/execute.py", line 59, in quick_execute
tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
tensorflow.python.framework.errors_impl.InvalidArgumentError: Shapes of all inputs must match: values[0].shape = [64,1] != values[1].shape = [1,1]
[[node loss/Max/input (defined at /home/m.shah/projects/projects/modules/_loss.py:14) ]] [Op:__inference_train_function_3127]
Errors may have originated from an input operation.
Input Source operations connected to node loss/Max/input:
sequential/dense/Relu (defined at /home/m.shah/projects/projects/models/_regression.py:52)
Function call stack:
train_function
краткое описание регрессионной модели выглядит следующим образом:
Layer (type) Output Shape Param #
=================================================================
lstm (LSTM) (None, 64) 17152
_________________________________________________________________
dropout (Dropout) (None, 64) 0
_________________________________________________________________
dense (Dense) (None, 1) 65
=================================================================
Total params: 17,217
Trainable params: 17,217
Non-trainable params: 0
_________________________________________________________________
Model Summary:
и я добавил tf.print(y_pred.shape)
и tf.print(y_true.shape)
как журналы, а затем:
TensorShape([None, 1])
TensorShape([None, 1])
Тем временем я изменил функцию на:
def loss(self, y_true, y_pred):
config = ConfigProto()
config.gpu_options.allow_growth = True
session = InteractiveSession(config=config)
self.y_true = tf.convert_to_tensor(y_true)
self.y_pred = tf.convert_to_tensor(y_pred)
tf.print("=========== shapes =========")
tf.print(y_pred.shape)
tf.print(y_true.shape)
tf.print("====================")
s = K.mean(((self.y_true - K.log(self.y_pred)) ((1-self.y_true) - K.log(1-self.y_pred))), axis = -1)
но опять же это привело к nan
тому , что, однако, в нем нет ошибки. Похоже, у меня возникли проблемы с K.max
У кого-нибудь есть какие-нибудь идеи?
Комментарии:
1. 1. Вы не опубликовали никакого кода для второй ошибки 2. Похоже, что вторая ошибка связана с формой ваших входных данных — можете ли вы опубликовать код модели и форму ваших входных данных?
2. @ML_Engine извини. часть моего поста была пропущена. Теперь все в порядке. буду признателен, если поделитесь своими идеями.