我正在尝试使用 Keras 的函数式 API 来处理多个输入,并使用自定义损失函数 RMSLE。下面是我的代码:
import tensorflow as tf
from tensorflow.keras.layers import *
from tensorflow.keras.models import Sequential, Model
from tensorflow.keras import backend as K
from tensorflow.keras.losses import MeanSquaredLogarithmicError
def rmsle(y_true, y_pred):
return K.sqrt(MeanSquaredLogarithmicError(y_true, y_pred))
def build_model():
i_language = Input(shape=(1,))
i_year = Input(shape=(1,))
i_abstract = Input(shape=(100,))
input = concatenate([i_language, i_year, i_abstract])
x = Dense(64)(input)
x = Dense(1, activation='softmax')(x)
model = Model(inputs=[i_language, i_year, i_abstract], outputs=x)
model.compile(optimizer = 'adam', loss = rmsle)
return model
model = build_model()
x1 = np.random.randint(3, size=(100, 1)).astype('float32')
x2 = np.random.randint(59, size=(100, 1)).astype('float32')
x3 = np.random.randn(100, 100)
y = np.random.rand(100,1)
model.fit([x1,x2,x3], y)
其中 x1,x2,x3 都是样本输入,y 是样本输出。但是,他的最后一行 model.fit()
抛出了错误:
TypeError Traceback (most recent call last)
<ipython-input-33-66ea59ad4aed> in <module>()
5 y = np.random.rand(100,1)
6
----> 7 model.fit([x1,x2,x3], y)
9 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/func_graph.py in wrapper(*args, **kwargs)
984 except Exception as e: # pylint:disable=broad-except
985 if hasattr(e, "ag_error_metadata"):
--> 986 raise e.ag_error_metadata.to_exception(e)
987 else:
988 raise
TypeError: in user code:
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/training.py:855 train_function *
return step_function(self, iterator)
<ipython-input-17-6a742f71a83b>:2 rmsle *
return K.sqrt(MeanSquaredLogarithmicError(y_true, y_pred))
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/losses.py:506 __init__ **
mean_squared_logarithmic_error, name=name, reduction=reduction)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/losses.py:241 __init__
super(LossFunctionWrapper, self).__init__(reduction=reduction, name=name)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/losses.py:102 __init__
losses_utils.ReductionV2.validate(reduction)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/utils/losses_utils.py:76 validate
if key not in cls.all():
/usr/local/lib/python3.7/dist-packages/tensorflow/python/util/dispatch.py:206 wrapper
return target(*args, **kwargs)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/ops/math_ops.py:1800 tensor_equals
self, other = maybe_promote_tensors(self, other)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/ops/math_ops.py:1202 maybe_promote_tensors
ops.convert_to_tensor(tensor, dtype, name="x"))
/usr/local/lib/python3.7/dist-packages/tensorflow/python/profiler/trace.py:163 wrapped
return func(*args, **kwargs)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/ops.py:1566 convert_to_tensor
ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/constant_op.py:339 _constant_tensor_conversion_function
return constant(v, dtype=dtype, name=name)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/constant_op.py:265 constant
allow_broadcast=True)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/constant_op.py:283 _constant_impl
allow_broadcast=allow_broadcast))
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/tensor_util.py:457 make_tensor_proto
_AssertCompatible(values, dtype)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/tensor_util.py:337 _AssertCompatible
(dtype.name, repr(mismatch), type(mismatch).__name__))
TypeError: Expected float32, got 'auto' of type 'str' instead.
我以前没有遇到过这个错误,不明白发生了什么。有人可以帮我摆脱这个错误吗?
用以下方法重新补偿您的自定义损失:
def rmsle(y_true, y_pred):
msle = MeanSquaredLogarithmicError()
return K.sqrt(msle(y_true, y_pred))
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句