[英]How to make sure con1D's output_shape is same as input_shape with time series in keras autoencoder?
I try to use keras autoencoder model to compress and decompress my time-series data.我尝试使用 keras 自动编码器 model 来压缩和解压缩我的时间序列数据。 but when I change the layer with
Conv1D
, the output shape is incorrect.但是当我使用Conv1D
更改图层时,output 形状不正确。I have some time series data with the shape of (4000, 689), where represent 4000 samples and each sample has 689 features.我有一些形状为 (4000, 689) 的时间序列数据,其中代表 4000 个样本,每个样本有 689 个特征。 I want to use
Conv1D
to compress the data but theUpsampling
layer's and lastConv1D
layer's output shape(?, 688, 1) is not equal to input shape (, 689, 1).我想使用Conv1D
压缩数据,但上Upsampling
层和最后一个Conv1D
层的 output 形状 (?, 688, 1) 不等于输入形状 (, 689, 1)。How should I set those layers' parameters?我应该如何设置这些图层的参数? Thanks in advance.提前致谢。
x_train = data[0:4000].values
x_test = data[4000:].values
print('x_train shape:', x_train.shape)
print('x_test shape:', x_test.shape)
x_train shape: (4000, 689) x_train 形状:(4000, 689)
x_test shape: (202, 689) x_test 形状: (202, 689)
I reshaped the x_train, x_test to 3dim, like below.我将 x_train、x_test 重塑为 3dim,如下所示。
x_tr = x_train.reshape(4000,689,1)
x_te = x_test.reshape(202,689,1)
print('x_tr shape:', x_tr.shape)
print('x_te shape:', x_te.shape)
x_tr shape: (4000, 689, 1) x_tr 形状:(4000, 689, 1)
x_te shape: (202, 689, 1) x_te 形状:(202, 689, 1)
input_img = Input(shape=(689,1))
x = Conv1D(16, 3, activation='relu', padding='same')(input_img)
print(x)
x = MaxPooling1D(2, padding='same')(x)
print(x)
x = Conv1D(8, 3, activation='relu', padding='same')(x)
print(x)
x = MaxPooling1D(2, padding='same')(x)
print(x)
x = Conv1D(8, 3, activation='relu', padding='same')(x)
print(x)
encoded = MaxPooling1D(2)(x)
print(encoded)
print('--------------')
x = Conv1D(8, 3, activation='relu', padding='same')(encoded)
print(x)
x = UpSampling1D(2)(x)
print(x)
x = Conv1D(8, 3, activation='relu', padding='same')(x)
print(x)
x = UpSampling1D(2)(x)
print(x)
x = Conv1D(16, 3, activation='relu', padding='same')(x)
print(x)
x = UpSampling1D(2)(x)
print(x)
decoded = Conv1D(1, 3, activation='sigmoid', padding='same')(x)
print(decoded)
autoencoder = Model(input_img, decoded)
autoencoder.compile(optimizer='adam', loss='mse')
When I imported those models and run cells above in Jupyter, It seems ok.当我导入这些模型并在 Jupyter 中运行上面的单元格时,它似乎没问题。 Maybe.或许。 But I get the error in next code when running
autoencoder.fit
.但是我在运行autoencoder.fit
时在下一个代码中遇到错误。
autoencoder.fit(x_tr, x_tr, epochs=50, batch_size=128, shuffle=True, validation_data=(x_te, x_te))
So I print
each layer.所以我print
每一层。
The each layers'
Tensor("conv1d_166/Relu:0", shape=(?, 689, 16), dtype=float32)
Tensor("max_pooling1d_71/Squeeze:0", shape=(?, 345, 16), dtype=float32)
Tensor("conv1d_167/Relu:0", shape=(?, 345, 8), dtype=float32)
Tensor("max_pooling1d_72/Squeeze:0", shape=(?, 173, 8), dtype=float32)
Tensor("conv1d_168/Relu:0", shape=(?, 173, 8), dtype=float32)
Tensor("max_pooling1d_73/Squeeze:0", shape=(?, 86, 8), dtype=float32)
Tensor("conv1d_169/Relu:0", shape=(?, 86, 8), dtype=float32)
Tensor("up_sampling1d_67/concat:0", shape=(?, 172, 8), dtype=float32)
Tensor("conv1d_170/Relu:0", shape=(?, 172, 8), dtype=float32)
Tensor("up_sampling1d_68/concat:0", shape=(?, 344, 8), dtype=float32)
Tensor("conv1d_171/Relu:0", shape=(?, 344, 16), dtype=float32)
Tensor("up_sampling1d_69/concat:0", shape=(?, 688, 16), dtype=float32)
Tensor("conv1d_172/Sigmoid:0", shape=(?, 688, 1), dtype=float32)
ValueError bellow: ValueError 波纹管:
ValueError Traceback (most recent call last)
<ipython-input-74-56836006a800> in <module>
3 batch_size=128,
4 shuffle=True,
----> 5 validation_data=(x_te, x_te)
6 )
~/anaconda3/envs/keras/lib/python3.6/site-packages/keras/engine/training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, **kwargs)
950 sample_weight=sample_weight,
951 class_weight=class_weight,
--> 952 batch_size=batch_size)
953 # Prepare validation data.
954 do_validation = False
~/anaconda3/envs/keras/lib/python3.6/site-packages/keras/engine/training.py in _standardize_user_data(self, x, y, sample_weight, class_weight, check_array_lengths, batch_size)
787 feed_output_shapes,
788 check_batch_axis=False, # Don't enforce the batch size.
--> 789 exception_prefix='target')
790
791 # Generate sample-wise weight values given the `sample_weight` and
~/anaconda3/envs/keras/lib/python3.6/site-packages/keras/engine/training_utils.py in standardize_input_data(data, names, shapes, check_batch_axis, exception_prefix)
136 ': expected ' + names[i] + ' to have shape ' +
137 str(shape) + ' but got array with shape ' +
--> 138 str(data_shape))
139 return data
140
ValueError: Error when checking target: expected conv1d_172 to have shape (688, 1) but got array with shape (689, 1)
Is
floor
function make this happen?floor
function 让这发生了吗?
How to fix the error andautoencoder.fit
correctly?如何正确修复错误和autoencoder.fit
?
Thanks in advance.提前致谢。
Using Convolutional layers, you need to infer your output size based on the input size, kernel size and other parameters.使用卷积层,你需要根据输入大小,kernel大小和其他参数来推断你的output大小。 The simplest way to do so is to feed a data sample through the.network and see the final vector size after your last convolutional layer.最简单的方法是通过网络提供数据样本,然后查看最后一个卷积层之后的最终向量大小。 Then, you can define further layers based on that size.然后,您可以根据该大小定义更多层。
Here is an example from my pytorch project:这是我的 pytorch 项目中的示例:
def _infer_flat_size(self):
encoder_output = self.encoder(torch.ones(1, *self.input_size))
return int(np.prod(encoder_output.size()[1:])), encoder_output.size()[1:]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.