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

时间:2019-09-29 17:12:19

标签: python keras conv-neural-network autoencoder

我有这个麻烦:检查目标时出错:预期的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

2 个答案:

答案 0 :(得分: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 [==============================]-0s 407ms / step-损失:0.9112-encode_output_loss: 0.9549-第二输出损失:0.9111-解码输出平均误差:0.9772-第二输出平均误差:0.9545

答案 1 :(得分:0)

这是错误: 错误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.