亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Tensorflow1 concat 二維張量與所有按行排列

Tensorflow1 concat 二維張量與所有按行排列

嗶嗶one 2023-06-20 15:50:41
假設我們有兩個形狀為 ( n, k) 和 ( n, k) 的二維張量。我們想要將兩個張量與所有行方向排列連接起來,使得所得張量的形狀為 ( n, n, 2*k)。例子,A = [[a, b], [c, d]]; B = [[e, f], [g, h]]得到的張量應該是:[[[a, b, e, f], [a, b, g, h]], [[c, d, e, f], [c, d, g, h]]]假設輸入張量 A 和 B 具有非靜態形狀,因此我們不能使用 for 循環索引tf.shape()值。任何幫助表示贊賞。非常感謝。
查看完整描述

2 回答

?
12345678_0001

TA貢獻1802條經驗 獲得超5個贊

tf.concattf.repeat和一起使用tf.tile

import tensorflow as tf

import numpy as np


# Input

A = tf.convert_to_tensor(np.array([['a', 'b'], ['c', 'd']]))

B = tf.convert_to_tensor(np.array([['e', 'f'], ['g', 'h']]))


# Repeat, tile and concat

C = tf.concat([tf.repeat(A, repeats=tf.shape(A)[-1], axis=0),?

? ? ? ? ? ? ? ?tf.tile(B, multiples=[tf.shape(A)[0], 1])],?

? ? ? ? ? ? ? axis=-1)


# Reshape to requested shape

C = tf.reshape(C, [tf.shape(A)[0], tf.shape(A)[0], -1])


print(C)


>>> <tf.Tensor: shape=(2, 2, 4), dtype=string, numpy=

>>> array([[[b'a', b'b', b'e', b'f'],

>>>? ? ? ? ?[b'a', b'b', b'g', b'h']],

>>>? ? ? ? [[b'c', b'd', b'e', b'f'],

>>>? ? ? ? ?[b'c', b'd', b'g', b'h']]], dtype=object)>


查看完整回答
反對 回復 2023-06-20
?
拉丁的傳說

TA貢獻1789條經驗 獲得超8個贊

試試這樣:


A = np.array([['a', 'b'], ['c', 'd']])

B = np.array([['e', 'f'], ['g', 'h']])


C = np.array([np.concatenate((a, b), axis=0) for a in A for b in B])


您可以像這樣輕松地將其轉換為張量流


data =tf.convert_to_tensor(C, dtype=tf.string)

輸出:


<tf.Tensor: shape=(4, 4), dtype=string, numpy=

array([[b'a', b'b', b'e', b'f'],

       [b'a', b'b', b'g', b'h'],

       [b'c', b'd', b'e', b'f'],

       [b'c', b'd', b'g', b'h']], dtype=object)>

不確定列表理解部分是否對大數據最有效


查看完整回答
反對 回復 2023-06-20
  • 2 回答
  • 0 關注
  • 160 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號