示例有以下两个数据集,第一个dataset元素是:
tf.Tensor([ 1. 72. 0. 5.6431 28.199 ], shape=(5,), dtype=float64)
第二个数据集元素是:
tf.Tensor(
[6.6200e-03 3.3800e-05 4.0100e-03 3.1700e-03 1.2040e-02 2.5650e-02
2.3000e-01 1.4380e-02 1.3090e-02 1.6620e-02 4.3140e-02 1.4290e-02
2.1640e+01 4.1888e-01 5.4842e-01 1.6006e-01], shape=(16,), dtype=float64)
tf.Tensor(34.398, shape=(), dtype=float64)
我想得到一个dataset元素,如下所示:
tf.Tensor(
[1. 72. 0. 5.6431 28.199, 6.6200e-03 3.3800e-05 4.0100e-03 3.1700e-03 1.2040e-02 2.5650e-02
2.3000e-01 1.4380e-02 1.3090e-02 1.6620e-02 4.3140e-02 1.4290e-02
2.1640e+01 4.1888e-01 5.4842e-01 1.6006e-01], shape=(21,), dtype=float64)
tf.Tensor(34.398, shape=(), dtype=float64)
我该怎么做呢?非常感谢你的帮助
以下代码生成一个错误:
for featb, feate,label in packed_ds.take(1):
print("featb: ", featb,'\n',"feate: " , feate,'\n')
tf.concat([featb, feate], axis=1)
featb: tf.Tensor([ 1. 72. 0. 5.6431 28.199 ], shape=(5,),
dtype=float64)
feate: tf.Tensor(
[6.6200e-03 3.3800e-05 4.0100e-03 3.1700e-03 1.2040e-02 2.5650e-02
2.3000e-01 1.4380e-02 1.3090e-02 1.6620e-02 4.3140e-02 1.4290e-02
2.1640e+01 4.1888e-01 5.4842e-01 1.6006e-01], shape=(16,), dtype=float64)
---------------------------------------------------------------------------
InvalidArgumentError Traceback (most recent call
last)
<ipython-input-16-3cb13cecc2c2> in <module>()
1 for featb, feate,label in packed_ds.take(1):
2 print("featb: ", featb,'\n',"feate: " , feate,'\n')
----> 3 tf.concat([featb, feate], axis=1)
4 frames
/usr/local/lib/python3.6/dist-packages/six.py in raise_from(value,
from_value)
InvalidArgumentError: ConcatOp : Expected concatenating dimensions in
the range [-1, 1), but got 1 [Op:ConcatV2] name: concat
As I could not add my error as comment, I have edited the question
发布于 2020-09-15 05:27:01
您需要使用tf.concat
函数,可以在以下链接获得更多详细信息:https://www.tensorflow.org/api_docs/python/tf/concat。
此外,请注意连接轴,在您的情况下为0,否则无法连接。
例如resulting_tensor = tf.concat([tensor1,tensor2],axis=0)
作为解释轴为0的另一种解释,是(5,)
和(16,)
等价于(5,1)
,(16,1)
,由于两个张量中的axis = 1
(第二轴)是相同的,所以我们可以在0轴上定义,从而导致(5+16,) = (21,)
最终的张量维数。
https://stackoverflow.com/questions/63895144
复制相似问题