Numpy 到 pyTorch:有不同的数据类型吗?
Numpy to pyTorch: are there different data types?
问题:有人可以帮助我调整这两种数据生成方法,以便它们都可以被下面的 nn-model 使用吗?当将 appraoch (2) 与 numpy
和 torch.from_numpy(x)
一起使用时,会发生 运行 时间错误(“ 预期标量类型 Float 但发现 Double”)
对于数据生成我有这两种方法:
import torch
import torch.nn as nn
import numpy as np
def get_training_data_1():
x = torch.randn(batch_size, n_in)
y = torch.tensor([[1.0], [0.0], [0.0], [1.0], [1.0], [1.0], [0.0], [0.0], [1.0], [1.0]])
return x,y
def get_training_data_2():
x = np.random.rand(batch_size, n_in)
y = np.array([[1.0], [0.0], [0.0], [1.0], [1.0], [1.0], [0.0], [0.0], [1.0], [1.0]])
x = torch.from_numpy(x)
y = torch.from_numpy(y)
return x,y
n_in, n_h, n_out, batch_size = 2, 5, 1, 10
x, y = get_training_data_2()
有了这个 模型 我 运行 在使用带有 numpy
和 torch.from_numpy(x)
的方法 (2) 时遇到问题,而当使用方法 (1)
#---- Create a NN-model
model = nn.Sequential( nn.Linear(n_in, n_h), # hidden layer
nn.ReLU(), # activation layer
nn.Linear(n_h, n_out), # output layer
nn.Sigmoid() ) # final 0, 1 rounding
#---- Construct the loss function
criterion = torch.nn.MSELoss()
#---- Construct the optimizer (Stochastic Gradient Descent in this case)
optimizer = torch.optim.SGD(model.parameters(), lr = 0.1)
#---- Gradient Descent
for epoch in range(1501):
y_pred = model(x) # Forward pass: Compute predicted y by passing x to the model
loss = criterion(y_pred, y) # Compute and print loss
if epoch%50 == 0:
print(epoch, loss.item())
optimizer.zero_grad() # Zero gradients, perform a backward pass, and update the weights.
loss.backward() # perform a backward pass (backpropagation)
optimizer.step() # Update the parameters
torch
中的默认浮点类型是 float32
(即单精度)。在 NumPy 中,默认值为 float64
(双精度)。尝试更改 get_training_data_2
以便在将它们转换为 torch 张量之前明确设置 numpy 数组 numpy.float32
的数据类型:
def get_training_data_2():
x = np.random.rand(batch_size, n_in).astype(np.float32)
y = np.array([[1.0], [0.0], [0.0], [1.0], [1.0], [1.0], [0.0], [0.0], [1.0], [1.0]],
dtype=np.float32)
x = torch.from_numpy(x)
y = torch.from_numpy(y)
return x,y
注意。 使用较新的 NumPy 随机 API,您可以直接生成 float32
个样本,而不是将 float64
值转换为 float32
.
def get_training_data_2(rng):
x = rng.random(size=(batch_size, n_in), dtype=np.float32)
y = np.array([[1.0], [0.0], [0.0], [1.0], [1.0], [1.0], [0.0], [0.0], [1.0], [1.0]],
dtype=np.float32)
x = torch.from_numpy(x)
y = torch.from_numpy(y)
return x,y
rng = np.random.default_rng()
x, y = get_training_data_2(rng)
问题:有人可以帮助我调整这两种数据生成方法,以便它们都可以被下面的 nn-model 使用吗?当将 appraoch (2) 与 numpy
和 torch.from_numpy(x)
一起使用时,会发生 运行 时间错误(“ 预期标量类型 Float 但发现 Double”)
对于数据生成我有这两种方法:
import torch
import torch.nn as nn
import numpy as np
def get_training_data_1():
x = torch.randn(batch_size, n_in)
y = torch.tensor([[1.0], [0.0], [0.0], [1.0], [1.0], [1.0], [0.0], [0.0], [1.0], [1.0]])
return x,y
def get_training_data_2():
x = np.random.rand(batch_size, n_in)
y = np.array([[1.0], [0.0], [0.0], [1.0], [1.0], [1.0], [0.0], [0.0], [1.0], [1.0]])
x = torch.from_numpy(x)
y = torch.from_numpy(y)
return x,y
n_in, n_h, n_out, batch_size = 2, 5, 1, 10
x, y = get_training_data_2()
有了这个 模型 我 运行 在使用带有 numpy
和 torch.from_numpy(x)
的方法 (2) 时遇到问题,而当使用方法 (1)
#---- Create a NN-model
model = nn.Sequential( nn.Linear(n_in, n_h), # hidden layer
nn.ReLU(), # activation layer
nn.Linear(n_h, n_out), # output layer
nn.Sigmoid() ) # final 0, 1 rounding
#---- Construct the loss function
criterion = torch.nn.MSELoss()
#---- Construct the optimizer (Stochastic Gradient Descent in this case)
optimizer = torch.optim.SGD(model.parameters(), lr = 0.1)
#---- Gradient Descent
for epoch in range(1501):
y_pred = model(x) # Forward pass: Compute predicted y by passing x to the model
loss = criterion(y_pred, y) # Compute and print loss
if epoch%50 == 0:
print(epoch, loss.item())
optimizer.zero_grad() # Zero gradients, perform a backward pass, and update the weights.
loss.backward() # perform a backward pass (backpropagation)
optimizer.step() # Update the parameters
torch
中的默认浮点类型是 float32
(即单精度)。在 NumPy 中,默认值为 float64
(双精度)。尝试更改 get_training_data_2
以便在将它们转换为 torch 张量之前明确设置 numpy 数组 numpy.float32
的数据类型:
def get_training_data_2():
x = np.random.rand(batch_size, n_in).astype(np.float32)
y = np.array([[1.0], [0.0], [0.0], [1.0], [1.0], [1.0], [0.0], [0.0], [1.0], [1.0]],
dtype=np.float32)
x = torch.from_numpy(x)
y = torch.from_numpy(y)
return x,y
注意。 使用较新的 NumPy 随机 API,您可以直接生成 float32
个样本,而不是将 float64
值转换为 float32
.
def get_training_data_2(rng):
x = rng.random(size=(batch_size, n_in), dtype=np.float32)
y = np.array([[1.0], [0.0], [0.0], [1.0], [1.0], [1.0], [0.0], [0.0], [1.0], [1.0]],
dtype=np.float32)
x = torch.from_numpy(x)
y = torch.from_numpy(y)
return x,y
rng = np.random.default_rng()
x, y = get_training_data_2(rng)