How to give the encoded output of an encoder to the inputs of a classifier?












2














I am trying to use an AutoEncoder to enhance the performance of a classifier.



This is the AutoEncoder I wrote using the keras documentation for MNIST data:



from keras.layers import Input, Dense
from keras.models import Model

# this is the size of our encoded representations
# 32 floats -> compression of factor 24.5, assuming the input is 784 floats
encoding_dim = 32

# this is our input placeholder
input_img = Input(shape=(784,))
# "encoded" is the encoded representation of the input
encoded = Dense(encoding_dim, activation='relu')(input_img)
# "decoded" is the lossy reconstruction of the input
decoded = Dense(784, activation='sigmoid')(encoded)

# this model maps an input to its reconstruction
autoencoder = Model(input_img, decoded)
# this model maps an input to its encoded representation
encoder = Model(input_img, encoded)
# create a placeholder for an encoded (32-dimensional) input
encoded_input = Input(shape=(encoding_dim,))
# retrieve the last layer of the autoencoder model
decoder_layer = autoencoder.layers[-1]
# create the decoder model
decoder = Model(encoded_input, decoder_layer(encoded_input))
autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy')

autoencoder.fit(x_train, x_train,
epochs=50,
batch_size=256,
shuffle=True,
validation_data=(x_test, x_test))


Now I'm trying to connect it to a classifier. Using this question, I wrote:



x = encoder.output
# h = Dense(3, activation='relu', name='hidden')(x)
y = Dense(1, activation='sigmoid', name='predictions')(x)

classifier = Model(inputs=autoencoder.inputs, outputs=y)


# Compile model
classifier.compile(loss='binary_crossentropy', optimizer='adam',
metrics=['accuracy'])

# Fit the model
history = classifier.fit(x_train, y_train,
epochs=10,
batch_size=10,
validation_split=.1)


First, I don't understand this code. Second, I guess the output of y must be 10 corresponding of 10 digits, but I can't set it to 10 because I get an error.



Anyway, the accuracy of the above classifier is very low (10%)! Is there anything wrong in my approach?










share|improve this question
























  • Your model has one layer, with very few parameters. The question is, why should you get good results in the first place?
    – Oringa
    Nov 28 '18 at 17:21
















2














I am trying to use an AutoEncoder to enhance the performance of a classifier.



This is the AutoEncoder I wrote using the keras documentation for MNIST data:



from keras.layers import Input, Dense
from keras.models import Model

# this is the size of our encoded representations
# 32 floats -> compression of factor 24.5, assuming the input is 784 floats
encoding_dim = 32

# this is our input placeholder
input_img = Input(shape=(784,))
# "encoded" is the encoded representation of the input
encoded = Dense(encoding_dim, activation='relu')(input_img)
# "decoded" is the lossy reconstruction of the input
decoded = Dense(784, activation='sigmoid')(encoded)

# this model maps an input to its reconstruction
autoencoder = Model(input_img, decoded)
# this model maps an input to its encoded representation
encoder = Model(input_img, encoded)
# create a placeholder for an encoded (32-dimensional) input
encoded_input = Input(shape=(encoding_dim,))
# retrieve the last layer of the autoencoder model
decoder_layer = autoencoder.layers[-1]
# create the decoder model
decoder = Model(encoded_input, decoder_layer(encoded_input))
autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy')

autoencoder.fit(x_train, x_train,
epochs=50,
batch_size=256,
shuffle=True,
validation_data=(x_test, x_test))


Now I'm trying to connect it to a classifier. Using this question, I wrote:



x = encoder.output
# h = Dense(3, activation='relu', name='hidden')(x)
y = Dense(1, activation='sigmoid', name='predictions')(x)

classifier = Model(inputs=autoencoder.inputs, outputs=y)


# Compile model
classifier.compile(loss='binary_crossentropy', optimizer='adam',
metrics=['accuracy'])

# Fit the model
history = classifier.fit(x_train, y_train,
epochs=10,
batch_size=10,
validation_split=.1)


First, I don't understand this code. Second, I guess the output of y must be 10 corresponding of 10 digits, but I can't set it to 10 because I get an error.



Anyway, the accuracy of the above classifier is very low (10%)! Is there anything wrong in my approach?










share|improve this question
























  • Your model has one layer, with very few parameters. The question is, why should you get good results in the first place?
    – Oringa
    Nov 28 '18 at 17:21














2












2








2







I am trying to use an AutoEncoder to enhance the performance of a classifier.



This is the AutoEncoder I wrote using the keras documentation for MNIST data:



from keras.layers import Input, Dense
from keras.models import Model

# this is the size of our encoded representations
# 32 floats -> compression of factor 24.5, assuming the input is 784 floats
encoding_dim = 32

