Я пытался сделать прогноз ежедневных шагов, пройденных на следующий день. Модель была составлена и снабжена данными обучения, но оценка не удалась с «Индексом вне диапазона». Оцените помощь, если в коде чего-то не хватает.
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')
Traceback (последний вызов был последним): файл "./Steps_walked.py", строка 96, в test_scores = model.evaluate (X_test, verbose = 2) Файл "/opt/python/anaconda3/lib/python3.6/site-packages/keras/engine/training.py", строка 1362, в оценке обратных вызовов = обратных вызовов) Файл "/ opt / python / anaconda3 / lib / python3 .6 / site-packages / keras / engine / training_arrays.py ", строка 407, в test_loop, если issparse (ins [i]), а не K.is_sparse (feed [i]): IndexError: список индекса вне диапазона
В файле keras / engine / training_arrays.py фид представляет собой список из 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]):
Оцените ваш предложения