1
這是我爲keras實現模型而編寫的代碼(參見The model architecture)。我想獲得共享網絡中最後一層的輸出張量(圖像中的A & B)。我在其他stackoverflow答案中看到很多例子。但是我的模型是一個暹羅網絡,我想獲得輸入到連體相似度函數的圖層的輸出。如何獲得最後一層的輸出(A和B張量的值)在這個連體實現中的輸出?
from __future__ import absolute_import
from __future__ import print_function
import numpy as np
import os
from keras.preprocessing import sequence
from keras.models import Sequential, Model
from keras.layers import Dense, Dropout, Embedding, LSTM, Bidirectional, Input, Lambda
from keras.datasets import imdb
from keras.layers.embeddings import Embedding
from keras.optimizers import RMSprop
from keras import backend as K
# custom module to read activations of layers
from read_activations import get_activations
# ignore TensorFlow messages and warnings
os.environ['TF_CPP_MIN_LOG_LEVEL']='2'
# os.system('clear')
# fix random seed for reproducibility
np.random.seed(7)
# importing custom module for data preprocessing
import preprocess_data
def euclidean_distance(vects):
x, y = vects
return K.sqrt(K.maximum(K.sum(K.square(x - y), axis=1, keepdims=True), K.epsilon()))
# return K.abs(x-y)
def eucl_dist_output_shape(shapes):
shape1, shape2 = shapes
return (shape1[0], 1)
def contrastive_loss(y_true, y_pred):
'''Contrastive loss from Hadsell-et-al.'06
http://yann.lecun.com/exdb/publis/pdf/hadsell-chopra-lecun-06.pdf
'''
margin = 1
return K.mean(y_true * K.square(y_pred) +
(1 - y_true) * K.square(K.maximum(margin - y_pred, 0)))
def compute_accuracy(predictions, labels):
'''Compute classification accuracy with a fixed threshold on distances.
'''
return labels[predictions.ravel() < 0.5].mean()
def create_base_network(input_dim):
'''Base network to be shared.
'''
seq = Sequential()
seq.add(Dense(128, input_shape=(input_dim,), activation='relu'))
seq.add(Dropout(0.1))
seq.add(Dense(128, activation='relu'))
seq.add(Dropout(0.1))
seq.add(Dense(128, activation='linear'))
return seq
x_data, y_data = preprocess_data.dataset()
input_dim = 1000
epochs = 25
tr_pairs = x_data[:263] # 263000
tr_y = y_data[:263]
te_pairs = x_data[263:] # 113000
te_y = y_data[263:]
# print(tr_pairs[:, 1])
base_network = create_base_network(input_dim)
print(base_network.summary())
input_a = Input(shape=(input_dim,))
input_b = Input(shape=(input_dim,))
# because we re-use the same instance `base_network`,
# the weights of the network
# will be shared across the two branches
processed_a = base_network(input_a)
processed_b = base_network(input_b)
distance = Lambda(euclidean_distance,
output_shape=eucl_dist_output_shape)([processed_a, processed_b])
model = Model([input_a, input_b], distance)
# train
rms = RMSprop()
model.compile(loss=contrastive_loss, optimizer=rms)
model.fit([tr_pairs[:, 0], tr_pairs[:, 1]], tr_y,
batch_size=128,
epochs=epochs,
validation_data=([te_pairs[:, 0], te_pairs[:, 1]], te_y))
# compute final accuracy on training and test sets
pred = model.predict([tr_pairs[:, 0], tr_pairs[:, 1]])
tr_acc = compute_accuracy(pred, tr_y)
pred = model.predict([te_pairs[:, 0], te_pairs[:, 1]])
te_acc = compute_accuracy(pred, te_y)
print(model.summary())
print('* Accuracy on training set: %0.2f%%' % (100 * tr_acc))
print('* Accuracy on test set: %0.2f%%' % (100 * te_acc))
其中:'layer_outs = [func([test,1.])for func in functors]'如果必須在我的實現中使用''表示''是什麼意思? –
'1'表示它處於訓練階段。在你的情況下,它可能應該是'0'來表示它的測試階段。這種區別是因爲像Dropout和BatchNormalization這樣的層在訓練和測試階段表現不同。 – indraforyou