Я новичок в python языке программирования. Я использую нейронную сеть Pytorch LSTM для прогнозирования цены на акцию. Я знаю, что это распространенный вопрос, но я не могу решить проблему
ValueError: не транслируемый выходной операнд с формой (389,1) не соответствует форме широковещания (389,7)
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import torch.nn as nn
import torch
from torch.autograd import Variable
# Importing the training set
dataset_train = pd.read_csv('data/trraining_set.csv')
training_set = dataset_train.iloc[:,[1,2,5,6,7,8,9]].values
from sklearn.preprocessing import MinMaxScaler
sc = MinMaxScaler(feature_range = (0, 1))
training_set_scaled = sc.fit_transform(training_set)
X_train = []
y_train = []
for i in range(INPUT_SIZE, 766):
X_train.append(training_set_scaled[i-INPUT_SIZE:i, 0])
y_train.append(training_set_scaled[i, 0])
X_train, y_train = np.array(X_train), np.array(y_train)
# Reshaping
X_train = np.reshape(X_train, (X_train.shape[0], 1, X_train.shape[1]))
class RNN(nn.Module):
def __init__(self, i_size, h_size, n_layers, o_size):
super(RNN, self).__init__()
self.rnn = nn.LSTM(
input_size=i_size,
hidden_size=h_size,
num_layers=n_layers
)
self.out = nn.Linear(h_size, o_size)
def forward(self, x, h_state):
r_out, hidden_state = self.rnn(x, h_state)
hidden_size = hidden_state[-1].size(-1)
r_out = r_out.view(-1, hidden_size)
outs = self.out(r_out)
return outs, hidden_state
rnn = RNN(INPUT_SIZE, HIDDEN_SIZE, NUM_LAYERS, OUTPUT_SIZE)
optimiser = torch.optim.Adam(rnn.parameters(), lr=learning_rate)
criterion = nn.MSELoss()
hidden_state = None
for epoch in range(num_epochs):
inputs = Variable(torch.from_numpy(X_train).float())
labels = Variable(torch.from_numpy(y_train).float())
output, hidden_state = rnn(inputs, hidden_state)
loss = criterion(output.view(-1), labels)
optimiser.zero_grad()
loss.backward(retain_graph=True)
optimiser.step()
print('epoch {}, loss {}'.format(epoch,loss.item()))
dataset_test = pd.read_csv('data/test.csv')
real_stock_price = dataset_test.iloc[:,[1,2,5,6,7,8,9]].values
dataset_total = pd.concat((dataset_train['Open'], dataset_test['Open']), axis = 0)
inputs = dataset_total[len(dataset_total) - len(dataset_test) - INPUT_SIZE :].values
inputs = inputs.reshape(-1,1)
inputs = sc.transform(inputs)
X_test = []
for i in range(INPUT_SIZE, 329):
X_test.append(inputs[i-INPUT_SIZE:i, 0])
X_test = np.array(X_test)
X_test = np.reshape(X_test, (X_test.shape[0], 1, X_test.shape[1]))
здесь следующая ошибка
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-117-249ca96baddc> in <module>
8 inputs = dataset_total[len(dataset_total) - len(dataset_test) - INPUT_SIZE :].values
9 inputs = inputs.reshape(-1,1)
---> 10 inputs = sc.transform(inputs)
11 X_test = []
12 for i in range(INPUT_SIZE, 329):
~\Anaconda3\lib\site-packages\sklearn\preprocessing\_data.py in transform(self, X)
412 force_all_finite="allow-nan")
413
--> 414 X *= self.scale_
415 X += self.min_
416 return X
ValueError: non-broadcastable output operand with shape (389,1) doesn't match the broadcast shape (389,7)