Using text classification for system calls
$begingroup$
I'm working on a project in which I should classify System calls sequences, my dataset is represented as sequences of integers (from 1 to 340). To do the classification I have inspired from Text classification projects. I'm trying to use on of them but I found a problem in my dataset shape, the code is:
df = pd.read_csv("data.txt")
#df_test = pd.read_csv("validation.txt")
#split arrays into train and test data (cross validation)
train_text, test_text, train_y, test_y = train_test_split(df,df,test_size =
0.2)
#train_text, train_y = (df,df)
#test_text, test_y = (df_test, df_test)
MAX_NB_WORDS = 5700
texts_train = train_text.astype(str)
texts_test = test_text.astype(str)
tokenizer = Tokenizer(nb_words=MAX_NB_WORDS, char_level=False)
tokenizer.fit_on_texts(texts_train)
sequences = tokenizer.texts_to_sequences(texts_train)
sequences_test = tokenizer.texts_to_sequences(texts_test)
word_index = tokenizer.word_index
#print('Found %s unique tokens.' % len(word_index))
type(tokenizer.word_index), len(tokenizer.word_index)
index_to_word = dict((i, w) for w, i in tokenizer.word_index.items())
" ".join([index_to_word[i] for i in sequences[0]])
seq_lens = [len(s) for s in sequences]
#print("average length: %0.1f" % np.mean(seq_lens))
#print("max length: %d" % max(seq_lens))
MAX_SEQUENCE_LENGTH = 100
# pad sequences with 0s
x_train = pad_sequences(sequences, maxlen=MAX_SEQUENCE_LENGTH) # former des
sequence de meme taille 150, en ajoutant des 0
x_test = pad_sequences(sequences_test, maxlen=MAX_SEQUENCE_LENGTH)
#print('Shape of data train:', x_train.shape) #it gives (1,100)
#print('Shape of data test tensor:', x_test.shape)
y_train = train_y
y_test = test_y
#if np.any(y_train):
#y_train = to_categorical(y_train)
print('Shape of label tensor:', y_train.shape)
EMBEDDING_DIM = 50
N_CLASSES = 2
# input: a sequence of MAX_SEQUENCE_LENGTH integers
sequence_input = Input(shape=(MAX_SEQUENCE_LENGTH,), dtype='float32')
embedding_layer = Embedding(MAX_NB_WORDS, EMBEDDING_DIM,
input_length=MAX_SEQUENCE_LENGTH,
trainable=True)
embedded_sequences = embedding_layer(sequence_input)
average = GlobalAveragePooling1D()(embedded_sequences)
predictions = Dense(N_CLASSES, activation='softmax')(average)
model = Model(sequence_input, predictions)
model.compile(loss='categorical_crossentropy',
optimizer='adam', metrics=['acc'])
model.fit(x_train, y_train, validation_split=0.1,
nb_epoch=10, batch_size=100)
output_test = model.predict(x_test)
print("test auc:", roc_auc_score(y_test,output_test[:,1]))
I got the error: ValueError Error when checking target expected dense_1 to have shape(2,), but got array with shape(1,)
Any suggestion, cause I don't know how to proceed .
Thank you
python deep-learning keras tensorflow nlp
$endgroup$
add a comment |
$begingroup$
I'm working on a project in which I should classify System calls sequences, my dataset is represented as sequences of integers (from 1 to 340). To do the classification I have inspired from Text classification projects. I'm trying to use on of them but I found a problem in my dataset shape, the code is:
df = pd.read_csv("data.txt")
#df_test = pd.read_csv("validation.txt")
#split arrays into train and test data (cross validation)
train_text, test_text, train_y, test_y = train_test_split(df,df,test_size =
0.2)
#train_text, train_y = (df,df)
#test_text, test_y = (df_test, df_test)
MAX_NB_WORDS = 5700
texts_train = train_text.astype(str)
texts_test = test_text.astype(str)
tokenizer = Tokenizer(nb_words=MAX_NB_WORDS, char_level=False)
tokenizer.fit_on_texts(texts_train)
sequences = tokenizer.texts_to_sequences(texts_train)
sequences_test = tokenizer.texts_to_sequences(texts_test)
word_index = tokenizer.word_index
#print('Found %s unique tokens.' % len(word_index))
type(tokenizer.word_index), len(tokenizer.word_index)
index_to_word = dict((i, w) for w, i in tokenizer.word_index.items())
" ".join([index_to_word[i] for i in sequences[0]])
seq_lens = [len(s) for s in sequences]
#print("average length: %0.1f" % np.mean(seq_lens))
#print("max length: %d" % max(seq_lens))
MAX_SEQUENCE_LENGTH = 100
# pad sequences with 0s
x_train = pad_sequences(sequences, maxlen=MAX_SEQUENCE_LENGTH) # former des
sequence de meme taille 150, en ajoutant des 0
x_test = pad_sequences(sequences_test, maxlen=MAX_SEQUENCE_LENGTH)
#print('Shape of data train:', x_train.shape) #it gives (1,100)
#print('Shape of data test tensor:', x_test.shape)
y_train = train_y
y_test = test_y
#if np.any(y_train):
#y_train = to_categorical(y_train)
print('Shape of label tensor:', y_train.shape)
EMBEDDING_DIM = 50
N_CLASSES = 2
# input: a sequence of MAX_SEQUENCE_LENGTH integers
sequence_input = Input(shape=(MAX_SEQUENCE_LENGTH,), dtype='float32')
embedding_layer = Embedding(MAX_NB_WORDS, EMBEDDING_DIM,
input_length=MAX_SEQUENCE_LENGTH,
trainable=True)
embedded_sequences = embedding_layer(sequence_input)
average = GlobalAveragePooling1D()(embedded_sequences)
predictions = Dense(N_CLASSES, activation='softmax')(average)
model = Model(sequence_input, predictions)
model.compile(loss='categorical_crossentropy',
optimizer='adam', metrics=['acc'])
model.fit(x_train, y_train, validation_split=0.1,
nb_epoch=10, batch_size=100)
output_test = model.predict(x_test)
print("test auc:", roc_auc_score(y_test,output_test[:,1]))
I got the error: ValueError Error when checking target expected dense_1 to have shape(2,), but got array with shape(1,)
Any suggestion, cause I don't know how to proceed .
Thank you
python deep-learning keras tensorflow nlp
$endgroup$
add a comment |
$begingroup$
I'm working on a project in which I should classify System calls sequences, my dataset is represented as sequences of integers (from 1 to 340). To do the classification I have inspired from Text classification projects. I'm trying to use on of them but I found a problem in my dataset shape, the code is:
df = pd.read_csv("data.txt")
#df_test = pd.read_csv("validation.txt")
#split arrays into train and test data (cross validation)
train_text, test_text, train_y, test_y = train_test_split(df,df,test_size =
0.2)
#train_text, train_y = (df,df)
#test_text, test_y = (df_test, df_test)
MAX_NB_WORDS = 5700
texts_train = train_text.astype(str)
texts_test = test_text.astype(str)
tokenizer = Tokenizer(nb_words=MAX_NB_WORDS, char_level=False)
tokenizer.fit_on_texts(texts_train)
sequences = tokenizer.texts_to_sequences(texts_train)
sequences_test = tokenizer.texts_to_sequences(texts_test)
word_index = tokenizer.word_index
#print('Found %s unique tokens.' % len(word_index))
type(tokenizer.word_index), len(tokenizer.word_index)
index_to_word = dict((i, w) for w, i in tokenizer.word_index.items())
" ".join([index_to_word[i] for i in sequences[0]])
seq_lens = [len(s) for s in sequences]
#print("average length: %0.1f" % np.mean(seq_lens))
#print("max length: %d" % max(seq_lens))
MAX_SEQUENCE_LENGTH = 100
# pad sequences with 0s
x_train = pad_sequences(sequences, maxlen=MAX_SEQUENCE_LENGTH) # former des
sequence de meme taille 150, en ajoutant des 0
x_test = pad_sequences(sequences_test, maxlen=MAX_SEQUENCE_LENGTH)
#print('Shape of data train:', x_train.shape) #it gives (1,100)
#print('Shape of data test tensor:', x_test.shape)
y_train = train_y
y_test = test_y
#if np.any(y_train):
#y_train = to_categorical(y_train)
print('Shape of label tensor:', y_train.shape)
EMBEDDING_DIM = 50
N_CLASSES = 2
# input: a sequence of MAX_SEQUENCE_LENGTH integers
sequence_input = Input(shape=(MAX_SEQUENCE_LENGTH,), dtype='float32')
embedding_layer = Embedding(MAX_NB_WORDS, EMBEDDING_DIM,
input_length=MAX_SEQUENCE_LENGTH,
trainable=True)
embedded_sequences = embedding_layer(sequence_input)
average = GlobalAveragePooling1D()(embedded_sequences)
predictions = Dense(N_CLASSES, activation='softmax')(average)
model = Model(sequence_input, predictions)
model.compile(loss='categorical_crossentropy',
optimizer='adam', metrics=['acc'])
model.fit(x_train, y_train, validation_split=0.1,
nb_epoch=10, batch_size=100)
output_test = model.predict(x_test)
print("test auc:", roc_auc_score(y_test,output_test[:,1]))
I got the error: ValueError Error when checking target expected dense_1 to have shape(2,), but got array with shape(1,)
Any suggestion, cause I don't know how to proceed .
Thank you
python deep-learning keras tensorflow nlp
$endgroup$
I'm working on a project in which I should classify System calls sequences, my dataset is represented as sequences of integers (from 1 to 340). To do the classification I have inspired from Text classification projects. I'm trying to use on of them but I found a problem in my dataset shape, the code is:
df = pd.read_csv("data.txt")
#df_test = pd.read_csv("validation.txt")
#split arrays into train and test data (cross validation)
train_text, test_text, train_y, test_y = train_test_split(df,df,test_size =
0.2)
#train_text, train_y = (df,df)
#test_text, test_y = (df_test, df_test)
MAX_NB_WORDS = 5700
texts_train = train_text.astype(str)
texts_test = test_text.astype(str)
tokenizer = Tokenizer(nb_words=MAX_NB_WORDS, char_level=False)
tokenizer.fit_on_texts(texts_train)
sequences = tokenizer.texts_to_sequences(texts_train)
sequences_test = tokenizer.texts_to_sequences(texts_test)
word_index = tokenizer.word_index
#print('Found %s unique tokens.' % len(word_index))
type(tokenizer.word_index), len(tokenizer.word_index)
index_to_word = dict((i, w) for w, i in tokenizer.word_index.items())
" ".join([index_to_word[i] for i in sequences[0]])
seq_lens = [len(s) for s in sequences]
#print("average length: %0.1f" % np.mean(seq_lens))
#print("max length: %d" % max(seq_lens))
MAX_SEQUENCE_LENGTH = 100
# pad sequences with 0s
x_train = pad_sequences(sequences, maxlen=MAX_SEQUENCE_LENGTH) # former des
sequence de meme taille 150, en ajoutant des 0
x_test = pad_sequences(sequences_test, maxlen=MAX_SEQUENCE_LENGTH)
#print('Shape of data train:', x_train.shape) #it gives (1,100)
#print('Shape of data test tensor:', x_test.shape)
y_train = train_y
y_test = test_y
#if np.any(y_train):
#y_train = to_categorical(y_train)
print('Shape of label tensor:', y_train.shape)
EMBEDDING_DIM = 50
N_CLASSES = 2
# input: a sequence of MAX_SEQUENCE_LENGTH integers
sequence_input = Input(shape=(MAX_SEQUENCE_LENGTH,), dtype='float32')
embedding_layer = Embedding(MAX_NB_WORDS, EMBEDDING_DIM,
input_length=MAX_SEQUENCE_LENGTH,
trainable=True)
embedded_sequences = embedding_layer(sequence_input)
average = GlobalAveragePooling1D()(embedded_sequences)
predictions = Dense(N_CLASSES, activation='softmax')(average)
model = Model(sequence_input, predictions)
model.compile(loss='categorical_crossentropy',
optimizer='adam', metrics=['acc'])
model.fit(x_train, y_train, validation_split=0.1,
nb_epoch=10, batch_size=100)
output_test = model.predict(x_test)
print("test auc:", roc_auc_score(y_test,output_test[:,1]))
I got the error: ValueError Error when checking target expected dense_1 to have shape(2,), but got array with shape(1,)
Any suggestion, cause I don't know how to proceed .
Thank you
python deep-learning keras tensorflow nlp
python deep-learning keras tensorflow nlp
asked yesterday
KikioKikio
214
214
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
$begingroup$
The problem seems to arise in this line.
predictions = Dense(N_CLASSES, activation='softmax')(average)
Here, N_CLASSES
has the value of 2. Meaning you have 2 classes. But the y_train
vector has shape ( 1, )
.
- You need to convert the
y_train
vector to a one hot vector. Here's any example.
If y_train = [ [ 0 ] , [ 1 ] ]
then its one hot vector would be [ [ 1 , 0 ] , [ 0 , 1 ] ]
. The one hot vector has shape ( 2 , )
which is needed in the above code snippet.
So make this edit.
y_train = keras.utils.to_categorical( y_train , N_CLASSES )
New contributor
$endgroup$
$begingroup$
Thanks for your answer. I tried this an I had this error: ValueError: Input arrays should have the same number of samples as target arrays. Found 1 input samples and 0 target samples
$endgroup$
– Kikio
yesterday
$begingroup$
Make sure that you have the number of labels equal to the number of samples.
$endgroup$
– Shubham Panchal
yesterday
$begingroup$
I have two classes which means there are two labels, or something else ? Sorry but I'm a beginner
$endgroup$
– Kikio
yesterday
$begingroup$
No, you will count only the number of labels and not the number of one hot numbers
$endgroup$
– Shubham Panchal
22 hours ago
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
});
});
}, "mathjax-editing");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "557"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdatascience.stackexchange.com%2fquestions%2f45874%2fusing-text-classification-for-system-calls%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
The problem seems to arise in this line.
predictions = Dense(N_CLASSES, activation='softmax')(average)
Here, N_CLASSES
has the value of 2. Meaning you have 2 classes. But the y_train
vector has shape ( 1, )
.
- You need to convert the
y_train
vector to a one hot vector. Here's any example.
If y_train = [ [ 0 ] , [ 1 ] ]
then its one hot vector would be [ [ 1 , 0 ] , [ 0 , 1 ] ]
. The one hot vector has shape ( 2 , )
which is needed in the above code snippet.
So make this edit.
y_train = keras.utils.to_categorical( y_train , N_CLASSES )
New contributor
$endgroup$
$begingroup$
Thanks for your answer. I tried this an I had this error: ValueError: Input arrays should have the same number of samples as target arrays. Found 1 input samples and 0 target samples
$endgroup$
– Kikio
yesterday
$begingroup$
Make sure that you have the number of labels equal to the number of samples.
$endgroup$
– Shubham Panchal
yesterday
$begingroup$
I have two classes which means there are two labels, or something else ? Sorry but I'm a beginner
$endgroup$
– Kikio
yesterday
$begingroup$
No, you will count only the number of labels and not the number of one hot numbers
$endgroup$
– Shubham Panchal
22 hours ago
add a comment |
$begingroup$
The problem seems to arise in this line.
predictions = Dense(N_CLASSES, activation='softmax')(average)
Here, N_CLASSES
has the value of 2. Meaning you have 2 classes. But the y_train
vector has shape ( 1, )
.
- You need to convert the
y_train
vector to a one hot vector. Here's any example.
If y_train = [ [ 0 ] , [ 1 ] ]
then its one hot vector would be [ [ 1 , 0 ] , [ 0 , 1 ] ]
. The one hot vector has shape ( 2 , )
which is needed in the above code snippet.
So make this edit.
y_train = keras.utils.to_categorical( y_train , N_CLASSES )
New contributor
$endgroup$
$begingroup$
Thanks for your answer. I tried this an I had this error: ValueError: Input arrays should have the same number of samples as target arrays. Found 1 input samples and 0 target samples
$endgroup$
– Kikio
yesterday
$begingroup$
Make sure that you have the number of labels equal to the number of samples.
$endgroup$
– Shubham Panchal
yesterday
$begingroup$
I have two classes which means there are two labels, or something else ? Sorry but I'm a beginner
$endgroup$
– Kikio
yesterday
$begingroup$
No, you will count only the number of labels and not the number of one hot numbers
$endgroup$
– Shubham Panchal
22 hours ago
add a comment |
$begingroup$
The problem seems to arise in this line.
predictions = Dense(N_CLASSES, activation='softmax')(average)
Here, N_CLASSES
has the value of 2. Meaning you have 2 classes. But the y_train
vector has shape ( 1, )
.
- You need to convert the
y_train
vector to a one hot vector. Here's any example.
If y_train = [ [ 0 ] , [ 1 ] ]
then its one hot vector would be [ [ 1 , 0 ] , [ 0 , 1 ] ]
. The one hot vector has shape ( 2 , )
which is needed in the above code snippet.
So make this edit.
y_train = keras.utils.to_categorical( y_train , N_CLASSES )
New contributor
$endgroup$
The problem seems to arise in this line.
predictions = Dense(N_CLASSES, activation='softmax')(average)
Here, N_CLASSES
has the value of 2. Meaning you have 2 classes. But the y_train
vector has shape ( 1, )
.
- You need to convert the
y_train
vector to a one hot vector. Here's any example.
If y_train = [ [ 0 ] , [ 1 ] ]
then its one hot vector would be [ [ 1 , 0 ] , [ 0 , 1 ] ]
. The one hot vector has shape ( 2 , )
which is needed in the above code snippet.
So make this edit.
y_train = keras.utils.to_categorical( y_train , N_CLASSES )
New contributor
New contributor
answered yesterday
Shubham PanchalShubham Panchal
1512
1512
New contributor
New contributor
$begingroup$
Thanks for your answer. I tried this an I had this error: ValueError: Input arrays should have the same number of samples as target arrays. Found 1 input samples and 0 target samples
$endgroup$
– Kikio
yesterday
$begingroup$
Make sure that you have the number of labels equal to the number of samples.
$endgroup$
– Shubham Panchal
yesterday
$begingroup$
I have two classes which means there are two labels, or something else ? Sorry but I'm a beginner
$endgroup$
– Kikio
yesterday
$begingroup$
No, you will count only the number of labels and not the number of one hot numbers
$endgroup$
– Shubham Panchal
22 hours ago
add a comment |
$begingroup$
Thanks for your answer. I tried this an I had this error: ValueError: Input arrays should have the same number of samples as target arrays. Found 1 input samples and 0 target samples
$endgroup$
– Kikio
yesterday
$begingroup$
Make sure that you have the number of labels equal to the number of samples.
$endgroup$
– Shubham Panchal
yesterday
$begingroup$
I have two classes which means there are two labels, or something else ? Sorry but I'm a beginner
$endgroup$
– Kikio
yesterday
$begingroup$
No, you will count only the number of labels and not the number of one hot numbers
$endgroup$
– Shubham Panchal
22 hours ago
$begingroup$
Thanks for your answer. I tried this an I had this error: ValueError: Input arrays should have the same number of samples as target arrays. Found 1 input samples and 0 target samples
$endgroup$
– Kikio
yesterday
$begingroup$
Thanks for your answer. I tried this an I had this error: ValueError: Input arrays should have the same number of samples as target arrays. Found 1 input samples and 0 target samples
$endgroup$
– Kikio
yesterday
$begingroup$
Make sure that you have the number of labels equal to the number of samples.
$endgroup$
– Shubham Panchal
yesterday
$begingroup$
Make sure that you have the number of labels equal to the number of samples.
$endgroup$
– Shubham Panchal
yesterday
$begingroup$
I have two classes which means there are two labels, or something else ? Sorry but I'm a beginner
$endgroup$
– Kikio
yesterday
$begingroup$
I have two classes which means there are two labels, or something else ? Sorry but I'm a beginner
$endgroup$
– Kikio
yesterday
$begingroup$
No, you will count only the number of labels and not the number of one hot numbers
$endgroup$
– Shubham Panchal
22 hours ago
$begingroup$
No, you will count only the number of labels and not the number of one hot numbers
$endgroup$
– Shubham Panchal
22 hours ago
add a comment |
Thanks for contributing an answer to Data Science Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdatascience.stackexchange.com%2fquestions%2f45874%2fusing-text-classification-for-system-calls%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown