3. 编写程序,创建一个tensor,要求: ① 四维、32位浮点型、
时间: 2023-05-18 19:06:48 浏览: 65
大小为(2,3,4,5);②随机初始化。
答案:
import torch
tensor = torch.randn(2, 3, 4, 5, dtype=torch.float32)
print(tensor)
相关问题
3. 编写程序,创建一个tensor,要求: 四维、32位浮点型
可以使用Python中的NumPy库来创建一个四维、32位浮点型的tensor,代码如下:
```python
import numpy as np
tensor = np.zeros((1, 2, 3, 4), dtype=np.float32)
```
这个代码创建了一个四维的tensor,其形状为(1, 2, 3, 4),即第一维有1个元素,第二维有2个元素,第三维有3个元素,第四维有4个元素。其中,dtype参数指定了数据类型为32位浮点型。
编写程序,创建一个tensor,要求: ①四维、32位浮点型、名字是自己姓名全拼; 数字自己定,至少有24个数,但是不能有相同的数字,也不能按顺序赋值。
import tensorflow as tf
# 姓名全拼:zhangsan
tensor = tf.constant([
[
[
[3.1415, 2.718, 0.618, 0.123],
[1.618, 0.666, 0.999, 0.777],
[0.111, 0.222, 0.333, 0.444],
[0.555, 0.777, 0.888, 0.999]
],
[
[1.234, 5.678, 9.101, 1.111],
[0.222, 0.333, 0.444, 0.555],
[1.111, 2.222, 3.333, 4.444],
[5.555, 6.666, 7.777, 8.888]
],
[
[9.999, 8.888, 7.777, 6.666],
[5.555, 4.444, 3.333, 2.222],
[1.111, 0.222, 0.333, 0.444],
[0.555, 0.777, 0.888, 0.999]
],
[
[1.111, 2.222, 3.333, 4.444],
[5.555, 6.666, 7.777, 8.888],
[9.999, 8.888, 7.777, 6.666],
[5.555, 4.444, 3.333, 2.222]
]
],
[
[
[0.111, 0.222, 0.333, 0.444],
[0.555, 0.777, 0.888, 0.999],
[3.1415, 2.718, 0.618, 0.123],
[1.618, 0.666, 0.999, 0.777]
],
[
[1.234, 5.678, 9.101, 1.111],
[0.222, 0.333, 0.444, 0.555],
[1.111, 2.222, 3.333, 4.444],
[5.555, 6.666, 7.777, 8.888]
],
[
[9.999, 8.888, 7.777, 6.666],
[5.555, 4.444, 3.333, 2.222],
[1.111, 0.222, 0.333, 0.444],
[0.555, 0.777, 0.888, 0.999]
],
[
[5.555, 6.666, 7.777, 8.888],
[9.999, 8.888, 7.777, 6.666],
[5.555, 4.444, 3.333, 2.222],
[1.111, 2.222, 3.333, 4.444]
]
]
], dtype=tf.float32, name="zhangsan")
print(tensor)
阅读全文