我正在使用 tensorflow v2.7.0 并嘗試使用不規則張量創建 ML 模型。
問題是 tf.linalg.diag、tf.matmul 和 tf.linalg.det 不能使用參差不齊的張量。我找到了一種解決方法,將 numpy 中的參差不齊的張量轉換回參差不齊的張量,但是在全域模型中應用該層時它不起作用。
以下代碼正在作業
import tensorflow as tf
class LRDet(tf.keras.layers.Layer):
def __init__(self,numItems,rank=10):
super(LRDet,self).__init__()
self.numItems = numItems
self.rank = rank
def build(self,input_shape):
V_init = tf.random_normal_initializer(mean=0.0,stddev=0.01)
D_init = tf.random_normal_initializer(mean=1.0,stddev=0.01)
self.V = tf.Variable(name='V',initial_value=V_init(shape=(self.numItems, self.rank)),trainable=True)
self.D = tf.Variable(name='D',initial_value=D_init(shape=(self.numItems,)),trainable=True)
def call(self,inputs):
batch_size = inputs.nrows()
subV = tf.gather(self.V,inputs)
subD = tf.square(tf.gather(self.D,inputs,batch_dims=0))#tf.linalg.diag(tf.square(tf.gather(D,Xrag,batch_dims=0)))
subD = tf.ragged.constant([tf.linalg.diag(subD[i]).numpy() for i in tf.range(batch_size)])
K = tf.ragged.constant([tf.matmul(subV[i],subV[i],transpose_b=True).numpy() for i in tf.range(batch_size)])
K = tf.add(K,subD)
res = tf.ragged.constant([tf.linalg.det(K[i].to_tensor()).numpy() for i in tf.range(batch_size)])
return res
numItems = 10
rank = 3
detX = LRDet(numItems,rank)
X = [[1,2],[3],[4,5,6]]
Xrag = tf.ragged.constant(X)
_ = detX(Xrag)
但是一旦我在更全域的模型中使用了這一層,我就會出現以下錯誤
OperatorNotAllowedInGraphError:呼叫層“lr_det_10”(LRDet 型別)時遇到例外。
in user code: File "<ipython-input-57-6b073a14386e>", line 18, in call * subD = tf.ragged.constant([tf.linalg.diag(subD[i]).numpy() for i in tf.range(batch_size)]) OperatorNotAllowedInGraphError: iterating over `tf.Tensor` is not allowed: AutoGraph did convert this function. This might indicate you are trying to use an unsupported feature.
我嘗試使用 tf.map_fn 而不是 .numpy() 的串列理解,但沒有成功。
任何幫助將不勝感激。
謝謝 !
uj5u.com熱心網友回復:
這是一個運行的選項tf.map_fn;然而,由于最近關于,參差不齊的張量和 GPU 的錯誤,它目前僅在 CPU 上運行:tf.map_fn
import os
os.environ["CUDA_VISIBLE_DEVICES"] = "-1" # do not access GPU
import tensorflow as tf
class LRDet(tf.keras.layers.Layer):
def __init__(self,numItems,rank=10):
super(LRDet,self).__init__()
self.numItems = numItems
self.rank = rank
def build(self,input_shape):
V_init = tf.random_normal_initializer(mean=0.0,stddev=0.01)
D_init = tf.random_normal_initializer(mean=1.0,stddev=0.01)
self.V = tf.Variable(name='V',initial_value=V_init(shape=(self.numItems, self.rank)),trainable=True)
self.D = tf.Variable(name='D',initial_value=D_init(shape=(self.numItems,)),trainable=True)
def call(self,inputs):
batch_size = inputs.nrows()
subV = tf.gather(self.V,inputs)
subD = tf.square(tf.gather(self.D, inputs, batch_dims=0))
subD = tf.map_fn(self.diag, subD, fn_output_signature=tf.RaggedTensorSpec(shape=[1, None, None],
dtype=tf.type_spec_from_value(subD).dtype,
ragged_rank=2,
row_splits_dtype=tf.type_spec_from_value(subD).row_splits_dtype))
subD = tf.squeeze(subD, 1)
K = tf.map_fn(self.matmul, subV, fn_output_signature=tf.RaggedTensorSpec(shape=[1, None, None],
dtype=tf.type_spec_from_value(subV).dtype,
ragged_rank=2,
row_splits_dtype=tf.type_spec_from_value(subV).row_splits_dtype))
K = tf.squeeze(K, 1)
K = tf.add(K,subD)
res = tf.map_fn(self.det, K, tf.TensorSpec(shape=(), dtype=tf.float32, name=None))
return res
def diag(self, x):
return tf.ragged.stack(tf.linalg.diag(x))
def matmul(self, x):
return tf.ragged.stack(tf.matmul(x, x,transpose_b=True))
def det(self, x):
return tf.linalg.det(x.to_tensor())
numItems = 10
rank = 3
input = tf.keras.layers.Input(shape=(None,), ragged=True, dtype=tf.int32)
detX = LRDet(numItems,rank)
output = detX(input)
model = tf.keras.Model(input, output)
X = [[1,2],[3],[4,5,6]]
Xrag = tf.ragged.constant(X)
y = tf.random.normal((3, 1))
model.compile(loss='mse', optimizer='adam')
model.fit(Xrag, y, batch_size=1, epochs=1)
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/388270.html
