将张量变换为矢量

时间:2018-05-02 14:09:40

标签: python numpy tensorflow neural-network keras

我有一个形状的张量:

2 x 2 x 3 x 2

即:{玩家,回合,加注次数,采取的行动}

这是一场双人游戏,在两轮之后结束,最多可以加注3次,每次加注都包含有关采取行动的信息。

我想创建一个代表游戏每一步的数组。所以每个轮次都有关于每次加注的每个球员动作的信息。

有没有可能做这样的事情:

players = array[0, 1]
rounds = array[2, 3]
raises = array[4, 5, 6]
actions = array[7, 8]

niceAndHandyArray = someFunction(players, rounds, raises, actions)
# Which outputs something like this:
[
    [0, [
            2, [
                   4, [7, 8]
                ],
            ...
        ],
    [1, 
         ... 
    ]
]

# And then access it like:
niceAndHandyAcrray[player_index][round_index][raise_index][action_index] = 1

# And pass it to my neural network later on as:
niceAndHandyArray.flatten()

1 个答案:

答案 0 :(得分:0)

尝试使用reshape

#Example:
# tensor 't' is [[[1, 1, 1],
#                 [2, 2, 2]],
#                [[3, 3, 3],
#                 [4, 4, 4]],
#                [[5, 5, 5],
#                 [6, 6, 6]]]
# tensor 't' has shape [3, 2, 3]
# pass '[-1]' to flatten 't'
tf.reshape(t, [-1])
#output: [1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6]