Keras CONV1D:检查目标时出错:预期解码输出具有形状 (50, 50),但得到形状为 (50, 1) 的数组



我有这个麻烦: 检查目标时出错: 预期decoded_output具有形状 (50, 50( 但得到具有形状 (50, 1( 的数组 使用此代码,带有 CONV1D 和两个输出的自动编码器,但问题是重建输出 (decode_output(:

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
TAM_VECTOR = X_train.shape[1]
input_tweet = Input(shape=(TAM_VECTOR,X_train.shape[2]))
encoded = Conv1D(64, kernel_size=1, activation='relu')(input_tweet)
encoded = Conv1D(32, kernel_size=1, activation='relu')(encoded)
decoded = Conv1D(32, kernel_size=1, activation='relu')(encoded)
decoded = Conv1D(64, kernel_size=1, activation='relu')(decoded)
decoded = Conv1D(TAM_VECTOR, kernel_size=1, activation='relu', name='decode_output')(decoded)
encoded = Flatten()(encoded)
second_output = Dense(1, activation='linear', name='second_output')(encoded)
autoencoder = Model(inputs=input_tweet, outputs=[decoded, second_output])
autoencoder.compile(optimizer="adam",
loss={'decode_output': 'mse', 'second_output': 'mse'},
loss_weights={'decode_output': 0.001, 'second_output': 0.999},
metrics=["mae"])
autoencoder.fit([X_train], [X_train, y_train], epochs=10, batch_size=32)

输入 (X( 的形状为 (50000,50(,但由于 Conv1D 接收 3D 输入,我将形状调整为:

X = np.reshape(X, (X.shape[0], X.shape[1], -1))

(50000,50,1(

y(第二个输出(为

y.shape

(50000,1(

这里是模型摘要

Layer (type)                    Output Shape         Param #     Connected to                     
==================================================================================================
input_43 (InputLayer)           (None, 50, 1)        0                                            
__________________________________________________________________________________________________
conv1d_169 (Conv1D)             (None, 50, 64)       128         input_43[0][0]                   
__________________________________________________________________________________________________
conv1d_170 (Conv1D)             (None, 50, 32)       2080        conv1d_169[0][0]                 
__________________________________________________________________________________________________
conv1d_171 (Conv1D)             (None, 50, 32)       1056        conv1d_170[0][0]                 
__________________________________________________________________________________________________
conv1d_172 (Conv1D)             (None, 50, 64)       2112        conv1d_171[0][0]                 
__________________________________________________________________________________________________
flatten_62 (Flatten)            (None, 1600)         0           conv1d_170[0][0]                 
__________________________________________________________________________________________________
decode_output (Conv1D)          (None, 50, 50)       3250        conv1d_172[0][0]                 
__________________________________________________________________________________________________
pib_output (Dense)              (None, 1)            1601        flatten_62[0][0]                 
==================================================================================================
Total params: 10,227
Trainable params: 10,227
Non-trainable params: 0

TAM_VECTOR应在下一行中替换为 1。

取代

decoded = Conv1D(TAM_VECTOR, kernel_size=1, activation='relu', name='decode_output')(decoded)

decoded = Conv1D(1, kernel_size=1, activation='relu', name='decode_output')(decoded)

解码的输出形状应与自动编码器的输入形状匹配 (50,1(。

from keras.layers import Conv1D, Flatten, Dense, Input
from keras.models import Model
import numpy as np
TAM_VECTOR = 50
input_tweet = Input(shape=(TAM_VECTOR,1))
encoded = Conv1D(64, kernel_size=1, activation='relu')(input_tweet)
encoded = Conv1D(32, kernel_size=1, activation='relu')(encoded)
decoded = Conv1D(32, kernel_size=1, activation='relu')(encoded)
decoded = Conv1D(64, kernel_size=1, activation='relu')(decoded)
decoded = Conv1D(1, kernel_size=1, activation='relu', name='decode_output')(decoded)
encoded = Flatten()(encoded)
second_output = Dense(1, activation='linear', name='second_output')(encoded)
autoencoder = Model(inputs=input_tweet, outputs=[decoded, second_output])
autoencoder.compile(optimizer="adam",
loss={'decode_output': 'mse', 'second_output': 'mse'},
loss_weights={'decode_output': 0.001, 'second_output': 0.999},
metrics=["mae"])
autoencoder.fit(np.ones((1,50,1)), [np.ones((1,50,1)), np.ones((1,1))])

1/1 [=================================================================================================================================================================================second_output_mean_absolute_error decode_output_mean_absolute_error second_output_loss decode_output_loss=

============================================================================================================================================

这是错误: 错误 1(:

InvalidArgumentError: 2 root error(s) found.
(0) Invalid argument: You must feed a value for placeholder tensor 'decode_output_sample_weights_32' with dtype float and shape [?]
[[{{node decode_output_sample_weights_32}}]]
[[loss_2/second_output_loss/Mean_3/_3217]]
(1) Invalid argument: You must feed a value for placeholder tensor 'decode_output_sample_weights_32' with dtype float and shape [?]
[[{{node decode_output_sample_weights_32}}]]
0 successful operations.
0 derived errors ignored.

错误 2(:

InvalidArgumentError: You must feed a value for placeholder tensor 'decode_output_target_17' with dtype float and shape [?,?,?] [[{{node decode_output_target_17}}]]

错误 3(:

UnknownError: 2 root error(s) found.
(0) Unknown: Failed to get convolution algorithm. This is probably because cuDNN failed to initialize, so try looking to see if a warning log message was printed above.
[[{{node conv1d_1/convolution}}]]
[[loss/add/_157]]
(1) Unknown: Failed to get convolution algorithm. This is probably because cuDNN failed to initialize, so try looking to see if a warning log message was printed above.
[[{{node conv1d_1/convolution}}]]
0 successful operations.
0 derived errors ignored.

错误 4(:

UnknownError: 2 root error(s) found.
(0) Unknown: Failed to get convolution algorithm. This is probably because cuDNN failed to initialize, so try looking to see if a warning log message was printed above.
[[{{node conv1d_25/convolution}}]]
[[loss_6/second_output_loss/Mean_3/_1025]]
(1) Unknown: Failed to get convolution algorithm. This is probably because cuDNN failed to initialize, so try looking to see if a warning log message was printed above.
[[{{node conv1d_25/convolution}}]]
0 successful operations.
0 derived errors ignored.

相关内容

  • 没有找到相关文章

最新更新