在训练模型评估期间列出“索引超出范围”

时间:2020-03-30 07:35:53

标签: keras

我试图预测第二天步行的每日步数。该模型已编译并适合训练数据,但评估失败,并显示“索引超出范围”。如果代码中缺少某些内容,请给予帮助。

from datetime import datetime
import pandas_datareader.data as web
from sklearn.preprocessing import StandardScaler
import pandas as pd
import numpy as np
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
from keras.models import Sequential
from keras.layers import Dense

"""
    The code attempts to create a model that trains on the dataset that predicts the steps walked
    observing the walking pattern
"""

ratio = 0.8 # 

class preprocess():
    """
        Process steps
    """
    def __init__(self, ratio):
        self.ratio = ratio

    def create_datasets(self):
        df = pd.read_csv("Steps.txt", sep=" ")
        length = len(df)
        print(df.head())

        df.reset_index(inplace = True)
        scaler = StandardScaler()
        self.X_train = df[['Steps Walked']][:int(length * ratio)]
        scaler.fit(self.X_train)
        self.X_train = scaler.transform(self.X_train.dropna())
        self.X_test = df[['Steps Walked']][(int(length *ratio) + 1):].dropna()
        self.X_test = scaler.transform(self.X_test)



if __name__ == '__main__':


    #
    # Preprocess the data
    #
    print("Getting preprocessed data")
    preprocess = preprocess(ratio)
    preprocess.create_datasets()
    #   Collect, scale, train-test split

    X_train = preprocess.X_train
    X_test = preprocess.X_test

    # Expected output
    Y_train = pd.DataFrame(X_train).shift(-1)[:-1]
    Y_test = pd.DataFrame(X_test).shift(-1)[:-1]

    X_train = X_train[:-1]

    Y_train = Y_train.to_numpy()


    # Clear notebook state
    tf.keras.backend.clear_session()

    """
    # Create the input layers
    # The neurons in a layer are arbitrarily chosen
    """
    model = Sequential()
    model.add(Dense(12, input_dim=1, activation='relu'))
    model.add(Dense(8, input_dim=1, activation='relu'))
    model.add(Dense(1, input_dim=1, activation='sigmoid'))

    # Print the model summary
    model.summary()

    # Print the mapping of the model
    keras.utils.plot_model(model, 'Steps_plot.png', show_shapes = True)

    """
    # Compile the model with the given losses, optimizer and metrics
    """
    model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])

    # Try to fit the model for the given dataset
    # How to fit in the batch size for different inputs ?
    history = model.fit(x = X_train, y = Y_train, batch_size = 2, epochs = 5,
            validation_split = 0.8, verbose = 1)
            # validation_data = X_test, verbose = 1)

    # Evaluate the model with the test dataset
    test_scores = model.evaluate(X_test, verbose = 2)
    print("Test loss: ", test_scores[0])
    print("Test accuracy: ", test_scores[1])
    print("\n ")

    # Save the model
    model.save('my_model')
    del model

    # Recreate the model from the file
    model = keras.models.load_model('my_model')

回溯(最近通话最近): 在第96行的文件“ ./Steps_walked.py”中 test_scores = model.evaluate(X_test,详细= 2) 评估中的文件“ /opt/python/anaconda3/lib/python3.6/site-packages/keras/engine/training.py”,行1362 回调=回调) 在test_loop中的第407行,文件“ /opt/python/anaconda3/lib/python3.6/site-packages/keras/engine/training_arrays.py” 如果issparse(ins [i])而不是K.is_sparse(feed [i]): IndexError:列表索引超出范围

在文件keras / engine / training_arrays.py中 feed是3个数组的列表,而ins是发生此错误时的单个数组的列表

feed = (model._feed_inputs +
        model._feed_targets +
        model._feed_sample_weights)
indices_for_conversion_to_dense = []
for i in range(len(feed)):
    if issparse(ins[i]) and not K.is_sparse(feed[i]):

欣赏您的建议

0 个答案:

没有答案