【问题标题】:Classification of variable sequence length data可变序列长度数据的分类
【发布时间】:2017-08-01 23:02:07
【问题描述】:

我的这些数据在一行中有不同数量的元素

sample feat1  feat2 feat3 feat4 feat5 feat6 feat7
 1       1      200  250    312   474  
 1       2      170  280    370
 ...
 1       12     220  400    470   520  620   720
 2       1      130  320    430   580  612   
 ...
 N       12     70   180    270   410

我找到了这个序列分类

from keras.datasets import imdb
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM
from keras.layers.convolutional import Convolution1D
from keras.layers.convolutional import MaxPooling1D
from keras.layers.embeddings import Embedding
from keras.preprocessing import sequence
numpy.random.seed(7)
top_words = 5000
(X_train, y_train), (X_test, y_test) = imdb.load_data(nb_words=top_words)
# truncate and pad input sequences
max_review_length = 500
X_train = sequence.pad_sequences(X_train, maxlen=max_review_length)
X_test = sequence.pad_sequences(X_test, maxlen=max_review_length)
# create the model
embedding_vecor_length = 32
model = Sequential()
model.add(Embedding(top_words, embedding_vecor_length, input_length=max_review_length))
model.add(Convolution1D(nb_filter=32, filter_length=3, border_mode='same', activation='relu'))
model.add(MaxPooling1D(pool_length=2))
model.add(LSTM(100))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
print(model.summary())
model.fit(X_train, y_train, nb_epoch=3, batch_size=64)

我可以使用或修改使用它吗?一些方向会很好。

另外,如果您有更好的建议使用哪种算法或如何使用,请提出建议。

【问题讨论】:

    标签: python tensorflow lstm recurrent-neural-network


    【解决方案1】:

    一般方法是指定一个表示“未知”的特定值。例如,如果您的所有值都是正数,您可以将其选为 -1。

    sample feat1  feat2 feat3 feat4 feat5 feat6 feat7
     1       1      200  250    312   474    -1    -1
     1       2      170  280    370    -1    -1    -1
     ...
     1       12     220  400    470   520   620   720
     2       1      130  320    430   580   612    -1  
     ...
     N       12     70   180    270   410    -1    -1
    

    然后网络学会忽略这个值。

    甚至还有一个名为 pad_sequences 的内置函数可以为您执行此操作。

    【讨论】:

      猜你喜欢
      • 2019-02-26
      • 2019-04-17
      • 1970-01-01
      • 2014-02-10
      • 1970-01-01
      • 2018-10-10
      • 1970-01-01
      • 1970-01-01
      • 2019-06-05
      相关资源
      最近更新 更多