gpt4 book ai didi

python - Keras错误: TypeError: 'int' object is not iterable

转载 作者:行者123 更新时间:2023-11-30 09:32:00 30 4
gpt4 key购买 nike

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,
output_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)


predictLSTM(ticker='MRIN')

我收到一个错误:

文件“/Users/yindeyong/Desktop/Django_Projects/envs/stockenv/lib/python3.6/site-packages/keras/engine/base_layer.py”,第 147 行,init batch_size,) + tuple(kwargs['input_shape'])类型错误:“int”对象不可迭代

我尝试将 input_shape=1 更改为 input_shape=(1,),然后收到另一个错误:

ValueError:输入 0 与层 lstm_1 不兼容:预期 ndim=3,发现 ndim=2

最佳答案

LSTM 是处理序列的循环网络

序列必须具有长度特征,您的输入形状必须包含这两个:input_shape=(length, features)

您的数据还必须使用(序列、长度、特征)进行相应的调整。

对于可变长度,您可以使用input_shape=(None,features)

关于python - Keras错误: TypeError: 'int' object is not iterable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53970034/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com