how to get data from within Keras model for visualisation?












1















I am using Tensorflow 1.12 which has Keras integrated together with Python 3.6.x



I wish to use Keras for its simplicity of model building, but also would like to use data on the intermediate layer for visualization of feature maps and kernels to better understand how machine learning works(even though this is admittedly not so evident)



I am using the mnist data base and a very basic Keras model to try to do what I want to do.



Here is the code



import tensorflow as tf
from tensorflow.keras import layers
from tensorflow import keras

print(tf.VERSION)
print(tf.keras.__version__)

tf.keras.backend.clear_session()

mnist = tf.keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train_shaped = np.expand_dims(x_train, axis=3) / 255.0
x_test_shaped = np.expand_dims(x_test, axis=3) / 255.0

def create_model():

model = tf.keras.models.Sequential([
keras.layers.Conv2D(32, kernel_size=(4, 4),strides=(1,1),activation='relu', input_shape=(28,28,1)),
keras.layers.Dropout(0.5),
keras.layers.MaxPooling2D(pool_size=(2,2), strides=(2,2)),
keras.layers.Conv2D(24, kernel_size=(8, 8),strides=(1,1)),
keras.layers.Flatten(),
keras.layers.Dropout(0.5),
keras.layers.Dense(128, activation=tf.nn.relu),
keras.layers.Dense(10, activation=tf.nn.softmax)
])

model.compile(optimizer=tf.keras.optimizers.Adam(),
loss=tf.keras.losses.sparse_categorical_crossentropy,
metrics=['accuracy'])

return model


The above sets up the dataset and the model
Next I define my session for Tensorflow and do the training.



This all works fine but now I want to get my data for the, as example, the first layer out as ideally a numpy array on which I can do the visualization.



My model.layers[0].output gives me a Tensor of (?,25,25,32) as expected and now I try to do a eval() and thenafter a .numpy() method to get my result.



The error message is



You must feed a value for placeholder tensor 'conv2d_6_input' with dtype float and shape [?,28,28,1]


I am looking for help on how to get my data (32 feature maps of 25x25 pixels) out as numpy array for visualization.



sess = tf.Session(graph=tf.get_default_graph())
tf.keras.backend.set_session(sess)

with sess.as_default():
model = create_model()
model.summary()

model.fit(x_train_shaped[:10000], y_train[:10000], epochs=2,
batch_size=64, validation_split=.2,)

model.layers[0].output
print(model.layers[0].output.shape)
my_array = model.layers[0].output
my_array.eval()

tf.keras.backend.clear_session()
sess.close()









share|improve this question

























  • Possible duplicate of Getting vector obtained in the last layer of CNN before softmax layer

    – today
    Nov 22 '18 at 10:39











  • Thanks already for the prompt answer. Now, the idea of building a new Keras model is counterproductive. When you have a large model in the first place, one would like to plug in some kind of ready-made code that can get the output of the feature maps and visualise them. So this route seems not really interesting.

    – Peter HIRT
    Nov 22 '18 at 18:57











  • Note also that in the code above I get with the variable my_array a tensor of correct size from the code, so I am not really looking for getting the output in yet another form. My Problem is really to get this tensor out nto the numpy world. I tried with the .eval() function as well as the .numpy() function.

    – Peter HIRT
    Nov 22 '18 at 18:59











  • BTW, with pure Tensorflow, I have little problems doing what I want to do, but TF is teh basics and Keras is adding things at compile to the picture I don't get

    – Peter HIRT
    Nov 22 '18 at 19:00
















1















I am using Tensorflow 1.12 which has Keras integrated together with Python 3.6.x



I wish to use Keras for its simplicity of model building, but also would like to use data on the intermediate layer for visualization of feature maps and kernels to better understand how machine learning works(even though this is admittedly not so evident)



I am using the mnist data base and a very basic Keras model to try to do what I want to do.



Here is the code



import tensorflow as tf
from tensorflow.keras import layers
from tensorflow import keras

print(tf.VERSION)
print(tf.keras.__version__)

tf.keras.backend.clear_session()

mnist = tf.keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train_shaped = np.expand_dims(x_train, axis=3) / 255.0
x_test_shaped = np.expand_dims(x_test, axis=3) / 255.0

def create_model():

