gpt4 book ai didi

python - Keras中 `TypeError: ' Tensor'对象不支持item assignment`的解决方法

转载 作者:太空宇宙 更新时间:2023-11-04 04:16:38 27 4
gpt4 key购买 nike

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 更改为张量。

最佳答案

这个表达式对张量无效:

robust_eta[robust_eta >= eta_thresh] = 0.0

首先,张量不支持这种奇特的索引语法。其次,张量是只读对象。如果你想要读写能力,你应该使用 tf.Variable

但在这种情况下创建另一个 Tensor 更为实用。此代码的 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)

关于python - Keras中 `TypeError: ' Tensor'对象不支持item assignment`的解决方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55281410/

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