Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
liuyunshengsir
Python_690654
提交
fdc2558d
P
Python_690654
项目概览
liuyunshengsir
/
Python_690654
与 Fork 源项目一致
Fork自
inscode / Python
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Python_690654
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
fdc2558d
编写于
7月 23, 2025
作者:
R
root
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Wed Jul 23 12:57:00 CST 2025 inscode
上级
359494af
变更
2
显示空白变更内容
内联
并排
Showing
2 changed file
with
71 addition
and
0 deletion
+71
-0
test01.py
test01.py
+40
-0
test02.py
test02.py
+31
-0
未找到文件。
test01.py
0 → 100644
浏览文件 @
fdc2558d
import
numpy
as
np
class
SimpleRNN
:
def
__init__
(
self
,
input_size
,
hidden_size
):
self
.
Wx
=
np
.
random
.
randn
(
hidden_size
,
input_size
)
# 输入权重
self
.
Wh
=
np
.
random
.
randn
(
hidden_size
,
hidden_size
)
# 隐藏状态权重
self
.
b
=
np
.
zeros
((
hidden_size
,
1
))
# 偏置项
def
forward
(
self
,
x
,
h_prev
):
h_next
=
np
.
tanh
(
np
.
dot
(
self
.
Wx
,
x
)
+
np
.
dot
(
self
.
Wh
,
h_prev
)
+
self
.
b
)
return
h_next
# 测试 SimpleRNN
def
test_simple_rnn
():
# 参数设置
input_size
=
30
# 输入向量的维度
hidden_size
=
20
# 隐藏状态的维度
seq_length
=
5
# 序列长度
# 初始化 RNN
rnn
=
SimpleRNN
(
input_size
,
hidden_size
)
# 生成随机输入序列 (input_size, seq_length)
np
.
random
.
seed
(
42
)
# 固定随机种子以便复现结果
x_sequence
=
[
np
.
random
.
randn
(
input_size
,
1
)
for
_
in
range
(
seq_length
)]
# 每个时间步的输入 (input_size, 1)
# 初始隐藏状态 (hidden_size, 1)
h
=
np
.
zeros
((
hidden_size
,
1
))
# 前向传播
print
(
"RNN 前向传播测试:"
)
for
t
,
x
in
enumerate
(
x_sequence
):
h
=
rnn
.
forward
(
x
,
h
)
print
(
f
"时间步
{
t
}
:"
)
print
(
f
"输入 x_
{
t
}
的形状:
{
x
.
shape
}
, 值:
\n
{
x
.
T
}
"
)
print
(
f
"隐藏状态 h_
{
t
}
的形状:
{
h
.
shape
}
, 值:
\n
{
h
.
T
}
"
)
print
(
"-"
*
50
)
if
__name__
==
"__main__"
:
test_simple_rnn
()
\ No newline at end of file
test02.py
0 → 100644
浏览文件 @
fdc2558d
import
torch
# 创建一个 2x3 的全 0 张量
a
=
torch
.
zeros
(
2
,
3
)
print
(
a
)
# 创建一个 2x3 的全 1 张量
b
=
torch
.
ones
(
2
,
3
)
print
(
b
)
# 创建一个 2x3 的随机数张量
c
=
torch
.
randn
(
2
,
3
)
print
(
c
)
# 从 NumPy 数组创建张量
import
numpy
as
np
numpy_array
=
np
.
array
([[
1
,
2
],
[
3
,
4
]])
tensor_from_numpy
=
torch
.
from_numpy
(
numpy_array
)
print
(
tensor_from_numpy
)
# 在指定设备(CPU/GPU)上创建张量
device
=
torch
.
device
(
"cuda"
if
torch
.
cuda
.
is_available
()
else
"cpu"
)
d
=
torch
.
randn
(
2
,
3
,
device
=
device
)
print
(
torch
.
cuda
.
is_available
())
print
(
d
)
# 逐元素乘法(要求形状相同)
X
=
torch
.
tensor
([[
1
,
2
],
[
3
,
4
]])
Y
=
torch
.
tensor
([[
5
,
6
],
[
7
,
8
]])
Z
=
X
*
Y
# 或 torch.mul(X, Y)
print
(
"逐元素乘法结果:
\n
"
,
Z
)
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录