model = tf.keras.models.Sequential([
keras.layers.Conv2D(32, kernel_size=(4, 4),strides=(1,1),activation='relu', input_shape=(28,28,1)),
keras.layers.Dropout(0.5),
keras.layers.MaxPooling2D(pool_size=(2,2), strides=(2,2)),
keras.layers.Conv2D(24, kernel_size=(8, 8),strides=(1,1)),
keras.layers.Flatten(),
keras.layers.Dropout(0.5),
keras.layers.Dense(128, activation=tf.nn.relu),
keras.layers.Dense(10, activation=tf.nn.softmax)
])

model.compile(optimizer=tf.keras.optimizers.Adam(),
loss=tf.keras.losses.sparse_categorical_crossentropy,
metrics=['accuracy'])

return model


The above sets up the dataset and the model
Next I define my session for Tensorflow and do the training.



This all works fine but now I want to get my data for the, as example, the first layer out as ideally a numpy array on which I can do the visualization.



My model.layers[0].output gives me a Tensor of (?,25,25,32) as expected and now I try to do a eval() and thenafter a .numpy() method to get my result.



The error message is



You must feed a value for placeholder tensor 'conv2d_6_input' with dtype float and shape [?,28,28,1]


I am looking for help on how to get my data (32 feature maps of 25x25 pixels) out as numpy array for visualization.



sess = tf.Session(graph=tf.get_default_graph())
tf.keras.backend.set_session(sess)

with sess.as_default():
model = create_model()
model.summary()

model.fit(x_train_shaped[:10000], y_train[:10000], epochs=2,
batch_size=64, validation_split=.2,)

model.layers[0].output
print(model.layers[0].output.shape)
my_array = model.layers[0].output
my_array.eval()

tf.keras.backend.clear_session()
sess.close()









share|improve this question

























  • Possible duplicate of Getting vector obtained in the last layer of CNN before softmax layer

    – today
    Nov 22 '18 at 10:39











  • Thanks already for the prompt answer. Now, the idea of building a new Keras model is counterproductive. When you have a large model in the first place, one would like to plug in some kind of ready-made code that can get the output of the feature maps and visualise them. So this route seems not really interesting.

    – Peter HIRT
    Nov 22 '18 at 18:57











  • Note also that in the code above I get with the variable my_array a tensor of correct size from the code, so I am not really looking for getting the output in yet another form. My Problem is really to get this tensor out nto the numpy world. I tried with the .eval() function as well as the .numpy() function.

    – Peter HIRT
    Nov 22 '18 at 18:59











  • BTW, with pure Tensorflow, I have little problems doing what I want to do, but TF is teh basics and Keras is adding things at compile to the picture I don't get

    – Peter HIRT
    Nov 22 '18 at 19:00














1












1








1








I am using Tensorflow 1.12 which has Keras integrated together with Python 3.6.x



I wish to use Keras for its simplicity of model building, but also would like to use data on the intermediate layer for visualization of feature maps and kernels to better understand how machine learning works(even though this is admittedly not so evident)



I am using the mnist data base and a very basic Keras model to try to do what I want to do.



Here is the code



import tensorflow as tf
from tensorflow.keras import layers
from tensorflow import keras

print(tf.VERSION)
print(tf.keras.__version__)

tf.keras.backend.clear_session()

mnist = tf.keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train_shaped = np.expand_dims(x_train, axis=3) / 255.0
x_test_shaped = np.expand_dims(x_test, axis=3) / 255.0

def create_model():

model = tf.keras.models.Sequential([
keras.layers.Conv2D(32, kernel_size=(4, 4),strides=(1,1),activation='relu', input_shape=(28,28,1)),
keras.layers.Dropout(0.5),
keras.layers.MaxPooling2D(pool_size=(2,2), strides=(2,2)),
keras.layers.Conv2D(24, kernel_size=(8, 8),strides=(1,1)),
keras.layers.Flatten(),
keras.layers.Dropout(0.5),
keras.layers.Dense(128, activation=tf.nn.relu),
keras.layers.Dense(10, activation=tf.nn.softmax)
])

model.compile(optimizer=tf.keras.optimizers.Adam(),
loss=tf.keras.losses.sparse_categorical_crossentropy,
metrics=['accuracy'])

return model


The above sets up the dataset and the model
Next I define my session for Tensorflow and do the training.



