python - 使用 4 channel DNA 数据输入 Keras 时出现格式错误

标签 python tensorflow neural-network keras reshape

我有 DNA 数据,我试图将其输入 Keras,并且我对其进行了 one-hot 编码,使得每个 DNA 序列都是 4 个 channel (每种类型的核苷酸一个)。我遵循了一些教程,但似乎遇到了格式问题。也许有人可以帮助我?这是我第一次尝试将自己的数据输入 Keras。我的数据如下所示:

打印(x_train.shape) (1509, 4, 476) 打印(y_train.shape) (1509,)

我的模型(到目前为止)看起来像这样:

###Setup Keras to create a convolutional recurrent NN
# set parameters:
batch_size = 32
filters = (32, 1, 2) #(number of filters, rows per convolution kernel, columns per convolution kernel)
kernel_size = 16
x_shape = (1509, 1, 476, 4) #(samples, height, width, depth)
epochs = 3

#declare model
model = Sequential()

#CNN Input layer
model.add(Conv2D(filters,
                 kernel_size,
                 padding='same',
                 activation='relu',
                 strides=(1,0),
                 input_shape=x_shape))
print(model.output_shape)

但我收到以下错误:

ValueError: Input 0 is incompatible with layer conv2d_0: expected ndim=4, found ndim=5

我不清楚为什么当我指定 4 个维度时,模型会为 input_shape 参数找到 5 个维度。我错过了什么?

最佳答案

您不应在 input_shape 参数中包含样本数。这就是错误的含义。 批量大小尺寸会自动添加。

此外,您应该重新调整 x_train 表的形状以匹配 input_shape :

x_train = np.reshape(np.transpose(x_train, (0, 2, 1)), (x_train.shape[0], 1, x_train.shape[1], x_train.shape[2]))

这样我首先将数组转置为 (1509, 476, 4),然后添加一个维度:(1509, 1, 476, 4)

希望这有帮助

关于python - 使用 4 channel DNA 数据输入 Keras 时出现格式错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44980787/

相关文章:

python - 选择可训练变量来计算梯度 "No variables to optimize"

python - 在 Windows 上使用 NodeBox 运行图形库 + 无类型错误

python - Django:如何在运行测试后保持没有数据的数据库结构?

python - 如何在 Python 中按每个元组的某个索引的整数值对元组列表进行排序?

Tensorflow 停止训练并随机卡在 GPU 上

python - 将形状不等的数组列表转换为 Tensorflow 2 数据集 : ValueError: Can't convert non-rectangular Python sequence to Tensor

machine-learning - 修改 Caffe 以接受 lmdb 内的 16 位数据

python - Python无效语法为何?c

python - LSTM模型的准确率很低

python - Keras - 如何获得非规范化的 logits 而不是概率