python - Keras 金融神经网络输入错误 : Expected 4 Dimensions, 收到输入形状 (1172, 1, 5)

标签 python keras neural-network finance quandl

这是我编写的代码,用于根据历史 Facebook 数据预测股市波动。我正在使用 Keras 神经网络,数据来自 Quandl。该程序利用之前引用的历史金融数据库中的信息来训练预测股票价格的神经网络,并从 Michael Grogan (MGCodesAndStats) 撰写的帖子中派生出组件。程序如下:

import tensorflow as tf
import keras
import numpy as np
import quandl
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
import pandas as pd
import sklearn
import math

df = quandl.get("WIKI/FB", api_key = '_msxC6xspj2ddytz7-4u')
print(df)

df = df[['Adj. Close']]

previous = 1

def create_dataset(df, previous):
    dataX, dataY = [], []
    for i in range(len(df)-previous-1):
        a = df[i:(i+previous), 0]
        dataX.append(a)
        dataY.append(df[i + previous, 0])
    return np.array(dataX), np.array(dataY)

scaler = sklearn.preprocessing.MinMaxScaler(feature_range=(0, 1))
df = scaler.fit_transform(df)
print(df)

train_size = math.ceil(len(df) * 0.8)

train, val = df[0:train_size,:], df[train_size:len(df),:]

X_train, Y_train = create_dataset(train, previous)

print(X_train)
print(Y_train)

X_val, Y_val = create_dataset(val, previous)

X_train = np.reshape(X_train, (X_train.shape[0], 1, X_train.shape[1]))
X_val = np.reshape(X_val, (X_val.shape[0], 1, X_val.shape[1]))

model = keras.models.Sequential() 
model.add(keras.layers.Dense(units = 64, activation = 'relu', input_shape = (1172, 1, 5)))
model.add(keras.layers.Dense(units = 1, activation = 'linear'))
model.compile(loss='mean_absolute_error', 
              optimizer='adam', 
              metrics=['accuracy'])

model.fit(X_train, Y_train, epochs=8)

然而,尽管提供给神经网络的信息的形状是指定的,程序还是产生了以下错误消息:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-3-d11b5f4c50ab> in <module>()
     53               metrics=['accuracy'])
     54 
---> 55 model.fit(X_train, Y_train, epochs=8)
     56 
     57 

2 frames
/usr/local/lib/python3.6/dist-packages/keras/engine/training_utils.py in standardize_input_data(data, names, shapes, check_batch_axis, exception_prefix)
    129                         ': expected ' + names[i] + ' to have ' +
    130                         str(len(shape)) + ' dimensions, but got array '
--> 131                         'with shape ' + str(data_shape))
    132                 if not check_batch_axis:
    133                     data_shape = data_shape[1:]

ValueError: Error when checking input: expected dense_1_input to have 4 dimensions, but got array with shape (1172, 1, 5)

尽管事实上提供给神经网络第一层的数组的形状在整体上被指定为拥有 (1172, 1, 5) 的形状,即程序所使用的形状,但仍然会发生此错误。状态不是预期的;有没有一种直接的方法可以毫不费力地解决这个问题?指定输入形状时,错误的主要原因是什么?感谢您的帮助。

最佳答案

您犯了一个典型的错误,即在输入形状中包含样本尺寸,如果当然不正确,您的输入形状应该是:

model.add(keras.layers.Dense(units = 64, activation = 'relu', input_shape = (1, 5)))

关于python - Keras 金融神经网络输入错误 : Expected 4 Dimensions, 收到输入形状 (1172, 1, 5),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59922209/

相关文章:

python - 使用 Popen 打开一个进程并在一段时间后终止

python - KDTree Python 实现细节

python - 对音频信号进行卷积

python - Keras,如何获得每一层的输出?

python - 神经网络中的线性函数正在产生巨大的输出值

python 模块和导入

tensorflow - 如何使用 LabelBinarizer 解决 2 标签问题?

python - 在 GPU 上使用 Keras 内存不足

python-3.x - 每次运行神经网络代码时结果都会改变

neural-network - 手电筒 7 : how to connect the neurons of the same layer?