python - 自编码器网络中的数组形状
问题描述
我正在尝试将自动编码器网络拟合到包含多维数组的数据集,但在自动编码器的解码器部分中的图层形状存在问题。我的网络的输入数据包含固定长度的形状段,(1,100,4)
因此总的来说,它包含(m, 1,100,4)
用于m
观察。
为了提供 MWE,我生成了以下类似于我的输入数据形状的数据。
#generate dummy data
X = np.random.randn(20, 1, 100, 4)
a,b,c = np.repeat(0, 7), np.repeat(1, 7), np.repeat(2, 6)
y = np.hstack((a,b,c))
X.shape
(20, 1, 100, 4)
x_train, x_test, y_train, y_test = train_test_split(X, y, test_size=0.20, random_state=7)
这是我的网络的代码:
class SingleEncoder:
def __init__(self, train, test):
self.x_train = train
self.x_test = test
self.first_dim = 1
self.second_dim = 100
self.channels = 4
self.input_dim = (self.first_dim, self.second_dim, self.channels)
def setSingleModel(self):
input_layer = self.input_dim
autoencoder = Sequential()
activ='relu'
# encoder
autoencoder.add(Flatten(input_shape=input_layer))
autoencoder.add(Dense(200, activation='relu'))
autoencoder.add(Dense(100, activation='relu'))
autoencoder.add(Dense(80, activation='linear'))
#decoder
autoencoder.add(Dense(80, activation='linear'))
autoencoder.add(Dense(100, activation='relu'))
autoencoder.add(Dense(200, activation='relu'))
#autoencoder.add(Reshape(input_layer))
autoencoder.compile(optimizer='adam', loss='mae', metrics=['mean_squared_error'])
autoencoder.summary()
filepath = "weights.best.hdf5"
checkpoint = ModelCheckpoint(filepath, monitor='loss', verbose=1, save_best_only=True, mode='max')
callbacks_list = [checkpoint]
autoencoder.fit(self.x_train, self.x_train, epochs=250, batch_size=256, shuffle=True,callbacks=callbacks_list)
return autoencoder
型号总结:
Model: "sequential_1"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
flatten_1 (Flatten) (None, 400) 0
_________________________________________________________________
dense_1 (Dense) (None, 200) 80200
_________________________________________________________________
dense_2 (Dense) (None, 100) 20100
_________________________________________________________________
dense_3 (Dense) (None, 80) 8080
_________________________________________________________________
dense_4 (Dense) (None, 80) 6480
_________________________________________________________________
dense_5 (Dense) (None, 100) 8100
_________________________________________________________________
dense_6 (Dense) (None, 200) 20200
=================================================================
Total params: 143,160
Trainable params: 143,160
Non-trainable params: 0
_________________________________________________________________
因此,创建一个自动编码器对象会产生我无法弄清楚如何解决的错误:
autoencoder = SingleEncoder(x_train, x_test)
autoencoder = autoencoder.setSingleModel()
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-9-7c9d08768298> in <module>()
1 autoencoder = SingleEncoder(x_train, x_test)
----> 2 autoencoder = autoencoder.setSingleModel()
3 frames
/usr/local/lib/python3.6/dist-packages/keras/engine/training_utils.py in standardize_input_data(data, names, shapes, check_batch_axis, exception_prefix)
133 ': expected ' + names[i] + ' to have ' +
134 str(len(shape)) + ' dimensions, but got array '
--> 135 'with shape ' + str(data_shape))
136 if not check_batch_axis:
137 data_shape = data_shape[1:]
ValueError: Error when checking target: expected dense_6 to have 2 dimensions, but got array with shape (16, 1, 100, 4)
有人可以帮助解决这个问题吗?
解决方案
这是执行此操作的最简单方法...删除第一个位置的展平,这可能会导致您出现一些形状问题,因为您正在从 4D 传递到 2D 并且您的目标仍然是 4D。使用解码器中与输入维度匹配的最后一层
class SingleEncoder:
def __init__(self, X):
self.X = X
self.first_dim = 1
self.second_dim = 100
self.channels = 4
self.input_dim = (self.first_dim, self.second_dim, self.channels)
def setSingleModel(self):
input_layer = self.input_dim
autoencoder = Sequential()
activ='relu'
# encoder
autoencoder.add(Dense(200, activation='relu', input_shape=input_layer))
autoencoder.add(Dense(100, activation='relu'))
autoencoder.add(Dense(80, activation='linear'))
#decoder
autoencoder.add(Dense(80, activation='linear'))
autoencoder.add(Dense(100, activation='relu'))
autoencoder.add(Dense(200, activation='relu'))
autoencoder.add(Dense(self.channels, activation='relu'))
autoencoder.compile(optimizer='adam', loss='mae',
metrics=['mean_squared_error'])
autoencoder.summary()
autoencoder.fit(self.X, self.X, epochs=3, batch_size=32)
return autoencoder
X = np.random.randn(20, 1, 100, 4)
autoencoder = SingleEncoder(X)
autoencoder = autoencoder.setSingleModel()
推荐阅读
- maven - Jenkins管道mvn:找不到命令
- scala - 如何为从 json 文件读取的 spark 应用程序编写单元测试
- excel - 将变量添加到 R1C1 公式中
- r - 将 R 中的面板数据重组为列
- python - 移动 PyCharm 项目文件夹并保留对 python 解释器的引用
- python-3.x - PySide2 如何取消分配当前布局并将新布局分配给窗口小部件?.count() 给出属性错误?
- react-native - React Native 错误:元素类型无效:应为字符串或类/函数,但得到:未定义
- javascript - 使用 jQuery 重置特定的表单输入
- sql - 使用 Postgres/PostGIS 高效更新包含 200 万行的表
- excel - 将过程的地址返回给变量