def keras_digits_vis(model, X_test, y_test):
layer_idx = utils.find_layer_idx(model, 'preds')
model.layers[layer_idx].activation = activations.linear
model = utils.apply_modifications(model)
for class_idx in np.arange(10):
indices = np.where(y_test[:, class_idx] == 1.)[0]
idx = indices[0]
f, ax = plt.subplots(1, 4)
ax[0].imshow(X_test[idx][..., 0])
for i, modifier in enumerate([None, 'guided', 'relu']):
heatmap = visualize_saliency(model, layer_idx, filter_indices=class_idx,
seed_input=X_test[idx], backprop_modifier=modifier)
if modifier is None:
modifier = 'vanilla'
ax[i+1].set_title(modifier)
ax[i+1].imshow(heatmap)
plt.imshow(heatmap)
plt.show()
python类linear()的实例源码
def test_linear():
'''
This function does no input validation, it just returns the thing
that was passed in.
'''
xs = [1, 5, True, None, 'foo']
for x in xs:
assert(x == activations.linear(x))
def find_activation_layer(layer, node_index):
"""
Args:
layer(Layer):
node_index:
"""
output_shape = layer.get_output_shape_at(node_index)
maybe_layer = layer
node = maybe_layer.inbound_nodes[node_index]
# Loop will be broken by an error if an output layer is encountered
while True:
# If maybe_layer has a nonlinear activation function return it and its index
activation = getattr(maybe_layer, 'activation', linear)
if activation.__name__ != 'linear':
if maybe_layer.get_output_shape_at(node_index) != output_shape:
ValueError('The activation layer ({0}), does not have the same'
' output shape as {1]'.format(maybe_layer.name,
layer.name))
return maybe_layer, node_index
# If not, move to the next layer in the datastream
next_nodes = get_shallower_nodes(node)
# test if node is a list of nodes with more than one item
if len(next_nodes) > 1:
ValueError('The model must not branch between the chosen layer'
' and the activation layer.')
node = next_nodes[0]
node_index = get_node_index(node)
maybe_layer = node.outbound_layer
# Check if maybe_layer has weights, no activation layer has been found
if maybe_layer.weights and (
not maybe_layer.__class__.__name__.startswith('Global')):
AttributeError('There is no nonlinear activation layer between {0}'
' and {1}'.format(layer.name, maybe_layer.name))
def test_linear():
'''
This function does no input validation, it just returns the thing
that was passed in.
'''
from keras.activations import linear as l
xs = [1, 5, True, None, 'foo']
for x in xs:
assert x == l(x)
def test_linear():
'''
This function does no input validation, it just returns the thing
that was passed in.
'''
xs = [1, 5, True, None, 'foo']
for x in xs:
assert(x == activations.linear(x))
def test_linear():
'''
This function does no input validation, it just returns the thing
that was passed in.
'''
xs = [1, 5, True, None, 'foo']
for x in xs:
assert(x == activations.linear(x))
def prep_model_for_vis(model, out_layer_name='predictions'):
layer_idx = find_layer_idx(model, out_layer_name)
model.layers[layer_idx].activation = activations.linear
model = apply_modifications(model)
return model
def test_linear():
'''
This function does no input validation, it just returns the thing
that was passed in.
'''
from keras.activations import linear as l
xs = [1, 5, True, None, 'foo']
for x in xs:
assert x == l(x)
def test_linear():
'''
This function does no input validation, it just returns the thing
that was passed in.
'''
from keras.activations import linear as l
xs = [1, 5, True, None, 'foo']
for x in xs:
assert x == l(x)