如何解决错误如果启用了Tensor相等,则变量不可散列。而是使用tensor.experimental_ref()作为键

时间:2020-05-17 18:39:32

标签: python tensorflow keras tensorflow2.0

我正在尝试重现代码生成的结果(改编自https://github.com/alexarnimueller/LSTM_peptide

这段代码是用TF.1.X编写的,我尝试使用TensorFlow中的纳米级知识来修改TF.2.X的代码,但是它从未起作用,并引发了以下错误。经过大量搜索,我了解到问题可能与图形和渴望模式有关。我该如何解决这个问题?

代码:由于char限制,我包括了显示错误完整代码的函数,可以在这里找到(https://github.com/alexarnimueller/LSTM_peptides/blob/master/LSTM_peptides.py):

def get_num_params(self):
    """Method to get the amount of trainable parameters in the model.
    """
    trainable = int(np.sum([K.count_params(p) for p in set(self.model.trainable_weights)]))
    non_trainable = int(np.sum([K.count_params(p) for p in set(self.model.non_trainable_weights)]))
    print('\nMODEL PARAMETERS')
    print('Total parameters:         %i' % (trainable + non_trainable))
    print('Trainable parameters:     %i' % trainable)
    print('Non-trainable parameters: %i' % non_trainable)

错误:

2020-05-17 14:20:12.184210: I tensorflow/core/common_runtime/process_util.cc:115] Creating new thread pool with default inter op setting: 2. Tune using inter_op_parallelism_threads for best performance.
Traceback (most recent call last):
  File "LSTM_peptides_II.py", line 792, in <module>
    finetune=args.finetune, references=args.refs)
  File "LSTM_peptides_II.py", line 714, in main
    dropoutfract=dropout, l2_reg=l2_rate, ask=True, seed=42)
  File "LSTM_peptides_II.py", line 443, in __init__
    self.initialize_model(seed=self.seed)
  File "LSTM_peptides_II.py", line 495, in initialize_model
    self.get_num_params()
  File "LSTM_peptides_II.py", line 677, in get_num_params
    trainable = int(np.sum([K.count_params(p) for p in set(self.model.trainable_weights)]))
  File "/home/User1/anaconda3/envs/TF2/lib/python3.7/site-packages/tensorflow_core/python/ops/variables.py", line 1086, in __hash__
    raise TypeError("Variable is unhashable if Tensor equality is enabled. "
TypeError: Variable is unhashable if Tensor equality is enabled. Instead, use tensor.experimental_ref() as the key.

0 个答案:

没有答案