gpt4 book ai didi

python - 测试期间 LSTM 出错

转载 作者:行者123 更新时间:2023-11-30 08:54:14 24 4
gpt4 key购买 nike

我的数据为 68871 x 43,其中特征位于第 1 列中。 1-42 和第 1-42 列中的标签43

我用于数据分类的 keras LSTM 代码是

import numpy
import matplotlib.pyplot as plt
import pandas
import math
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import mean_squared_error
# convert an array of values into a dataset matrix
def create_dataset(dataset, look_back=1):
dataX, dataY = [], []
for i in range(len(dataset)-look_back-1):
a = dataset[i:(i+look_back), 0]
#if i==0
# print len(a)
dataX.append(a)
dataY.append(dataset[i + look_back, 43])
return numpy.array(dataX), numpy.array(dataY)
# fix random seed for reproducibility
numpy.random.seed(7)
# load the dataset
#dataframe = pandas.read_csv('international-airline-passengers.csv', usecols=[1], engine='python', skipfooter=3)
dataset = numpy.loadtxt("Source.txt", delimiter=" ")
#dataset = dataframe.values
#dataset = dataset.astype('float32')
# normalize the dataset
scaler = MinMaxScaler(feature_range=(0, 1))
dataset = scaler.fit_transform(dataset)
# split into train and test sets
train_size = int(len(dataset) * 0.67)
test_size = len(dataset) - train_size
train, test = dataset[0:train_size,:], dataset[train_size:len(dataset),:]
# reshape into X=t and Y=t+1
look_back = 1
trainX, trainY = create_dataset(train, look_back)
testX, testY = create_dataset(test, look_back)
# reshape input to be [samples, time steps, features]
trainX = numpy.reshape(trainX, (trainX.shape[0], 1, trainX.shape[1]))
testX = numpy.reshape(testX, (testX.shape[0], 1, testX.shape[1]))
# create and fit the LSTM network
model = Sequential()
model.add(LSTM(3, input_dim=look_back))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='adam')
model.fit(trainX, trainY, nb_epoch=1, batch_size=1)
score, acc = model.evaluate(testX, testY)
print('Test score:', score)
print('Test accuracy:', acc)

我在测试期间收到此错误 enter image description here

请帮忙解决这个问题,非常感谢

最佳答案

我认为你的问题是model.evaluate(testX, testY)仅返回一个值。

您的错误消息告诉您 numpy.float64是不可迭代的。 model.evaluate(testX, testY) 这意味着什么返回 float64因此,您不能将其返回值放入两个变量 score, acc .

这就像做:

def single_return():
return np.float64(10)
a, b = single_return()

(请注意,此代码将引发完全相同的错误)。

然后我建议,既要立即修复它,又要作为将来始终返回到单个变量然后进行拆分的一个很好的实践。它使错误消息更加清晰,因为只有有问题的行才会是假象,而不是 evaluation一个。

希望有帮助。
pltrdy

关于python - 测试期间 LSTM 出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42346516/

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