【问题标题】:Keras - TypeError: only integer scalar arrays can be converted to a scalar indexKeras - TypeError:只有整数标量数组可以转换为标量索引
【发布时间】:2017-12-15 16:07:47
【问题描述】:

我正在尝试学习 keras,特别是用于时间序列异常检测的 LSTM,为此我一直在关注在线示例。然而由于某种原因,它不起作用。我已经按照之前与TypeError: only integer scalar arrays can be converted to a scalar index 相关的帖子中的建议进行了操作,但没有任何效果。据此,我认为这与 Numpy 有关。这是我的代码:

import numpy
import pandas
import matplotlib.pyplot as plt
import math
import tensorflow as tf
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

# 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 = 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),:]
print(len(train), len(test))

# 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]
        dataX.append(a)
        dataY.append(dataset[i + look_back, 0])
    return numpy.array(dataX), numpy.array(dataY)

# reshape into X=t and Y=t+1
look_back = 1
trainX = create_dataset(train, look_back)[0]
trainY = create_dataset(train, look_back)[0]
testX = create_dataset(test, look_back)[0]
testY = create_dataset(test, look_back)[0]

#reshape input to be [samples, time steps, features]
trainX = numpy.reshape(trainX, (trainX[0], 1, trainX.shape[1]))[0]
testX = numpy.reshape(testX)

# create and fit the LSTM network
model = Sequential()[0]
model.add(LSTM(4, input_shape=(1, look_back)))
model.add(Dense(1))[0]
model.compile(loss='mean_squared_error', optimizer='adam')
model.fit(trainX, trainY, epochs=100, batch_size=1, verbose=2)

#make predictions
trainPredict = model.predict(trainX)
testPredict = model.predict(testX)

#invert predictions
trainPredict = scaler.inverse_transform(trainPredict)
trainY = scaler.inverse_transform([trainY])[0]

# calculate root mean squared error
trainScore = math.sqrt(mean_squared_error(train[0], trainPredict[:,0]))
print('Train Score: %.2f RMSE' % (trainScore))
testScore = math.sqrt(mean_squared_error(testY[0], testPredict[:,0]))
print('Test Score: %.2f' % (testScore))

# shift train  predictions for plotting
trainPredictPlot = numpy.empty_like(dataset)
trainPredictPlot[:, :] = numpy.nan
trainPredictPlot[look_back:len(trainPredict)+look_back, :] = trainPredict
# shift test predictions for plotting
testPredictPlot = numpy.empty_like(dataset)
testPredictPlot[:, :] = numpy.nan
testPredictPlot[len(trainPredict)+(look_back*2)+1:len(dataset)-1, :] = testPredict
# plot baseline and predictions
plt.plot(scaler.inverse_transform(dataset))
plt.plot(trainPredictPlot)
plt.plot(testPredictPlot)
plt.show()

然后我得到错误:

Using TensorFlow backend.
96 48
Traceback (most recent call last):
  File "C:\Users\fires\Anaconda3\envs\python3.5\lib\site-packages\numpy\core\fromnumeric.py", line 57, in _wrapfunc
    return getattr(obj, method)(*args, **kwds)
TypeError: only integer scalar arrays can be converted to a scalar index


During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:/Users/fires/PycharmProjects/RSI/Test 1.py", line 52, in <module>
    trainX = numpy.reshape(trainX, (trainX[0], 1, trainX.shape[1]))[0]
  File "C:\Users\fires\Anaconda3\envs\python3.5\lib\site-packages\numpy\core\fromnumeric.py", line 232, in reshape
    return _wrapfunc(a, 'reshape', newshape, order=order)
  File "C:\Users\fires\Anaconda3\envs\python3.5\lib\site-packages\numpy\core\fromnumeric.py", line 67, in _wrapfunc
    return _wrapit(obj, method, *args, **kwds)
  File "C:\Users\fires\Anaconda3\envs\python3.5\lib\site-packages\numpy\core\fromnumeric.py", line 47, in _wrapit
    result = getattr(asarray(obj), method)(*args, **kwds)
TypeError: only integer scalar arrays can be converted to a scalar index

【问题讨论】:

  • Using Tensorflow backend 不是错误消息 - 它是一条信息性消息,因为您可以使用其他后端(Theano,以及最近的 CNTK)运行 Keras。我已经相应地更新了你的帖子标题

标签: python numpy keras lstm


【解决方案1】:

您正在尝试将数组重塑为非整数长度。

您已经编写了以下代码;

#reshape input to be [samples, time steps, features]
trainX = numpy.reshape(trainX, (trainX[0], 1, trainX.shape[1]))[0]
testX = numpy.reshape(testX)

但是我怀疑您的意思是 trainX.shape[0] 而不是 trainX[0]。这修复了only integer arrays can be converted to a scalar index 错误。但是在下面的行中,您编写了 testX = numpy.reshape(testX),这是无效的,因为 numpy.reshape 需要一个形状参数。我不确定你到底想用那条线实现什么,但希望引起你的注意可以解决你的问题!

【讨论】:

    猜你喜欢
    • 2021-01-27
    • 2019-04-26
    • 2021-10-31
    • 1970-01-01
    • 2018-04-04
    • 2022-01-03
    • 2018-09-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多