首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >InvalidArgumentError:无法使用shape [32]的张量更新shape []的变量

InvalidArgumentError:无法使用shape [32]的张量更新shape []的变量
EN

Stack Overflow用户
提问于 2019-09-06 22:04:19
回答 1查看 1.2K关注 0票数 1

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

我试着压缩维度,我尝试了不同版本的tensorflow -我对tensorflow还很陌生,所以在这一点上我真的是在猜测。

代码语言:javascript
复制
# 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}}]]

这个模型应该是训练的,我从它得到了一些准确性。我不明白问题是从我的代码中哪里来的。

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-09-06 23:28:24

尽管在任何要求中都没有明确提到,但在all three (到目前为止) tutorials中,第一步是安装TensorFlow2.0:

在相关的blog post中也有一个与您相同的错误报告,该错误是通过升级到TensorFlow2.0而被resolved

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

代码语言:javascript
复制
pip install --quiet tensorflow==2.0.0-rc0
pip install --quiet neural-structured-learning

你应该会好起来的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57823554

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档