不使用tf.RaggedTensor从tensorflow中的张量中删除某些行
给定张量数据不使用tf.RaggedTensor从tensorflow中的张量中删除某些行,tensorflow,select,conditional-statements,where-clause,mask,Tensorflow,Select,Conditional Statements,Where Clause,Mask,给定张量数据 [[[ 0., 0.], [ 1., 1.], [-1., -1.]], [[-1., -1.], [ 4., 4.], [ 5., 5.]]] 我要删除[-1,-1]并获取 [[[ 0., 0.], [ 1., 1.]], [[ 4., 4.], [ 5., 5.]]] 如何在不使用tensorflow中的参差不齐功能的情况下实现上述功能?您可以这样做: 将tensorflow导入为t
[[[ 0., 0.],
[ 1., 1.],
[-1., -1.]],
[[-1., -1.],
[ 4., 4.],
[ 5., 5.]]]
我要删除[-1,-1]并获取
[[[ 0., 0.],
[ 1., 1.]],
[[ 4., 4.],
[ 5., 5.]]]
如何在不使用tensorflow中的参差不齐功能的情况下实现上述功能?您可以这样做:
将tensorflow导入为tf
将numpy作为np导入
数据=[[0,0.],
[ 1., 1.],
[-1., -1.]],
[[-1., -1.],
[ 4., 4.],
[ 5., 5.]]]
数据=tf常数(数据)
指数=tf.math.not_equal(数据,tf.constant([-1.,-1.]))
res=数据[指数]
shape=tf.shape(数据)
总计=tf.reduce\u和(
cast(tf.math.logical_和(索引[:,:,0],索引[:,:,1])[0],tf.int32))
res=tf.重塑(res,(形状[0],总计,形状[-1]))
使用tf.Session()作为sess:
打印(sess.run(res))
# [[[0. 0.]
# [1. 1.]]
# [[4. 4.]
# [5. 5.]]]
您可以这样做:
将tensorflow导入为tf
将numpy作为np导入
数据=[[0,0.],
[ 1., 1.],
[-1., -1.]],
[[-1., -1.],
[ 4., 4.],
[ 5., 5.]]]
数据=tf常数(数据)
指数=tf.math.not_equal(数据,tf.constant([-1.,-1.]))
res=数据[指数]
shape=tf.shape(数据)
总计=tf.reduce\u和(
cast(tf.math.logical_和(索引[:,:,0],索引[:,:,1])[0],tf.int32))
res=tf.重塑(res,(形状[0],总计,形状[-1]))
使用tf.Session()作为sess:
打印(sess.run(res))
# [[[0. 0.]
# [1. 1.]]
# [[4. 4.]
# [5. 5.]]]
您可以尝试以下方法:
x = tf.constant(
[[[ 0., 0.],
[ 1., 1.],
[-1., -2.]],
[[-1., -2.],
[ 4., 4.],
[ 5., 5.]]])
mask = tf.math.not_equal(x, np.array([-1, -1]))
result = tf.boolean_mask(x, mask)
shape = tf.shape(x)
result = tf.reshape(result, (shape[0], -1, shape[2]))
您可以尝试以下方法:
x = tf.constant(
[[[ 0., 0.],
[ 1., 1.],
[-1., -2.]],
[[-1., -2.],
[ 4., 4.],
[ 5., 5.]]])
mask = tf.math.not_equal(x, np.array([-1, -1]))
result = tf.boolean_mask(x, mask)
shape = tf.shape(x)
result = tf.reshape(result, (shape[0], -1, shape[2]))