Почему я не могу предсказать мою модель Keras с размером 128 партий? - PullRequest
0 голосов
/ 08 апреля 2020

Первый извините за мой плохой Энгли sh. Я создаю модель Keras LSTM для прогнозирования цен на акции. Вот мой код:

from tqdm import tqdm
import numpy as np
import pandas as pd
from keras.models import Sequential
from keras.layers.recurrent import LSTM
from keras.layers.core import Dense,Activation,Dropout,Flatten,Reshape
from sklearn.preprocessing import MinMaxScaler
import keras as kr
from sklearn.model_selection import train_test_split
from keras import optimizers
veri =pd.read_csv("eurusd.csv")
veri['trh'] = pd.to_datetime(veri.trh, format='%d.%m.%Y')
########################
del veri['puan']
del veri['yuzde']
del veri['sira']
del veri['trh']
df_train, df_test = train_test_split(veri, train_size=0.8, test_size=0.2, shuffle=False)
print("Train and Test size", len(df_train), len(df_test))
x = df_train.loc[:,:].values
scaler = MinMaxScaler(feature_range=(0,1))
x_train = scaler.fit_transform(x)
x_test = scaler.transform(df_test.loc[:,:])
TIME_STEPS=7
BATCH_SIZE=128
def build_timeseries(mat, y_col_index):

    # y_col_index tahmin etmek istediğimiz değerin sütun numarası
    # total number of time-series samples would be len(mat) - TIME_STEPS
    dim_0 = mat.shape[0] - TIME_STEPS #1328-7 gibi bir şey
    dim_1 = mat.shape[1]
    x = np.zeros((dim_0, TIME_STEPS, dim_1))
    y = np.zeros((dim_0,))

    for i in tqdm(range(dim_0)):
        x[i] = mat[i:TIME_STEPS + i]
        y[i] = mat[TIME_STEPS + i, y_col_index]
    print("length of time-series i/o", x.shape, y.shape)
    return x, y


def trim_dataset(mat, batch_size):
    """
    trims dataset to a size that's divisible by BATCH_SIZE
    """
    no_of_rows_drop = mat.shape[0]%batch_size
    if(no_of_rows_drop > 0):
        return mat[:-no_of_rows_drop]
    else:
        return mat

x_t, y_t = build_timeseries(x_train, 0)
#x_t =3 boyutlu besleme verileri
#y_t =de sonuç satırının timestepsten sonraki kısmı(1. değişkeni aldık)
x_t = trim_dataset(x_t, BATCH_SIZE)#xtrain
y_t = trim_dataset(y_t, BATCH_SIZE)#ytrain(sonuc)
x_temp, y_temp = build_timeseries(x_test, 0)
x_val, x_test_t = np.split(trim_dataset(x_temp, BATCH_SIZE),2)
y_val, y_test_t = np.split(trim_dataset(y_temp, BATCH_SIZE),2)

model = Sequential()
model.add(LSTM(100, batch_input_shape=(BATCH_SIZE, TIME_STEPS, x_t.shape[2]), dropout=0.0, recurrent_dropout=0.0, stateful=True, kernel_initializer='random_uniform'))
model.add(Dropout(0.2))
model.add(Dense(20, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='mean_squared_error', optimizer=kr.optimizers.rmsprop(0.01))

csv_logger = kr.callbacks.CSVLogger('sonuclar.log')

history = model.fit(x_t, #train girdiler
                    y_t, #train çıktılar
                    epochs=200,
                    verbose=2,
                    batch_size=BATCH_SIZE,
                    shuffle=False,
                    validation_data=((trim_dataset(x_val, BATCH_SIZE)),
                                     (trim_dataset(y_val, BATCH_SIZE))),
                    callbacks=[csv_logger])

grafik1=model.predict(x_test_t)

и это мои результаты, например:

.
.
.
.
Epoch 192/200
 - 0s - loss: 3.8528e-04 - val_loss: 1.0298e-04
Epoch 193/200
 - 0s - loss: 3.1330e-04 - val_loss: 3.7064e-04
Epoch 194/200
 - 0s - loss: 9.6561e-04 - val_loss: 1.4455e-04
Epoch 195/200
 - 0s - loss: 2.7916e-04 - val_loss: 3.4224e-04
Epoch 196/200
 - 0s - loss: 8.4071e-04 - val_loss: 4.0075e-04
Epoch 197/200
 - 0s - loss: 4.9036e-04 - val_loss: 1.1518e-04
Epoch 198/200
 - 0s - loss: 3.2914e-04 - val_loss: 1.2514e-04
Epoch 199/200
 - 0s - loss: 0.0010 - val_loss: 3.6713e-04
Epoch 200/200
 - 0s - loss: 4.0553e-04 - val_loss: 1.7644e-04
2020-04-08 14:23:10.216020: W tensorflow/core/common_runtime/base_collective_executor.cc:217] BaseCollectiveExecutor::StartAbort Invalid argument: Specified a list with shape [128,4] from a tensor with shape [32,4]
     [[{{node lstm_1/TensorArrayUnstack/TensorListFromTensor}}]]
Traceback (most recent call last):
  File "/home/phylo/PycharmProjects/Keras1/kerassecond.py", line 80, in <module>
    grafik1=model.predict(x_test_t)
  File "/home/phylo/.local/lib/python3.6/site-packages/keras/engine/training.py", line 1462, in predict
    callbacks=callbacks)
  File "/home/phylo/.local/lib/python3.6/site-packages/keras/engine/training_arrays.py", line 324, in predict_loop
    batch_outs = f(ins_batch)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/keras/backend.py", line 3727, in __call__
    outputs = self._graph_fn(*converted_inputs)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/eager/function.py", line 1551, in __call__
    return self._call_impl(args, kwargs)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/eager/function.py", line 1591, in _call_impl
    return self._call_flat(args, self.captured_inputs, cancellation_manager)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/eager/function.py", line 1692, in _call_flat
    ctx, args, cancellation_manager=cancellation_manager))
  File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/eager/function.py", line 545, in call
    ctx=ctx)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/eager/execute.py", line 67, in quick_execute
    six.raise_from(core._status_to_exception(e.code, message), None)
  File "<string>", line 3, in raise_from
tensorflow.python.framework.errors_impl.InvalidArgumentError:  Specified a list with shape [128,4] from a tensor with shape [32,4]
     [[node lstm_1/TensorArrayUnstack/TensorListFromTensor (defined at home/phylo/.local/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py:3009) ]] [Op:__inference_keras_scratch_graph_10579]

Function call stack:
keras_scratch_graph


Process finished with exit code 1

Когда я изменяю размер моей партии 128 и мой код предсказания (grafik1 = model.predict (x_test_t)) , я получаю эту ошибку, но если я пытаюсь изменить BATC_SIZE на 32, этот код не выдает никакой ошибки. Почему? Я проверил разные значения, и я вижу, 128 Batchsize является лучшим из меня. Как я могу решить эту проблему?

DATA SET

1 Ответ

1 голос
/ 08 апреля 2020

На страницах документации Keras для функции прогнозирования (https://keras.io/models/sequential/):

batch_size : целое число или None. Количество образцов на обновление градиента. Если не указано, batch_size будет по умолчанию равным 32. Не указывайте batch_size, если ваши данные представлены в виде символов c тензоров, генераторов или keras.utils.Sequence экземпляров (поскольку они генерируют пакеты).

Если указать batch_size, это, вероятно, решит проблему:

grafik1=model.predict(x_test_t, batch_size=BATCH_SIZE)
...