gpt4 book ai didi

python - 无效参数错误 : Cannot update variable with shape [] using a Tensor with shape [32]

转载 作者:太空宇宙 更新时间:2023-11-03 20:25:57 25 4
gpt4 key购买 nike

我正在尝试开始使用神经结构化学习,但是当我运行页面上给出的示例进行测试时,出现以下错误

我尝试过压缩维度,我尝试过不同版本的 tensorflow ——我对 tensorflow 仍然很陌生,所以在这一点上我真的会猜测。

# Create a base model -- sequential, functional, or subclass.
model = tf.keras.Sequential([
tf.keras.Input((28, 28), name='feature'),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(128, activation=tf.nn.relu),
tf.keras.layers.Dense(10, activation=tf.nn.softmax)
])

# Wrap the model with adversarial regularization.
adv_config = nsl.configs.make_adv_reg_config(multiplier=0.2, adv_step_size=0.05)
adv_model = nsl.keras.AdversarialRegularization(model, adv_config=adv_config)


# Compile, train, and evaluate.
adv_model.compile(optimizer='adam',loss='sparse_categorical_crossentropy',metrics=['accuracy'])


#let us now fit the model
adv_model.fit({'feature': x_train, 'label': y_train}, batch_size=32, epochs=5)



W0906 13:48:30.427690 140388427564928 training_utils.py:1101] Output output_1 missing from loss dictionary. We assume this was done on purpose. The fit and evaluate APIs will not be expecting any data to be passed to output_1.
Epoch 1/5
---------------------------------------------------------------------------
InvalidArgumentError Traceback (most recent call last)
<ipython-input-21-a5b951c24c49> in <module>()
----> 1 adv_model.fit({'feature': x_train, 'label': y_train}, batch_size=32, epochs=5)

3 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/client/session.py in __call__(self, *args, **kwargs)
1456 ret = tf_session.TF_SessionRunCallable(self._session._session,
1457 self._handle, args,
-> 1458 run_metadata_ptr)
1459 if run_metadata:
1460 proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)

InvalidArgumentError: Cannot update variable with shape [] using a Tensor with shape [32], shapes must be equal.
[[{{node AdversarialRegularization_1/AssignAddVariableOp_2}}]]

该模型应该可以进行训练,并且我可以从中获得一些准确性。我不明白问题出在我的代码中。

我正在 Google Colab 上使用 Tensorflow v1.14.0 运行此程序

最佳答案

尽管在任何要求中都没有明确提及,但在 allthree (到目前为止)tutorials ,第一步是安装Tensorflow 2.0:

enter image description here

在相关的blog post中也有一个与您的错误相同的报告。 ,即 resolved通过升级到 Tensorflow 2.0

因此,创建一个安装 TF 2.0 和软件包的环境:

pip install --quiet tensorflow==2.0.0-rc0
pip install --quiet neural-structured-learning

你应该没问题。

关于python - 无效参数错误 : Cannot update variable with shape [] using a Tensor with shape [32],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57823554/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com