# this is our input placeholder
input_img = Input(shape=(784,))
# "encoded" is the encoded representation of the input
encoded = Dense(encoding_dim, activation='relu')(input_img)
# "decoded" is the lossy reconstruction of the input
decoded = Dense(784, activation='sigmoid')(encoded)

# this model maps an input to its reconstruction
autoencoder = Model(input_img, decoded)
# this model maps an input to its encoded representation
encoder = Model(input_img, encoded)
# create a placeholder for an encoded (32-dimensional) input
encoded_input = Input(shape=(encoding_dim,))
# retrieve the last layer of the autoencoder model
decoder_layer = autoencoder.layers[-1]
# create the decoder model
decoder = Model(encoded_input, decoder_layer(encoded_input))
autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy')

autoencoder.fit(x_train, x_train,
epochs=50,
batch_size=256,
shuffle=True,
validation_data=(x_test, x_test))


Now I'm trying to connect it to a classifier. Using this question, I wrote:



x = encoder.output
# h = Dense(3, activation='relu', name='hidden')(x)
y = Dense(1, activation='sigmoid', name='predictions')(x)

classifier = Model(inputs=autoencoder.inputs, outputs=y)


# Compile model
classifier.compile(loss='binary_crossentropy', optimizer='adam',
metrics=['accuracy'])

# Fit the model
history = classifier.fit(x_train, y_train,
epochs=10,
batch_size=10,
validation_split=.1)


First, I don't understand this code. Second, I guess the output of y must be 10 corresponding of 10 digits, but I can't set it to 10 because I get an error.



Anyway, the accuracy of the above classifier is very low (10%)! Is there anything wrong in my approach?










share|improve this question















I am trying to use an AutoEncoder to enhance the performance of a classifier.



This is the AutoEncoder I wrote using the keras documentation for MNIST data:



from keras.layers import Input, Dense
from keras.models import Model

# this is the size of our encoded representations
# 32 floats -> compression of factor 24.5, assuming the input is 784 floats
encoding_dim = 32

# this is our input placeholder
input_img = Input(shape=(784,))
# "encoded" is the encoded representation of the input
encoded = Dense(encoding_dim, activation='relu')(input_img)
# "decoded" is the lossy reconstruction of the input
decoded = Dense(784, activation='sigmoid')(encoded)

# this model maps an input to its reconstruction
autoencoder = Model(input_img, decoded)
# this model maps an input to its encoded representation
encoder = Model(input_img, encoded)
# create a placeholder for an encoded (32-dimensional) input
encoded_input = Input(shape=(encoding_dim,))
# retrieve the last layer of the autoencoder model
decoder_layer = autoencoder.layers[-1]
# create the decoder model
decoder = Model(encoded_input, decoder_layer(encoded_input))
autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy')

autoencoder.fit(x_train, x_train,
epochs=50,
batch_size=256,
shuffle=True,
validation_data=(x_test, x_test))


Now I'm trying to connect it to a classifier. Using this question, I wrote:



x = encoder.output
# h = Dense(3, activation='relu', name='hidden')(x)
y = Dense(1, activation='sigmoid', name='predictions')(x)

classifier = Model(inputs=autoencoder.inputs, outputs=y)


# Compile model
classifier.compile(loss='binary_crossentropy', optimizer='adam',
metrics=['accuracy'])

# Fit the model
history = classifier.fit(x_train, y_train,
epochs=10,
batch_size=10,
validation_split=.1)


First, I don't understand this code. Second, I guess the output of y must be 10 corresponding of 10 digits, but I can't set it to 10 because I get an error.



Anyway, the accuracy of the above classifier is very low (10%)! Is there anything wrong in my approach?







python keras neural-network classification autoencoder






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 '18 at 17:53

























asked Nov 19 '18 at 17:44









Ahmad

2,77333058




2,77333058












  • Your model has one layer, with very few parameters. The question is, why should you get good results in the first place?
    – Oringa
    Nov 28 '18 at 17:21


















  • Your model has one layer, with very few parameters. The question is, why should you get good results in the first place?
    – Oringa
    Nov 28 '18 at 17:21
















Your model has one layer, with very few parameters. The question is, why should you get good results in the first place?
– Oringa
Nov 28 '18 at 17:21




Your model has one layer, with very few parameters. The question is, why should you get good results in the first place?
– Oringa
Nov 28 '18 at 17:21












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
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: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53380052%2fhow-to-give-the-encoded-output-of-an-encoder-to-the-inputs-of-a-classifier%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • 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.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • 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.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53380052%2fhow-to-give-the-encoded-output-of-an-encoder-to-the-inputs-of-a-classifier%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

Npm cannot find a required file even through it is in the searched directory