我是torch7的新手,我无法找到一种方法来获得基于另一个张量的张量的一些非连续索引。在numpy中,我所做的是以下内容:
array = np.zeros(5) # array = [0 0 0 0 0]
indices = np.array([0, 2, 4])
array[indices] = np.array([1, 2, 3]) # array = [1 0 2 0 3]
有没有办法在火炬7中做类似的事情?类似的东西:
array = torch.zeros(5) -- array = [0 0 0 0 0]
indices = torch.Tensor({1, 3, 5})
array[indices] = torch.Tensor({1, 2, 3}) -- array = [1 0 2 0 3]
谢谢!
答案 0 :(得分:1)
好的,看着周围,我无法找到一个确切的解决方案,但我找到了我想做的事情的近似值,我分享它以防其他人认为它有用:
array = torch.zeros(5) -- array = [0 0 0 0 0]
indices = torch.LongTensor({1, 3, 5}) -- Is important that this is a LongTensor
array:indexAdd(1, indices, torch.Tensor({1, 2, 3})) -- array = [1 0 2 0 3]
答案 1 :(得分:1)
torch.IndexCopy
完全符合您的要求:
using UnityEngine;
using System.Collections;
public class AtackPlayer : MonoBehaviour {
public string playerTag = "Player";
public AnimationClip startAtack;
public AnimationClip endAtack;
public float atackInterval = 2f;
public GameObject enemyBullet;
public float bulletSpeed = 20f;
private Animator _anim;
private Transform _transform;
// Use this for initialization
void Start () {
_anim = GetComponentInParent<Animator>();
_transform = GetComponent<Transform>();
}
private IEnumerator Atack(Vector2 playerPosition)
{
_anim.Play(startAtack.name);
yield return new WaitForSeconds(startAtack.length); // executa o clipe e ataca
GameObject thisBullet = Instantiate(enemyBullet, _transform.position, Quaternion.identity) as GameObject; //instancia o bullet prefab
thisBullet.transform.position = Vector2.Lerp(thisBullet.transform.position, playerPosition, bulletSpeed * Time.deltaTime);
_anim.Play(endAtack.name);
yield return new WaitForSeconds(endAtack.length); // executa o clipe de finalização do ataque
yield return new WaitForSeconds(atackInterval); // executa o clipe de finalização do ataque
}
// Update is called once per frame
void Update () {
}
void OnTriggerEnter2D(Collider2D player)
{
if (player.gameObject.tag == playerTag)
{
Vector2 playerPos = player.transform.position;
StartCoroutine(Atack(playerPos));
}
}
}
答案 2 :(得分:0)
如果您是python用户,也许您也可以尝试https://github.com/imodpasteur/lutorpy。例如,您可以在python中处理数组,然后将其转换为torch tensor。如果你想转换回numpy数组,转换是即时的,因为它只传递指针,python和lua中的两个对象共享相同的内存。
array = np.zeros(5) # array = [0 0 0 0 0]
indices = np.array([0, 2, 4])
array[indices] = np.array([1, 2, 3]) # array = [1 0 2 0 3]
require("torch")
# convert numpy array to torch tensor
tensor = torch.fromNumpyArray(array)
# put your torch code here
#convert back to numpy array
array = tensor.asNumpyArray()
# now array is sharing memory with tensor