How to pass 2 features to LSTM , one of them is one-hot-encoded with Keras?
$begingroup$
I have a very simple LSTM model
model = Sequential()
model.add(LSTM(64, input_shape=(seq_length, X_train.shape[2]) , return_sequences=True))
model.add(Dense(y_cat_train.shape[2], activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(X_train, y_cat_train, epochs=100, batch_size=10, verbose=2)
The input X_train
has 2 feature , one is categorical (values 1-4) and the other is numeric (values 1-100). There are 4 classes in y_test
that I one-hot-encoded with keras's to_categorical
.
- Should I encode the categorical input feature as well ? If I do , how can I pass it along with the other feature ? (e.g. now a timestep looks like this for example:
[1,44]
) - Later , I would like to take make a sampling , meaning I need to take the predicted
y_hat<t-1>
and pass it asx<t>
. I will have to pass the second numeric feature (1-100) along with it. How can it be done ?
EDIT : note that I do not want my numeric feature to become categorical since there is importance to the values (meaning 2<10<90 etc)
python keras lstm rnn
$endgroup$
bumped to the homepage by Community♦ 16 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |
$begingroup$
I have a very simple LSTM model
model = Sequential()
model.add(LSTM(64, input_shape=(seq_length, X_train.shape[2]) , return_sequences=True))
model.add(Dense(y_cat_train.shape[2], activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(X_train, y_cat_train, epochs=100, batch_size=10, verbose=2)
The input X_train
has 2 feature , one is categorical (values 1-4) and the other is numeric (values 1-100). There are 4 classes in y_test
that I one-hot-encoded with keras's to_categorical
.
- Should I encode the categorical input feature as well ? If I do , how can I pass it along with the other feature ? (e.g. now a timestep looks like this for example:
[1,44]
) - Later , I would like to take make a sampling , meaning I need to take the predicted
y_hat<t-1>
and pass it asx<t>
. I will have to pass the second numeric feature (1-100) along with it. How can it be done ?
EDIT : note that I do not want my numeric feature to become categorical since there is importance to the values (meaning 2<10<90 etc)
python keras lstm rnn
$endgroup$
bumped to the homepage by Community♦ 16 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |
$begingroup$
I have a very simple LSTM model
model = Sequential()
model.add(LSTM(64, input_shape=(seq_length, X_train.shape[2]) , return_sequences=True))
model.add(Dense(y_cat_train.shape[2], activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(X_train, y_cat_train, epochs=100, batch_size=10, verbose=2)
The input X_train
has 2 feature , one is categorical (values 1-4) and the other is numeric (values 1-100). There are 4 classes in y_test
that I one-hot-encoded with keras's to_categorical
.
- Should I encode the categorical input feature as well ? If I do , how can I pass it along with the other feature ? (e.g. now a timestep looks like this for example:
[1,44]
) - Later , I would like to take make a sampling , meaning I need to take the predicted
y_hat<t-1>
and pass it asx<t>
. I will have to pass the second numeric feature (1-100) along with it. How can it be done ?
EDIT : note that I do not want my numeric feature to become categorical since there is importance to the values (meaning 2<10<90 etc)
python keras lstm rnn
$endgroup$
I have a very simple LSTM model
model = Sequential()
model.add(LSTM(64, input_shape=(seq_length, X_train.shape[2]) , return_sequences=True))
model.add(Dense(y_cat_train.shape[2], activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(X_train, y_cat_train, epochs=100, batch_size=10, verbose=2)
The input X_train
has 2 feature , one is categorical (values 1-4) and the other is numeric (values 1-100). There are 4 classes in y_test
that I one-hot-encoded with keras's to_categorical
.
- Should I encode the categorical input feature as well ? If I do , how can I pass it along with the other feature ? (e.g. now a timestep looks like this for example:
[1,44]
) - Later , I would like to take make a sampling , meaning I need to take the predicted
y_hat<t-1>
and pass it asx<t>
. I will have to pass the second numeric feature (1-100) along with it. How can it be done ?
EDIT : note that I do not want my numeric feature to become categorical since there is importance to the values (meaning 2<10<90 etc)
python keras lstm rnn
python keras lstm rnn
edited Jan 10 at 22:53
M.F
asked Jan 10 at 21:18
M.FM.F
167
167
bumped to the homepage by Community♦ 16 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
bumped to the homepage by Community♦ 16 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
$begingroup$
(1) Yes, it is a common practice to encode the categorical feature by one-hot encoding, for example, encode [1,44]
as [1,0,0,0,44]
, encode [2,44]
as [0,1,0,0,44]
, etc.
(2) Same as (1), just concatenate the one-hot encoded categorical feature and the numerical feature(s).
$endgroup$
$begingroup$
So it will be treated as one feature ? The categorical part is actuallyy<t-1>
soX<t> = [y<t-1>,feature2]
- won't concatenating lose some of the importance of one of the features ?
$endgroup$
– M.F
Jan 11 at 15:40
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%2f43801%2fhow-to-pass-2-features-to-lstm-one-of-them-is-one-hot-encoded-with-keras%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$
(1) Yes, it is a common practice to encode the categorical feature by one-hot encoding, for example, encode [1,44]
as [1,0,0,0,44]
, encode [2,44]
as [0,1,0,0,44]
, etc.
(2) Same as (1), just concatenate the one-hot encoded categorical feature and the numerical feature(s).
$endgroup$
$begingroup$
So it will be treated as one feature ? The categorical part is actuallyy<t-1>
soX<t> = [y<t-1>,feature2]
- won't concatenating lose some of the importance of one of the features ?
$endgroup$
– M.F
Jan 11 at 15:40
add a comment |
$begingroup$
(1) Yes, it is a common practice to encode the categorical feature by one-hot encoding, for example, encode [1,44]
as [1,0,0,0,44]
, encode [2,44]
as [0,1,0,0,44]
, etc.
(2) Same as (1), just concatenate the one-hot encoded categorical feature and the numerical feature(s).
$endgroup$
$begingroup$
So it will be treated as one feature ? The categorical part is actuallyy<t-1>
soX<t> = [y<t-1>,feature2]
- won't concatenating lose some of the importance of one of the features ?
$endgroup$
– M.F
Jan 11 at 15:40
add a comment |
$begingroup$
(1) Yes, it is a common practice to encode the categorical feature by one-hot encoding, for example, encode [1,44]
as [1,0,0,0,44]
, encode [2,44]
as [0,1,0,0,44]
, etc.
(2) Same as (1), just concatenate the one-hot encoded categorical feature and the numerical feature(s).
$endgroup$
(1) Yes, it is a common practice to encode the categorical feature by one-hot encoding, for example, encode [1,44]
as [1,0,0,0,44]
, encode [2,44]
as [0,1,0,0,44]
, etc.
(2) Same as (1), just concatenate the one-hot encoded categorical feature and the numerical feature(s).
answered Jan 11 at 2:35
user12075user12075
1,276515
1,276515
$begingroup$
So it will be treated as one feature ? The categorical part is actuallyy<t-1>
soX<t> = [y<t-1>,feature2]
- won't concatenating lose some of the importance of one of the features ?
$endgroup$
– M.F
Jan 11 at 15:40
add a comment |
$begingroup$
So it will be treated as one feature ? The categorical part is actuallyy<t-1>
soX<t> = [y<t-1>,feature2]
- won't concatenating lose some of the importance of one of the features ?
$endgroup$
– M.F
Jan 11 at 15:40
$begingroup$
So it will be treated as one feature ? The categorical part is actually
y<t-1>
so X<t> = [y<t-1>,feature2]
- won't concatenating lose some of the importance of one of the features ?$endgroup$
– M.F
Jan 11 at 15:40
$begingroup$
So it will be treated as one feature ? The categorical part is actually
y<t-1>
so X<t> = [y<t-1>,feature2]
- won't concatenating lose some of the importance of one of the features ?$endgroup$
– M.F
Jan 11 at 15:40
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%2f43801%2fhow-to-pass-2-features-to-lstm-one-of-them-is-one-hot-encoded-with-keras%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