我如何遍历张量并获得所需位置的值?

时间:2020-10-29 17:38:58

标签: python tensorflow

例如,我有那个张量:

Boxes(tensor([[ 138.7087,  670.4597,  194.0305,  788.7614],
    [1744.7915,  597.5836, 1790.3419,  709.9775],
    [ 384.6486,  526.4615,  428.3247,  622.8542],
    [1396.4264,  562.2295, 1444.1472,  653.7578],
    [1135.2161,  504.2900, 1169.5103,  608.7569],
    [1035.7961,  771.2336, 1100.9679,  919.1385],
    [ 696.5236,  419.2245,  738.7255,  503.7422],
    [  63.7905,  362.0703,   93.2846,  439.7708],
    [ 834.4216,  591.6379,  880.6455,  690.0402],
    [1003.2484,  612.4662, 1055.1136,  704.1541],
    [ 852.7735,  330.7743,  879.5329,  396.9597],
    [ 840.9529,  526.4127,  871.9255,  594.8165],
    [ 798.7436,  520.0127,  834.4247,  601.9252],
    [1539.8649,  600.5634, 1576.6362,  679.7695],
    [ 151.1197,  366.5715,  186.4236,  434.6742],
    [ 152.5436,  322.7310,  196.8471,  429.3589],
    [ 164.2602,  322.5941,  195.3645,  386.3824]], device='cuda:0'))

我想获取每一行的所有for值并将其写入不同的变量,这怎么可能?

3 个答案:

答案 0 :(得分:0)

假设import { Config } from '@stencil/core'; export const config: Config = { outputTargets: [ { type: 'www', baseUrl: 'https://somedomain.com/somepath', } ] }; 是您的张量对象。

SELECT pbi.gender, COUNT(*),
       COUNT(*) FILTER (WHERE gender = 'Female') as female,
       COUNT(*) FILTER (WHERE gender = 'Male') as male,
       COUNT(*) FILTER (WHERE pf.dominate_feature = 'Conscientiousness') as Conscientiousness,
       . . .
FROM person_basic_info pbi JOIN
     country_names cn
     ON cn.id = pbi.country_id JOIN
     persons_features pf
     ON pf.person_id = pbi.id
GROUP BY gender

答案 1 :(得分:0)

@Eloi_martins, 使用tf.split()获取张量列表(每行一个):

boxes = tf.constant([[ 138.7087,  670.4597,  194.0305,  788.7614],
    [1744.7915,  597.5836, 1790.3419,  709.9775],
    [ 384.6486,  526.4615,  428.3247,  622.8542],
    [1396.4264,  562.2295, 1444.1472,  653.7578],
    [1135.2161,  504.2900, 1169.5103,  608.7569],
    [1035.7961,  771.2336, 1100.9679,  919.1385]],)

tf.split(boxes, num_or_size_splits=boxes.shape[0], axis = 0)

[<tf.Tensor: shape=(1, 4), dtype=float32, numpy=array([[138.7087, 670.4597, 194.0305, 788.7614]], dtype=float32)>,
 <tf.Tensor: shape=(1, 4), dtype=float32, numpy=array([[1744.7915,  597.5836, 1790.3419,  709.9775]], dtype=float32)>,
 <tf.Tensor: shape=(1, 4), dtype=float32, numpy=array([[384.6486, 526.4615, 428.3247, 622.8542]], dtype=float32)>,
 <tf.Tensor: shape=(1, 4), dtype=float32, numpy=array([[1396.4264,  562.2295, 1444.1472,  653.7578]], dtype=float32)>,
 <tf.Tensor: shape=(1, 4), dtype=float32, numpy=array([[1135.2161,  504.29  , 1169.5103,  608.7569]], dtype=float32)>,
 <tf.Tensor: shape=(1, 4), dtype=float32, numpy=array([[1035.7961,  771.2336, 1100.9679,  919.1385]], dtype=float32)>]

答案 2 :(得分:0)

import tensorflow as tf

a = tf.convert_to_tensor([
    [ 138.7087,  670.4597,  194.0305,  788.7614],
    [1744.7915,  597.5836, 1790.3419,  709.9775],
    [ 384.6486,  526.4615,  428.3247,  622.8542],
    ...
    [ 152.5436,  322.7310,  196.8471,  429.3589],
    [ 164.2602,  322.5941,  195.3645,  386.3824]
])

b = tf.unstack(a, axis=0)
相关问题