当前位置:网站首页>tensorflow转pytorch笔记;tf.gather_nd(x,y)转pytorch
tensorflow转pytorch笔记;tf.gather_nd(x,y)转pytorch
2022-07-03 02:39:00 【strawberry47】
记录了将tensorflow转pytorch时,一些常用的函数转换:
不能直接转换
tf.transpose(input,[1, 0, 2])->input.permute([1, 0, 2])不能直接换成torch.transpose,因为操作不了多维tf.expand_dims(input), axis=1)->input.unsqueeze(1)tf.concat([content1,content2], axis=1->torch.cat((content1,content2), dim=1)记得把axis换成dimtf.tile(input, [2, 1])->input.repeat([2, 1])tf.range(10)->torch.arange(0)tf.reduce_sum(x, axis=1, keep_dims=True)->torch.sum(x,dim=1,keepdim=True)tf.clip_by_value(x, min, max)->torch.clamp(x, min, max)tf.multinomial(logits=a, num_samples=1)->torch.multinomial(input=a, num_samples=1, replacement=False)tf.equal(x, y)->torch.eq(x, y)tf.nn.embedding_lookup(W_fe, Feature_input + 1)->torch.index_select(W_fe, 0, Feature_input + 1)tf.one_hot()->functional.one_hot()
tf.gather_nd(x,y)转换
def gather_nd(self,params, indices):
''' 4D example params: tensor shaped [n_1, n_2, n_3, n_4] --> 4 dimensional indices: tensor shaped [m_1, m_2, m_3, m_4, 4] --> multidimensional list of 4D indices returns: tensor shaped [m_1, m_2, m_3, m_4] ND_example params: tensor shaped [n_1, ..., n_p] --> d-dimensional tensor indices: tensor shaped [m_1, ..., m_i, d] --> multidimensional list of d-dimensional indices returns: tensor shaped [m_1, ..., m_1] '''
out_shape = indices.shape[:-1]
indices = indices.unsqueeze(0).transpose(0, -1) # roll last axis to fring
ndim = indices.shape[0]
indices = indices.long()
idx = torch.zeros_like(indices[0], device=indices.device).long()
m = 1
for i in range(ndim)[::-1]:
idx += indices[i] * m
m *= params.size(i)
out = torch.take(params, idx)
return out.view(out_shape)
可以直接转换
tf.reshape()->torch.reshape()tf.log()tf.squeeze
边栏推荐
- GBase 8c 函数/存储过程参数(一)
- javeScript 0.1 + 0.2 == 0.3的问题
- Xiaodi notes
- Monitoring and management of JVM
- GBase 8c系统表-pg_attribute
- Awk from entry to burial (1) awk first meeting
- Tongda OA homepage portal workbench
- sql server 查询指定表的表结构
- Thread safe singleton mode
- Mathematical statistics -- Sampling and sampling distribution
猜你喜欢

A2L file parsing based on CAN bus (2)

Your family must be very poor if you fight like this!
![[shutter] banner carousel component (shutter_wiper plug-in | swiper component)](/img/a6/5c97ef3f34702b83ebf0511501d757.gif)
[shutter] banner carousel component (shutter_wiper plug-in | swiper component)

【Flutter】shared_ Preferences local storage (introduction | install the shared_preferences plug-in | use the shared_preferences process)

SQL statement
![[translation] modern application load balancing with centralized control plane](/img/b0/22e9bf098d580b2af67255ddcdc0d5.jpg)
[translation] modern application load balancing with centralized control plane

Choose it when you decide

Oauth2.0 authentication, login and access "/oauth/token", how to get the value of request header authorization (basictoken)???

UDP receive queue and multiple initialization test

Return a tree structure data
随机推荐
JS的装箱和拆箱
easyExcel
Gbase 8C trigger (II)
4. Classes and objects
面试项目技术栈总结
UDP receive queue and multiple initialization test
Matlab tips (24) RBF, GRNN, PNN neural network
[tutorial] chrome turns off cross domain policies CORS and samesite, and brings cookies across domains
[shutter] bottom navigation bar page frame (bottomnavigationbar bottom navigation bar | pageview sliding page | bottom navigation and sliding page associated operation)
Face recognition 6-face_ recognition_ Py based on OpenCV, face detection and real-time tracking using Haar cascade and Dlib Library
Check log4j problems using stain analysis
[flutter] example of asynchronous programming code between future and futurebuilder (futurebuilder constructor setting | handling flutter Chinese garbled | complete code example)
Your family must be very poor if you fight like this!
【ROS进阶篇】第六讲 ROS中的录制与回放(rosbag)
线程安全的单例模式
Cancer biopsy instruments and kits - market status and future development trends
Javescript 0.1 + 0.2 = = 0.3 problem
Practice of traffic recording and playback in vivo
A2L file parsing based on CAN bus (2)
Hcip137-147 title + analysis