This all works fine but now I want to get my data for the, as example, the first layer out as ideally a numpy array on which I can do the visualization.



My model.layers[0].output gives me a Tensor of (?,25,25,32) as expected and now I try to do a eval() and thenafter a .numpy() method to get my result.



The error message is



You must feed a value for placeholder tensor 'conv2d_6_input' with dtype float and shape [?,28,28,1]


I am looking for help on how to get my data (32 feature maps of 25x25 pixels) out as numpy array for visualization.



sess = tf.Session(graph=tf.get_default_graph())
tf.keras.backend.set_session(sess)

with sess.as_default():
model = create_model()
model.summary()

model.fit(x_train_shaped[:10000], y_train[:10000], epochs=2,
batch_size=64, validation_split=.2,)

model.layers[0].output
print(model.layers[0].output.shape)
my_array = model.layers[0].output
my_array.eval()

tf.keras.backend.clear_session()
sess.close()









share|improve this question
















I am using Tensorflow 1.12 which has Keras integrated together with Python 3.6.x



I wish to use Keras for its simplicity of model building, but also would like to use data on the intermediate layer for visualization of feature maps and kernels to better understand how machine learning works(even though this is admittedly not so evident)



I am using the mnist data base and a very basic Keras model to try to do what I want to do.



Here is the code



import tensorflow as tf
from tensorflow.keras import layers
from tensorflow import keras

print(tf.VERSION)
print(tf.keras.__version__)

tf.keras.backend.clear_session()

mnist = tf.keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train_shaped = np.expand_dims(x_train, axis=3) / 255.0
x_test_shaped = np.expand_dims(x_test, axis=3) / 255.0

def create_model():

model = tf.keras.models.Sequential([
keras.layers.Conv2D(32, kernel_size=(4, 4),strides=(1,1),activation='relu', input_shape=(28,28,1)),
keras.layers.Dropout(0.5),
keras.layers.MaxPooling2D(pool_size=(2,2), strides=(2,2)),
keras.layers.Conv2D(24, kernel_size=(8, 8),strides=(1,1)),
keras.layers.Flatten(),
keras.layers.Dropout(0.5),
keras.layers.Dense(128, activation=tf.nn.relu),
keras.layers.Dense(10, activation=tf.nn.softmax)
])

model.compile(optimizer=tf.keras.optimizers.Adam(),
loss=tf.keras.losses.sparse_categorical_crossentropy,
metrics=['accuracy'])

return model


The above sets up the dataset and the model
Next I define my session for Tensorflow and do the training.



This all works fine but now I want to get my data for the, as example, the first layer out as ideally a numpy array on which I can do the visualization.



My model.layers[0].output gives me a Tensor of (?,25,25,32) as expected and now I try to do a eval() and thenafter a .numpy() method to get my result.



The error message is



You must feed a value for placeholder tensor 'conv2d_6_input' with dtype float and shape [?,28,28,1]


I am looking for help on how to get my data (32 feature maps of 25x25 pixels) out as numpy array for visualization.



sess = tf.Session(graph=tf.get_default_graph())
tf.keras.backend.set_session(sess)

with sess.as_default():
model = create_model()
model.summary()

model.fit(x_train_shaped[:10000], y_train[:10000], epochs=2,
batch_size=64, validation_split=.2,)

model.layers[0].output
print(model.layers[0].output.shape)
my_array = model.layers[0].output
my_array.eval()

tf.keras.backend.clear_session()
sess.close()






python tensorflow keras conv-neural-network






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 6:59









Milo Lu

1,61911527




1,61911527










asked Nov 21 '18 at 19:18









Peter HIRTPeter HIRT

2661614




