Kreas错误TypeError:__init __()缺少1个必需的位置参数:'units'

时间:2018-12-29 03:15:38

标签: tensorflow machine-learning keras deep-learning

我正在尝试使用Kreas预测股价。

代码如下:

import pandas
import numpy
from keras.layers.core import Dense, Activation, Dropout
from keras.layers.recurrent import LSTM
from keras.models import Sequential
import matplotlib.pyplot as plt

CONST_TRAINTING_SEQUENCE_LENGTH = 12

CONST_TESTING_CASES = 5


def dataNormalization(data):
    return [(datum - data[0]) / data[0] for datum in data]


def dataDeNormalization(data, base):
    return [(datum + 1) * base for datum in data]


def getDeepLearningData(ticker):
    # Step 1. Load data
    data = pandas.read_csv('/Users/yindeyong/Desktop/Django_Projects/pythonstock/data/Intraday/' + ticker + '.csv')[
        'close'].tolist()

    # Step 2. Building Training data
    dataTraining = []
    for i in range(len(data) - CONST_TESTING_CASES * CONST_TRAINTING_SEQUENCE_LENGTH):
        dataSegment = data[i:i + CONST_TRAINTING_SEQUENCE_LENGTH + 1]
        dataTraining.append(dataNormalization(dataSegment))

    dataTraining = numpy.array(dataTraining)
    numpy.random.shuffle(dataTraining)
    X_Training = dataTraining[:, :-1]
    Y_Training = dataTraining[:, -1]

    # Step 3. Building Testing data
    X_Testing = []
    Y_Testing_Base = []
    for i in range(CONST_TESTING_CASES, 0, -1):
        dataSegment = data[-(i + 1) * CONST_TRAINTING_SEQUENCE_LENGTH:-i * CONST_TRAINTING_SEQUENCE_LENGTH]
        Y_Testing_Base.append(dataSegment[0])
        X_Testing.append(dataNormalization(dataSegment))

    Y_Testing = data[-CONST_TESTING_CASES * CONST_TRAINTING_SEQUENCE_LENGTH:]

    X_Testing = numpy.array(X_Testing)
    Y_Testing = numpy.array(Y_Testing)

    # Step 4. Reshape for deep learning
    X_Training = numpy.reshape(X_Training, (X_Training.shape[0], X_Training.shape[1], 1))
    X_Testing = numpy.reshape(X_Testing, (X_Testing.shape[0], X_Testing.shape[1], 1))

    return X_Training, Y_Training, X_Testing, Y_Testing, Y_Testing_Base


def predictLSTM(ticker):
    # Step 1. Load data

    X_Training, Y_Training, X_Testing, Y_Testing, Y_Testing_Base = getDeepLearningData(ticker)

    # Step 2. Build model
model = Sequential()

model.add(LSTM(
    input_shape=1,
    dropout_dim=50,
    return_sequences=True))
model.add(Dropout(0.2))

model.add(LSTM(
    200,
    return_sequences=False))
model.add(Dropout(0.2))

model.add(Dense(output_dim=1))
model.add(Activation('linear'))

model.compile(lose='mse', optimizer='rmsprop')

# Step 3. Train model
model.fit(X_Training, Y_Training,
          batch_size=512,
          nb_epoch=5,
          validation_split=0.05)

但是当我运行它时,出现了一个错误:

使用TensorFlow后端。 追溯(最近一次通话):   文件“ /Users/yindeyong/Desktop/Django_Projects/pythonstock/deeplearningprediction.py”,第127行,在     预测LSTM(ticker ='MRIN')   在predictLSTM中,文件“ /Users/yindeyong/Desktop/Django_Projects/pythonstock/deeplearningprediction.py”,第96行     return_sequences = True))   包装中的文件“ /Users/yindeyong/Desktop/Django_Projects/envs/stockenv/lib/python3.6/site-packages/keras/legacy/interfaces.py”,第91行     返回func(* args,** kwargs)

  

TypeError: init ()缺少1个必需的位置参数:'units'   流程结束,退出代码为1

1 个答案:

答案 0 :(得分:1)

您必须在此指定LSTM单位的数量

model.add(LSTM(200,
    input_shape=1,
    dropout_dim=50,
    return_sequences=True))

以类似的方式处理LSTM的下一层。