gpt4 book ai didi

machine-learning - 在这个暹罗实现中,如何获得最后一层的输出(A 和 B 张量的值)?

转载 作者:行者123 更新时间:2023-11-30 08:46:21 25 4
gpt4 key购买 nike

这是我编写的用于在 keras 中实现模型的代码(参见 The model architecture 中的图像)。我想获取共享网络最后一层的输出张量(图中的 A 和 B)。我在其他 stackoverflow 答案中看到了很多例子。但我的模型是一个暹罗网络,我想获得该层的输出,该输出将成为暹罗相似函数的输入。

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))

最佳答案

processed_aprocessed_b 是 siamese 相似度函数的输入。

如上所述here ,您可以使用创建张量函数来获取输出。

仿函数 = K.function([input_a, input_b]+ [K.learning_phase()], [processed_a,processed_b])

并在新输入上使用相同的方法。

关于machine-learning - 在这个暹罗实现中,如何获得最后一层的输出(A 和 B 张量的值)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44553901/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com