如何在CNN中的每一层之后打印特征矢量形状

时间:2019-01-29 04:58:58

标签: python tflearn

我的CNN模型架构如下: def model_a(x_train):

input_batch = tflearn.layers.core.input_data(shape=(None, x_train.shape[1], x_train.shape[2], x_train.shape[3]))

input_batch = tflearn.layers.normalization.batch_normalization(input_batch)

network = tflearn.layers.conv.conv_2d(input_batch, 32, 5, activation='relu')
network = tflearn.layers.conv.max_pool_2d(network, 2,2)
network = tflearn.dropout(network, .8)

network = tflearn.layers.conv.conv_2d(network, 32, 5, activation='relu')
network = tflearn.layers.conv.max_pool_2d(network, 2,2)

network = tflearn.dropout(network, .8)

network = tflearn.fully_connected(network, 256, activation='relu')
network = tflearn.dropout(network, .8)
network = tflearn.fully_connected(network, 128, activation='relu')
network= tflearn.dropout(network, .8)
network = tflearn.fully_connected(network,2, activation='softmax')

return network

我想在训练结束后打印并保存每一层的特征矢量形状以及每一层的权重。如何在tflearn中做到这一点?

1 个答案:

答案 0 :(得分:0)

您可以使用以下方法获取层的权重和偏差值:

  

model.get_weights(layer_name.W)

     

model.get_weights(layer_name.b)

例如:

network1 = tflearn.layers.conv.conv_2d(input_batch, 32, 5, activation='relu')
network2 = tflearn.layers.conv.max_pool_2d(network1, 2,2)
network3 = tflearn.dropout(network2, .8)
// ...
// To get the weights and bias value of the convolutional layer i.e. network1
weight_network1 = model.get_weights(network1.W)
bias_network1 = model.get_weights(network1.b)

您可以查看此example以获得更多详细信息。