第二章:Tensor

dhadiudhj
This commit is contained in:
wyy4github
2019-05-20 10:50:51 +08:00
parent a95c687963
commit 110491140f
5 changed files with 87 additions and 73 deletions

75
.idea/workspace.xml generated
View File

@ -2,17 +2,10 @@
<project version="4">
<component name="ChangeListManager">
<list default="true" id="dba69579-024f-442a-b5bf-b31a92577c82" name="Default Changelist" comment="">
<change afterPath="$PROJECT_DIR$/Image/9.PNG" afterDir="false" />
<change afterPath="$PROJECT_DIR$/Image/输出01.PNG" afterDir="false" />
<change afterPath="$PROJECT_DIR$/Image/输出02.PNG" afterDir="false" />
<change afterPath="$PROJECT_DIR$/Image/输出03.PNG" afterDir="false" />
<change afterPath="$PROJECT_DIR$/Image/输出04.PNG" afterDir="false" />
<change afterPath="$PROJECT_DIR$/Image/输出05.PNG" afterDir="false" />
<change afterPath="$PROJECT_DIR$/Image/输出06.PNG" afterDir="false" />
<change afterPath="$PROJECT_DIR$/Image/输出07.PNG" afterDir="false" />
<change afterPath="$PROJECT_DIR$/Image/输出08.PNG" afterDir="false" />
<change afterPath="$PROJECT_DIR$/Image/输出09.PNG" afterDir="false" />
<change afterPath="$PROJECT_DIR$/Image/输出10.PNG" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/hello.md" beforeDir="false" afterPath="$PROJECT_DIR$/Tensor.md" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Tensor.md" beforeDir="false" afterPath="$PROJECT_DIR$/Tensor.md" afterDir="false" />
<change beforePath="$PROJECT_DIR$/tensorTest.py" beforeDir="false" afterPath="$PROJECT_DIR$/tensorTest.py" afterDir="false" />
</list>
<option name="EXCLUDED_CONVERTED_TO_IGNORED" value="true" />
@ -26,8 +19,8 @@
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/tensorTest.py">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="270">
<caret line="27" column="25" lean-forward="true" selection-start-line="27" selection-start-column="25" selection-end-line="27" selection-end-column="25" />
<state relative-caret-position="277">
<caret line="38" column="15" selection-start-line="38" selection-start-column="15" selection-end-line="38" selection-end-column="15" />
<folding>
<element signature="e#0#37#0" expanded="true" />
</folding>
@ -39,19 +32,14 @@
<entry file="file://$PROJECT_DIR$/Tensor.md">
<provider selected="true" editor-type-id="split-provider[text-editor;markdown-preview-editor]">
<state split_layout="SPLIT">
<first_editor relative-caret-position="180">
<caret line="111" column="6" selection-start-line="111" selection-start-column="6" selection-end-line="111" selection-end-column="6" />
<first_editor relative-caret-position="-1359">
<caret line="24" selection-start-line="24" selection-end-line="24" />
</first_editor>
<second_editor />
</state>
</provider>
</entry>
</file>
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/Image/输出08.PNG">
<provider selected="true" editor-type-id="images" />
</entry>
</file>
</leaf>
</component>
<component name="FileTemplateManagerImpl">
@ -78,10 +66,10 @@
</list>
</option>
</component>
<component name="ProjectFrameBounds" extendedState="6">
<option name="x" value="256" />
<option name="y" value="106" />
<option name="width" value="1565" />
<component name="ProjectFrameBounds" extendedState="7">
<option name="x" value="141" />
<option name="y" value="151" />
<option name="width" value="1150" />
<option name="height" value="937" />
</component>
<component name="ProjectView">
@ -89,6 +77,7 @@
<foldersAlwaysOnTop value="true" />
</navigator>
<panes>
<pane id="Scope" />
<pane id="ProjectPane">
<subPane>
<expand>
@ -110,7 +99,6 @@
<select />
</subPane>
</pane>
<pane id="Scope" />
</panes>
</component>
<component name="PropertiesComponent">
@ -129,13 +117,13 @@
</option>
</component>
<component name="RecentsManager">
<key name="CopyFile.RECENT_KEYS">
<recent name="G:\github clone project\PyTorchDocs\Image" />
</key>
<key name="MoveFile.RECENT_KEYS">
<recent name="G:\github clone project\PyTorchDocs\Image" />
<recent name="G:\github clone project\PyTorchDocs\FirstSection" />
</key>
<key name="CopyFile.RECENT_KEYS">
<recent name="G:\github clone project\PyTorchDocs\Image" />
</key>
</component>
<component name="RunDashboard">
<option name="ruleStates">
@ -190,14 +178,15 @@
<option name="totallyTimeSpent" value="12000" />
</component>
<component name="ToolWindowManager">
<frame x="-8" y="-8" width="1936" height="1056" extended-state="7" />
<frame x="-8" y="-8" width="1936" height="1056" extended-state="6" />
<editor active="true" />
<layout>
<window_info active="true" content_ui="combo" id="Project" order="0" visible="true" weight="0.077976815" />
<window_info content_ui="combo" id="Project" order="0" visible="true" weight="0.1369863" />
<window_info id="Structure" order="1" side_tool="true" weight="0.25" />
<window_info id="Favorites" order="2" side_tool="true" />
<window_info anchor="bottom" id="Message" order="0" />
<window_info anchor="bottom" id="Find" order="1" />
<window_info anchor="bottom" id="Run" order="2" visible="true" weight="0.51895994" />
<window_info active="true" anchor="bottom" id="Run" order="2" visible="true" weight="0.45178765" />
<window_info anchor="bottom" id="Debug" order="3" weight="0.4" />
<window_info anchor="bottom" id="Cvs" order="4" weight="0.25" />
<window_info anchor="bottom" id="Inspection" order="5" weight="0.4" />
@ -262,31 +251,35 @@
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/Image/输出1.PNG">
<provider selected="true" editor-type-id="images" />
</entry>
<entry file="file://$PROJECT_DIR$/Image/输出1.PNG" />
<entry file="file://$PROJECT_DIR$/Image/输出05.PNG">
<provider selected="true" editor-type-id="images" />
</entry>
<entry file="file://$PROJECT_DIR$/Image/输出04.PNG">
<provider selected="true" editor-type-id="images" />
</entry>
<entry file="file://$PROJECT_DIR$/Image/输出03.PNG">
<provider selected="true" editor-type-id="images" />
</entry>
<entry file="file://$PROJECT_DIR$/Image/8.PNG">
<provider selected="true" editor-type-id="images" />
</entry>
<entry file="file://$PROJECT_DIR$/Image/输出08.PNG">
<provider selected="true" editor-type-id="images" />
</entry>
<entry file="file://$PROJECT_DIR$/Image/输出01.PNG">
<provider selected="true" editor-type-id="images" />
</entry>
<entry file="file://$PROJECT_DIR$/Image/输出02.PNG">
<provider selected="true" editor-type-id="images" />
</entry>
<entry file="file://$PROJECT_DIR$/Image/输出03.PNG">
<provider selected="true" editor-type-id="images" />
</entry>
<entry file="file://$PROJECT_DIR$/Image/输出08.PNG">
<entry file="file://$PROJECT_DIR$/Image/输出07.PNG">
<provider selected="true" editor-type-id="images" />
</entry>
<entry file="file://$PROJECT_DIR$/tensorTest.py">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="270">
<caret line="27" column="25" lean-forward="true" selection-start-line="27" selection-start-column="25" selection-end-line="27" selection-end-column="25" />
<state relative-caret-position="277">
<caret line="38" column="15" selection-start-line="38" selection-start-column="15" selection-end-line="38" selection-end-column="15" />
<folding>
<element signature="e#0#37#0" expanded="true" />
</folding>
@ -296,8 +289,8 @@
<entry file="file://$PROJECT_DIR$/Tensor.md">
<provider selected="true" editor-type-id="split-provider[text-editor;markdown-preview-editor]">
<state split_layout="SPLIT">
<first_editor relative-caret-position="180">
<caret line="111" column="6" selection-start-line="111" selection-start-column="6" selection-end-line="111" selection-end-column="6" />
<first_editor relative-caret-position="-1359">
<caret line="24" selection-start-line="24" selection-end-line="24" />
</first_editor>
<second_editor />
</state>

BIN
Image/输出09.PNG Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.0 KiB

BIN
Image/输出10.PNG Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.4 KiB

View File

@ -22,7 +22,6 @@ x = torch.empty(5, 3)
print(x)
```
输出结果:
![](Image/输出01.PNG)
<h4>2.构建随机初始化矩阵
@ -31,7 +30,6 @@ x = torch.rand(5, 3)
print(x)
```
输出结果:
![](Image/输出02.PNG)
<h4>3.构造一个long类型的矩阵且全为0
@ -40,7 +38,6 @@ x = torch.zeros(5, 3, dtype=torch.long)
print(x)
```
输出结果:
![](Image/输出03.PNG)
<h4>4.创建张量,并自定义数据
@ -49,7 +46,6 @@ x = torch.tensor([5.5, 3])
print(x)
```
输出结果:
![](Image/输出04.PNG)
<h4>5.基于已有的tensor创建新的tensor
@ -63,7 +59,6 @@ print(x)
# 矩阵的大小相同
```
输出结果:
![](Image/输出05.PNG)
<h4>6.获取张量的维度
@ -71,7 +66,6 @@ print(x)
print(x.size())
```
输出结果:
![](Image/输出06.PNG)
<h4>7.张量的加法运算
@ -97,7 +91,6 @@ y.add_(x)
print(y)
```
输出结果:
![](Image/输出07.PNG)
注意:任何使张量发生变化的操作都有一个前缀'_'。例如x_copy_(y),x_t()将会改变x
@ -110,3 +103,22 @@ print(x[:,1])
输出结果:
![](./Image/输出08.PNG)
<h4>8.view()函数改变tensor的大小或形状
```buildoutcfg
x = torch.randn(4, 4)
y = x.view(16)
z = x.view(-1, 8) # the size -1 is inferred from other dimensions
print(x.size(), y.size(), z.size())
```
输出结果:
![](Image/输出09.PNG)
<h4>9.item()函数获取tensor的value值
```buildoutcfg
x = torch.randn(1)
print(x)
print(x.item())
```
输出结果:
![](Image/输出10.PNG)

View File

@ -1,30 +1,39 @@
from __future__ import print_function
import torch
x = torch.empty(5, 3)
print(x)
x = torch.rand(5, 3)
print(x)
x = torch.zeros(5, 3, dtype=torch.long)
print(x)
x = torch.tensor([5.5, 3])
print(x)
x = x.new_ones(5, 3, dtype=torch.double)
print(x)
x = torch.randn_like(x, dtype=torch.float)
# 重写了数据类型
print(x)
# 结果大小相同
# x = torch.empty(5, 3)
# print(x)
#
print(x.size())
# x = torch.rand(5, 3)
# print(x)
#
y = torch.rand(5, 3)
# #加法方式1
print(x + y)
# #加法方式2
# #print(torch.add(x, y))
# x = torch.zeros(5, 3, dtype=torch.long)
# print(x)
#
print(x[:,1])
# x = torch.tensor([5.5, 3])
# print(x)
#
# x = x.new_ones(5, 3, dtype=torch.double)
# print(x)
# x = torch.randn_like(x, dtype=torch.float)
# # 重写了数据类型
# print(x)
# # 结果大小相同
# #
# print(x.size())
# #
# y = torch.rand(5, 3)
# # #加法方式1
# print(x + y)
# # #加法方式2
# # #print(torch.add(x, y))
# #
# print(x[:,1])
# x = torch.randn(4, 4)
# y = x.view(16)
# z = x.view(-1, 8) # the size -1 is inferred from other dimensions
# print(x.size(), y.size(), z.size())
x = torch.randn(1)
print(x)
print(x.item())