'Fit unequal data into Linear Regression Model
How do I fit two unproportional arrays to a regression model? Is it possible to resize/reshape one without loosing the data?
I used the code from here but my train data has a completely different shape. It's 10000x1 and 10000x50.
There is an example below:
x_values
# Convert to numpy
x_train = np.array(x_values, dtype=np.float32)
x_train.shape
# IMPORTANT: 2D required
x_train = x_train.reshape(-1, 2)
x_train.shape
#(10, 2)
y_values
y_train = np.array(y_values, dtype=np.float32)
y_train.shape
# IMPORTANT: 2D required
y_train = y_train.reshape(-1, 1)
y_train.shape
#(10, 1)
With the following code I get RuntimeError: mat1 and mat2 shapes cannot be multiplied (10x2 and 1x1)
import torch
import torch.nn as nn
# Create class
class LinearRegressionModel(nn.Module):
def __init__(self, input_dim, output_dim):
super(LinearRegressionModel, self).__init__()
self.linear = nn.Linear(input_dim, output_dim)
def forward(self, x):
out = self.linear(x)
return out
input_dim = 1
output_dim = 1
model = LinearRegressionModel(input_dim, output_dim)
criterion = nn.MSELoss()
learning_rate = 0.01
optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate)
epochs= 10
for epoch in range(epochs):
epoch += 1
# Convert numpy array to torch Variable
inputs = torch.from_numpy(x_train).requires_grad_()
labels = torch.from_numpy(y_train)
# Clear gradients w.r.t. parameters
optimizer.zero_grad()
# Forward to get output
outputs = model(inputs)
# Calculate Loss
loss = criterion(outputs, labels)
# Getting gradients w.r.t. parameters
loss.backward()
# Updating parameters
optimizer.step()
print('epoch {}, loss {}'.format(epoch, loss.item()))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
