Traceback (most recent call last):
File "C:/Users/lenovo/PycharmProjects/keras_bert/venv/ks.py", line 208, in
train_model_pred, test_model_pred = run_cv(2, DATA_LIST, None, DATA_LIST_TEST)
File "C:/Users/lenovo/PycharmProjects/keras_bert/venv/ks.py", line 191, in run_cv
callbacks=[plateau, checkpoint],
File "C:\Users\lenovo\AppData\Local\Programs\Python\Python37\lib\site-packages\keras\legacy\interfaces.py", line 91, in wrapper
return func(*args, **kwargs)
File "C:\Users\lenovo\AppData\Local\Programs\Python\Python37\lib\site-packages\keras\engine\training.py", line 1732, in fit_generator
initial_epoch=initial_epoch)
File "C:\Users\lenovo\AppData\Local\Programs\Python\Python37\lib\site-packages\keras\engine\training_generator.py", line 42, in fit_generator
model._make_train_function()
File "C:\Users\lenovo\AppData\Local\Programs\Python\Python37\lib\site-packages\keras\engine\training.py", line 316, in _make_train_function
loss=self.total_loss)
File "C:\Users\lenovo\AppData\Local\Programs\Python\Python37\lib\site-packages\keras\legacy\interfaces.py", line 91, in wrapper
return func(*args, **kwargs)
File "C:\Users\lenovo\AppData\Local\Programs\Python\Python37\lib\site-packages\keras\optimizers.py", line 504, in get_updates
grads = self.get_gradients(loss, params)
File "C:\Users\lenovo\AppData\Local\Programs\Python\Python37\lib\site-packages\keras\optimizers.py", line 93, in get_gradients
raise ValueError('An operation has `None` for gradient. '
ValueError: An operation has `None` for gradient. Please make sure that all of your ops have a gradient defined (i.e. are differentiable). Common ops without gradient: K.argmax, K.round, K.eval.
Я использую пакет модели bert — keras-bert. Я пытался сделать берт с мягкой маской самостоятельно. Когда я работал над seq2seq с жесткой маской, я тоже получил ту же ошибку.
Мне нужна помощь по этой ошибке, потому что, насколько я знаю, keras.losses.categorical_crossentropy дифференцируема.
Я постоянно получаю эту ошибку со следующим кодом [code]config_path = 'E:/chinese_roberta_wwm_large_ext_L-24_H-1024_A-16/bert_config.json' checkpoint_path = 'E:/chinese_roberta_wwm_large_ext_L-24_H-1024_A-16/bert_model.ckpt' dict_path = 'E:/chinese_roberta_wwm_large_ext_L-24_H-1024_A-16/vocab.txt'
# 将词表中的词编号转换为字典 token_dict = {} with codecs.open(dict_path, 'r', 'utf8') as reader: for line in reader: token = line.strip() token_dict[token] = len(token_dict)
# 重写tokenizer class OurTokenizer(Tokenizer): def _tokenize(self, text): R = [] for c in text: if c in self._token_dict: R.append(c) elif self._is_space(c): R.append('[unused1]') # 用[unused1]来表示空格类字符 else: R.append('[UNK]') # 不在列表的字符用[UNK]表示 return R
tokenizer = OurTokenizer(token_dict)
# 让每条文本的长度相同,用0填充 def seq_padding(X, padding=0): L = [len(x) for x in X] ML = max(L) return np.array([ np.concatenate([x, [padding] * (ML - len(x))]) if len(x) < ML else x for x in X ])
def seq_padding_2(X, padding=0): L = [x.shape[0] for x in X] ML = max(L)
results = [] for x in X:
if x.shape[0] < ML: r = np.array([0] * 21128) x_1 = list(x) for i in range((ML-x.shape[0])): x_1.append(r) results.append(np.array(x_1)) else: results.append(x)
bert_model_2 = load_trained_model_from_checkpoint(config_path, checkpoint_path, seq_len=None, training=True) # load pretrain bert model for l in bert_model_2.layers: l.trainable = True
x2 = bert_model_2([x1_in, x2_in, x3])[0] model = Model([x1_in, x2_in], x2) model.compile(loss='sparse_categorical_crossentropy', optimizer=Adam(1e-5)) return model
DATA_LIST = [] for data_row in train_df.iloc[:].itertuples(): DATA_LIST.append((data_row.testing_sentence, to_categorical(tokenizer.encode(data_row.correct_sentence)[0], 21128))) DATA_LIST = np.array(DATA_LIST)
DATA_LIST_TEST = [] for data_row in train_df.iloc[:].itertuples(): DATA_LIST_TEST.append((data_row.testing_sentence, to_categorical(tokenizer.encode(data_row.correct_sentence)[0], 21128))) DATA_LIST_TEST = np.array(DATA_LIST_TEST)
test_pred = [np.argmax(x) for x in test_model_pred]
[/code] отслеживание ошибок показано ниже [code]Traceback (most recent call last): File "C:/Users/lenovo/PycharmProjects/keras_bert/venv/ks.py", line 208, in train_model_pred, test_model_pred = run_cv(2, DATA_LIST, None, DATA_LIST_TEST) File "C:/Users/lenovo/PycharmProjects/keras_bert/venv/ks.py", line 191, in run_cv callbacks=[plateau, checkpoint], File "C:\Users\lenovo\AppData\Local\Programs\Python\Python37\lib\site-packages\keras\legacy\interfaces.py", line 91, in wrapper return func(*args, **kwargs) File "C:\Users\lenovo\AppData\Local\Programs\Python\Python37\lib\site-packages\keras\engine\training.py", line 1732, in fit_generator initial_epoch=initial_epoch) File "C:\Users\lenovo\AppData\Local\Programs\Python\Python37\lib\site-packages\keras\engine\training_generator.py", line 42, in fit_generator model._make_train_function() File "C:\Users\lenovo\AppData\Local\Programs\Python\Python37\lib\site-packages\keras\engine\training.py", line 316, in _make_train_function loss=self.total_loss) File "C:\Users\lenovo\AppData\Local\Programs\Python\Python37\lib\site-packages\keras\legacy\interfaces.py", line 91, in wrapper return func(*args, **kwargs) File "C:\Users\lenovo\AppData\Local\Programs\Python\Python37\lib\site-packages\keras\optimizers.py", line 504, in get_updates grads = self.get_gradients(loss, params) File "C:\Users\lenovo\AppData\Local\Programs\Python\Python37\lib\site-packages\keras\optimizers.py", line 93, in get_gradients raise ValueError('An operation has `None` for gradient. ' ValueError: An operation has `None` for gradient. Please make sure that all of your ops have a gradient defined (i.e. are differentiable). Common ops without gradient: K.argmax, K.round, K.eval. [/code] Я использую пакет модели bert — keras-bert. Я пытался сделать берт с мягкой маской самостоятельно. Когда я работал над seq2seq с жесткой маской, я тоже получил ту же ошибку. Мне нужна помощь по этой ошибке, потому что, насколько я знаю, keras.losses.categorical_crossentropy дифференцируема.