任意形状输入的简单网络

时间:2017-11-20 14:41:05

标签: python numpy tensorflow deep-learning keras

我正在尝试使用Keras后端在Tensorflow中创建自动编码器。我跟着this tutorial来制作自己的。{3}}。对网络的输入是任意的,即每个样本是具有固定列数的二维数组(在这种情况下为12),但行的范围在424之间。

到目前为止我尝试的是:

# Generating random data
myTraces = []
for i in range(100):
    num_events = random.randint(4, 24) 
    traceTmp = np.random.randint(2, size=(num_events, 12))

    myTraces.append(traceTmp)

myTraces = np.array(myTraces) # (read Note down below) 

这是我的样本模型

input = Input(shape=(None, 12))

x = Conv1D(64, 3, padding='same', activation='relu')(input)

x = MaxPool1D(strides=2, pool_size=2)(x)

x = Conv1D(128, 3, padding='same', activation='relu')(x)

x = UpSampling1D(2)(x)
x = Conv1D(64, 3, padding='same', activation='relu')(x)

x = Conv1D(12, 1, padding='same', activation='relu')(x)

model = Model(input, x)
model.compile(optimizer='adadelta', loss='binary_crossentropy')

model.fit(myTraces, myTraces, epochs=50, batch_size=10, shuffle=True, validation_data=(myTraces, myTraces))

注意:根据Keras Doc,它表示输入应该是一个numpy数组,如果我这样做,我会收到以下错误:

ValueError: Error when checking input: expected input_1 to have 3 dimensions, but got array with shape (100, 1)

如果我不将它转换为numpy数组并让它成为numpy数组的列表,我会得到以下错误:

ValueError: Error when checking model input: the list of Numpy arrays that you are passing to your model is not the size the model expected. Expected to see 1 array(s), but instead got the following list of 100 arrays: [array([[0, 1, 0, 0 ...

我不知道我在这里做错了什么。我也是Keras的新手。我真的很感激有关这方面的任何帮助。

1 个答案:

答案 0 :(得分:3)

Numpy不知道如何处理具有不同行大小的数组列表(请参阅this answer)。使用traceTmp调用np.array时,它将返回一个数组列表,而不是一个3D数组(形状为(100,1)的数组表示100个数组的列表)。 Keras也需要一个齐次数组,这意味着所有输入数组应该具有相同的形状。

你可以做的是用零填充数组,使它们都具有形状(24,12):然后np.array可以返​​回一个三维数组,而keras输入层不会抱怨。

相关问题