任意形状inpu的简单网络

2024-10-06 11:19:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用Tensorflow后端在Keras中创建自动编码器。我跟着this tutorial去做我自己的。网络的输入是任意的,即每个样本是一个具有固定列数的2d数组(在本例中为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还是个新手。我真的很感激任何帮助。你知道吗


Tags: thenumpyinputsizemodelrandom数组array
1条回答
网友
1楼 · 发布于 2024-10-06 11:19:44

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

你能做的是用0填充数组,使它们都具有(24,12)的形状:然后np.数组可以返回一个三维数组和keras输入层不抱怨。你知道吗

相关问题 更多 >