Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 串联Tensorflow中的空数组_Python_Tensorflow - Fatal编程技术网

Python 串联Tensorflow中的空数组

Python 串联Tensorflow中的空数组,python,tensorflow,Python,Tensorflow,基本上,我的问题和Tensorflow的问题一样 主要的动机是以比使用if语句更漂亮的方式处理初始数组。我当前的伪代码是: E = None for a in A: if E is None: E = a else: E = tf.concat([E, a], axis=0) 这项技术很有效,但我想让它变得更漂亮,也许只使用tf.Tensor。这是一个自定义层的代码,所以我对在模型中工作的代码感兴趣 我想要一个更接近已接受响应的解决方案,初始化E

基本上,我的问题和Tensorflow的问题一样

主要的动机是以比使用if语句更漂亮的方式处理初始数组。我当前的伪代码是:

E = None
for a in A:
    if E is None:
        E = a
    else:
        E = tf.concat([E, a], axis=0)
这项技术很有效,但我想让它变得更漂亮,也许只使用
tf.Tensor
。这是一个自定义层的代码,所以我对在模型中工作的代码感兴趣

我想要一个更接近已接受响应的解决方案,初始化E为:
E=np.array([],dtype=np.int64)。重塑(0,5)

这已经足够接近了,但当我初始化为:

E = tf.zeros([a.shape[0], a.shape[1], 0])
...

我得到一个空的张量,结果只有正确的形状,但没有填充。

在TF2中,您可以简单地使用TensorFlow函数移植numpy解决方案:

>>> xs = tf.constant([[1,2,3,4,5],[10,20,30,40,50]])
>>> ys = tf.reshape(tf.constant([], dtype=tf.int32),(0,5))
>>> ys
<tf.Tensor: shape=(0, 5), dtype=int32, numpy=array([], shape=(0, 5), dtype=int32)>
>>> tf.concat([ys,xs], axis=0)
<tf.Tensor: shape=(2, 5), dtype=int32, numpy=
array([[ 1,  2,  3,  4,  5],
       [10, 20, 30, 40, 50]], dtype=int32)>
xs=tf.常数([1,2,3,4,5],[10,20,30,40,50]) >>>ys=tf.reforme(tf.constant([],dtype=tf.int32),(0,5)) >>>ys >>>tf.concat([ys,xs],轴=0)
在TF2中,您可以简单地使用TensorFlow函数移植numpy解决方案:

>>> xs = tf.constant([[1,2,3,4,5],[10,20,30,40,50]])
>>> ys = tf.reshape(tf.constant([], dtype=tf.int32),(0,5))
>>> ys
<tf.Tensor: shape=(0, 5), dtype=int32, numpy=array([], shape=(0, 5), dtype=int32)>
>>> tf.concat([ys,xs], axis=0)
<tf.Tensor: shape=(2, 5), dtype=int32, numpy=
array([[ 1,  2,  3,  4,  5],
       [10, 20, 30, 40, 50]], dtype=int32)>
xs=tf.常数([1,2,3,4,5],[10,20,30,40,50]) >>>ys=tf.reforme(tf.constant([],dtype=tf.int32),(0,5)) >>>ys >>>tf.concat([ys,xs],轴=0)
我试着这么做,结果得到:
***tensorflow.python.framework.errors\u impl.InvalidArgumentError:ConcatOp:输入的维度应该匹配:shape[0]=[0,1,2,3]vs.shape[1]=[1,1,2,3][Op:ConcatV2 name:concat
。我的E是
,a是
,我无法复制。您确定要沿正确的
轴连接吗?Ups,您是对的!我用的是另一个轴!感谢我尝试这么做,我得到:
***tensorflow.python.framework.errors\u impl.invalidargumeinterror:ConcatOp:输入的维度应该匹配:shape[0]=[0,1,2,3]vs.shape[1]=[1,1,2,3][Op:ConcatV2 name:concat
。我的E是
,a是
,我无法复制。您确定要沿正确的
轴连接吗?Ups,您是对的!我用的是另一个轴!谢谢