Lambda 如何向Keras网络提供示例矩阵以进行调试?
我一直在努力,我想确切地了解图层操作是如何工作的。因此,我扩展了第一个示例,如下所示。我不确定我能从这个网络中得到什么,所以我想输入一个维度正确的张量,看看输出是什么。我该怎么做 使用:keras 2.0.2Lambda 如何向Keras网络提供示例矩阵以进行调试?,lambda,keras,layer,Lambda,Keras,Layer,我一直在努力,我想确切地了解图层操作是如何工作的。因此,我扩展了第一个示例,如下所示。我不确定我能从这个网络中得到什么,所以我想输入一个维度正确的张量,看看输出是什么。我该怎么做 使用:keras 2.0.2 from keras.models import Sequential from keras.layers import Dense, Activation from keras.layers import Lambda model = Sequential([ Dense(32
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
=================================================================
您只需执行一个
predicts=model.predict(data)
其中数据是您的输入数据,其形状必须为(任意,10,12,14)
对于通过单个样品而不是批次,形状必须为
(1,10,12,14)
您只需执行一个预测=模型。预测(数据)
其中数据是您的输入数据,其形状必须为(任意,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]))
这使您可以灵活地查看任何层的输出,只需
通过改变K函数<代码>[model.outputs[0]]到[model.layers[2].output],这将为您提供第二个密集层的输出
有关此问题,请参阅keras常见问题解答: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]))
这使您可以灵活地查看任何层的输出,只需
通过改变K函数<代码>[model.outputs[0]]到[model.layers[2].output],这将为您提供第二个密集层的输出
有关此问题,请参阅keras常见问题解答:谢谢您的提示!谢谢你的提示!