Python TensorFlow 2:如何使用散射函数?
我正在努力理解TensorFlow中的散射函数。例如,我想从第二个索引使用Python TensorFlow 2:如何使用散射函数?,python,tensorflow,tensorflow2.0,Python,Tensorflow,Tensorflow2.0,我正在努力理解TensorFlow中的散射函数。例如,我想从第二个索引使用tf.compat.v1.scatter\u subto sub,如下所示: a = tf.Variable(tf.random.uniform(shape=[2])) b = tf.Variable(tf.random.uniform(shape=[3, 2])) WAREagoes: <tf.Variable 'Variable:0' shape=(2,) dtype=float32, numpy=array(
tf.compat.v1.scatter\u sub
to sub,如下所示:
a = tf.Variable(tf.random.uniform(shape=[2]))
b = tf.Variable(tf.random.uniform(shape=[3, 2]))
WAREa
goes:
<tf.Variable 'Variable:0' shape=(2,) dtype=float32, numpy=array([0.62174237, 0.7431344 ], dtype=float32)>
<tf.Variable 'Variable:0' shape=(3, 2) dtype=float32, numpy=
array([[0.01709783, 0.72348535],
[0.48500955, 0.7092271 ],
[0.62199426, 0.26062095]], dtype=float32)>
我想从b
的第二行减去a
,这样我就可以做到:
array([[0.01709783, 0.72348535 ],
[-0.13673282, -0.0339073],
[0.62199426, 0.26062095]], dtype=float32)>
我认为tf.compat.v1.scatter\u sub(b[1],a)
必须工作,但失败了。我试着把转换成,但也失败了。完全错误如下:
InvalidArgumentError Traceback (most recent call last)
<ipython-input-37-90775abdb544> in <module>()
9 print("------------------------")
10
---> 11 tf.compat.v1.scatter_sub(b, [1], a)
3 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/state_ops.py in scatter_sub(ref, indices, updates, use_locking, name)
535 return ref._lazy_read(gen_resource_variable_ops.resource_scatter_sub( # pylint: disable=protected-access
536 ref.handle, indices, ops.convert_to_tensor(updates, ref.dtype),
--> 537 name=name))
538
539
/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/gen_resource_variable_ops.py in resource_scatter_sub(resource, indices, updates, name)
1077 try:
1078 return resource_scatter_sub_eager_fallback(
-> 1079 resource, indices, updates, name=name, ctx=_ctx)
1080 except _core._SymbolicException:
1081 pass # Add nodes to the TensorFlow graph.
/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/gen_resource_variable_ops.py in resource_scatter_sub_eager_fallback(resource, indices, updates, name, ctx)
1095 _attrs = ("dtype", _attr_dtype, "Tindices", _attr_Tindices)
1096 _result = _execute.execute(b"ResourceScatterSub", 0, inputs=_inputs_flat,
-> 1097 attrs=_attrs, ctx=ctx, name=name)
1098 _result = None
1099 return _result
/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/execute.py in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
58 ctx.ensure_initialized()
59 tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
---> 60 inputs, attrs, num_outputs)
61 except core._NotOkStatusException as e:
62 if name is not None:
InvalidArgumentError: Must have updates.shape = indices.shape + params.shape[1:] or updates.shape = [], got updates.shape [2], indices.shape [1], params.shape [3,2] [Op:ResourceScatterSub]
InvalidArgumentError回溯(最近一次调用上次)
在()
9打印(“---------------------------”)
10
--->11 tf.compat.v1.scatter_sub(b[1],a)
3帧
/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/state_ops.py in scatter_sub(参考、索引、更新、使用锁定、名称)
535返回ref._lazy_read(gen_资源_变量_ops.资源_分散_sub(#pylint:disable=受保护访问
536参考手柄,索引,运算转换为张量(更新,参考数据类型),
-->537名称=名称)
538
539
/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/gen\u resource\u variable\u ops.py位于resource\u scatter\u sub(资源、索引、更新、名称)
1077尝试:
1078返回资源\u分散\u子\u紧急\u回退(
->1079资源,索引,更新,name=name,ctx=\u ctx)
1080除核心符号外例外:
1081通过#将节点添加到TensorFlow图。
/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/gen_resource_variable_ops_ops.py在resource_scatter_sub_earge_earge_fallback中(资源、索引、更新、名称、ctx)
1095 _attrs=(“数据类型”、“属性类型”、“小游戏”、“小游戏”)
1096 _result=_execute.execute(b“ResourceScatterSub”,0,inputs=_inputs\u flat,
->1097 attrs=_attrs,ctx=ctx,name=name)
1098_结果=无
1099返回结果
/快速执行中的usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/execute.py(op_name、num_output、input、attrs、ctx、name)
58 ctx.确保_已初始化()
59张量=pywrap\u tfe.tfe\u Py\u Execute(ctx.\u句柄、设备名称、操作名称、,
--->60个输入、属性、数量输出)
61除堆芯外,其他状态除外,如e:
62如果名称不是无:
InvalidArgumentError:必须有updates.shape=index.shape+params.shape[1]或updates.shape=[],获得updates.shape[2],index.shape[1],params.shape[3,2][Op:ResourceScatterSub]
使用此功能的正确方法是什么?您看过这里的文档了吗
Args:
- 指数
张量。必须是以下类型之一:int32、int64。指数张量
- 更新
张量。更新以分散到输出中
- 形状
张量。必须具有与索引相同的类型。1-D.结果张量的形状
- 名称
操作的名称(可选)
我明白了。问题是更新(这里是
a
)假设是一个多个更新的列表,但这里我只给出了向量a
本身,而不是只包含a
的列表
现在我应该将a
扩展一个维度。我的意思是a
现在是[0.62174237,0.7431344]
,我应该把它改成[[0.62174237,0.7431344]
,我可以通过tf来实现
因此,解决方案是:
tf.compat.v1.scatter_sub(b, [1], tf.expand_dims(a, axis=0))
是的,我知道,但我不明白。我想我在这方面有点懒。根据我的理解,tf.compat.v1.scatter\u sub(b[2],a)
应该可以工作。这是tensorflow的实际文档。我不确定b和a的值是什么,但如果您想查看此处的注释,可能会有所帮助: