如何解决 tensorflow.keras 中的值错误?
Posted
技术标签:
【中文标题】如何解决 tensorflow.keras 中的值错误?【英文标题】:how to solve Value Error in tensorflow.keras? 【发布时间】:2021-12-28 01:37:17 【问题描述】:我有一些关于韩国 NLP 的项目。我的项目的目的是将句子分为三类(无,冒犯,讨厌)。输入数据由 45 长度填充。所以我创建了简单的 DL 模型并将预处理数据输入到模型中。我想创建 DL 模型来对 cursed_sentence 进行分类
所以我使用的是 tensorflow-cpu 的 keras(版本:2.5.0 / python 版本 = 3.7.9)。我在使用 keras 时遇到了一些问题。我使用 Keras 创建了非常简单的 LSTM 模型。我创建了嵌入层。嵌入层的 input_dim 为 vocab_size + 1(vocab_size 为 24844)并创建了 LSTM 层和最终层,使用 softmax 作为激活函数
但我检查了发生的“ValueError:形状 (None, 3) 和 (None, 1) 不兼容。”我提交了一些代码和错误信息。我不明白为什么会发生这个错误以及发生了哪一部分错误
import pickle
import numpy as np
from tensorflow import keras
from tensorflow.keras import layers
METRICS = [
keras.metrics.TruePositives(name='tp'),
keras.metrics.FalsePositives(name='fp'),
keras.metrics.TrueNegatives(name='tn'),
keras.metrics.FalseNegatives(name='fn'),
keras.metrics.BinaryAccuracy(name='accuracy'),
keras.metrics.Precision(name='precision'),
keras.metrics.Recall(name='recall'),
keras.metrics.AUC(name='auc')
]
model = keras.Sequential()
model.add(layers.Embedding(len(tk.word_index)+1, 100, input_length=45))
model.add(layers.LSTM(100))
model.add(layers.Dense(3, activation='softmax'))
model.summary()
early_stopping = keras.callbacks.EarlyStopping(
monitor = 'val_auc',
verbose = 1,
patience = 10,
mode = 'max',
restore_best_weights=True)
model.compile(optimizer=keras.optimizers.RMSprop(), loss='sparse_categorical_crossentropy', metrics=METRICS)
baseline_history = model.fit(train_data, train_label, batch_size = 8192, epochs = 100, callbacks = [early_stopping], validation_split = 0.2, class_weight = class_weight)
以下内容是创建简单模型的总结
Layer (type) Output Shape Param #
=================================================================
embedding (Embedding) (None, 45, 100) 2484500
_________________________________________________________________
lstm (LSTM) (None, 100) 80400
_________________________________________________________________
dense (Dense) (None, 3) 303
=================================================================
Total params: 2,565,203
Trainable params: 2,565,203
Non-trainable params: 0
以下内容出现错误提示
Traceback (most recent call last):
File "learning.py", line 85, in <module>
baseline_history = model.fit(train_data, train_label, batch_size = 8192, epochs = 100, callbacks = [early_stopping], validation_split = 0.2, class_weight = class_weight)
File "C:\Users\pllab\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\keras\engine\training.py", line 1183, in fit
tmp_logs = self.train_function(iterator)
File "C:\Users\pllab\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\eager\def_function.py", line 889, in __call__
result = self._call(*args, **kwds)
File "C:\Users\pllab\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\eager\def_function.py", line 933, in _call
self._initialize(args, kwds, add_initializers_to=initializers)
File "C:\Users\pllab\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\eager\def_function.py", line 764, in _initialize
*args, **kwds))
File "C:\Users\pllab\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\eager\function.py", line 3050, in _get_concrete_function_internal_garbage_collected
graph_function, _ = self._maybe_define_function(args, kwargs)
File "C:\Users\pllab\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\eager\function.py", line 3444, in _maybe_define_function
graph_function = self._create_graph_function(args, kwargs)
File "C:\Users\pllab\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\eager\function.py", line 3289, in _create_graph_function
capture_by_value=self._capture_by_value),
File "C:\Users\pllab\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\framework\func_graph.py", line 999, in func_graph_from_py_func
func_outputs = python_func(*func_args, **func_kwargs)
File "C:\Users\pllab\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\eager\def_function.py", line 672, in wrapped_fn
out = weak_wrapped_fn().__wrapped__(*args, **kwds)
File "C:\Users\pllab\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\framework\func_graph.py", line 986, in wrapper
raise e.ag_error_metadata.to_exception(e)
ValueError: in user code:
C:\Users\pllab\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\keras\engine\training.py:855 train_function *
return step_function(self, iterator)
C:\Users\pllab\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\keras\engine\training.py:845 step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
C:\Users\pllab\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:1285 run
return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
C:\Users\pllab\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:2833 call_for_each_replica
return self._call_for_each_replica(fn, args, kwargs)
C:\Users\pllab\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:3608 _call_for_each_replica
return fn(*args, **kwargs)
C:\Users\pllab\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\keras\engine\training.py:838 run_step **
outputs = model.train_step(data)
C:\Users\pllab\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\keras\engine\training.py:800 train_step
self.compiled_metrics.update_state(y, y_pred, sample_weight)
C:\Users\pllab\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\keras\engine\compile_utils.py:460 update_state
metric_obj.update_state(y_t, y_p, sample_weight=mask)
C:\Users\pllab\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\keras\utils\metrics_utils.py:86 decorated
update_op = update_state_fn(*args, **kwargs)
C:\Users\pllab\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\keras\metrics.py:177 update_state_fn
return ag_update_state(*args, **kwargs)
C:\Users\pllab\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\keras\metrics.py:1005 update_state **
sample_weight=sample_weight)
C:\Users\pllab\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\keras\utils\metrics_utils.py:366 update_confusion_matrix_variables
y_pred.shape.assert_is_compatible_with(y_true.shape)
C:\Users\pllab\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\framework\tensor_shape.py:1161 assert_is_compatible_with
raise ValueError("Shapes %s and %s are incompatible" % (self, other))
ValueError: Shapes (None, 3) and (None, 1) are incompatible
我试图解决这个问题,但我找不到合适的答案。对不起我的英语,请给我一些关于这个错误的建议。
【问题讨论】:
你能显示train_data.shape
和train_label.shape
吗?
感谢您的帮助!我的 train_data.shape 是 (190053, 45) 并且 train_label.shape 是 (190053, ) train_data 填充了 45 长度并且 train_label 的值是 0,1,2(0 是无,1 是冒犯,2 是讨厌)
【参考方案1】:
我不是很喜欢机器学习,但尝试改变
model.add(layers.Dense(3, activation='softmax'))
到
model.add(layers.Dense(1, activation='softmax'))
看看错误是否仍然发生
编辑: 或检查 train_label 的元素。我认为它应该有 3 个标签值(因为你的最后一层有 3 个输出)
【讨论】:
我试过你建议的。所以我检查了没有再发生同样的错误。谢谢。但我有一些新错误。 tensorflow.python.framework.errors_impl.InvalidArgumentError: indices[3845,21] = 45098 is not in [0, 24845) [[nodesequential/embedding/embedding_lookup (defined at learning.py:86)]] [Op:__inference_train_function_3203]错误可能源于输入操作。连接到节点顺序/嵌入/嵌入查找的输入源操作:我会找到一些方法来处理它...谢谢! 这是错误的,softmax 不应该有 1 个神经元作为输出。 如果你有一个单层作为输出,那么激活函数应该是 sigmoid,对于 softmax,你可以期望与标签一样多的神经元...... 是的,稍微记住之后,你的softmax层是正确的(因为你想分类3个类别)。所以,检查你的 train_label,我认为它应该有 3 个值。 @CeliusStingher 你能确认这是否正确吗? 我们真的不知道 OPs 数据有多少类别,也不知道数据的大小,所以我让他们告诉我们标签的形状和数量。以上是关于如何解决 tensorflow.keras 中的值错误?的主要内容,如果未能解决你的问题,请参考以下文章
Tensorflow+keras解决 Fail to find the dnn implementation.
Tensorflow+keras解决 Fail to find the dnn implementation.
如何在训练 tensorflow.keras 期间替换损失函数
如何选择/配置 AWS GPU 实例以加速 TensorFlow.keras?