2017-05-26 42 views
0

我想製作一個變化的自動編碼器來學習編碼DNA序列,但是我得到一個意外的錯誤。我的數據是一個熱門數組的數組。ValueError:輸入0與圖層conv_1不兼容:預計ndim = 3,找到ndim = 4

我收到的問題是一個值錯誤。它告訴我,我有一個四維輸入,當我的輸入顯然是三維(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) 
+0

'?'是batch_size。當你輸入數據時,應該包括batch_size作爲第一維。 另一件事情..爲什麼你的輸入==輸出? –

+0

*?是樣本的數量。 –

+0

輸入==輸出,因爲他在製作自動編碼器,所以輸入和輸出按照定義是相等的。 – quil

回答

0

試圖把它像這樣: Input(shape=(None, 4)

通常是因爲你不知道你的序列的長度的情況下,但我有同樣的問題,出於某種原因,當我做到了

希望你的作品這樣就解決了......

0

在documentatio提到我們需要提及特定格式的輸入,即(無,NumberOfFeatureVectors)。在您的情況下,將(無,4)

https://keras.io/layers/convolutional/

When using this layer as the first layer in a model, provide an input_shape argument (tuple of integers or None, e.g. (10, 128) for sequences of 10 vectors of 128-dimensional vectors, or (None, 128) for variable-length sequences of 128-dimensional vectors.

0

指定kernel_size在卷積層爲元組,而不是整數,即使它僅需要一個尺寸:

e = Conv1D(9, (9), activation = 'relu', name='conv_1')(seq) 

儘管在Keras documentation中陳述了一個整數和一個元組都是有效的,但我發現第二個元素在維度上更方便。

相關問題