简体   繁体   中英

How to solve the problem that `TypeError: 'Tensor' object does not support item assignment ` in Keras

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

Error:

---> 37                 robust_eta[robust_eta >= eta_thresh] = 0.0
TypeError: 'Tensor' object does not support item assignment

Now I don't know how to change the numpy dtype into tensor in assigning the values.

This expression is not valid for tensors:

robust_eta[robust_eta >= eta_thresh] = 0.0

First, this fancy indexing syntax is not supported for Tensors. Second, Tensors are read-only objects. If you want read-write abilities, you should use tf.Variable .

But it is more practical in this case to create another Tensor. The TensorFlow equivalent of this code would be:

robust_eta = tf.where(tf.greater(robust_eta, eta_thresh), tf.zeros_like(robust_eta), robust_eta)

However, this will not help you in writing a working loss function, as the next line:

np.argmax(robust_eta)

Will fail expecting a ndarray. You have there a mixture of numpy and TensorFlow code. You need to stick to either Tensors or NumPy arrays. I think the easiest way would be to get the value of eta_corr as NumPy array at the beginning:

eta_corr = K.eval(self.output)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM