from keras import backend as K
from keras.optimizers import Adam
from keras.models import Model
from keras.layers.core import Dense, Activation, Flatten
from keras.layers import Input,Concatenate
from keras.layers.normalization import BatchNormalization
from keras.layers import LSTM
class MyLoss(object):
def __init__(self, classes, filter_outlier= True ):
self.filter_outlier = filter_outlier
self.classes = classes
def getMyLoss(self, y_true, y_pred):
# number of classes
c = self.classes
T = np.empty((c, c))
# predict probability on the fresh sample
eta_corr =self.output
# Get Matrix T
for i in np.arange(c):
if not self.filter_outlier:
idx_best = np.argmax(eta_corr[:, i])
else:
eta_thresh = np.percentile(eta_corr[:, i], 97,
interpolation='higher')
robust_eta = eta_corr[:, i]
robust_eta[robust_eta >= eta_thresh] = 0.0
idx_best = np.argmax(robust_eta)
for j in np.arange(c):
T[i, j] = eta_corr[idx_best, j]
T_inv = K.constant(np.linalg.inv(T))
y_pred /= K.sum(y_pred, axis=-1, keepdims=True)
y_pred = K.clip(y_pred, K.epsilon(), 1.0 - K.epsilon())
return -K.sum(K.dot(y_true, T_inv) * K.log(y_pred), axis=-1)
class MyModel(object):
'''
BiLstm 网络
'''
def __init__(self, config):
self.max_len = config["max_len"]
self.hidden_size = config["hidden_size"]
self.vocab_size = config["vocab_size"]
self.embedding_size = config["embedding_size"]
self.n_class = config["n_class"]
self.learning_rate = config["learning_rate"]
def build_model(self,):
print("building model")
input = Input(shape = (self.max_len, self.embedding_size))
rnn_outputs, forward_h, forward_c, backward_h, backward_c = \
Bidirectional(LSTM(self.hidden_size, return_sequences = True,
return_state = True))(input)
h_total = Concatenate()([forward_h, backward_h])
# Fully connected layer(dense layer)
output = Dense(self.n_class, kernel_initializer = 'he_normal')(h_total)
# Add softmax
output = Activation('softmax')(output)
model = Model(inputs = input, outputs = output)
# My own Loss Function
loss_fn = MyLoss(classes = self.n_class)
self.loss = loss_fn.getLoss
model.compile(loss = self.loss, optimizer = Adam(
lr = self.learning_rate))
错误:
---> 37 robust_eta[robust_eta >= eta_thresh] = 0.0
TypeError: 'Tensor' object does not support item assignment
现在我不知道如何在分配值时将numpy dtype更改为张量。
答案 0 :(得分:1)
此表达式不适用于张量:
robust_eta[robust_eta >= eta_thresh] = 0.0
首先,Tensors不支持这种奇特的索引语法。其次,张量是只读对象。如果您需要读写功能,则应使用tf.Variable
。
但是在这种情况下,创建另一个张量更为实用。此代码的TensorFlow等效项为:
robust_eta = tf.where(tf.greater(robust_eta, eta_thresh), tf.zeros_like(robust_eta), robust_eta)
但是,这不会帮助您编写工作损失函数,如下一行:
np.argmax(robust_eta)
期望ndarray失败。您在那里混合了numpy和TensorFlow代码。您需要坚持使用Tensors或NumPy数组。 我认为最简单的方法是在开始时将eta_corr的值作为NumPy数组获取:
eta_corr = K.eval(self.output)