简体   繁体   English

使用 Tensorflow 构建 RNN。 如何正确预处理我的数据集以匹配 RNN 的输入和 output 形状?

[英]Building RNN with Tensorflow. How do I preprocess my dataset correctly to match the RNN's input and output shape?

I'm working on a project about detecting drum onsets from audio.我正在做一个关于从音频中检测鼓声的项目。 I've currently preprocessed my training data and tried putting together a SimpleRNN neural network in tensorflow but couldn't get the two working together.我目前已经预处理了我的训练数据并尝试在 tensorflow 中组合一个 SimpleRNN 神经网络,但无法让两者一起工作。

During each timestep, my input consists of a 1D tensor of shape (84), and the output should be a tensor of shape (3).在每个时间步长中,我的输入由一个形状为 (84) 的一维张量组成,而 output 应该是一个形状为 (3) 的张量。

Currently my code looks like this:目前我的代码如下所示:

train_epochs = 10
batch_num = 10
learning_Rate = 0.001

''' I also tried using tf.dataset but couldn't get it to work
train_dataset = dataset.batch(batch_num, drop_remainder=True)
test_dataset = dataset.take(10000).batch(batch_num, drop_remainder=True)
print(train_dataset.element_spec)
'''
x_data = x_data[:70000]
y_data = y_data[:70000]
x_data.resize((70000, 84))
y_data.resize((70000, 3))
print(x_data.shape, y_data.shape) 

model = keras.Sequential()
model.add(keras.Input(shape=(None,84)))
model.add(layers.SimpleRNN(200,activation='relu', dropout=0.2))
model.add(layers.Dense(3, activation='sigmoid'))

model.compile(
    optimizer=keras.optimizers.RMSprop(learning_rate=learning_Rate),
    loss=keras.losses.BinaryCrossentropy(),
    #metrics F measure
    metrics=['acc',f1_m,precision_m, recall_m]
)
model.summary()

history = model.fit(
    x_data,y_data,
    epochs=train_epochs,
    batch_size=batch_num,
    # We pass some validation for
    # monitoring validation loss and metrics
    # at the end of each epoch
    validation_data=(x_data, y_data)
)

print("Evaluate on test data")
results = model.evaluate(test_dataset)
print("test loss, test acc:", results)

When I execute it, it gives me the error message:当我执行它时,它给了我错误消息:

 ValueError: Input 0 of layer sequential_35 is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: (10, 84)

If I change x_data and y_data to the shape (7000,10, 84) (7000,10, 3), the error message becomes如果我将 x_data 和 y_data 更改为形状 (7000,10, 84) (7000,10, 3),则错误消息变为

 ValueError: logits and labels must have the same shape ((10, 3) vs (10, 10, 3))

How can I fix this issue?我该如何解决这个问题? I'm very new to deep learning, so any advice on how to work on the project is much appreciated.我对深度学习非常陌生,因此非常感谢有关如何处理该项目的任何建议。

Input of simpleRNN should be 3D: simpleRNN 的输入应该是 3D:

x_data.resize((70000, 84, 1))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM