gpt4 book ai didi

python - len 没有很好地定义符号。请调用 `x.shape` 而不是 `len(x)` 获取形状信息

转载 作者:太空宇宙 更新时间:2023-11-03 20:04:18 31 4
gpt4 key购买 nike

我有一个输出 17 个维度的解码器,其中不同部分是标签和数字。因此,对于标签,我使用了 one-hot 编码并使用“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)

关于python - len 没有很好地定义符号。请调用 `x.shape` 而不是 `len(x)` 获取形状信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59057775/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com