2661614













  • Possible duplicate of Getting vector obtained in the last layer of CNN before softmax layer

    – today
    Nov 22 '18 at 10:39











  • Thanks already for the prompt answer. Now, the idea of building a new Keras model is counterproductive. When you have a large model in the first place, one would like to plug in some kind of ready-made code that can get the output of the feature maps and visualise them. So this route seems not really interesting.

    – Peter HIRT
    Nov 22 '18 at 18:57











  • Note also that in the code above I get with the variable my_array a tensor of correct size from the code, so I am not really looking for getting the output in yet another form. My Problem is really to get this tensor out nto the numpy world. I tried with the .eval() function as well as the .numpy() function.

    – Peter HIRT
    Nov 22 '18 at 18:59











  • BTW, with pure Tensorflow, I have little problems doing what I want to do, but TF is teh basics and Keras is adding things at compile to the picture I don't get

    – Peter HIRT
    Nov 22 '18 at 19:00



















  • Possible duplicate of Getting vector obtained in the last layer of CNN before softmax layer

    – today
    Nov 22 '18 at 10:39











  • Thanks already for the prompt answer. Now, the idea of building a new Keras model is counterproductive. When you have a large model in the first place, one would like to plug in some kind of ready-made code that can get the output of the feature maps and visualise them. So this route seems not really interesting.

    – Peter HIRT
    Nov 22 '18 at 18:57











  • Note also that in the code above I get with the variable my_array a tensor of correct size from the code, so I am not really looking for getting the output in yet another form. My Problem is really to get this tensor out nto the numpy world. I tried with the .eval() function as well as the .numpy() function.

    – Peter HIRT
    Nov 22 '18 at 18:59











  • BTW, with pure Tensorflow, I have little problems doing what I want to do, but TF is teh basics and Keras is adding things at compile to the picture I don't get

    – Peter HIRT
    Nov 22 '18 at 19:00

















Possible duplicate of Getting vector obtained in the last layer of CNN before softmax layer

– today
Nov 22 '18 at 10:39





Possible duplicate of Getting vector obtained in the last layer of CNN before softmax layer

– today
Nov 22 '18 at 10:39













Thanks already for the prompt answer. Now, the idea of building a new Keras model is counterproductive. When you have a large model in the first place, one would like to plug in some kind of ready-made code that can get the output of the feature maps and visualise them. So this route seems not really interesting.

– Peter HIRT
Nov 22 '18 at 18:57





Thanks already for the prompt answer. Now, the idea of building a new Keras model is counterproductive. When you have a large model in the first place, one would like to plug in some kind of ready-made code that can get the output of the feature maps and visualise them. So this route seems not really interesting.

– Peter HIRT
Nov 22 '18 at 18:57













Note also that in the code above I get with the variable my_array a tensor of correct size from the code, so I am not really looking for getting the output in yet another form. My Problem is really to get this tensor out nto the numpy world. I tried with the .eval() function as well as the .numpy() function.

– Peter HIRT
Nov 22 '18 at 18:59





Note also that in the code above I get with the variable my_array a tensor of correct size from the code, so I am not really looking for getting the output in yet another form. My Problem is really to get this tensor out nto the numpy world. I tried with the .eval() function as well as the .numpy() function.

– Peter HIRT
Nov 22 '18 at 18:59













BTW, with pure Tensorflow, I have little problems doing what I want to do, but TF is teh basics and Keras is adding things at compile to the picture I don't get

– Peter HIRT
Nov 22 '18 at 19:00





BTW, with pure Tensorflow, I have little problems doing what I want to do, but TF is teh basics and Keras is adding things at compile to the picture I don't get

– Peter HIRT
Nov 22 '18 at 19:00












1 Answer
1






active

oldest

votes


















1














First of all, you must note that getting the output of a model or a layer only makes sense when you feed the input layers with some data. You get the model something (i.e. input data), you get something in return (i.e. output or feature map or activation map). That's why it would produce the following error:




You must feed a value for placeholder tensor 'conv2d_6_input'




You haven't fed the baby, so it would cry :)




Now, the idea of building a new Keras model is counterproductive. When you have a large model in the first place, one would like to plug in some kind of ready-made code that can get the output of the feature maps and visualize them. So this route seems not really interesting.




I think you are mistakenly thinking that when you construct a new model out of the layers of another model, a whole new model is cloned. That's not the case since the parameters of the layers would be shared.



Concretely, what you are looking for can be achieved like this:



viz_conv = Model(model.input, model.layers[0].output)
conv_active = viz_conv(my_input_data) # my_input_data is a numpy array of shape `(num_samples,28,28,1)`


All the parameters of viz_conv are shared with model and they have not been copied either. Under the hood they are using the same weight Tensors.



Alternatively, you could define a backend function to do this:



from tensorflow.keras import backend as K

viz_func = K.function([model.input], [any layer(s) you would like in the model])
output = viz_func([my_input_data])


This has been covered in Keras documentation and I highly recommend to read that as well.






