可变序列长度数据的分类
16lz
2021-01-22
I have this data with varying number of elements in a row
我有这个数据连续不同数量的元素
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
I found this sequence classification
我发现了这个序列分类
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)
Can I use this or modify to use it? Some direction would be nice.
我可以使用它或修改使用它吗?一些方向会很好。
Also, if you have better suggestion which algorithm to use or how to do it please suggest.
此外,如果您有更好的建议使用哪种算法或如何做,请建议。
1 个解决方案
#1
0
A general approach would be to specify a specific value that means "unknown". For example, if all your values are positive you can pick it to be -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
The network then learns to ignore this value.
然后网络学会忽略这个值。
There is even a built in function called pad_sequences that does this for you.
甚至还有一个名为pad_sequences的内置函数可以为您执行此操作。
更多相关文章
- 任何人都可以提供更多的pythonic方式来生成morris序列吗?
- 如何在序列化后从查询中更新json数据?
- 16讲 序列!序列!
- python排序列表与铸造
- PHP“智能”搜索引擎搜索Mysql表的建议
- 建议一种有效的查询方式
- PostgreSQL之序列使用
- 成为Android高手必须掌握的28大项内容和10个建议
- 如何在Android 7.0+中检索SD卡的序列号?