ValueError: Input 0 is in compatible with layer conv_1: expected ndim=3, found ndim=4
Posted
技术标签:
【中文标题】ValueError: Input 0 is in compatible with layer conv_1: expected ndim=3, found ndim=4【英文标题】:ValueError: Input 0 is incompatible with layer conv_1: expected ndim=3, found ndim=4 【发布时间】:2017-10-26 22:42:00 【问题描述】:我正在尝试制作一个变分自动编码器来学习编码 DNA 序列,但遇到了意外错误。
我的数据是一个单热数组。
我遇到的问题是值错误。它告诉我我有一个四维输入,而我的输入显然是三维(100、4008、4)。
实际上,当我打印出seq
层时,它说它的形状是(?, 100, 4008, 4)。
当我取出一个维度时,它会给我一个二维的错误。
任何帮助将不胜感激!
代码是:
from keras.layers import Input
from keras.layers.convolutional import Conv1D
from keras.layers.core import Dense, Activation, Flatten, RepeatVector, Lambda
from keras import backend as K
from keras.layers.wrappers import TimeDistributed
from keras.layers.recurrent import GRU
from keras.models import Model
from keras import objectives
from one_hot import dna_sequence_to_one_hot
from random import shuffle
import numpy as np
# take FASTA file and convert into array of vectors
seqs = [line.rstrip() for line in open("/home/ubuntu/sequences.fa", "r").readlines() if line[0] != ">"]
seqs = [dna_sequence_to_one_hot(s) for s in seqs]
seqs = np.array(seqs)
# first random thousand are training, next thousand are validation
test_data = seqs[:1000]
validation_data = seqs[1000:2000]
latent_rep_size = 292
batch_size = 100
epsilon_std = 0.01
max_length = len(seqs[0])
charset_length = 4
epochs = 100
def sampling(args):
z_mean_, z_log_var_ = args
# batch_size = K.shape(z_mean_)[0]
epsilon = K.random_normal_variable((batch_size, latent_rep_size), 0., epsilon_std)
return z_mean_ + K.exp(z_log_var_ / 2) * epsilon
# loss function
def vae_loss(x, x_decoded_mean):
x = K.flatten(x)
x_decoded_mean = K.flatten(x_decoded_mean)
xent_loss = max_length * objectives.categorical_crossentropy(x, x_decoded_mean)
kl_loss = - 0.5 * K.mean(1 + z_log_var - K.square(z_mean) - K.exp(z_log_var), axis = -1)
return xent_loss + kl_loss
# Encoder
seq = Input(shape=(100, 4008, 4), name='one_hot_sequence')
e = Conv1D(9, 9, activation = 'relu', name='conv_1')(seq)
e = Conv1D(9, 9, activation = 'relu', name='conv_2')(e)
e = Conv1D(9, 9, activation = 'relu', name='conv_3')(e)
e = Conv1D(10, 11, activation = 'relu', name='conv_4')(e)
e = Flatten(name='flatten_1')(e)
e = Dense(435, activation = 'relu', name='dense_1')(e)
z_mean = Dense(latent_rep_size, name='z_mean', activation = 'linear')(e)
z_log_var = Dense(latent_rep_size, name='z_log_var', activation = 'linear')(e)
z = Lambda(sampling, output_shape=(latent_rep_size,), name='lambda')([z_mean, z_log_var])
encoder = Model(seq, z)
# Decoder
d = Dense(latent_rep_size, name='latent_input', activation = 'relu')(z)
d = RepeatVector(max_length, name='repeat_vector')(d)
d = GRU(501, return_sequences = True, name='gru_1')(d)
d = GRU(501, return_sequences = True, name='gru_2')(d)
d = GRU(501, return_sequences = True, name='gru_3')(d)
d = TimeDistributed(Dense(charset_length, activation='softmax'), name='decoded_mean')(d)
# create the model, compile it, and fit it
vae = Model(seq, d)
vae.compile(optimizer='Adam', loss=vae_loss, metrics=['accuracy'])
vae.fit(x=test_data, y=test_data, epochs=epochs, batch_size=batch_size, validation_data=validation_data)
【问题讨论】:
?
是批大小。当您输入数据时,应该包含 batch_size 作为第一个维度。另一件事.. 为什么你的输入 == 输出?
* ?是样本数。
Input == output 因为他在做自动编码器,所以输入和输出在定义上是相等的。
现在我有同样的问题,我找不到任何解决方案。太奇怪了!
【参考方案1】:
尝试像这样将输入放到网络中:
Input(shape=(None, 4)
一般情况下,你不知道你的序列的长度,但我有同样的问题,由于某种原因,当我这样做的时候就解决了
希望它有效!
【讨论】:
【参考方案2】:在文档中提到我们需要以特定格式提及输入,即 (None,NumberOfFeatureVectors)。在您的情况下,它将是 (None,4)
https://keras.io/layers/convolutional/
当将此层用作模型中的第一层时,请提供 input_shape 参数(整数元组或无,例如 (10, 128) 为 128 维向量的 10 个向量的序列,或 (None, 128) 为 128维向量的变长序列。
【讨论】:
【参考方案3】:将卷积层中的 kernel_size 指定为元组,而不是整数,即使它只需要一个维度:
e = Conv1D(9, (9), activation = 'relu', name='conv_1')(seq)
虽然在Keras documentation 中声明整数和元组都是有效的,但我发现第二个在维度方面更方便。
【讨论】:
【参考方案4】:我最近解决了这个问题。会报错,因为你在使用 Conv1D 函数的时候在 input_shape 中包含了通道。
【讨论】:
以上是关于ValueError: Input 0 is in compatible with layer conv_1: expected ndim=3, found ndim=4的主要内容,如果未能解决你的问题,请参考以下文章
ValueError: Input 0 is in compatible with layer conv_1: expected ndim=3, found ndim=4
ValueError: Input 0 is in compatible with layer vggface_resnet50: expected shape=(None, 224, 224, 3)
如何修复:ValueError: Input 0 is in compatible with layer lstm_2: expected ndim=3, found ndim=2
TensorFlow ValueError: Input 0 is in compatible with layer model_1: expected shape=(None, 32, 32, 1)
Keras LSTM ValueError: Input 0 of layer "sequential" is in compatible with the layer: expe
Keras ValueError: Input 0 is in compatible with layer conv2d_1: expected ndim=4, found ndim=5