简体   繁体   English

如何输出网络的第二层?

[英]How to output the second layer of a network?

My model is trained on digit images ( MNIST dataset ).我的模型是在数字图像( MNIST dataset )上训练的。 I am trying to print the output of the second layer of my network - an array of 128 numbers.我正在尝试打印网络第二层的输出 - 一个 128 个数字的数组。

After reading a lot of examples - for instance this , and this , or this .在阅读了很多例子之后——比如这个这个或者这个

I did not manage to do this on my own network.我没有设法在我自己的网络上做到这一点。 Neither of the solutions work of my own algorithm.两种解决方案都不适用于我自己的算法。

Link to Colab: https://colab.research.google.com/drive/1MLbpWJmq8JZB4_zKongaHP2o3M1FpvAv?fbclid=IwAR20xRz2i6sFS-Nm6Xwfk5hztdXOuxY4tZaDRXxAx3b986HToa9-IaTgASU Colab 链接: https ://colab.research.google.com/drive/1MLbpWJmq8JZB4_zKongaHP2o3M1FpvAv ? fbclid = IwAR20xRz2i6sFS-Nm6Xwfk5hztdXOuxY4tZaDRXxAx9b986TogA

I received a lot of different error messages.我收到了很多不同的错误消息。 I tried to handle each of them, but couldn't figure it on my own.我试图处理它们中的每一个,但无法自己解决。

What am I missing?我错过了什么? How to output the Second layer?如何输出第二层? If my Shape is (28,28) - what should be the type & value of input_shape ?如果我的外形是(28,28) -应该是什么类型和值input_shape


Failed trials & Errors for example:失败的试验和错误,例如:

(1) (1)

for layer in model.layers:

    get_2nd_layer_output = K.function([model.layers[0].input],[model.layers[2].output])
    layer_output = get_2nd_layer_output(layer)[0]
    print('\nlayer output: get_2nd_layer_output=, layer=', layer, '\nlayer output: get_2nd_layer_output=', get_2nd_layer_output)

TypeError: inputs should be a list or tuple.类型错误:输入应该是列表或元组。

(2) (2)

input_shape=(28, 28)
inp = model.input                                           # input placeholder
outputs = [layer.output for layer in model.layers]          # all layer outputs
functor = K.function([inp, K.learning_phase()], outputs )   # evaluation function

# Testing
test = np.random.random(input_shape)[np.newaxis,...]
layer_outs = functor([test, 0.])
print('layer_outs',layer_outs)

tensorflow.python.framework.errors_impl.FailedPreconditionError: Error while reading resource variable dense_1/bias from Container: localhost. tensorflow.python.framework.errors_impl.FailedPreconditionError:从容器读取资源变量dense_1/bias时出错:本地主机。 This could mean that the variable was uninitialized.这可能意味着该变量未初始化。 Not found: Container localhost does not exist.未找到:容器 localhost 不存在。 (Could not find resource: localhost/dense_1/bias) [[{{node dense_1/BiasAdd/ReadVariableOp}}]] (找不到资源:localhost/dense_1/bias)[[{{nodedense_1/BiasAdd/ReadVariableOp}}]]

Looks like you are mixing old keras (before tensorflow 2.0: import keras ) and new keras ( from tensorflow import keras ).看起来您正在混合旧 keras(在 tensorflow 2.0 之前: import keras )和新 keras ( from tensorflow import keras )。

Try not to use old keras alongside tensorflow>=2.0 (and not to refer to the old documentation as in your first link), as it is easily confused with the new one (although nothing strictly illogical):尽量不要在 tensorflow>=2.0 旁边使用旧的 keras (并且不要像在第一个链接中那样引用旧文档),因为它很容易与新文档混淆(尽管没有什么严格不合逻辑的):

from tensorflow import keras
from keras.models import Model
print(Model.__module__) #outputs 'keras.engine.training'

from tensorflow.keras.models import Model
print(Model.__module__) #outputs 'tensorflow.python.keras.engine.training'

Behaviour will be highly unstable mixing those two libraries.混合这两个库的行为将非常不稳定。

Once this is done, using an answer from what you tried, m being your model, and my_input_shape being the shape of your models input ie the shape of one picture (here (28, 28) or (1, 28, 28) if you have batches):完成后,使用您尝试的答案,m 是您的模型,而my_input_shape是您的模型输入的形状,即一张图片的形状(此处为 (28, 28) 或 (1, 28, 28) 如果您有批次):

from tensorflow import keras as K
my_input_data = np.random.rand(*my_input_shape) 
new_temp_model = K.Model(m.input, m.layers[3].output) #replace 3 with index of desired layer
output_of_3rd_layer = new_temp_model.predict(my_input_data) #this is what you want

If you have one image img you can directly write new_temp_model.predict(img)如果你有一张图片img你可以直接写new_temp_model.predict(img)

(Assuming TF2) (假设TF2)

I think the most straightforward approach would be to name your layers, and then call them with standard input, so your model might look like我认为最直接的方法是命名你的层,然后用标准输入调用它们,所以你的模型可能看起来像

model = keras.Sequential([
    keras.layers.Flatten(input_shape=(28, 28), name='flatten'),
    keras.layers.Dense(128, activation='relu', name='hidden'),
    keras.layers.Dense(10, activation='softmax')
])

Then just create an inputs and然后只需创建一个输入和

my_input = tf.random.normal((1, 28, 28)) # Should be like the standard input to your network
output_of_flatten = model.get_layer('flatten')(my_input)
output_of_hidden = model.get_layer('hidden')(output_of_flatten)

output_of_hidden is what you are looking for output_of_hidden就是你要找的

Alternative approach替代方法

If you are looking for a more general solution, assuming your model is sequential, you can use the index keyword of get_layer like this如果您正在寻找更通用的解决方案,假设您的模型是顺序的,您可以像这样使用get_layerindex关键字

my_input = tf.random.normal((1, 28, 28)) # Should be like the standard input to your network
desired_index = 1  # 1 == second layer

for i in range(desired_index):
    my_input = model.get_layer(index=i)(my_input)

At the end of this loop my_input should be what you are looking for在此循环结束时my_input应该是您要查找的内容

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Keras中保存神经网络层的输出 - How to save the output from a layer of a neural network in keras 如何将 output 层连接到另一个神经网络的输入层? - How to connect output layers to the input layer of another neural network? 一个神经网络的输出层应该有多少个神经元用于分类? - How many neurons should be there in the output layer of a neural network for classification? PyTorch:如何打印网络中每一层的输出Blob大小? - PyTorch: How to print output blob size of each layer in network? 如何使用 dnnlib.tflib.network.Network 获得中间层 output - How can I get the intermediate layer output with dnnlib.tflib.network.Network 如何用max代替tensorflow softmax在神经网络的输出层生成一个热矢量? - How to replace tensorflow softmax with max for generating one hot vector at the output layer of Neural Network? 在具有急切执行的 TensorFlow 2.0 中,如何计算特定层的网络输出的梯度? - In TensorFlow 2.0 with eager-execution, how to compute the gradients of a network output wrt a specific layer? 即使输出层有任何大小,keras 神经网络如何找到要属性的类? - How does keras neural network find the class to attribute even if the output layer has any size? 卷积生成对抗网络判别器的output是如何工作的,可以有全连接层吗? - How does the output of the Discriminator of a Convolutional Generative Adversarial Network work, can it have a Fully Connected Layer? 我想将简单RNN层的输出用作其他网络中的输入。 怎么做? - I want to use the output of Simple RNN layer as input in other network. How to do that?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM