我有一个解码器输出17个维度,其中不同部分是标签和数字。因此,对于标签,我使用了一种热编码,并使用了“ softmax”激活;对于数字,我使用了“ Sigmoid”激活函数。
这里是解码器:
latent_inputs = Input(shape=(latent_dim,), name='z_sampling')
x = Dense(intermediate_dim, activation='relu')(latent_inputs)
x1 = Dense(intermediate_dim, activation='relu')(x)
x2 = Dense(intermediate_dim, activation='relu')(x1)
output1 = Dense(Num_classes, activation='softmax')(x2)
output2 = Dense(3, activation='sigmoid')(x2)
output3 = Dense(Num_classes, activation='softmax')(x2)
output4 = Dense(2, activation='sigmoid')(x2)
outputs = [output1, output2, output3, output4]
# instantiate decoder model
decoder = Model(latent_inputs, outputs, name='decoder')
而且,这是我定义损失函数的方式:
reconstruction_loss = categorical_crossentropy(inputs[0:Num_classes],outputs[0:Num_classes] )
+ binary_crossentropy(inputs[Num_classes:Num_classes+3],outputs[Num_classes:Num_classes+3])\
+categorical_crossentropy(inputs[Num_classes+3:2*Num_classes+3],outputs[Num_classes+3:2*Num_classes+3])\
+ binary_crossentropy(inputs[2*Num_classes+3:2*Num_classes+5],outputs[2*Num_classes+3:2*Num_classes+5])
但是,我在这个问题上弄错了。您能帮忙吗?
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-19-4de2050a20eb> in <module>
17 reconstruction_loss = mse(inputs, outputs)
18 else:
---> 19 reconstruction_loss = categorical_crossentropy(inputs[0:Num_classes],outputs[0:Num_classes] )
20 + binary_crossentropy(inputs[Num_classes:Num_classes+3],outputs[Num_classes:Num_classes+3])\
21 + categorical_crossentropy(inputs[Num_classes+3:2*Num_classes+3],outputs[Num_classes+3:2*Num_classes+3])\
C:\ProgramData\Anaconda3\lib\site-packages\keras\losses.py in categorical_crossentropy(y_true, y_pred, from_logits, label_smoothing)
678
679 def categorical_crossentropy(y_true, y_pred, from_logits=False, label_smoothing=0):
--> 680 y_pred = K.constant(y_pred) if not K.is_tensor(y_pred) else y_pred
681 y_true = K.cast(y_true, y_pred.dtype)
682
C:\ProgramData\Anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py in constant(value, dtype, shape, name)
647 with tf_ops.init_scope():
648 return tf_keras_backend.constant(
--> 649 value, dtype=dtype, shape=shape, name=name)
650
651
C:\ProgramData\Anaconda3\lib\site-packages\tensorflow_core\python\keras\backend.py in constant(value, dtype, shape, name)
935 dtype = floatx()
936
--> 937 return constant_op.constant(value, dtype=dtype, shape=shape, name=name)
938
939
C:\ProgramData\Anaconda3\lib\site-packages\tensorflow_core\python\framework\constant_op.py in constant(value, dtype, shape, name)
256 """
257 return _constant_impl(value, dtype, shape, name, verify_shape=False,
--> 258 allow_broadcast=True)
259
260
C:\ProgramData\Anaconda3\lib\site-packages\tensorflow_core\python\framework\constant_op.py in _constant_impl(value, dtype, shape, name, verify_shape, allow_broadcast)
264 ctx = context.context()
265 if ctx.executing_eagerly():
--> 266 t = convert_to_eager_tensor(value, ctx, dtype)
267 if shape is None:
268 return t
C:\ProgramData\Anaconda3\lib\site-packages\tensorflow_core\python\framework\constant_op.py in convert_to_eager_tensor(value, ctx, dtype)
94 dtype = dtypes.as_dtype(dtype).as_datatype_enum
95 ctx.ensure_initialized()
---> 96 return ops.EagerTensor(value, ctx.device_name, dtype)
97
98
ValueError: TypeError: len is not well defined for symbolic Tensors. (decoder/dense_7/Softmax:0) Please call `x.shape` rather than `len(x)` for shape information.
Traceback (most recent call last):
File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow_core\python\framework\ops.py", line 733, in __len__
"shape information.".format(self.name))
TypeError: len is not well defined for symbolic Tensors. (decoder/dense_7/Softmax:0) Please call `x.shape` rather than `len(x)` for shape information.
为了解决这个问题,我为编码器定义了四个输入,如下所示:
input1 = Input(shape=(Num_classes,), name='encoder_input1')
input2 = Input(shape=(3,), name='encoder_input2')
input3 = Input(shape=(Num_classes,), name='encoder_input3')
input4 = Input(shape=(2,), name='encoder_input4')
added = keras.layers.concatenate([input1, input2, input3, input4], axis = -1)
x = Dense(128, activation='relu')(added)
.
.
.
我用四个输出定义了解码器:
.
.
.
x3 = Dense(128, activation='relu')(x2)
out1 = Dense(Num_classes, activation='softmax',name='label1')(x3)
out2 = Dense(3, activation='sigmoid',name='dim1')(x3)
out3 = Dense(Num_classes, activation='softmax',name='label2')(x3)
out4 = Dense(2, activation='sigmoid',name='dim2')(x3)
以及损失函数如下:
reconstruction_loss = categorical_crossentropy(input1,out1) +\
binary_crossentropy(input2,out2)+\
categorical_crossentropy(input3,out3) +\
binary_crossentropy(input4,out4)