share|improve this answer
























  • Hello, your help was leading me to get it running as desired, so, many thanks. I don't know the stackoverflow system with thubs up or similar, but let me know what I can do. You definitely are in for a thumbs up!!!

    – Peter HIRT
    Nov 23 '18 at 14:13






  • 1





    @PeterHIRT I am glad to hear that! You can consult the Stack Overflow guidelines - see What should I do when someone answers my question?

    – today
    Nov 23 '18 at 14:37











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%2f53419127%2fhow-to-get-data-from-within-keras-model-for-visualisation%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









1














First of all, you must note that getting the output of a model or a layer only makes sense when you feed the input layers with some data. You get the model something (i.e. input data), you get something in return (i.e. output or feature map or activation map). That's why it would produce the following error:




You must feed a value for placeholder tensor 'conv2d_6_input'




You haven't fed the baby, so it would cry :)




Now, the idea of building a new Keras model is counterproductive. When you have a large model in the first place, one would like to plug in some kind of ready-made code that can get the output of the feature maps and visualize them. So this route seems not really interesting.




I think you are mistakenly thinking that when you construct a new model out of the layers of another model, a whole new model is cloned. That's not the case since the parameters of the layers would be shared.



Concretely, what you are looking for can be achieved like this:



viz_conv = Model(model.input, model.layers[0].output)
conv_active = viz_conv(my_input_data) # my_input_data is a numpy array of shape `(num_samples,28,28,1)`


All the parameters of viz_conv are shared with model and they have not been copied either. Under the hood they are using the same weight Tensors.



Alternatively, you could define a backend function to do this:



from tensorflow.keras import backend as K

viz_func = K.function([model.input], [any layer(s) you would like in the model])
output = viz_func([my_input_data])


This has been covered in Keras documentation and I highly recommend to read that as well.






share|improve this answer
























  • Hello, your help was leading me to get it running as desired, so, many thanks. I don't know the stackoverflow system with thubs up or similar, but let me know what I can do. You definitely are in for a thumbs up!!!

    – Peter HIRT
    Nov 23 '18 at 14:13






  • 1





    @PeterHIRT I am glad to hear that! You can consult the Stack Overflow guidelines - see What should I do when someone answers my question?

    – today
    Nov 23 '18 at 14:37
















1














First of all, you must note that getting the output of a model or a layer only makes sense when you feed the input layers with some data. You get the model something (i.e. input data), you get something in return (i.e. output or feature map or activation map). That's why it would produce the following error:




You must feed a value for placeholder tensor 'conv2d_6_input'




You haven't fed the baby, so it would cry :)




Now, the idea of building a new Keras model is counterproductive. When you have a large model in the first place, one would like to plug in some kind of ready-made code that can get the output of the feature maps and visualize them. So this route seems not really interesting.




I think you are mistakenly thinking that when you construct a new model out of the layers of another model, a whole new model is cloned. That's not the case since the parameters of the layers would be shared.



Concretely, what you are looking for can be achieved like this:



viz_conv = Model(model.input, model.layers[0].output)
conv_active = viz_conv(my_input_data) # my_input_data is a numpy array of shape `(num_samples,28,28,1)`


All the parameters of viz_conv are shared with model and they have not been copied either. Under the hood they are using the same weight Tensors.



Alternatively, you could define a backend function to do this:



from tensorflow.keras import backend as K

viz_func = K.function([model.input], [any layer(s) you would like in the model])
output = viz_func([my_input_data])


This has been covered in Keras documentation and I highly recommend to read that as well.






share|improve this answer
























  • Hello, your help was leading me to get it running as desired, so, many thanks. I don't know the stackoverflow system with thubs up or similar, but let me know what I can do. You definitely are in for a thumbs up!!!

    – Peter HIRT
    Nov 23 '18 at 14:13






  • 1





    @PeterHIRT I am glad to hear that! You can consult the Stack Overflow guidelines - see What should I do when someone answers my question?

    – today
    Nov 23 '18 at 14:37














1












1








1







First of all, you must note that getting the output of a model or a layer only makes sense when you feed the input layers with some data. You get the model something (i.e. input data), you get something in return (i.e. output or feature map or activation map). That's why it would produce the following error:




You must feed a value for placeholder tensor 'conv2d_6_input'




You haven't fed the baby, so it would cry :)




Now, the idea of building a new Keras model is counterproductive. When you have a large model in the first place, one would like to plug in some kind of ready-made code that can get the output of the feature maps and visualize them. So this route seems not really interesting.




I think you are mistakenly thinking that when you construct a new model out of the layers of another model, a whole new model is cloned. That's not the case since the parameters of the layers would be shared.



Concretely, what you are looking for can be achieved like this:



viz_conv = Model(model.input, model.layers[0].output)
conv_active = viz_conv(my_input_data) # my_input_data is a numpy array of shape `(num_samples,28,28,1)`


All the parameters of viz_conv are shared with model and they have not been copied either. Under the hood they are using the same weight Tensors.



Alternatively, you could define a backend function to do this:



from tensorflow.keras import backend as K

viz_func = K.function([model.input], [any layer(s) you would like in the model])
output = viz_func([my_input_data])


This has been covered in Keras documentation and I highly recommend to read that as well.






share|improve this answer













First of all, you must note that getting the output of a model or a layer only makes sense when you feed the input layers with some data. You get the model something (i.e. input data), you get something in return (i.e. output or feature map or activation map). That's why it would produce the following error:




You must feed a value for placeholder tensor 'conv2d_6_input'




You haven't fed the baby, so it would cry :)




Now, the idea of building a new Keras model is counterproductive. When you have a large model in the first place, one would like to plug in some kind of ready-made code that can get the output of the feature maps and visualize them. So this route seems not really interesting.




I think you are mistakenly thinking that when you construct a new model out of the layers of another model, a whole new model is cloned. That's not the case since the parameters of the layers would be shared.



Concretely, what you are looking for can be achieved like this:



viz_conv = Model(model.input, model.layers[0].output)
conv_active = viz_conv(my_input_data) # my_input_data is a numpy array of shape `(num_samples,28,28,1)`


All the parameters of viz_conv are shared with model and they have not been copied either. Under the hood they are using the same weight Tensors.



Alternatively, you could define a backend function to do this:



from tensorflow.keras import backend as K

viz_func = K.function([model.input], [any layer(s) you would like in the model])
output = viz_func([my_input_data])


This has been covered in Keras documentation and I highly recommend to read that as well.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 22 '18 at 19:57









todaytoday

10.9k21837




10.9k21837













  • Hello, your help was leading me to get it running as desired, so, many thanks. I don't know the stackoverflow system with thubs up or similar, but let me know what I can do. You definitely are in for a thumbs up!!!

    – Peter HIRT
    Nov 23 '18 at 14:13






  • 1





    @PeterHIRT I am glad to hear that! You can consult the Stack Overflow guidelines - see What should I do when someone answers my question?

    – today
    Nov 23 '18 at 14:37



















  • Hello, your help was leading me to get it running as desired, so, many thanks. I don't know the stackoverflow system with thubs up or similar, but let me know what I can do. You definitely are in for a thumbs up!!!

    – Peter HIRT
    Nov 23 '18 at 14:13






  • 1





    @PeterHIRT I am glad to hear that! You can consult the Stack Overflow guidelines - see What should I do when someone answers my question?

    – today
    Nov 23 '18 at 14:37

















Hello, your help was leading me to get it running as desired, so, many thanks. I don't know the stackoverflow system with thubs up or similar, but let me know what I can do. You definitely are in for a thumbs up!!!

– Peter HIRT
Nov 23 '18 at 14:13





Hello, your help was leading me to get it running as desired, so, many thanks. I don't know the stackoverflow system with thubs up or similar, but let me know what I can do. You definitely are in for a thumbs up!!!

– Peter HIRT
Nov 23 '18 at 14:13




1




1





@PeterHIRT I am glad to hear that! You can consult the Stack Overflow guidelines - see What should I do when someone answers my question?

– today
Nov 23 '18 at 14:37





@PeterHIRT I am glad to hear that! You can consult the Stack Overflow guidelines - see What should I do when someone answers my question?

– today
Nov 23 '18 at 14:37




















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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53419127%2fhow-to-get-data-from-within-keras-model-for-visualisation%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

Can a sorcerer learn a 5th-level spell early by creating spell slots using the Font of Magic feature?

Does disintegrating a polymorphed enemy still kill it after the 2018 errata?

A Topological Invariant for $pi_3(U(n))$