层conv1d_1的输入0与该层不兼容:预期ndim = 3,找到的ndim = 2.收到的完整形状:[无,200] [英] Input 0 of layer conv1d_1 is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: [None, 200]

查看:62
本文介绍了层conv1d_1的输入0与该层不兼容:预期ndim = 3,找到的ndim = 2.收到的完整形状:[无,200]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发应预测10秒音频文件中有趣时刻的应用程序.我将音频分割为50ms的块并提取了音符,因此每个示例都有200个音符.当我添加卷积层时,它将返回错误:

I'm working on application that should predict interesting moments in 10 sec audio files. I divided audio on 50ms chunks and extracted notes, so I have 200 notes for each example. When I add convolutional layer it returns an error:

ValueError:conv1d_1层的输入0与该层不兼容:预期ndim = 3,找到的ndim = 2.收到的完整图形:[无,200]

ValueError: Input 0 of layer conv1d_1 is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: [None, 200]

这是我的代码:

def get_dataset(file_path):
  dataset = tf.data.experimental.make_csv_dataset(
      file_path,
      batch_size=12,
      label_name='label',
      na_value='?',
      num_epochs=1,
      ignore_errors=False)
  return dataset

train = get_dataset('/content/gdrive/My Drive/MyProject/train.csv')
test = get_dataset('/content/gdrive/My Drive/MyProject/TestData/manual.csv')
feature_columns = []

for number in range(200):
  feature_columns.append(tf.feature_column.numeric_column('note' + str(number + 1) ))

preprocessing_layer = tf.keras.layers.DenseFeatures(feature_columns)

model = tf.keras.Sequential([
    preprocessing_layer,
    tf.keras.layers.Conv1D(32, 3, padding='same', activation=tf.nn.relu, input_shape=[None, 200]),
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(50, activation=tf.nn.relu),
    tf.keras.layers.Dense(1, activation=tf.nn.sigmoid)
])
model.compile(
    loss='binary_crossentropy',
    optimizer='adam',
    metrics=['accuracy'])
model.fit(train, epochs=20)

什么原因导致此问题,以及如何解决?

What causes this problem and how can it be fixed?

推荐答案

序列上的1D卷积需要3D输入.换句话说,对于批次中的每个元素,每个时间步长,单个向量.请考虑以下内容:

The 1D convolution over sequences expects a 3D input. In other words, for each element in the batch, for each time step, a single vector. Consider the following:

X = tf.random.normal([10, 200])
convolved = tf.keras.layers.Conv1D(32, 3, padding='same', activation=tf.nn.relu, input_shape=[None, 200])
print(convolved(X))

这会引发错误:

ValueError:conv1d_3层的输入0与该层不兼容:预期ndim = 3,找到的ndim = 2.收到的完整图形:[10,200]

ValueError: Input 0 of layer conv1d_3 is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: [10, 200]

但是,如果我们提供10个批次样本中的每个样本,则对于5个时间步长中的每一个,都会提供200维矢量:

However, If we provide for each of the 10 batch samples, for each of the 5 time steps, a 200 dimensional vector:

X = tf.random.normal([10, 5, 200])
convolved = tf.keras.layers.Conv1D(32, 3, padding='same', activation=tf.nn.relu, input_shape=[None, 200])
print(convolved(X)

这可以正常工作.因此,在您的情况下,对于每个音频文件,每一秒钟(取决于您对数据进行采样的方式),都将具有单个矢量.

This works as it should. Therefore, in your case, for each audio file, for each second (depends on how you sample the data), you will have a single vector.

这篇关于层conv1d_1的输入0与该层不兼容:预期ndim = 3,找到的ndim = 2.收到的完整形状:[无,200]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