如何为 Keras 网络提供样本矩阵进行调试?
How do I feed a Keras network a sample matrix to debug?
我正在研究 this tutorial,我想准确了解图层操作的工作原理。所以我扩展了第一个示例,如下所示。我不确定我将从这个网络中得到什么,所以我想输入一个具有正确尺寸的张量并查看输出是什么。我怎么做?
使用:keras 2.0.2
from keras.models import Sequential
from keras.layers import Dense, Activation
from keras.layers import Lambda
model = Sequential([
Dense(32, input_shape=(10, 12, 14)),
Activation('relu'),
Dense(16),
Activation('softmax'),
])
def output_of_lambda(input_shape):
return (input_shape[0], 1, input_shape[2])
def mean(x):
return K.mean(x, axis=1, keepdims=True)
model.add(Lambda(mean, output_shape=output_of_lambda))
model.summary()
输出:
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
dense_9 (Dense) (None, 10, 12, 32) 480
_________________________________________________________________
activation_9 (Activation) (None, 10, 12, 32) 0
_________________________________________________________________
dense_10 (Dense) (None, 10, 12, 16) 528
_________________________________________________________________
activation_10 (Activation) (None, 10, 12, 16) 0
_________________________________________________________________
lambda_6 (Lambda) (None, 1, 12) 0
=================================================================
你只需做一个predictions = model.predict(data)
。
其中data是你输入的数据,必须是(any,10,12,14)
的形状。
为了传递单个样本而不是批次,形状必须是 (1,10,12,14)
。
Daniel 是对的,也可以使用后端创建一个 keras 函数
这是一个例子:
from keras import backend as K
from keras.models import Sequential
from keras.layers import Dense, Activation
from keras.layers import Lambda
model = Sequential([
Dense(32, input_shape=(10, 12, 14)),
Activation('relu'),
Dense(16),
Activation('softmax'),
])
def output_of_lambda(input_shape):
return (input_shape[0], 1, input_shape[2])
def mean(x):
return K.mean(x, axis=1, keepdims=True)
model.add(Lambda(mean, output_shape=output_of_lambda))
model.summary()
# add a function to push some data through the model
func = K.function([model.inputs[0], K.learning_phase()], [model.outputs[0]]
X = np.random.randn(100, 10, 12, 14)
print(func([X, 0]))
这使您可以灵活地查看 any 层输出的内容,只是
通过改变 K.function ... [model.outputs[0]] to [model.layers[2].output]
这给你第二个密集层的输出
请参阅有关此事的 keras 常见问题解答:how-can-i-obtain-the-output-of-an-intermediate-layer
我正在研究 this tutorial,我想准确了解图层操作的工作原理。所以我扩展了第一个示例,如下所示。我不确定我将从这个网络中得到什么,所以我想输入一个具有正确尺寸的张量并查看输出是什么。我怎么做?
使用:keras 2.0.2
from keras.models import Sequential
from keras.layers import Dense, Activation
from keras.layers import Lambda
model = Sequential([
Dense(32, input_shape=(10, 12, 14)),
Activation('relu'),
Dense(16),
Activation('softmax'),
])
def output_of_lambda(input_shape):
return (input_shape[0], 1, input_shape[2])
def mean(x):
return K.mean(x, axis=1, keepdims=True)
model.add(Lambda(mean, output_shape=output_of_lambda))
model.summary()
输出:
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
dense_9 (Dense) (None, 10, 12, 32) 480
_________________________________________________________________
activation_9 (Activation) (None, 10, 12, 32) 0
_________________________________________________________________
dense_10 (Dense) (None, 10, 12, 16) 528
_________________________________________________________________
activation_10 (Activation) (None, 10, 12, 16) 0
_________________________________________________________________
lambda_6 (Lambda) (None, 1, 12) 0
=================================================================
你只需做一个predictions = model.predict(data)
。
其中data是你输入的数据,必须是(any,10,12,14)
的形状。
为了传递单个样本而不是批次,形状必须是 (1,10,12,14)
。
Daniel 是对的,也可以使用后端创建一个 keras 函数
这是一个例子:
from keras import backend as K
from keras.models import Sequential
from keras.layers import Dense, Activation
from keras.layers import Lambda
model = Sequential([
Dense(32, input_shape=(10, 12, 14)),
Activation('relu'),
Dense(16),
Activation('softmax'),
])
def output_of_lambda(input_shape):
return (input_shape[0], 1, input_shape[2])
def mean(x):
return K.mean(x, axis=1, keepdims=True)
model.add(Lambda(mean, output_shape=output_of_lambda))
model.summary()
# add a function to push some data through the model
func = K.function([model.inputs[0], K.learning_phase()], [model.outputs[0]]
X = np.random.randn(100, 10, 12, 14)
print(func([X, 0]))
这使您可以灵活地查看 any 层输出的内容,只是
通过改变 K.function ... [model.outputs[0]] to [model.layers[2].output]
这给你第二个密集层的输出
请参阅有关此事的 keras 常见问题解答:how-can-i-obtain-the-output-of-an-intermediate-layer