当尝试使用多个热编码数组时,我不太了解keras模型的输入形状和尺寸如何工作。
例如,这是我的特征状态,其中包含9个单热编码数组。
features = [[first_one_hot] + [second_one_hot] + \
[third_one_hot] + [fourth_one_hot] + [sixt_one_hot] + [seventh_one_hot]+ ...]
具有以下形状:(3、4、4、5、5、5、10、10、10),其中:
形状:形状(30,4,10)表示具有3个维度的数组或张量,其中第一个维度包含30个元素,第二个维度包含4个元素,第三个维度包含10个元素,总计30 * 4 * 10 = 1200个元素或数字。
如果我仅解压缩一个热阵列中的每个热阵列,则模型的形状为(1,56)-但据我所知,该模型并不十分了解哪个值对应于哪个热阵列。
问题1 首先,我是否理解正确,上述数组中串联的每个功能都应该分开,而不是像我提到的那样使用(1,56)数组?可以说,而不是:
[1,0,0,0,1,0,0,0,...]使用:
[1,0,0],[1,0,0,0],...
如果是这样,我应该如何将分离的onehot给予模型?我是机器学习的新手,所以这可能是一个奇怪的问题。
问题2 如果是这样,将主题相似的onehots分组到单独的输入层中有什么好处?
我的build_model现在仅使用一个输入控件,其图层大小为(1,56):
def _build_model(self, hl1_dims, hl2_dims, hl3_dims, input_layer_size, output_layer_size, optimizer, loss):
model = Sequential()
# My input_layer_size is set to 9, as I have 9 dimensions
model.add(Dense(hl1_dims, input_dim=input_layer_size))
model.add(BatchNormalization())
model.add(Activation('relu'))
# Second Hidden Layer
...
据我了解,我还可以使用多个输入层,例如:
input_3d = Input(shape=(3,))
input_4d = Input(shape=(4,))
input_5d = Input(shape=(5,))
input_10d = Input(shape=(10,))
# multiple branches, for example:
branch_3d = Dense(32, activation='relu')(input_3d)
branch_3d = Dense(32, activation='relu')(branch_3d)
m_3d = Model(inputs=input_3d, outputs=branch_3d)
# Combine all output of branches
combined = Concatenate(axis=1)([m_3d.output, m_4d.output, m_5d.output, m_10d.output])
# Apply FC Layer
out = Dense(16, activation='relu')(combined)
out = Dense(output_layer_size, activation='linear')(out)
# Model accepts inputs of all branches and output action space based on output_layer_size
model = Model(inputs=[m_3d.input, m_4d.input, m_5d.input, m_10d.input], outputs=out)
我尝试了上述实现,但从未真正实现过,大多数错误如下:
ValueError: Error when checking input: expected dense_input to have 2 dimensions, but got array with shape (1, 1, 9)
但是,正如我说的,我什至不确定您是否将分类输入拆分为单独的层,还是将所有分类要素组合为一个形状的最佳实践。非常感谢您对此提供的任何投入。