为什么张量流可能要指定动态尺寸

时间:2019-03-13 15:35:11

标签: python tensorflow

我有一个现有的复杂模型。里面有张量Button button = (Button)v.findViewById(R.id.elemzes); elemzes.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Fragment jegyekAllando = new jegyekAllando(); // Get a bundle Bundle bundle = new Bundle(); bundle.putString("test", teszt); // Set as argument jegyekAllando.setArguments(bundle); // Begin the transaction FragmentTransaction FragTan2 = getFragmentManager().beginTransaction(); FragTan2.replace(R.id.fragment_container_lista,jegyekAllando); FragTan2.commit(); } }); ,其形状为(无,128、128、3)。第一条轴具有动态形状,当批次传递到x中的feed_dict时应实现。但是,当我尝试将广播操作定义为x的形状时:

session.run

引发异常:

y = tf.broadcast_to(z, (x.shape[0], x.shape[1], x.shape[2], 1))

在创建模型时(而不是在运行模型时)会发生异常。将第一个元素转换为数字会有所帮助,但这不是解决方案。

1 个答案:

答案 0 :(得分:1)

.shape属性为您提供图形构造时已知的形状,它是tf.TensorShape结构。如果x的形状是众所周知的,则可以使代码按以下方式工作:

y = tf.broadcast_to(z, (x.shape[0].value, x.shape[1].value, x.shape[2].value, 1))

但是,在您的情况下,x的第一维未知。为了将实际的张量形状用作常规的tf.Tensor(其值仅在运行时已知),可以使用tf.shape

x_shape = tf.shape(x)
y = tf.broadcast_to(z, (x_shape[0], x_shape[1], x_shape[2], 1))
相关问题