【问题标题】:Simple network for arbitrary shape input用于任意形状输入的简单网络
【发布时间】:2017-11-20 14:41:05
【问题描述】:

我正在尝试使用Tensorflow 后端在Keras 中创建一个自动编码器。我关注this tutorial 是为了自己制作。网络的输入是任意的,即每个样本都是具有固定列数的二维数组(在这种情况下为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 的新手。我非常感谢您对此的任何帮助。

【问题讨论】:

    标签: python numpy tensorflow deep-learning keras


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2016-11-12
      • 2019-12-16
      • 2020-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多