pytorch和tensorflow函数对应关系(持续更新)

人工智能75

方法名称大写一般为类,小写为函数,如A,a,使用方法为A()(),a()

作用Pytorchtensorflow

tensor常量troch.tensor()tf.constant()rangetorch.arange()tf.range()求和元素.sum()tf.reduce_sum()随机变量torch.normal()tf.random.normal()矩阵相乘torch.matmul() / @tf.matmul() / @logtorch.log()tf.math.log()最大下标元素.argmax()tf.argmax()转换类型元素.type()tf.cast()zerostorch.zeros()tf.zeros()实现relutorch.max(x, 0)tf.math.maximum(X, 0)

relutorch.relu()tf.nn.relu()sigmoidtorch.sigmoid()tf.nn.sigmoid()tanhtorch.tanh()tf.nn.tanh()张量 reshape元素.reshape( (,) )tf.reshape(元素, (, ))

Crossentropytorch.nn.CrossEntropyLoss(reduction='none')tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)MSEtorch.nn.MSELoss(reduction='none')tf.keras.losses.MeanSquaredError()

SGDtorch.optim.SGD()tf.keras.optimizers.SGD()RMSproptorch.optim.RMSprop()tf.keras.optimizers.RMSprop()Adamtorch.optim.Adam()tf.keras.optimizers.Adam()AdaGradtorch.optim.AdaGrad()tf.keras.optimizers.AdaGrad()

均匀分布torch.nn.init.uniform_()tf.keras.initializers.RandomUniform( )正态分布torch.nn.init.normal_(()tf.keras.initializers.RandomNormal( )常数torch.nn.init.constant_()tf.keras.initializers.Constant( )xavier均匀分布torch.nn.init.xavier_uniform_()tf.keras.initializers.glorot_uniform()xavier正态分布torch.nn.init.xavier_uniform_()tf.keras.initializers.glorot_normal()he 正太分布torch.nn.init.kaiming_uniform_()tf.keras.initializers.he_normal()he 均匀分布torch.nn.init.kaiming_normal_()tf.keras.initializers.he_uniform()

全连接层torch.nn.Linear()tf.keras.layers.Dense()Flattentorch.nn.Flatten()tf.keras.layers.Flatten()Sequentialtorch.nn.Sequential()tf.keras.models.Sequential()Module继承torch.nn.Moduletf.keras.Model记录参数net.train()with tf.GradientTape() as tape:可求导变量torch.normal(...,requires_grad=True) / nn.Parameter()tf.variable()计算梯度loss.backward()grads = tape.gradient(l, params)参数优化updater.step()updater.apply_gradients(zip(grads, params))权重衰减初始化优化器时加上 'weight_decay': wd 键值对增加参数kernel_regularizer=tf.keras.regularizers.l2(wd)参数访问net[i].state_dict()net.layers[2].weights某一个参数访问net[i].biasnet.layers[2].weights[1]

注意:
1、pytorch当中和tensorflow当中的Sequential使用的时候,有 [ ] 的区别:
如下:

pytorch
net = nn.Sequential(nn.Flatten(),
                    nn.Linear(784, 256),
                    nn.ReLU(),
                    nn.Linear(256, 10))

def init_weights(m):
    if type(m) == nn.Linear:
        nn.init.normal_(m.weight, std=0.01)

net.apply(init_weights);
tensorflow
net = tf.keras.models.Sequential([
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(256, activation='relu'),
    tf.keras.layers.Dense(10)])

2、手动进行反向传播的时候的也有很大的差别

pytorch
for x, y in data:
    # 每次更新完成需要手动的将更新器当中的值清零
    optimizer.zero_grad()

    # 自定义的网络 - selfnet
    y = selfnet(x)

    # 自定义的损失函数,或者使用系统自带的
    loss = selfloss(y,y_true)

    # 使用损失值计算梯度
    loss.backward()

    # 使用自定义的优化器或者系统自带的优化器,更新参数,
    # 优化器在定义的时候就已经将网络的参数加了进行,所以显得在这里没有输入参数
    # optimizer= torch.optim.SGD(selfnet.parameters(), lr=lr)
    optimizer.step()
tensorflow
for x, y in data:
    with tf.GradientTape() as tape:
        # 自定义的网络 - selfnet
        y = selfnet(x)
        # 自定义的损失函数,或者使用系统自带的
        loss = selfloss(y,y_true)

    # 将损失值和网络的可更新权重都放入tape中求导
    grads = tape.gradient(loss , selfnet.trainable_variables)

    # 使用自定义的优化器或者系统自带的优化器,更新参数
    optimizer.apply_gradients(zip(grads, net.trainable_variables))

3、查看网络模型

pytorch
1、 # 直接打印网络
print(net)

2、安装pip install torchsummary
from torchsummary import summary
summary(net,input_size=(,))
tensorflow
net.summary()

4、是否要自加 relu 层,

pytorch
dropout1, dropout2 = 0.2, 0.5

class Net(nn.Module):
    def __init__(self, num_inputs, num_outputs, num_hiddens1, num_hiddens2,
                 is_training = True):
        super(Net, self).__init__()
        self.num_inputs = num_inputs
        self.training = is_training
        self.lin1 = nn.Linear(num_inputs, num_hiddens1)
        self.lin2 = nn.Linear(num_hiddens1, num_hiddens2)
        self.lin3 = nn.Linear(num_hiddens2, num_outputs)
        # 不能通过使用关键字的形似来使用直接使用 relu
        # 须添加 relu 层
        self.relu = nn.ReLU()

    def forward(self, X):
        H1 = self.relu(self.lin1(X.reshape((-1, self.num_inputs))))
        # 只有在训练模型时才使用dropout
        if self.training == True:
            # 在第一个全连接层之后添加一个dropout层
            H1 = dropout_layer(H1, dropout1)
        H2 = self.relu(self.lin2(H1))
        if self.training == True:
            # 在第二个全连接层之后添加一个dropout层
            H2 = dropout_layer(H2, dropout2)
        out = self.lin3(H2)
        return out

net = Net(num_inputs, num_outputs, num_hiddens1, num_hiddens2)
tensorflow
dropout1, dropout2 = 0.2, 0.5

class Net(tf.keras.Model):
    def __init__(self, num_outputs, num_hiddens1, num_hiddens2):
        super().__init__()
        self.input_layer = tf.keras.layers.Flatten()
        # 我们可以在每一层中通过添加关键字 activation='relu' 来确定是否使用relu
        self.hidden1 = tf.keras.layers.Dense(num_hiddens1, activation='relu')
        self.hidden2 = tf.keras.layers.Dense(num_hiddens2, activation='relu')
        self.output_layer = tf.keras.layers.Dense(num_outputs)

    def call(self, inputs, training=None):
        x = self.input_layer(inputs)
        x = self.hidden1(x)
        # 只有在训练模型时才使用dropout
        if training:
            # 在第一个全连接层之后添加一个dropout层
            x = dropout_layer(x, dropout1)
        x = self.hidden2(x)
        if training:
            # 在第二个全连接层之后添加一个dropout层
            x = dropout_layer(x, dropout2)
        x = self.output_layer(x)
        return x

net = Net(num_outputs, num_hiddens1, num_hiddens2)

Original: https://blog.csdn.net/To_be_little/article/details/124382699
Author: 浅冲一下
Title: pytorch和tensorflow函数对应关系(持续更新)



相关阅读1

Title: Ubuntu18.04安装安装ROS2-Dashing

1、设置UTF-8编码

sudo locale-gen en_US en_US.UTF-8
sudo update-locale LC_ALL=en_US.UTF-8 LANG=en_US.UTF-8
export LANG=en_US.UTF-8

2、更新软件源

sudo apt update && sudo apt install curl gnupg2 lsb-release
curl http://repo.ros2.org/repos.key | sudo apt-key add -
sudo sh -c 'echo "deb [arch=amd64,arm64] http://packages.ros.org/ros2/ubuntu `lsb_release -cs` main" > /etc/apt/sources.list.d/ros2-latest.list'

3、安装ros2

桌面版 : 包含ROS, RViz, demos, tutorials [推荐]

sudo apt install ros-dashing-desktop

基础版:(包含通讯库、消息包、命令行工具,没有GUI工具)

sudo apt install ros-dashing-ros-base

4、安装自动补全工具

ros2的命令行使用argcomplete工具进行补全:

 sudo apt install python3-argcomplete

添加环境变量

echo "source /opt/ros/dashing/setup.bash" >> ~/.bashrc

如果出现

ROS_DISTRO was set to 'dashing' before. Please make sure that the environment does not mix paths from different distributions.

ROS_DISTRO was set to 'melodic' before . Please make sure that the environment does not mix paths from different distributions.

注释以下文件内的内容

sudo vim /opt/ros/melodic/share/ros_environment/catkin_env_hook/1.ros_distro.sh
sudo vim /opt/ros/dashing/share/ros_environment/environment/1.ros_distro.sh

5、ROS 2和ROS 1之间通过ros-bridge通信

sudo apt update
sudo apt install ros-dashing-ros1-bridge

6、安装ros2编译工具

ROS2软件包编译工具是colcon。ROS编译工具目前经历了三个阶段:1. catkin 2. ament 3. colcon。
使用以下命令进行安装:

sudo apt update && sudo apt install -y \
build-essential \
cmake \
git \
python3-colcon-common-extensions \
python3-pip \
python-rosdep \
python3-vcstool \
wget
# install some pip packages needed for testing
sudo -H python3 -m pip install -U \
argcomplete \
flake8 \
flake8-blind-except \
flake8-builtins \
flake8-class-newline \
flake8-comprehensions \
flake8-deprecated \
flake8-docstrings \
flake8-import-order \
flake8-quotes \
pytest-repeat \
pytest-rerunfailures
# [Ubuntu 16.04] install extra packages not available or recent enough on Xenial
python3 -m pip install -U \
pytest \
pytest-cov \
pytest-runner \
setuptools
# install Fast-RTPS dependencies
sudo apt install --no-install-recommends -y \
libasio-dev \
libtinyxml2-dev

7、测试

创建工作空间,Clone并编译

mkdir  ~/ros2_ws
cd /ros2_ws
git clone https://github.com/zhangrelay/ros2_tutorials
cd ros2_tutorials
colcon build

第一个终端运行

ros2 run turtlesim turtlesim_node

第二个终端运行

ros2 run turtlesim draw_square

Original: https://blog.csdn.net/feileibeng4501/article/details/122810537
Author: Nova555
Title: Ubuntu18.04安装安装ROS2-Dashing

相关阅读2

Title: 汽车螺丝扭力标准

汽车螺丝扭力标准如下:

1、支座与车身螺栓(13MM)--25Nm;

2、支座与车身螺栓(18MM)--40Nm+90度/50Nm;

3、支座与发动机支座螺栓(18Mm)--100Nm;

4、支座与车身螺栓(13MM)--25Nm;

5、支座与车身螺栓(18MM)--40Nm+90度/50Nm;

6、支座与变速器支座螺栓(18MM)--100Nm;

7、摆动支架摆动支架与变速箱螺栓--40Nm+90度/50Nm;

8、摆动支架与副车架螺栓--20Nm+90度/25Nm;

9、发动机部分火花塞--25Nm(1.6/2.0),30Nm(1.8T);

10、放油螺栓--30Nm;

11、机油滤清器--25Nm;

12、曲轴正时轮螺栓--90Nm+90度;

13、凸轮轴正时轮螺栓--65Nm;

14、曲轴轴瓦--65Nm+90度;

15、连杆轴瓦--80Nm+90度;

16、缸盖螺栓第一步--40Nm;

17、缸盖螺栓第二步--90度;

18、缸盖螺栓第三步--90度;

19、爆震传感器--20Nm;

20、双温开关--15Nm;

21、氧传感器--50Nm;

22、三元催化器与排气管连接螺栓--40Nm;

23、排气管双箍螺栓--40Nm;

24、变速箱部分变速箱与发动机连接部分--80Nm;

25、变速箱与启动机连接部分--80Nm;

26、变速箱与油底壳螺丝--40Nm;

27、换档杆壳体与车身--25Nm;

28、换档拉锁支架到变速器连接螺栓--25Nm;

29、变速箱油堵--25Nm;

30、速度表驱动轴--30Nm;

31、变速器壳盖螺栓--10Nm;

32、离合器总泵--25Nm;

33、离合器分泵--25Nm;

34、底盘部分制动踏板与助力器之间螺栓--20Nm;

35、前制动卡钳--35Nm;

36、后制动卡钳--35Nm;

37、转向拉杆与万向节连接--45Nm;

38、方向盘紧固螺母--50Nm;

39、转向十字轴与转向机连接--30Nm;

40、控制臂球头与控制臂连接--20Nm+90度;

41、控制臂球头自锁螺母--450Nm;

42、稳定杆与控制臂连接螺栓--45Nm;

43、稳定杆穿销自锁螺母--45Nm;

44、控制臂与副车架--70Nm+90度;

45、控制臂与车身--100Nm+90度;

46、副车架与转向机--20Nm+90度;

47、前减震器与转向节连接螺栓--65Nm+90度/75Nm;

48、前减震器上部六角螺母--60Nm;

49、后轮轴头自锁螺母--175Nm;

50、后桥支架与后桥连接--80Nm;

51、后桥支架与车身连接--75Nm;

52、后桥与后分泵连接--65Nm;

53、后轮轴头连接--60Nm;

54、后减震器与后桥连接--60Nm;

55、后减震器与车身连接--75Nm;

56、驱动轮法兰连接螺栓--50Nm;

57、轮胎螺栓--120Nm;

Original: https://www.cnblogs.com/LiuYanYGZ/p/16418376.html
Author: LiuYanYGZ
Title: 汽车螺丝扭力标准

相关阅读3

Title: what's surface cc chromium

Goals

Surfaces are a concept to allow graphical embedding of heterogeneous untrusting clients efficiently into one scene.

  • embedding - the core concept of a surface is that it may contain references to surfaces from the same client or different clients
  • heterogeneous - rendering to a surface does not require that a client use a specific library or toolkit, only that it be able to speak the surface protocol
  • untrusting - a client does not have to trust its embedding or embedded clients in order to render into a surface. Having access to a surface from another client does not provide any access to that surface's contents.

  • efficiently - rendering to a surface should have minimal overhead compared to rendering directly to the native screen and should be equally efficient regardless of the embedding depth.

Use cases

In Chromium, we can use surfaces for many of the embedding cases we have today:

  • embedding a blink-rendered tab in the browser's UI
  • embedding a plugin or video within a page
  • embedding an iframe rendered from one process into an iframe rendered in a different process

Concepts

A Surface is a fixed size rectangle that can be rendered into by submitting frames. A client can create a surface by asking the SurfaceManager to construct one for a client (possibly itself) to render into. A Surface can be identified by two IDs generated by the SurfaceManager - one for identifying the surface when issuing frames to render to the surface, one to identify the surface when rendering from it to embed.

A Frame is a series of quads to draw, some of which may reference other surfaces. A frame also contains a set of resources and associated synchronization points. Here's a (rough) outline of the structure of a frame:

  • List of prerequisite sequence numbers for the frame
  • List of resources with associated synchronization points
  • List of passes, each of which contains
  • Transform of the pass
  • Damage and output rects
  • List of quads within the pass, each of which has
    • Transform / rect / clip state / opacity / blend mode / etc (may be shared with other quads)
    • Material - maybe solid color, texture, surface, etc
    • Material-specific properties such as color, texture ID, surface ID

The act of submitting a frame generates an implicit sequence number that can be used to synchronize presentation with other frames, potentially submitted by other clients. A surface identifier + sequence number tuple unique identifies a frame for all clients of an instance of the service (and there will typically only be one surface service in the system).

A Display is a physical display (when Chromium is the operating system) or a native OS window object (when Chromium is running inside another operating system). The surface service provides a surface for each display for a designated client to issue frames to. In the case of Chromium running on windows, for example, the surface service would generate a surface identifier for each top-level HWND and provide them to the browser UI code to render into.

Of particular note is that on Mac we can construct a display wrapping an IOSurface for each tab and let CoreAnimation composite the tabs with the browser UI. This does not provide the bandwidth benefits of ÜberCompositor but it does allow everything outside of the browser process to use the same presentation path as platforms using Aura/ÜberCompositor and reduce a lot of platform-specific complexity in our code.

Processing model

For clients:

Whenever a client wants to update the rendering of a surface it owns, it generates a new frame and submits it to the SurfaceManager. This frame may contain quads that references surfaces being embedded by this client. A client does not have to issue a new frame whenever a surface it embeds updates its rendering. Issuing a frame also transfers ownership of resources (GL textures, software shared memory buffers).

Whenever a client wants to start embedding another client, it first generates an appropriately sized surface through the SurfaceManager and then sends it to the client. The embedding client can start immediately issuing frames referencing the new surface or it may wait to receive an acknowledgement from the embedded client that the surface is ready, depending on the desired application semantics.

Resizing is analogous to creating a new surface.

For the service:

Whenever the manager receives a new frame, it performs some sanity checks on the frame (i.e. making sure it only references frames that the client should be referencing) and then saves it in the surface's potential frame list along. Whenever this frame's prerequisites are satisfied, it is moved into the eligible frame list for the surface. Only one frame can be rendered for a given surface at a time, but a client is allowed to pipeline multiple frames.

Whenever a display is ready for a new frame and something has changed, the service aggregates frames from the surfaces that contribute to that display and then renders them. The aggregation algorithm is simple:

When the service knows that it will never render from a given frame again - for instance if it has started rendering a newer frame for a given surface or if the embedding client has told the manager that it wants to destroy a surface - the service sends an acknowledgement to the client with a set of resources to return to the client along with associated synchronization points.

SurfaceService structure

The SurfaceManager keeps track of all surfaces created in the system. For each surface, it keeps track of:

  • The client that created the surface and will be embedding (rendering from) the surface
  • The client that will be rendering into the surface (may be the same as the creator)
  • List of submitted frames for the given surface

The ResourceProvider keeps track of resources that the service has ownership of and how to return ownership to clients. For GL textures, for instance, this means managing mailboxes.

The DisplayManager keeps track of all displays that the surface service is responsible for rendering into. For each display, the DisplayManager owns a surface used to render into the display as well as some state for hit testing against surfaces and determining which surfaces contributed to the display's last produced frame.

There is only one instance of each of the Manager types in an instance of the service.

A SurfaceAggregator implements the aggregation algorithm and knows how to submit an aggregated frame to a renderer. Aggregators are (nearly) stateless and can be created whenever necessary.

A Renderer translates an aggregated frame into draw commands appropriate for a given display. In GPU rendering mode, this means GL draw calls and a swap into the display. In software rendering mode, this means skia calls into the appropriate SkCanvas.

Synchronization

Resource synchronization is the same as it is with ÜberCompositor, with the slight simplification that the pipeline depth is not influenced by the nesting level of the embedding.

Clients can optionally synchronize frames with each other using the prerequisite / postrequisite synchronization points. This has to be done with care but can be useful to do things like prevent resize guttering. 99% of the use cases in Chromium will not require any explicit synchronization between different surfaces - in nearly all cases it's perfectly fine (and desirable) to let clients render independently of each other.

Here's an example of a possible gutter prevention algorithm. Assume that client Alice is embedding client Bob and wants to resize its surface for Bob from 100x100 to 200x200. If Bob responds fast enough to Alice's resize message, Alice wants to make sure that Bob shows up at 200x200 in the same frame as Alice's decorations.

Start conditions:

Alice is embedding Bob. Alice owns a 100x100 surface that Bob is rendering into. Alice and Bob both have pending frames referencing the 100x100 surface.

Sequence for Alice:

  • Alice decides to resize Bob to 200x200 and change decorations that Alice is rendering.

  • Alice requests a new 200x200 surface from the SurfaceManager

  • Alice sends Bob a resize request and a handle to the new 200x200 surface
  • Alice starts a timeout
  • If Bob responds to the resize message before the timeout:
    • Alice issues the first frame referencing the 200x200 surface with a prerequisite sequence number that it got from Bob
  • If Bob doesn't respond before the timeout:
    • Alice issues a frame referencing the 100x100 surface and appropriate quads to stretch or gutter as appropriate
  • Regardless of when the resize response comes in, Alice issues a destroy call for the 100x100 surface to the SurfaceManager after starting to issues frames referencing the 200x200 surface.

Sequence for Bob:

  • Bob receives a resize message with the new surface identifier
  • Bob issues a new frame appropriate for a 200x200 surface which generates a sequence number for the frame
  • Bob sends a resize response to Alice with this sequence number

End conditions:

Alice and Bob are referencing a 200x200 surface

The SurfaceManager knows that the 100x100 surface can be destroyed as soon as the service no longer needs it.

If Bob is slow to respond, Alice may stall or submit one or more frames that gutter. However if Bob responds fast enough the service can guarantee using the sequence numbers that the new frame from Bob and the new decorations from Alice show up on screen at the same time.

Original: https://www.cnblogs.com/bigben0123/p/15234500.html
Author: Bigben
Title: what's surface cc chromium