简体   繁体   中英

'Tensor' object does not support item assignment

from keras import backend as K
def TSNR(source_input,transformer):
        M=(source_input+transformer)/2
        std1=(source_input-M)**2
        std2=(transformer-M)**2
        std=K.sqrt((std1+std2)/2)
        f=(M/std)

>         f[f >1000000]=0

        final= K.average(f)
        return final

model.compile(optimizer='Adam', loss=losses, loss_weights=loss_weights, metrics=[TSNR])
model.fit(train_generator,
          epochs=nb_epochs,
          steps_per_epoch=steps_per_epoch,
          verbose=2,
          callbacks=[checkpoint],
          validation_data=val_generator(data_dir,1,1584,.3)
                    );
TypeError: in user code:

File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1021, in train_function  *
    return step_function(self, iterator)
File "<ipython-input-25-75c8cc34142e>", line 8, in TSNR  *
    f[f >1000000]=0

TypeError: 'UnliftedInitializerVariable' object does not support item assignment

Hi, I try to write tSNR metric for my.network but I got that problem. how can I fix the assignment problem in this case? I trying to change the value that greater than a specific number in 'f' variable.

If this is about that line assigning values to a tensor based on a condition then it can be done in various ways.

import tensorflow as tf

aa = tf.Variable(tf.zeros([10, 4]))
tensor = tf.constant(10,shape=(4,3))
aa[0:4, 1:4 ].assign(tf.ones_like(tensor, dtype=tf.float32))
print(aa)

#In your code this example should work
aa.assign(tf.where(aa>0,aa,9))
print(aa)

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