一次迭代两个Pytorch张量?

时间:2019-04-03 03:30:49

标签: python pytorch tensor

我有两个t1t2的Pytorch张量(实际上只是一维列表)。是否可以并行遍历它们,即做类似

的操作

for a,b in zip(t1,t2)

谢谢。

4 个答案:

答案 0 :(得分:3)

对于我来说(Python版本3.7.3和PyTorch版本1.0.0),zip函数可与PyTorch张量正常工作:

>>> import torch
>>> t1 = torch.ones(3)
>>> t2 = torch.zeros(3)
>>> list(zip(t1, t2))
[(tensor(1.), tensor(0.)), (tensor(1.), tensor(0.)), (tensor(1.), tensor(0.))]

只需要list调用即可显示结果。遍历zip正常工作。

答案 1 :(得分:1)

torch.cat(dim=1) concatenate来使它们更有意义;然后,您可以遍历新的张量。

答案 2 :(得分:0)

您可以尝试: export class AuthService { private subscription: Subscription = new Subscription(); user: Observable<firebase.User>; private currentUserSubject: BehaviorSubject<User | null>; authState: any = null; constructor(private firebaseAuth: AngularFireAuth, private router: Router, private userService:UserService) { this.user = firebaseAuth.authState; this.subscription.add(this.firebaseAuth.authState.subscribe((auth) => { this.authState = auth; //getting the user object if he already signed-in this.currentUserSubject = new BehaviorSubject<User>(localStorage.getItem('currentUser') | null); this.currentUser = this.currentUserSubject.asObservable(); })) signin(email: string, password: string) { return new Promise<any>((resolve, reject) => { // --- login with email and password in firebase this.firebaseAuth.auth.signInWithEmailAndPassword(email, password) .then( res => { // --- Once login is successful, get the user object form the backend this.subscription =this.userService.findByUsername(res.user.uid).subscribe(data=>{ this.currentUserSubject.next(data); localStorage.setItem("currentUser", JSON.stringify(data)); resolve(this.firebaseAuth.auth.currentUser); }) } , err => {reject(err); } ).catch(err => { console.log('Something went wrong:', err.message); }); }); } async logout() { await this.firebaseAuth.auth.signOut().then(()=>{ this.currentUserSubject.next(null); this.currentUserSubject.complete(); this.subscription.unsubscribe() localStorage.removeItem('currentUser'); this.router.navigate(['/signin']); }) } }
有关详细信息,请参见pytoch documentation

答案 3 :(得分:0)

PyTorch 中的张量压缩成一个toString()Date.toString()

一起使用

示例

torch.stack